blob: 37a0b8fee1fd9d9e17dcb5309b08ae24013e80d3 [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 Blaikie3945d1b2014-12-29 18:18:45 +000055ArtificialLocation::ArtificialLocation(CodeGenFunction &CGF)
56 : ApplyDebugLocation(CGF) {
57 if (auto *DI = CGF.getDebugInfo()) {
Adrian Prantl2e0637f2013-07-18 00:28:02 +000058 // Construct a location that has a valid scope, but no line info.
Adrian Prantl49a78562013-07-24 20:34:39 +000059 assert(!DI->LexicalBlockStack.empty());
60 llvm::DIDescriptor Scope(DI->LexicalBlockStack.back());
David Blaikie3945d1b2014-12-29 18:18:45 +000061 CGF.Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(0, 0, Scope));
Adrian Prantl2e0637f2013-07-18 00:28:02 +000062 }
63}
64
David Blaikie3945d1b2014-12-29 18:18:45 +000065ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
66 SourceLocation TemporaryLocation,
67 bool ForceColumnInfo)
68 : CGF(CGF) {
69 if (auto *DI = CGF.getDebugInfo()) {
70 OriginalLocation = CGF.Builder.getCurrentDebugLocation();
71 if (TemporaryLocation.isInvalid())
72 CGF.Builder.SetCurrentDebugLocation(llvm::DebugLoc());
73 else
74 DI->EmitLocation(CGF.Builder, TemporaryLocation, ForceColumnInfo);
75 }
Adrian Prantl2e0637f2013-07-18 00:28:02 +000076}
77
David Blaikie3945d1b2014-12-29 18:18:45 +000078ApplyDebugLocation::~ApplyDebugLocation() {
79 CGF.Builder.SetCurrentDebugLocation(OriginalLocation);
80}
81
82/// ArtificialLocation - An RAII object that temporarily switches to
83/// an artificial debug location that has a valid scope, but no line
Guy Benyei11169dd2012-12-18 14:30:41 +000084void CGDebugInfo::setLocation(SourceLocation Loc) {
85 // If the new location isn't valid return.
Eric Christophere7b87e52014-10-26 23:40:33 +000086 if (Loc.isInvalid())
87 return;
Guy Benyei11169dd2012-12-18 14:30:41 +000088
89 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
90
91 // If we've changed files in the middle of a lexical scope go ahead
92 // and create a new lexical scope with file node if it's different
93 // from the one in the scope.
Eric Christophere7b87e52014-10-26 23:40:33 +000094 if (LexicalBlockStack.empty())
95 return;
Guy Benyei11169dd2012-12-18 14:30:41 +000096
97 SourceManager &SM = CGM.getContext().getSourceManager();
David Blaikieaabde052014-05-14 00:29:00 +000098 llvm::DIScope Scope(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +000099 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000100
David Blaikieaabde052014-05-14 00:29:00 +0000101 if (PCLoc.isInvalid() || Scope.getFilename() == PCLoc.getFilename())
Guy Benyei11169dd2012-12-18 14:30:41 +0000102 return;
103
Guy Benyei11169dd2012-12-18 14:30:41 +0000104 if (Scope.isLexicalBlockFile()) {
David Blaikieaabde052014-05-14 00:29:00 +0000105 llvm::DILexicalBlockFile LBF = llvm::DILexicalBlockFile(Scope);
Eric Christophere7b87e52014-10-26 23:40:33 +0000106 llvm::DIDescriptor D = DBuilder.createLexicalBlockFile(
107 LBF.getScope(), getOrCreateFile(CurLoc));
Guy Benyei11169dd2012-12-18 14:30:41 +0000108 llvm::MDNode *N = D;
109 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000110 LexicalBlockStack.emplace_back(N);
David Blaikie0a21d0d2013-01-26 22:16:26 +0000111 } else if (Scope.isLexicalBlock() || Scope.isSubprogram()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000112 llvm::DIDescriptor D =
113 DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc));
Guy Benyei11169dd2012-12-18 14:30:41 +0000114 llvm::MDNode *N = D;
115 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000116 LexicalBlockStack.emplace_back(N);
Guy Benyei11169dd2012-12-18 14:30:41 +0000117 }
118}
119
120/// getContextDescriptor - Get context info for the decl.
David Blaikiebfa52742013-04-19 06:56:38 +0000121llvm::DIScope CGDebugInfo::getContextDescriptor(const Decl *Context) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000122 if (!Context)
123 return TheCU;
124
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000125 auto I = RegionMap.find(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +0000126 if (I != RegionMap.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000127 llvm::Metadata *V = I->second;
David Blaikiebfa52742013-04-19 06:56:38 +0000128 return llvm::DIScope(dyn_cast_or_null<llvm::MDNode>(V));
Guy Benyei11169dd2012-12-18 14:30:41 +0000129 }
130
131 // Check namespace.
132 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000133 return getOrCreateNameSpace(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +0000134
David Blaikiebfa52742013-04-19 06:56:38 +0000135 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context))
136 if (!RDecl->isDependentType())
137 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Eric Christophere7b87e52014-10-26 23:40:33 +0000138 getOrCreateMainFile());
Guy Benyei11169dd2012-12-18 14:30:41 +0000139 return TheCU;
140}
141
142/// getFunctionName - Get function name for the given FunctionDecl. If the
Benjamin Kramer60509af2013-09-09 14:48:42 +0000143/// name is constructed on demand (e.g. C++ destructor) then the name
Guy Benyei11169dd2012-12-18 14:30:41 +0000144/// is stored on the side.
145StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000146 assert(FD && "Invalid FunctionDecl!");
Guy Benyei11169dd2012-12-18 14:30:41 +0000147 IdentifierInfo *FII = FD->getIdentifier();
Eric Christophere7b87e52014-10-26 23:40:33 +0000148 FunctionTemplateSpecializationInfo *Info =
149 FD->getTemplateSpecializationInfo();
Guy Benyei11169dd2012-12-18 14:30:41 +0000150 if (!Info && FII)
151 return FII->getName();
152
153 // Otherwise construct human readable name for debug info.
Benjamin Kramer9170e912013-02-22 15:46:01 +0000154 SmallString<128> NS;
155 llvm::raw_svector_ostream OS(NS);
156 FD->printName(OS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000157
158 // Add any template specialization args.
159 if (Info) {
160 const TemplateArgumentList *TArgs = Info->TemplateArguments;
161 const TemplateArgument *Args = TArgs->data();
162 unsigned NumArgs = TArgs->size();
163 PrintingPolicy Policy(CGM.getLangOpts());
Benjamin Kramer9170e912013-02-22 15:46:01 +0000164 TemplateSpecializationType::PrintTemplateArgumentList(OS, Args, NumArgs,
165 Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000166 }
167
168 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000169 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000170}
171
172StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
173 SmallString<256> MethodName;
174 llvm::raw_svector_ostream OS(MethodName);
175 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
176 const DeclContext *DC = OMD->getDeclContext();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000177 if (const ObjCImplementationDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000178 dyn_cast<const ObjCImplementationDecl>(DC)) {
179 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000180 } else if (const ObjCInterfaceDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000181 dyn_cast<const ObjCInterfaceDecl>(DC)) {
182 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000183 } else if (const ObjCCategoryImplDecl *OCD =
Eric Christophere7b87e52014-10-26 23:40:33 +0000184 dyn_cast<const ObjCCategoryImplDecl>(DC)) {
185 OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '('
186 << OCD->getIdentifier()->getNameStart() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000187 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000188 // We can extract the type of the class from the self pointer.
Eric Christophere7b87e52014-10-26 23:40:33 +0000189 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000190 QualType ClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000191 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000192 ClassTy.print(OS, PrintingPolicy(LangOptions()));
193 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000194 }
195 OS << ' ' << OMD->getSelector().getAsString() << ']';
196
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000197 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000198}
199
200/// getSelectorName - Return selector name. This is used for debugging
201/// info.
202StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000203 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000204}
205
206/// getClassName - Get class name including template argument list.
Eric Christophere7b87e52014-10-26 23:40:33 +0000207StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
David Blaikie65813a32014-04-02 18:21:09 +0000208 // quick optimization to avoid having to intern strings that are already
209 // stored reliably elsewhere
210 if (!isa<ClassTemplateSpecializationDecl>(RD))
Guy Benyei11169dd2012-12-18 14:30:41 +0000211 return RD->getName();
212
David Blaikie65813a32014-04-02 18:21:09 +0000213 SmallString<128> Name;
Benjamin Kramer9170e912013-02-22 15:46:01 +0000214 {
David Blaikie65813a32014-04-02 18:21:09 +0000215 llvm::raw_svector_ostream OS(Name);
216 RD->getNameForDiagnostic(OS, CGM.getContext().getPrintingPolicy(),
217 /*Qualified*/ false);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000218 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000219
220 // Copy this name on the side and use its reference.
David Blaikie65813a32014-04-02 18:21:09 +0000221 return internString(Name);
Guy Benyei11169dd2012-12-18 14:30:41 +0000222}
223
224/// getOrCreateFile - Get the file debug info descriptor for the input location.
225llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
226 if (!Loc.isValid())
227 // If Location is not valid then use main input file.
228 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
229
230 SourceManager &SM = CGM.getContext().getSourceManager();
231 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
232
233 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
234 // If the location is not valid then use main input file.
235 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
236
237 // Cache the results.
238 const char *fname = PLoc.getFilename();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000239 auto it = DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000240
241 if (it != DIFileCache.end()) {
242 // Verify that the information still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000243 if (llvm::Metadata *V = it->second)
Guy Benyei11169dd2012-12-18 14:30:41 +0000244 return llvm::DIFile(cast<llvm::MDNode>(V));
245 }
246
247 llvm::DIFile F = DBuilder.createFile(PLoc.getFilename(), getCurrentDirname());
248
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000249 DIFileCache[fname].reset(F);
Guy Benyei11169dd2012-12-18 14:30:41 +0000250 return F;
251}
252
253/// getOrCreateMainFile - Get the file info for main compile unit.
254llvm::DIFile CGDebugInfo::getOrCreateMainFile() {
255 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
256}
257
258/// getLineNumber - Get line number for the location. If location is invalid
259/// then use current location.
260unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
261 if (Loc.isInvalid() && CurLoc.isInvalid())
262 return 0;
263 SourceManager &SM = CGM.getContext().getSourceManager();
264 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000265 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000266}
267
268/// getColumnNumber - Get column number for the location.
Adrian Prantlc7822422013-03-12 20:43:25 +0000269unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000270 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000271 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000272 return 0;
273
274 // If the location is invalid then use the current column.
275 if (Loc.isInvalid() && CurLoc.isInvalid())
276 return 0;
277 SourceManager &SM = CGM.getContext().getSourceManager();
278 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000279 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000280}
281
282StringRef CGDebugInfo::getCurrentDirname() {
283 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
284 return CGM.getCodeGenOpts().DebugCompilationDir;
285
286 if (!CWDName.empty())
287 return CWDName;
288 SmallString<256> CWD;
289 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000290 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000291}
292
293/// CreateCompileUnit - Create new compile unit.
294void CGDebugInfo::CreateCompileUnit() {
295
David Blaikieaabde052014-05-14 00:29:00 +0000296 // Should we be asking the SourceManager for the main file name, instead of
297 // accepting it as an argument? This just causes the main file name to
298 // mismatch with source locations and create extra lexical scopes or
299 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
300 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
301 // because that's what the SourceManager says)
302
Guy Benyei11169dd2012-12-18 14:30:41 +0000303 // Get absolute path name.
304 SourceManager &SM = CGM.getContext().getSourceManager();
305 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
306 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000307 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000308
309 // The main file name provided via the "-main-file-name" option contains just
310 // the file name itself with no path information. This file name may have had
311 // a relative path, so we look into the actual file entry for the main
312 // file to determine the real absolute path for the file.
313 std::string MainFileDir;
314 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
315 MainFileDir = MainFile->getDir()->getName();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000316 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000317 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
318 llvm::sys::path::append(MainFileDirSS, MainFileName);
319 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000320 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000321 }
322
323 // Save filename string.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000324 StringRef Filename = internString(MainFileName);
Eric Christopherf1545832013-02-22 23:50:16 +0000325
326 // Save split dwarf file string.
327 std::string SplitDwarfFile = CGM.getCodeGenOpts().SplitDwarfFile;
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000328 StringRef SplitDwarfFilename = internString(SplitDwarfFile);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000329
Ed Masteda706022014-05-07 12:49:30 +0000330 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000331 const LangOptions &LO = CGM.getLangOpts();
332 if (LO.CPlusPlus) {
333 if (LO.ObjC1)
334 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
335 else
336 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
337 } else if (LO.ObjC1) {
338 LangTag = llvm::dwarf::DW_LANG_ObjC;
339 } else if (LO.C99) {
340 LangTag = llvm::dwarf::DW_LANG_C99;
341 } else {
342 LangTag = llvm::dwarf::DW_LANG_C89;
343 }
344
345 std::string Producer = getClangFullVersion();
346
347 // Figure out which version of the ObjC runtime we have.
348 unsigned RuntimeVers = 0;
349 if (LO.ObjC1)
350 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
351
352 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000353 // FIXME - Eliminate TheCU.
Eric Christophere4200a22014-02-27 01:25:08 +0000354 TheCU = DBuilder.createCompileUnit(
355 LangTag, Filename, getCurrentDirname(), Producer, LO.Optimize,
356 CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers, SplitDwarfFilename,
Diego Novillo913690c2014-06-24 17:02:17 +0000357 DebugKind <= CodeGenOptions::DebugLineTablesOnly
Eric Christophere4200a22014-02-27 01:25:08 +0000358 ? llvm::DIBuilder::LineTablesOnly
Diego Novillo913690c2014-06-24 17:02:17 +0000359 : llvm::DIBuilder::FullDebug,
360 DebugKind != CodeGenOptions::LocTrackingOnly);
Guy Benyei11169dd2012-12-18 14:30:41 +0000361}
362
363/// CreateType - Get the Basic type from the cache or create a new
364/// one if necessary.
365llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000366 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000367 StringRef BTName;
368 switch (BT->getKind()) {
369#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000370#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000371#include "clang/AST/BuiltinTypes.def"
372 case BuiltinType::Dependent:
373 llvm_unreachable("Unexpected builtin type");
374 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000375 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000376 case BuiltinType::Void:
377 return llvm::DIType();
378 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000379 if (!ClassTy)
380 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
381 "objc_class", TheCU,
382 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000383 return ClassTy;
384 case BuiltinType::ObjCId: {
385 // typedef struct objc_class *Class;
386 // typedef struct objc_object {
387 // Class isa;
388 // } *id;
389
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000390 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000391 return ObjTy;
392
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000393 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000394 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
395 "objc_class", TheCU,
396 getOrCreateMainFile(), 0);
397
398 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000399
Guy Benyei11169dd2012-12-18 14:30:41 +0000400 llvm::DIType ISATy = DBuilder.createPointerType(ClassTy, Size);
401
Eric Christopher5c7ee8b2013-04-02 22:59:11 +0000402 ObjTy =
David Blaikie6d4fe152013-02-25 01:07:08 +0000403 DBuilder.createStructType(TheCU, "objc_object", getOrCreateMainFile(),
404 0, 0, 0, 0, llvm::DIType(), llvm::DIArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000405
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000406 DBuilder.replaceArrays(
407 ObjTy,
408 DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
409 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0, 0, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000410 return ObjTy;
411 }
412 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000413 if (!SelTy)
414 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
415 "objc_selector", TheCU,
416 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000417 return SelTy;
418 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000419
420 case BuiltinType::OCLImage1d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000421 return getOrCreateStructPtrType("opencl_image1d_t", OCLImage1dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000422 case BuiltinType::OCLImage1dArray:
Eric Christopherb2a008c2013-05-16 00:45:12 +0000423 return getOrCreateStructPtrType("opencl_image1d_array_t",
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000424 OCLImage1dArrayDITy);
425 case BuiltinType::OCLImage1dBuffer:
426 return getOrCreateStructPtrType("opencl_image1d_buffer_t",
427 OCLImage1dBufferDITy);
428 case BuiltinType::OCLImage2d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000429 return getOrCreateStructPtrType("opencl_image2d_t", OCLImage2dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000430 case BuiltinType::OCLImage2dArray:
431 return getOrCreateStructPtrType("opencl_image2d_array_t",
432 OCLImage2dArrayDITy);
433 case BuiltinType::OCLImage3d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000434 return getOrCreateStructPtrType("opencl_image3d_t", OCLImage3dDITy);
Guy Benyei61054192013-02-07 10:55:47 +0000435 case BuiltinType::OCLSampler:
Eric Christophere7b87e52014-10-26 23:40:33 +0000436 return DBuilder.createBasicType(
437 "opencl_sampler_t", CGM.getContext().getTypeSize(BT),
438 CGM.getContext().getTypeAlign(BT), llvm::dwarf::DW_ATE_unsigned);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000439 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000440 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000441
Guy Benyei11169dd2012-12-18 14:30:41 +0000442 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000443 case BuiltinType::Char_U:
444 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
445 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000446 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000447 case BuiltinType::SChar:
448 Encoding = llvm::dwarf::DW_ATE_signed_char;
449 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000450 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000451 case BuiltinType::Char32:
452 Encoding = llvm::dwarf::DW_ATE_UTF;
453 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000454 case BuiltinType::UShort:
455 case BuiltinType::UInt:
456 case BuiltinType::UInt128:
457 case BuiltinType::ULong:
458 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000459 case BuiltinType::ULongLong:
460 Encoding = llvm::dwarf::DW_ATE_unsigned;
461 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000462 case BuiltinType::Short:
463 case BuiltinType::Int:
464 case BuiltinType::Int128:
465 case BuiltinType::Long:
466 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000467 case BuiltinType::LongLong:
468 Encoding = llvm::dwarf::DW_ATE_signed;
469 break;
470 case BuiltinType::Bool:
471 Encoding = llvm::dwarf::DW_ATE_boolean;
472 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000473 case BuiltinType::Half:
474 case BuiltinType::Float:
475 case BuiltinType::LongDouble:
Eric Christophere7b87e52014-10-26 23:40:33 +0000476 case BuiltinType::Double:
477 Encoding = llvm::dwarf::DW_ATE_float;
478 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000479 }
480
481 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000482 case BuiltinType::Long:
483 BTName = "long int";
484 break;
485 case BuiltinType::LongLong:
486 BTName = "long long int";
487 break;
488 case BuiltinType::ULong:
489 BTName = "long unsigned int";
490 break;
491 case BuiltinType::ULongLong:
492 BTName = "long long unsigned int";
493 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000494 default:
495 BTName = BT->getName(CGM.getLangOpts());
496 break;
497 }
498 // Bit size, align and offset of the type.
499 uint64_t Size = CGM.getContext().getTypeSize(BT);
500 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Eric Christophere7b87e52014-10-26 23:40:33 +0000501 llvm::DIType DbgTy = DBuilder.createBasicType(BTName, Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000502 return DbgTy;
503}
504
505llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty) {
506 // Bit size, align and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000507 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000508 if (Ty->isComplexIntegerType())
509 Encoding = llvm::dwarf::DW_ATE_lo_user;
510
511 uint64_t Size = CGM.getContext().getTypeSize(Ty);
512 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000513 llvm::DIType DbgTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000514 DBuilder.createBasicType("complex", Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000515
516 return DbgTy;
517}
518
519/// CreateCVRType - Get the qualified type from the cache or create
520/// a new one if necessary.
David Blaikie99dab3b2013-09-04 22:03:57 +0000521llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000522 QualifierCollector Qc;
523 const Type *T = Qc.strip(Ty);
524
525 // Ignore these qualifiers for now.
526 Qc.removeObjCGCAttr();
527 Qc.removeAddressSpace();
528 Qc.removeObjCLifetime();
529
530 // We will create one Derived type for one qualifier and recurse to handle any
531 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000532 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000533 if (Qc.hasConst()) {
534 Tag = llvm::dwarf::DW_TAG_const_type;
535 Qc.removeConst();
536 } else if (Qc.hasVolatile()) {
537 Tag = llvm::dwarf::DW_TAG_volatile_type;
538 Qc.removeVolatile();
539 } else if (Qc.hasRestrict()) {
540 Tag = llvm::dwarf::DW_TAG_restrict_type;
541 Qc.removeRestrict();
542 } else {
543 assert(Qc.empty() && "Unknown type qualifier for debug info");
544 return getOrCreateType(QualType(T, 0), Unit);
545 }
546
David Blaikie99dab3b2013-09-04 22:03:57 +0000547 llvm::DIType FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000548
549 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
550 // CVR derived types.
551 llvm::DIType DbgTy = DBuilder.createQualifiedType(Tag, FromTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000552
Guy Benyei11169dd2012-12-18 14:30:41 +0000553 return DbgTy;
554}
555
556llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
557 llvm::DIFile Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000558
559 // The frontend treats 'id' as a typedef to an ObjCObjectType,
560 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
561 // debug info, we want to emit 'id' in both cases.
562 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000563 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000564
Eric Christophere7b87e52014-10-26 23:40:33 +0000565 llvm::DIType DbgTy = CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type,
566 Ty, Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000567 return DbgTy;
568}
569
Eric Christophere7b87e52014-10-26 23:40:33 +0000570llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty, llvm::DIFile Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000571 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000572 Ty->getPointeeType(), Unit);
573}
574
Manman Rene0064d82013-08-29 23:19:58 +0000575/// In C++ mode, types have linkage, so we can rely on the ODR and
576/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000577static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
578 CodeGenModule &CGM,
579 llvm::DICompileUnit TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000580 SmallString<256> FullName;
581 // FIXME: ODR should apply to ObjC++ exactly the same wasy it does to C++.
582 // For now, only apply ODR with C++.
583 const TagDecl *TD = Ty->getDecl();
584 if (TheCU.getLanguage() != llvm::dwarf::DW_LANG_C_plus_plus ||
585 !TD->isExternallyVisible())
586 return FullName;
587 // Microsoft Mangler does not have support for mangleCXXRTTIName yet.
588 if (CGM.getTarget().getCXXABI().isMicrosoft())
589 return FullName;
590
591 // TODO: This is using the RTTI name. Is there a better way to get
592 // a unique string for a type?
593 llvm::raw_svector_ostream Out(FullName);
594 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
595 Out.flush();
596 return FullName;
597}
598
Guy Benyei11169dd2012-12-18 14:30:41 +0000599// Creates a forward declaration for a RecordDecl in the given context.
David Blaikie8d5e1282013-08-20 21:03:29 +0000600llvm::DICompositeType
Manman Ren1b457022013-08-28 21:20:28 +0000601CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
David Blaikie8d5e1282013-08-20 21:03:29 +0000602 llvm::DIDescriptor Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000603 const RecordDecl *RD = Ty->getDecl();
David Blaikie4e7ef802013-08-15 20:17:25 +0000604 if (llvm::DIType T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
David Blaikie8d5e1282013-08-20 21:03:29 +0000605 return llvm::DICompositeType(T);
Guy Benyei11169dd2012-12-18 14:30:41 +0000606 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
607 unsigned Line = getLineNumber(RD->getLocation());
608 StringRef RDName = getClassName(RD);
609
Ed Masteda706022014-05-07 12:49:30 +0000610 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000611 if (RD->isStruct() || RD->isInterface())
612 Tag = llvm::dwarf::DW_TAG_structure_type;
613 else if (RD->isUnion())
614 Tag = llvm::dwarf::DW_TAG_union_type;
615 else {
616 assert(RD->isClass());
617 Tag = llvm::dwarf::DW_TAG_class_type;
618 }
619
620 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000621 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
David Blaikief427b002014-05-06 03:42:01 +0000622 llvm::DICompositeType RetTy = DBuilder.createReplaceableForwardDecl(
623 Tag, RDName, Ctx, DefUnit, Line, 0, 0, 0, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000624 ReplaceMap.emplace_back(
625 std::piecewise_construct, std::make_tuple(Ty),
626 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000627 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000628}
629
Ed Masteda706022014-05-07 12:49:30 +0000630llvm::DIType CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Eric Christopherb2a008c2013-05-16 00:45:12 +0000631 const Type *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000632 QualType PointeeTy,
633 llvm::DIFile Unit) {
634 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
635 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
David Blaikie99dab3b2013-09-04 22:03:57 +0000636 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit));
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000637
Guy Benyei11169dd2012-12-18 14:30:41 +0000638 // Bit size, align and offset of the type.
639 // Size is always the size of a pointer. We can't use getTypeSize here
640 // because that does not return the correct value for references.
641 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000642 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000643 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
644
David Blaikie99dab3b2013-09-04 22:03:57 +0000645 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
646 Align);
Guy Benyei11169dd2012-12-18 14:30:41 +0000647}
648
Eric Christopher0fdcb312013-05-16 00:52:20 +0000649llvm::DIType CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
650 llvm::DIType &Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000651 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000652 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000653 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
654 TheCU, getOrCreateMainFile(), 0);
655 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
656 Cache = DBuilder.createPointerType(Cache, Size);
657 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000658}
659
Guy Benyei11169dd2012-12-18 14:30:41 +0000660llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
661 llvm::DIFile Unit) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000662 if (BlockLiteralGeneric)
Guy Benyei11169dd2012-12-18 14:30:41 +0000663 return BlockLiteralGeneric;
664
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000665 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000666 llvm::DIType FieldTy;
667 QualType FType;
668 uint64_t FieldSize, FieldOffset;
669 unsigned FieldAlign;
670 llvm::DIArray Elements;
671 llvm::DIType EltTy, DescTy;
672
673 FieldOffset = 0;
674 FType = CGM.getContext().UnsignedLongTy;
675 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
676 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
677
678 Elements = DBuilder.getOrCreateArray(EltTys);
679 EltTys.clear();
680
681 unsigned Flags = llvm::DIDescriptor::FlagAppleBlock;
682 unsigned LineNo = getLineNumber(CurLoc);
683
Eric Christophere7b87e52014-10-26 23:40:33 +0000684 EltTy = DBuilder.createStructType(Unit, "__block_descriptor", Unit, LineNo,
685 FieldOffset, 0, Flags, llvm::DIType(),
686 Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000687
688 // Bit size, align and offset of the type.
689 uint64_t Size = CGM.getContext().getTypeSize(Ty);
690
691 DescTy = DBuilder.createPointerType(EltTy, Size);
692
693 FieldOffset = 0;
694 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
695 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
696 FType = CGM.getContext().IntTy;
697 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
698 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000699 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000700 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
701
702 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
703 FieldTy = DescTy;
704 FieldSize = CGM.getContext().getTypeSize(Ty);
705 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Eric Christophere7b87e52014-10-26 23:40:33 +0000706 FieldTy =
707 DBuilder.createMemberType(Unit, "__descriptor", Unit, LineNo, FieldSize,
708 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000709 EltTys.push_back(FieldTy);
710
711 FieldOffset += FieldSize;
712 Elements = DBuilder.getOrCreateArray(EltTys);
713
Eric Christophere7b87e52014-10-26 23:40:33 +0000714 EltTy = DBuilder.createStructType(Unit, "__block_literal_generic", Unit,
715 LineNo, FieldOffset, 0, Flags,
716 llvm::DIType(), Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000717
Guy Benyei11169dd2012-12-18 14:30:41 +0000718 BlockLiteralGeneric = DBuilder.createPointerType(EltTy, Size);
719 return BlockLiteralGeneric;
720}
721
Eric Christophere7b87e52014-10-26 23:40:33 +0000722llvm::DIType CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
723 llvm::DIFile Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000724 assert(Ty->isTypeAlias());
725 llvm::DIType Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000726
727 SmallString<128> NS;
728 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000729 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
730 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000731
732 TemplateSpecializationType::PrintTemplateArgumentList(
733 OS, Ty->getArgs(), Ty->getNumArgs(),
734 CGM.getContext().getPrintingPolicy());
735
Eric Christophere7b87e52014-10-26 23:40:33 +0000736 TypeAliasDecl *AliasDecl = cast<TypeAliasTemplateDecl>(
737 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000738
739 SourceLocation Loc = AliasDecl->getLocation();
740 llvm::DIFile File = getOrCreateFile(Loc);
741 unsigned Line = getLineNumber(Loc);
742
Eric Christophere7b87e52014-10-26 23:40:33 +0000743 llvm::DIDescriptor Ctxt =
744 getContextDescriptor(cast<Decl>(AliasDecl->getDeclContext()));
David Blaikief1b382e2014-04-06 17:14:06 +0000745
746 return DBuilder.createTypedef(Src, internString(OS.str()), File, Line, Ctxt);
747}
748
David Blaikie99dab3b2013-09-04 22:03:57 +0000749llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000750 // Typedefs are derived from some other type. If we have a typedef of a
751 // typedef, make sure to emit the whole chain.
David Blaikie99dab3b2013-09-04 22:03:57 +0000752 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000753 // We don't set size information, but do specify where the typedef was
754 // declared.
Adrian Prantl3eff2252014-01-21 18:42:27 +0000755 SourceLocation Loc = Ty->getDecl()->getLocation();
756 llvm::DIFile File = getOrCreateFile(Loc);
757 unsigned Line = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000758 const TypedefNameDecl *TyDecl = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000759
Guy Benyei11169dd2012-12-18 14:30:41 +0000760 llvm::DIDescriptor TypedefContext =
Eric Christophere7b87e52014-10-26 23:40:33 +0000761 getContextDescriptor(cast<Decl>(Ty->getDecl()->getDeclContext()));
Eric Christopherb2a008c2013-05-16 00:45:12 +0000762
Eric Christophere7b87e52014-10-26 23:40:33 +0000763 return DBuilder.createTypedef(Src, TyDecl->getName(), File, Line,
764 TypedefContext);
Guy Benyei11169dd2012-12-18 14:30:41 +0000765}
766
767llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
768 llvm::DIFile Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000769 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000770
771 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000772 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000773
774 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000775 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000776 if (isa<FunctionNoProtoType>(Ty))
777 EltTys.push_back(DBuilder.createUnspecifiedParameter());
778 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
Alp Toker9cacbab2014-01-20 20:26:09 +0000779 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
780 EltTys.push_back(getOrCreateType(FPT->getParamType(i), Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000781 if (FPT->isVariadic())
782 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000783 }
784
Manman Ren67f005e2014-07-28 22:24:34 +0000785 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Guy Benyei11169dd2012-12-18 14:30:41 +0000786 return DBuilder.createSubroutineType(Unit, EltTypeArray);
787}
788
Adrian Prantl21361fb2014-08-29 22:44:27 +0000789/// Convert an AccessSpecifier into the corresponding DIDescriptor flag.
790/// As an optimization, return 0 if the access specifier equals the
791/// default for the containing type.
792static unsigned getAccessFlag(AccessSpecifier Access, const RecordDecl *RD) {
793 AccessSpecifier Default = clang::AS_none;
794 if (RD && RD->isClass())
795 Default = clang::AS_private;
796 else if (RD && (RD->isStruct() || RD->isUnion()))
797 Default = clang::AS_public;
798
799 if (Access == Default)
800 return 0;
801
Eric Christophere7b87e52014-10-26 23:40:33 +0000802 switch (Access) {
803 case clang::AS_private:
804 return llvm::DIDescriptor::FlagPrivate;
805 case clang::AS_protected:
806 return llvm::DIDescriptor::FlagProtected;
807 case clang::AS_public:
808 return llvm::DIDescriptor::FlagPublic;
809 case clang::AS_none:
810 return 0;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000811 }
812 llvm_unreachable("unexpected access enumerator");
813}
Guy Benyei11169dd2012-12-18 14:30:41 +0000814
Eric Christophere7b87e52014-10-26 23:40:33 +0000815llvm::DIType CGDebugInfo::createFieldType(
816 StringRef name, QualType type, uint64_t sizeInBitsOverride,
817 SourceLocation loc, AccessSpecifier AS, uint64_t offsetInBits,
818 llvm::DIFile tunit, llvm::DIScope scope, const RecordDecl *RD) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000819 llvm::DIType debugType = getOrCreateType(type, tunit);
820
821 // Get the location for the field.
822 llvm::DIFile file = getOrCreateFile(loc);
823 unsigned line = getLineNumber(loc);
824
David Majnemer34b57492014-07-30 01:30:47 +0000825 uint64_t SizeInBits = 0;
826 unsigned AlignInBits = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000827 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +0000828 TypeInfo TI = CGM.getContext().getTypeInfo(type);
829 SizeInBits = TI.Width;
830 AlignInBits = TI.Align;
Guy Benyei11169dd2012-12-18 14:30:41 +0000831
832 if (sizeInBitsOverride)
David Majnemer34b57492014-07-30 01:30:47 +0000833 SizeInBits = sizeInBitsOverride;
Guy Benyei11169dd2012-12-18 14:30:41 +0000834 }
835
Adrian Prantl21361fb2014-08-29 22:44:27 +0000836 unsigned flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +0000837 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
838 AlignInBits, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +0000839}
840
Eric Christopher91a31902013-01-16 01:22:32 +0000841/// CollectRecordLambdaFields - Helper for CollectRecordFields.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000842void CGDebugInfo::CollectRecordLambdaFields(
843 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
844 llvm::DIType RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +0000845 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
846 // has the name and the location of the variable so we should iterate over
847 // both concurrently.
848 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
849 RecordDecl::field_iterator Field = CXXDecl->field_begin();
850 unsigned fieldno = 0;
851 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +0000852 E = CXXDecl->captures_end();
853 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000854 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +0000855 if (C.capturesVariable()) {
856 VarDecl *V = C.getCapturedVar();
857 llvm::DIFile VUnit = getOrCreateFile(C.getLocation());
858 StringRef VName = V->getName();
859 uint64_t SizeInBitsOverride = 0;
860 if (Field->isBitField()) {
861 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
862 assert(SizeInBitsOverride && "found named 0-width bitfield");
863 }
Eric Christophere7b87e52014-10-26 23:40:33 +0000864 llvm::DIType fieldType = createFieldType(
865 VName, Field->getType(), SizeInBitsOverride, C.getLocation(),
866 Field->getAccess(), layout.getFieldOffset(fieldno), VUnit, RecordTy,
867 CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000868 elements.push_back(fieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +0000869 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +0000870 // TODO: Need to handle 'this' in some way by probably renaming the
871 // this of the lambda class and having a field member of 'this' or
872 // by using AT_object_pointer for the function and having that be
873 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +0000874 FieldDecl *f = *Field;
875 llvm::DIFile VUnit = getOrCreateFile(f->getLocation());
876 QualType type = f->getType();
Eric Christophere7b87e52014-10-26 23:40:33 +0000877 llvm::DIType fieldType = createFieldType(
878 "this", type, 0, f->getLocation(), f->getAccess(),
879 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000880
881 elements.push_back(fieldType);
882 }
883 }
884}
885
David Blaikie6943dea2013-08-20 01:28:15 +0000886/// Helper for CollectRecordFields.
Eric Christophere7b87e52014-10-26 23:40:33 +0000887llvm::DIDerivedType CGDebugInfo::CreateRecordStaticField(const VarDecl *Var,
888 llvm::DIType RecordTy,
889 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000890 // Create the descriptor for the static variable, with or without
891 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +0000892 Var = Var->getCanonicalDecl();
Eric Christopher91a31902013-01-16 01:22:32 +0000893 llvm::DIFile VUnit = getOrCreateFile(Var->getLocation());
894 llvm::DIType VTy = getOrCreateType(Var->getType(), VUnit);
895
Eric Christopher91a31902013-01-16 01:22:32 +0000896 unsigned LineNumber = getLineNumber(Var->getLocation());
897 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +0000898 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +0000899 if (Var->getInit()) {
900 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +0000901 if (Value) {
902 if (Value->isInt())
903 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
904 if (Value->isFloat())
905 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
906 }
Eric Christopher91a31902013-01-16 01:22:32 +0000907 }
908
Adrian Prantl21361fb2014-08-29 22:44:27 +0000909 unsigned Flags = getAccessFlag(Var->getAccess(), RD);
David Blaikieae019462013-08-15 22:50:29 +0000910 llvm::DIDerivedType GV = DBuilder.createStaticMemberType(
911 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000912 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +0000913 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +0000914}
915
916/// CollectRecordNormalField - Helper for CollectRecordFields.
Eric Christophere7b87e52014-10-26 23:40:33 +0000917void CGDebugInfo::CollectRecordNormalField(
918 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000919 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +0000920 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000921 StringRef name = field->getName();
922 QualType type = field->getType();
923
924 // Ignore unnamed fields unless they're anonymous structs/unions.
925 if (name.empty() && !type->isRecordType())
926 return;
927
928 uint64_t SizeInBitsOverride = 0;
929 if (field->isBitField()) {
930 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
931 assert(SizeInBitsOverride && "found named 0-width bitfield");
932 }
933
Eric Christophere7b87e52014-10-26 23:40:33 +0000934 llvm::DIType fieldType =
935 createFieldType(name, type, SizeInBitsOverride, field->getLocation(),
936 field->getAccess(), OffsetInBits, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +0000937
938 elements.push_back(fieldType);
939}
940
Guy Benyei11169dd2012-12-18 14:30:41 +0000941/// CollectRecordFields - A helper function to collect debug info for
942/// record fields. This is used while creating debug info entry for a Record.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000943void CGDebugInfo::CollectRecordFields(
944 const RecordDecl *record, llvm::DIFile tunit,
945 SmallVectorImpl<llvm::Metadata *> &elements,
946 llvm::DICompositeType RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000947 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
948
Eric Christopher91a31902013-01-16 01:22:32 +0000949 if (CXXDecl && CXXDecl->isLambda())
950 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
951 else {
952 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +0000953
Eric Christopher91a31902013-01-16 01:22:32 +0000954 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +0000955 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +0000956
Eric Christopher91a31902013-01-16 01:22:32 +0000957 // Static and non-static members should appear in the same order as
958 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +0000959 for (const auto *I : record->decls())
960 if (const auto *V = dyn_cast<VarDecl>(I)) {
David Blaikiece763042013-08-20 21:49:21 +0000961 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000962 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +0000963 if (MI != StaticDataMemberCache.end()) {
964 assert(MI->second &&
965 "Static data member declaration should still exist");
966 elements.push_back(
967 llvm::DIDerivedType(cast<llvm::MDNode>(MI->second)));
Adrian Prantl21361fb2014-08-29 22:44:27 +0000968 } else {
969 auto Field = CreateRecordStaticField(V, RecordTy, record);
970 elements.push_back(Field);
971 }
Aaron Ballman629afae2014-03-07 19:56:05 +0000972 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000973 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
974 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +0000975
976 // Bump field number for next field.
977 ++fieldNo;
Guy Benyei11169dd2012-12-18 14:30:41 +0000978 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000979 }
980}
981
982/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
983/// function type is not updated to include implicit "this" pointer. Use this
984/// routine to get a method type which includes "this" pointer.
David Blaikie469f0792013-05-22 23:22:42 +0000985llvm::DICompositeType
Guy Benyei11169dd2012-12-18 14:30:41 +0000986CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
987 llvm::DIFile Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +0000988 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +0000989 if (Method->isStatic())
David Blaikie469f0792013-05-22 23:22:42 +0000990 return llvm::DICompositeType(getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +0000991 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
992 Func, Unit);
993}
David Blaikie2aaf0652013-01-07 22:24:59 +0000994
David Blaikie469f0792013-05-22 23:22:42 +0000995llvm::DICompositeType CGDebugInfo::getOrCreateInstanceMethodType(
David Blaikie7eb06852013-01-07 23:06:35 +0000996 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000997 // Add "this" pointer.
Manman Ren67f005e2014-07-28 22:24:34 +0000998 llvm::DITypeArray Args = llvm::DISubroutineType(
999 getOrCreateType(QualType(Func, 0), Unit)).getTypeArray();
Eric Christophere7b87e52014-10-26 23:40:33 +00001000 assert(Args.getNumElements() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001001
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001002 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001003
1004 // First element is always return type. For 'void' functions it is NULL.
1005 Elts.push_back(Args.getElement(0));
1006
David Blaikie2aaf0652013-01-07 22:24:59 +00001007 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001008 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001009 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1010 // Create pointer type directly in this case.
1011 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1012 QualType PointeeTy = ThisPtrTy->getPointeeType();
1013 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001014 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie2aaf0652013-01-07 22:24:59 +00001015 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
1016 llvm::DIType PointeeType = getOrCreateType(PointeeTy, Unit);
Eric Christopher0fdcb312013-05-16 00:52:20 +00001017 llvm::DIType ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001018 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001019 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001020 // TODO: This and the artificial type below are misleading, the
1021 // types aren't artificial the argument is, but the current
1022 // metadata doesn't represent that.
1023 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1024 Elts.push_back(ThisPtrType);
1025 } else {
1026 llvm::DIType ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001027 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001028 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1029 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001030 }
1031
1032 // Copy rest of the arguments.
1033 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
1034 Elts.push_back(Args.getElement(i));
1035
Manman Ren67f005e2014-07-28 22:24:34 +00001036 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001037
Adrian Prantl0630eb72013-12-18 21:48:18 +00001038 unsigned Flags = 0;
1039 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
1040 Flags |= llvm::DIDescriptor::FlagLValueReference;
1041 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
1042 Flags |= llvm::DIDescriptor::FlagRValueReference;
1043
1044 return DBuilder.createSubroutineType(Unit, EltTypeArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001045}
1046
Eric Christopherb2a008c2013-05-16 00:45:12 +00001047/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001048/// inside a function.
1049static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1050 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1051 return isFunctionLocalClass(NRD);
1052 if (isa<FunctionDecl>(RD->getDeclContext()))
1053 return true;
1054 return false;
1055}
1056
1057/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
1058/// a single member function GlobalDecl.
1059llvm::DISubprogram
1060CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Eric Christophere7b87e52014-10-26 23:40:33 +00001061 llvm::DIFile Unit, llvm::DIType RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001062 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001063 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001064
Guy Benyei11169dd2012-12-18 14:30:41 +00001065 StringRef MethodName = getFunctionName(Method);
David Blaikie469f0792013-05-22 23:22:42 +00001066 llvm::DICompositeType MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001067
1068 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1069 // make sense to give a single ctor/dtor a linkage name.
1070 StringRef MethodLinkageName;
1071 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1072 MethodLinkageName = CGM.getMangledName(Method);
1073
1074 // Get the location for the method.
David Blaikie7fceebf2013-08-19 03:37:48 +00001075 llvm::DIFile MethodDefUnit;
1076 unsigned MethodLine = 0;
1077 if (!Method->isImplicit()) {
1078 MethodDefUnit = getOrCreateFile(Method->getLocation());
1079 MethodLine = getLineNumber(Method->getLocation());
1080 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001081
1082 // Collect virtual method info.
1083 llvm::DIType ContainingType;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001084 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001085 unsigned VIndex = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001086
Guy Benyei11169dd2012-12-18 14:30:41 +00001087 if (Method->isVirtual()) {
1088 if (Method->isPure())
1089 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1090 else
1091 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001092
Guy Benyei11169dd2012-12-18 14:30:41 +00001093 // It doesn't make sense to give a virtual destructor a vtable index,
1094 // since a single destructor has two entries in the vtable.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001095 // FIXME: Add proper support for debug info for virtual calls in
1096 // the Microsoft ABI, where we may use multiple vptrs to make a vftable
1097 // lookup if we have multiple or virtual inheritance.
1098 if (!isa<CXXDestructorDecl>(Method) &&
1099 !CGM.getTarget().getCXXABI().isMicrosoft())
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001100 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
Guy Benyei11169dd2012-12-18 14:30:41 +00001101 ContainingType = RecordTy;
1102 }
1103
1104 unsigned Flags = 0;
1105 if (Method->isImplicit())
1106 Flags |= llvm::DIDescriptor::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001107 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
Guy Benyei11169dd2012-12-18 14:30:41 +00001108 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1109 if (CXXC->isExplicit())
1110 Flags |= llvm::DIDescriptor::FlagExplicit;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001111 } else if (const CXXConversionDecl *CXXC =
Eric Christophere7b87e52014-10-26 23:40:33 +00001112 dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001113 if (CXXC->isExplicit())
1114 Flags |= llvm::DIDescriptor::FlagExplicit;
1115 }
1116 if (Method->hasPrototype())
1117 Flags |= llvm::DIDescriptor::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001118 if (Method->getRefQualifier() == RQ_LValue)
1119 Flags |= llvm::DIDescriptor::FlagLValueReference;
1120 if (Method->getRefQualifier() == RQ_RValue)
1121 Flags |= llvm::DIDescriptor::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001122
1123 llvm::DIArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
Eric Christophere7b87e52014-10-26 23:40:33 +00001124 llvm::DISubprogram SP = DBuilder.createMethod(
1125 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
1126 MethodTy, /*isLocalToUnit=*/false,
1127 /* isDefinition=*/false, Virtuality, VIndex, ContainingType, Flags,
1128 CGM.getLangOpts().Optimize, nullptr, TParamsArray);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001129
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001130 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001131
1132 return SP;
1133}
1134
1135/// CollectCXXMemberFunctions - A helper function to collect debug info for
Eric Christopherb2a008c2013-05-16 00:45:12 +00001136/// C++ member functions. This is used while creating debug info entry for
Guy Benyei11169dd2012-12-18 14:30:41 +00001137/// a Record.
Eric Christophere7b87e52014-10-26 23:40:33 +00001138void CGDebugInfo::CollectCXXMemberFunctions(
1139 const CXXRecordDecl *RD, llvm::DIFile Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001140 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001141
1142 // Since we want more than just the individual member decls if we
1143 // have templated functions iterate over every declaration to gather
1144 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001145 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001146 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1147 // If the member is implicit, don't add it to the member list. This avoids
1148 // the member being added to type units by LLVM, while still allowing it
1149 // to be emitted into the type declaration/reference inside the compile
1150 // unit.
David Blaikie6dddfe32014-10-06 05:52:27 +00001151 // FIXME: Handle Using(Shadow?)Decls here to create
1152 // DW_TAG_imported_declarations inside the class for base decls brought into
1153 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1154 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1155 // referenced)
David Blaikiefd580722014-10-06 05:18:55 +00001156 if (!Method || Method->isImplicit())
1157 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001158
1159 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1160 continue;
1161
David Blaikiefd580722014-10-06 05:18:55 +00001162 // Reuse the existing member function declaration if it exists.
1163 // It may be associated with the declaration of the type & should be
1164 // reused as we're building the definition.
1165 //
1166 // This situation can arise in the vtable-based debug info reduction where
1167 // implicit members are emitted in a non-vtable TU.
1168 auto MI = SPCache.find(Method->getCanonicalDecl());
1169 EltTys.push_back(MI == SPCache.end()
1170 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001171 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001172 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001173}
Guy Benyei11169dd2012-12-18 14:30:41 +00001174
Guy Benyei11169dd2012-12-18 14:30:41 +00001175/// CollectCXXBases - A helper function to collect debug info for
Eric Christopherb2a008c2013-05-16 00:45:12 +00001176/// C++ base classes. This is used while creating debug info entry for
Guy Benyei11169dd2012-12-18 14:30:41 +00001177/// a Record.
Eric Christophere7b87e52014-10-26 23:40:33 +00001178void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001179 SmallVectorImpl<llvm::Metadata *> &EltTys,
Eric Christophere7b87e52014-10-26 23:40:33 +00001180 llvm::DIType RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001181
1182 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Aaron Ballman574705e2014-03-13 15:41:46 +00001183 for (const auto &BI : RD->bases()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001184 unsigned BFlags = 0;
1185 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001186
Guy Benyei11169dd2012-12-18 14:30:41 +00001187 const CXXRecordDecl *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001188 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001189
Aaron Ballman574705e2014-03-13 15:41:46 +00001190 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001191 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1192 // virtual base offset offset is -ve. The code generator emits dwarf
1193 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001194 BaseOffset = 0 - CGM.getItaniumVTableContext()
1195 .getVirtualBaseOffsetOffset(RD, Base)
1196 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001197 } else {
1198 // In the MS ABI, store the vbtable offset, which is analogous to the
1199 // vbase offset offset in Itanium.
1200 BaseOffset =
1201 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1202 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001203 BFlags = llvm::DIDescriptor::FlagVirtual;
1204 } else
1205 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1206 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1207 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001208
Adrian Prantl21361fb2014-08-29 22:44:27 +00001209 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001210 llvm::DIType DTy = DBuilder.createInheritance(
1211 RecordTy, getOrCreateType(BI.getType(), Unit), BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001212 EltTys.push_back(DTy);
1213 }
1214}
1215
1216/// CollectTemplateParams - A helper function to collect template parameters.
Eric Christophere7b87e52014-10-26 23:40:33 +00001217llvm::DIArray
1218CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1219 ArrayRef<TemplateArgument> TAList,
1220 llvm::DIFile Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001221 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001222 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1223 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001224 StringRef Name;
1225 if (TPList)
1226 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001227 switch (TA.getKind()) {
1228 case TemplateArgument::Type: {
Guy Benyei11169dd2012-12-18 14:30:41 +00001229 llvm::DIType TTy = getOrCreateType(TA.getAsType(), Unit);
1230 llvm::DITemplateTypeParameter TTP =
David Blaikie47c11502013-06-22 18:59:18 +00001231 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00001232 TemplateParams.push_back(TTP);
David Blaikie38079fd2013-05-10 21:53:14 +00001233 } break;
1234 case TemplateArgument::Integral: {
Guy Benyei11169dd2012-12-18 14:30:41 +00001235 llvm::DIType TTy = getOrCreateType(TA.getIntegralType(), Unit);
1236 llvm::DITemplateValueParameter TVP =
David Blaikie38079fd2013-05-10 21:53:14 +00001237 DBuilder.createTemplateValueParameter(
David Blaikie47c11502013-06-22 18:59:18 +00001238 TheCU, Name, TTy,
David Blaikie38079fd2013-05-10 21:53:14 +00001239 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral()));
1240 TemplateParams.push_back(TVP);
1241 } break;
1242 case TemplateArgument::Declaration: {
1243 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001244 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
David Blaikie38079fd2013-05-10 21:53:14 +00001245 llvm::DIType TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001246 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001247 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001248 // Variable pointer template parameters have a value that is the address
1249 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001250 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001251 V = CGM.GetAddrOfGlobalVar(VD);
1252 // Member function pointers have special support for building them, though
1253 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001254 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Blaikie0a7c9d52014-10-20 20:29:35 +00001255 V = CGM.getCXXABI().EmitMemberPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001256 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001257 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001258 // Member data pointers have special handling too to compute the fixed
1259 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001260 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001261 // These five lines (& possibly the above member function pointer
1262 // handling) might be able to be refactored to use similar code in
1263 // CodeGenModule::getMemberPointerConstant
1264 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1265 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001266 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001267 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001268 }
1269 llvm::DITemplateValueParameter TVP =
Duncan P. N. Exon Smith2f68dad2014-11-15 00:24:50 +00001270 DBuilder.createTemplateValueParameter(
1271 TheCU, Name, TTy,
1272 cast_or_null<llvm::Constant>(V->stripPointerCasts()));
David Blaikie38079fd2013-05-10 21:53:14 +00001273 TemplateParams.push_back(TVP);
1274 } break;
1275 case TemplateArgument::NullPtr: {
1276 QualType T = TA.getNullPtrType();
1277 llvm::DIType TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001278 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001279 // Special case member data pointer null values since they're actually -1
1280 // instead of zero.
1281 if (const MemberPointerType *MPT =
1282 dyn_cast<MemberPointerType>(T.getTypePtr()))
1283 // But treat member function pointers as simple zero integers because
1284 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1285 // CodeGen grows handling for values of non-null member function
1286 // pointers then perhaps we could remove this special case and rely on
1287 // EmitNullMemberPointer for member function pointers.
1288 if (MPT->isMemberDataPointer())
1289 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1290 if (!V)
1291 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
1292 llvm::DITemplateValueParameter TVP =
Duncan P. N. Exon Smith2f68dad2014-11-15 00:24:50 +00001293 DBuilder.createTemplateValueParameter(TheCU, Name, TTy,
1294 cast<llvm::Constant>(V));
David Blaikie38079fd2013-05-10 21:53:14 +00001295 TemplateParams.push_back(TVP);
1296 } break;
David Blaikie47c11502013-06-22 18:59:18 +00001297 case TemplateArgument::Template: {
Eric Christophere7b87e52014-10-26 23:40:33 +00001298 llvm::DITemplateValueParameter
1299 TVP = DBuilder.createTemplateTemplateParameter(
1300 TheCU, Name, llvm::DIType(),
1301 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString());
David Blaikie47c11502013-06-22 18:59:18 +00001302 TemplateParams.push_back(TVP);
1303 } break;
1304 case TemplateArgument::Pack: {
Eric Christophere7b87e52014-10-26 23:40:33 +00001305 llvm::DITemplateValueParameter TVP = DBuilder.createTemplateParameterPack(
1306 TheCU, Name, llvm::DIType(),
1307 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit));
David Blaikie47c11502013-06-22 18:59:18 +00001308 TemplateParams.push_back(TVP);
1309 } break;
David Majnemer5559d472013-08-24 08:21:10 +00001310 case TemplateArgument::Expression: {
1311 const Expr *E = TA.getAsExpr();
1312 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001313 if (E->isGLValue())
1314 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001315 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001316 assert(V && "Expression in template argument isn't constant");
1317 llvm::DIType TTy = getOrCreateType(T, Unit);
1318 llvm::DITemplateValueParameter TVP =
Duncan P. N. Exon Smith2f68dad2014-11-15 00:24:50 +00001319 DBuilder.createTemplateValueParameter(
1320 TheCU, Name, TTy, cast<llvm::Constant>(V->stripPointerCasts()));
David Majnemer5559d472013-08-24 08:21:10 +00001321 TemplateParams.push_back(TVP);
1322 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001323 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001324 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001325 case TemplateArgument::Null:
1326 llvm_unreachable(
1327 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001328 }
1329 }
1330 return DBuilder.getOrCreateArray(TemplateParams);
1331}
1332
1333/// CollectFunctionTemplateParams - A helper function to collect debug
1334/// info for function template parameters.
Eric Christophere7b87e52014-10-26 23:40:33 +00001335llvm::DIArray CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
1336 llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001337 if (FD->getTemplatedKind() ==
1338 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001339 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1340 ->getTemplate()
1341 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001342 return CollectTemplateParams(
1343 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001344 }
1345 return llvm::DIArray();
1346}
1347
1348/// CollectCXXTemplateParams - A helper function to collect debug info for
1349/// template parameters.
Eric Christophere7b87e52014-10-26 23:40:33 +00001350llvm::DIArray CGDebugInfo::CollectCXXTemplateParams(
1351 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001352 // Always get the full list of parameters, not just the ones from
1353 // the specialization.
1354 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001355 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001356 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001357 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001358}
1359
1360/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
1361llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
1362 if (VTablePtrType.isValid())
1363 return VTablePtrType;
1364
1365 ASTContext &Context = CGM.getContext();
1366
1367 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001368 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Manman Ren67f005e2014-07-28 22:24:34 +00001369 llvm::DITypeArray SElements = DBuilder.getOrCreateTypeArray(STy);
Guy Benyei11169dd2012-12-18 14:30:41 +00001370 llvm::DIType SubTy = DBuilder.createSubroutineType(Unit, SElements);
1371 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00001372 llvm::DIType vtbl_ptr_type =
1373 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001374 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1375 return VTablePtrType;
1376}
1377
1378/// getVTableName - Get vtable name for the given Class.
1379StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001380 // Copy the gdb compatible name on the side and use its reference.
1381 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001382}
1383
Guy Benyei11169dd2012-12-18 14:30:41 +00001384/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
1385/// debug info entry in EltTys vector.
Eric Christophere7b87e52014-10-26 23:40:33 +00001386void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001387 SmallVectorImpl<llvm::Metadata *> &EltTys) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001388 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1389
1390 // If there is a primary base then it will hold vtable info.
1391 if (RL.getPrimaryBase())
1392 return;
1393
1394 // If this class is not dynamic then there is not any vtable info to collect.
1395 if (!RD->isDynamicClass())
1396 return;
1397
1398 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00001399 llvm::DIType VPTR = DBuilder.createMemberType(
1400 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
1401 llvm::DIDescriptor::FlagArtificial, getOrCreateVTablePtrType(Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001402 EltTys.push_back(VPTR);
1403}
1404
Eric Christopherb2a008c2013-05-16 00:45:12 +00001405/// getOrCreateRecordType - Emit record type's standalone debug info.
1406llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
Guy Benyei11169dd2012-12-18 14:30:41 +00001407 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001408 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00001409 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
1410 return T;
1411}
1412
1413/// getOrCreateInterfaceType - Emit an objective c interface type standalone
1414/// debug info.
1415llvm::DIType CGDebugInfo::getOrCreateInterfaceType(QualType D,
Eric Christopherc0c5d462013-02-21 22:35:08 +00001416 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001417 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00001418 llvm::DIType T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantl73409ce2013-03-11 18:33:46 +00001419 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001420 return T;
1421}
1422
David Blaikie483a9da2014-05-06 18:35:21 +00001423void CGDebugInfo::completeType(const EnumDecl *ED) {
1424 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1425 return;
1426 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001427 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001428 auto I = TypeCache.find(TyPtr);
1429 if (I == TypeCache.end() ||
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001430 !llvm::DIType(cast<llvm::MDNode>(I->second)).isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001431 return;
1432 llvm::DIType Res = CreateTypeDefinition(Ty->castAs<EnumType>());
1433 assert(!Res.isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001434 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001435}
1436
David Blaikieb2e86eb2013-08-15 20:49:17 +00001437void CGDebugInfo::completeType(const RecordDecl *RD) {
1438 if (DebugKind > CodeGenOptions::LimitedDebugInfo ||
1439 !CGM.getLangOpts().CPlusPlus)
1440 completeRequiredType(RD);
1441}
1442
1443void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
David Blaikie0856f662014-03-04 22:01:08 +00001444 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1445 return;
1446
David Blaikie6943dea2013-08-20 01:28:15 +00001447 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1448 if (CXXDecl->isDynamicClass())
1449 return;
1450
David Blaikieb2e86eb2013-08-15 20:49:17 +00001451 QualType Ty = CGM.getContext().getRecordType(RD);
1452 llvm::DIType T = getTypeOrNull(Ty);
David Blaikie6943dea2013-08-20 01:28:15 +00001453 if (T && T.isForwardDecl())
1454 completeClassData(RD);
1455}
1456
1457void CGDebugInfo::completeClassData(const RecordDecl *RD) {
1458 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001459 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001460 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001461 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001462 auto I = TypeCache.find(TyPtr);
1463 if (I != TypeCache.end() &&
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001464 !llvm::DIType(cast<llvm::MDNode>(I->second)).isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001465 return;
1466 llvm::DIType Res = CreateTypeDefinition(Ty->castAs<RecordType>());
1467 assert(!Res.isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001468 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001469}
1470
David Blaikie0e716b42014-03-03 23:48:23 +00001471static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1472 CXXRecordDecl::method_iterator End) {
1473 for (; I != End; ++I)
1474 if (FunctionDecl *Tmpl = I->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001475 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
1476 !I->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001477 return true;
1478 return false;
1479}
1480
1481static bool shouldOmitDefinition(CodeGenOptions::DebugInfoKind DebugKind,
1482 const RecordDecl *RD,
1483 const LangOptions &LangOpts) {
1484 if (DebugKind > CodeGenOptions::LimitedDebugInfo)
1485 return false;
1486
1487 if (!LangOpts.CPlusPlus)
1488 return false;
1489
1490 if (!RD->isCompleteDefinitionRequired())
1491 return true;
1492
1493 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1494
1495 if (!CXXDecl)
1496 return false;
1497
1498 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass())
1499 return true;
1500
1501 TemplateSpecializationKind Spec = TSK_Undeclared;
1502 if (const ClassTemplateSpecializationDecl *SD =
1503 dyn_cast<ClassTemplateSpecializationDecl>(RD))
1504 Spec = SD->getSpecializationKind();
1505
1506 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1507 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1508 CXXDecl->method_end()))
1509 return true;
1510
1511 return false;
1512}
1513
Guy Benyei11169dd2012-12-18 14:30:41 +00001514/// CreateType - get structure or union type.
David Blaikie99dab3b2013-09-04 22:03:57 +00001515llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001516 RecordDecl *RD = Ty->getDecl();
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001517 llvm::DICompositeType T(getTypeOrNull(QualType(Ty, 0)));
David Blaikie0e716b42014-03-03 23:48:23 +00001518 if (T || shouldOmitDefinition(DebugKind, RD, CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001519 if (!T)
David Blaikie65ec94e2014-02-18 20:52:05 +00001520 T = getOrCreateRecordFwdDecl(
1521 Ty, getContextDescriptor(cast<Decl>(RD->getDeclContext())));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001522 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001523 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001524
David Blaikieb2e86eb2013-08-15 20:49:17 +00001525 return CreateTypeDefinition(Ty);
1526}
1527
1528llvm::DIType CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
1529 RecordDecl *RD = Ty->getDecl();
1530
Guy Benyei11169dd2012-12-18 14:30:41 +00001531 // Get overall information about the record type for the debug info.
1532 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
1533
1534 // Records and classes and unions can all be recursive. To handle them, we
1535 // first generate a debug descriptor for the struct as a forward declaration.
1536 // Then (if it is a definition) we go through and get debug info for all of
1537 // its members. Finally, we create a descriptor for the complete type (which
1538 // may refer to the forward decl if the struct is recursive) and replace all
1539 // uses of the forward declaration with the final definition.
1540
David Blaikie4a2b5ef2013-08-12 22:24:20 +00001541 llvm::DICompositeType FwdDecl(getOrCreateLimitedType(Ty, DefUnit));
Manman Ren0d441f12013-07-02 19:01:53 +00001542 assert(FwdDecl.isCompositeType() &&
David Blaikie469f0792013-05-22 23:22:42 +00001543 "The debug type of a RecordType should be a llvm::DICompositeType");
Guy Benyei11169dd2012-12-18 14:30:41 +00001544
1545 if (FwdDecl.isForwardDecl())
1546 return FwdDecl;
1547
David Blaikieadfbf992013-08-18 16:55:33 +00001548 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1549 CollectContainingType(CXXDecl, FwdDecl);
1550
Guy Benyei11169dd2012-12-18 14:30:41 +00001551 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001552 LexicalBlockStack.emplace_back(&*FwdDecl);
1553 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001554
Guy Benyei11169dd2012-12-18 14:30:41 +00001555 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001556 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001557 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001558
1559 // Note: The split of CXXDecl information here is intentional, the
1560 // gdb tests will depend on a certain ordering at printout. The debug
1561 // information offsets are still correct if we merge them all together
1562 // though.
1563 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1564 if (CXXDecl) {
1565 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1566 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1567 }
1568
Eric Christopher91a31902013-01-16 01:22:32 +00001569 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001570 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001571 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001572 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001573
1574 LexicalBlockStack.pop_back();
1575 RegionMap.erase(Ty->getDecl());
1576
1577 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001578 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001579
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001580 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001581 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001582}
1583
1584/// CreateType - get objective-c object type.
1585llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1586 llvm::DIFile Unit) {
1587 // Ignore protocols.
1588 return getOrCreateType(Ty->getBaseType(), Unit);
1589}
1590
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001591/// \return true if Getter has the default name for the property PD.
1592static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1593 const ObjCMethodDecl *Getter) {
1594 assert(PD);
1595 if (!Getter)
1596 return true;
1597
1598 assert(Getter->getDeclName().isObjCZeroArgSelector());
1599 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001600 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001601}
1602
1603/// \return true if Setter has the default name for the property PD.
1604static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1605 const ObjCMethodDecl *Setter) {
1606 assert(PD);
1607 if (!Setter)
1608 return true;
1609
1610 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001611 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001612 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001613}
1614
Guy Benyei11169dd2012-12-18 14:30:41 +00001615/// CreateType - get objective-c interface type.
1616llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1617 llvm::DIFile Unit) {
1618 ObjCInterfaceDecl *ID = Ty->getDecl();
1619 if (!ID)
1620 return llvm::DIType();
1621
1622 // Get overall information about the record type for the debug info.
1623 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
1624 unsigned Line = getLineNumber(ID->getLocation());
Ed Masteda706022014-05-07 12:49:30 +00001625 llvm::dwarf::SourceLanguage RuntimeLang = TheCU.getLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001626
1627 // If this is just a forward declaration return a special forward-declaration
1628 // debug type since we won't be able to lay out the entire type.
1629 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001630 if (!Def || !Def->getImplementation()) {
David Blaikief427b002014-05-06 03:42:01 +00001631 llvm::DIType FwdDecl = DBuilder.createReplaceableForwardDecl(
1632 llvm::dwarf::DW_TAG_structure_type, ID->getName(), TheCU, DefUnit, Line,
1633 RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001634 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001635 return FwdDecl;
1636 }
1637
David Blaikieef8a9512014-05-05 23:23:53 +00001638 return CreateTypeDefinition(Ty, Unit);
1639}
1640
Eric Christophere7b87e52014-10-26 23:40:33 +00001641llvm::DIType CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
1642 llvm::DIFile Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00001643 ObjCInterfaceDecl *ID = Ty->getDecl();
1644 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
1645 unsigned Line = getLineNumber(ID->getLocation());
1646 unsigned RuntimeLang = TheCU.getLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001647
1648 // Bit size, align and offset of the type.
1649 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1650 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1651
1652 unsigned Flags = 0;
1653 if (ID->getImplementation())
1654 Flags |= llvm::DIDescriptor::FlagObjcClassComplete;
1655
Eric Christophere7b87e52014-10-26 23:40:33 +00001656 llvm::DICompositeType RealDecl = DBuilder.createStructType(
1657 Unit, ID->getName(), DefUnit, Line, Size, Align, Flags, llvm::DIType(),
1658 llvm::DIArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001659
David Blaikieef8a9512014-05-05 23:23:53 +00001660 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001661 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001662
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001663 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001664 LexicalBlockStack.emplace_back(static_cast<llvm::MDNode *>(RealDecl));
1665 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001666
1667 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001668 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001669
1670 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1671 if (SClass) {
1672 llvm::DIType SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001673 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001674 if (!SClassTy.isValid())
1675 return llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001676
Eric Christophere7b87e52014-10-26 23:40:33 +00001677 llvm::DIType InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001678 EltTys.push_back(InhTag);
1679 }
1680
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001681 // Create entries for all of the properties.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001682 for (const auto *PD : ID->properties()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001683 SourceLocation Loc = PD->getLocation();
1684 llvm::DIFile PUnit = getOrCreateFile(Loc);
1685 unsigned PLine = getLineNumber(Loc);
1686 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1687 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001688 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
1689 PD->getName(), PUnit, PLine,
1690 hasDefaultGetterName(PD, Getter) ? ""
1691 : getSelectorName(PD->getGetterName()),
1692 hasDefaultSetterName(PD, Setter) ? ""
1693 : getSelectorName(PD->getSetterName()),
1694 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001695 EltTys.push_back(PropertyNode);
1696 }
1697
1698 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1699 unsigned FieldNo = 0;
1700 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1701 Field = Field->getNextIvar(), ++FieldNo) {
1702 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1703 if (!FieldTy.isValid())
1704 return llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001705
Guy Benyei11169dd2012-12-18 14:30:41 +00001706 StringRef FieldName = Field->getName();
1707
1708 // Ignore unnamed fields.
1709 if (FieldName.empty())
1710 continue;
1711
1712 // Get the location for the field.
1713 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1714 unsigned FieldLine = getLineNumber(Field->getLocation());
1715 QualType FType = Field->getType();
1716 uint64_t FieldSize = 0;
1717 unsigned FieldAlign = 0;
1718
1719 if (!FType->isIncompleteArrayType()) {
1720
1721 // Bit size, align and offset of the type.
1722 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001723 ? Field->getBitWidthValue(CGM.getContext())
1724 : CGM.getContext().getTypeSize(FType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001725 FieldAlign = CGM.getContext().getTypeAlign(FType);
1726 }
1727
1728 uint64_t FieldOffset;
1729 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1730 // We don't know the runtime offset of an ivar if we're using the
1731 // non-fragile ABI. For bitfields, use the bit offset into the first
1732 // byte of storage of the bitfield. For other fields, use zero.
1733 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001734 FieldOffset =
1735 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00001736 FieldOffset %= CGM.getContext().getCharWidth();
1737 } else {
1738 FieldOffset = 0;
1739 }
1740 } else {
1741 FieldOffset = RL.getFieldOffset(FieldNo);
1742 }
1743
1744 unsigned Flags = 0;
1745 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
1746 Flags = llvm::DIDescriptor::FlagProtected;
1747 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
1748 Flags = llvm::DIDescriptor::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001749 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
1750 Flags = llvm::DIDescriptor::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00001751
Craig Topper8a13c412014-05-21 05:09:00 +00001752 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001753 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001754 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00001755 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001756 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00001757 SourceLocation Loc = PD->getLocation();
1758 llvm::DIFile PUnit = getOrCreateFile(Loc);
1759 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001760 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1761 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001762 PropertyNode = DBuilder.createObjCProperty(
1763 PD->getName(), PUnit, PLine,
1764 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
1765 PD->getGetterName()),
1766 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
1767 PD->getSetterName()),
1768 PD->getPropertyAttributes(),
1769 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001770 }
1771 }
1772 }
Eric Christophere7b87e52014-10-26 23:40:33 +00001773 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
1774 FieldSize, FieldAlign, FieldOffset, Flags,
1775 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00001776 EltTys.push_back(FieldTy);
1777 }
1778
1779 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001780 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00001781
Guy Benyei11169dd2012-12-18 14:30:41 +00001782 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001783 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001784}
1785
1786llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty, llvm::DIFile Unit) {
1787 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1788 int64_t Count = Ty->getNumElements();
1789 if (Count == 0)
1790 // If number of elements are not known then this is an unbounded array.
1791 // Use Count == -1 to express such arrays.
1792 Count = -1;
1793
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001794 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Guy Benyei11169dd2012-12-18 14:30:41 +00001795 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
1796
1797 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1798 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1799
1800 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
1801}
1802
Eric Christophere7b87e52014-10-26 23:40:33 +00001803llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001804 uint64_t Size;
1805 uint64_t Align;
1806
1807 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
1808 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
1809 Size = 0;
1810 Align =
Eric Christophere7b87e52014-10-26 23:40:33 +00001811 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Guy Benyei11169dd2012-12-18 14:30:41 +00001812 } else if (Ty->isIncompleteArrayType()) {
1813 Size = 0;
1814 if (Ty->getElementType()->isIncompleteType())
1815 Align = 0;
1816 else
1817 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikief03b2e82013-05-09 20:48:12 +00001818 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001819 Size = 0;
1820 Align = 0;
1821 } else {
1822 // Size and align of the whole array, not the element type.
1823 Size = CGM.getContext().getTypeSize(Ty);
1824 Align = CGM.getContext().getTypeAlign(Ty);
1825 }
1826
1827 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1828 // interior arrays, do we care? Why aren't nested arrays represented the
1829 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001830 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001831 QualType EltTy(Ty, 0);
1832 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1833 // If the number of elements is known, then count is that number. Otherwise,
1834 // it's -1. This allows us to represent a subrange with an array of 0
1835 // elements, like this:
1836 //
1837 // struct foo {
1838 // int x[0];
1839 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00001840 int64_t Count = -1; // Count == -1 is an unbounded array.
Guy Benyei11169dd2012-12-18 14:30:41 +00001841 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1842 Count = CAT->getSize().getZExtValue();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001843
Guy Benyei11169dd2012-12-18 14:30:41 +00001844 // FIXME: Verify this is right for VLAs.
1845 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
1846 EltTy = Ty->getElementType();
1847 }
1848
1849 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
1850
Eric Christophere7b87e52014-10-26 23:40:33 +00001851 llvm::DIType DbgTy = DBuilder.createArrayType(
1852 Size, Align, getOrCreateType(EltTy, Unit), SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00001853 return DbgTy;
1854}
1855
Eric Christopherb2a008c2013-05-16 00:45:12 +00001856llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001857 llvm::DIFile Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001858 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
1859 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001860}
1861
Eric Christopherb2a008c2013-05-16 00:45:12 +00001862llvm::DIType CGDebugInfo::CreateType(const RValueReferenceType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001863 llvm::DIFile Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001864 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
1865 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001866}
1867
Eric Christopherb2a008c2013-05-16 00:45:12 +00001868llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001869 llvm::DIFile U) {
David Blaikie2c705ca2013-01-19 19:20:56 +00001870 llvm::DIType ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
1871 if (!Ty->getPointeeType()->isFunctionType())
1872 return DBuilder.createMemberPointerType(
Adrian Prantlee24e142014-12-23 19:11:54 +00001873 getOrCreateType(Ty->getPointeeType(), U), ClassType,
1874 CGM.PointerWidthInBits);
Adrian Prantl0866acd2013-12-19 01:38:47 +00001875
1876 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00001877 Ty->getPointeeType()->getAs<FunctionProtoType>();
1878 return DBuilder.createMemberPointerType(
1879 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
1880 Ty->getClass(), FPT->getTypeQuals())),
1881 FPT, U),
Adrian Prantlee24e142014-12-23 19:11:54 +00001882 ClassType, CGM.PointerWidthInBits);
Guy Benyei11169dd2012-12-18 14:30:41 +00001883}
1884
Eric Christophere7b87e52014-10-26 23:40:33 +00001885llvm::DIType CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile U) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001886 // Ignore the atomic wrapping
1887 // FIXME: What is the correct representation?
1888 return getOrCreateType(Ty->getValueType(), U);
1889}
1890
1891/// CreateEnumType - get enumeration type.
Manman Ren501ecf92013-08-28 21:46:36 +00001892llvm::DIType CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00001893 const EnumDecl *ED = Ty->getDecl();
Guy Benyei11169dd2012-12-18 14:30:41 +00001894 uint64_t Size = 0;
1895 uint64_t Align = 0;
1896 if (!ED->getTypeForDecl()->isIncompleteType()) {
1897 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1898 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1899 }
1900
Manman Rene0064d82013-08-29 23:19:58 +00001901 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1902
Guy Benyei11169dd2012-12-18 14:30:41 +00001903 // If this is just a forward declaration, construct an appropriately
1904 // marked node and just return it.
1905 if (!ED->getDefinition()) {
1906 llvm::DIDescriptor EDContext;
1907 EDContext = getContextDescriptor(cast<Decl>(ED->getDeclContext()));
1908 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1909 unsigned Line = getLineNumber(ED->getLocation());
1910 StringRef EDName = ED->getName();
David Blaikief427b002014-05-06 03:42:01 +00001911 llvm::DIType RetTy = DBuilder.createReplaceableForwardDecl(
1912 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
1913 0, Size, Align, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001914 ReplaceMap.emplace_back(
1915 std::piecewise_construct, std::make_tuple(Ty),
1916 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00001917 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00001918 }
1919
David Blaikie483a9da2014-05-06 18:35:21 +00001920 return CreateTypeDefinition(Ty);
1921}
1922
1923llvm::DIType CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
1924 const EnumDecl *ED = Ty->getDecl();
1925 uint64_t Size = 0;
1926 uint64_t Align = 0;
1927 if (!ED->getTypeForDecl()->isIncompleteType()) {
1928 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1929 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1930 }
1931
1932 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1933
Guy Benyei11169dd2012-12-18 14:30:41 +00001934 // Create DIEnumerator elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001935 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00001936 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00001937 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001938 Enumerators.push_back(DBuilder.createEnumerator(
1939 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001940 }
1941
1942 // Return a CompositeType for the enum itself.
1943 llvm::DIArray EltArray = DBuilder.getOrCreateArray(Enumerators);
1944
1945 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1946 unsigned Line = getLineNumber(ED->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001947 llvm::DIDescriptor EnumContext =
Eric Christophere7b87e52014-10-26 23:40:33 +00001948 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
1949 llvm::DIType ClassTy = ED->isFixed()
1950 ? getOrCreateType(ED->getIntegerType(), DefUnit)
1951 : llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001952 llvm::DIType DbgTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001953 DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit, Line,
1954 Size, Align, EltArray, ClassTy, FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00001955 return DbgTy;
1956}
1957
David Blaikie05491062013-01-21 04:37:12 +00001958static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
1959 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00001960 do {
Adrian Prantl179af902013-09-26 21:35:50 +00001961 Qualifiers InnerQuals = T.getLocalQualifiers();
1962 // Qualifiers::operator+() doesn't like it if you add a Qualifier
1963 // that is already there.
1964 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
1965 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00001966 QualType LastT = T;
1967 switch (T->getTypeClass()) {
1968 default:
David Blaikie05491062013-01-21 04:37:12 +00001969 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00001970 case Type::TemplateSpecialization: {
1971 const auto *Spec = cast<TemplateSpecializationType>(T);
1972 if (Spec->isTypeAlias())
1973 return C.getQualifiedType(T.getTypePtr(), Quals);
1974 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00001975 break;
1976 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001977 case Type::TypeOfExpr:
1978 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
1979 break;
1980 case Type::TypeOf:
1981 T = cast<TypeOfType>(T)->getUnderlyingType();
1982 break;
1983 case Type::Decltype:
1984 T = cast<DecltypeType>(T)->getUnderlyingType();
1985 break;
1986 case Type::UnaryTransform:
1987 T = cast<UnaryTransformType>(T)->getUnderlyingType();
1988 break;
1989 case Type::Attributed:
1990 T = cast<AttributedType>(T)->getEquivalentType();
1991 break;
1992 case Type::Elaborated:
1993 T = cast<ElaboratedType>(T)->getNamedType();
1994 break;
1995 case Type::Paren:
1996 T = cast<ParenType>(T)->getInnerType();
1997 break;
David Blaikie05491062013-01-21 04:37:12 +00001998 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00001999 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002000 break;
2001 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00002002 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002003 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002004 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002005 break;
2006 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002007
Guy Benyei11169dd2012-12-18 14:30:41 +00002008 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002009 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002010 } while (true);
2011}
2012
Eric Christopher0fdcb312013-05-16 00:52:20 +00002013/// getType - Get the type from the cache or return null type if it doesn't
2014/// exist.
Guy Benyei11169dd2012-12-18 14:30:41 +00002015llvm::DIType CGDebugInfo::getTypeOrNull(QualType Ty) {
2016
2017 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002018 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002019
David Blaikief427b002014-05-06 03:42:01 +00002020 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002021 if (it != TypeCache.end()) {
2022 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002023 if (llvm::Metadata *V = it->second)
Guy Benyei11169dd2012-12-18 14:30:41 +00002024 return llvm::DIType(cast<llvm::MDNode>(V));
2025 }
2026
2027 return llvm::DIType();
2028}
2029
David Blaikie0e716b42014-03-03 23:48:23 +00002030void CGDebugInfo::completeTemplateDefinition(
2031 const ClassTemplateSpecializationDecl &SD) {
David Blaikie0856f662014-03-04 22:01:08 +00002032 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2033 return;
2034
David Blaikie0e716b42014-03-03 23:48:23 +00002035 completeClassData(&SD);
2036 // In case this type has no member function definitions being emitted, ensure
2037 // it is retained
2038 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2039}
2040
Guy Benyei11169dd2012-12-18 14:30:41 +00002041/// getOrCreateType - Get the type from the cache or create a new
2042/// one if necessary.
David Blaikie99dab3b2013-09-04 22:03:57 +00002043llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002044 if (Ty.isNull())
2045 return llvm::DIType();
2046
2047 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002048 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002049
David Blaikieef8a9512014-05-05 23:23:53 +00002050 if (llvm::DIType T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002051 return T;
2052
2053 // Otherwise create the type.
David Blaikie99dab3b2013-09-04 22:03:57 +00002054 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Eric Christophere7b87e52014-10-26 23:40:33 +00002055 void *TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002056
2057 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002058 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002059
Guy Benyei11169dd2012-12-18 14:30:41 +00002060 return Res;
2061}
2062
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002063/// Currently the checksum of an interface includes the number of
2064/// ivars and property accessors.
Eric Christopher1ecc5632013-06-07 22:54:39 +00002065unsigned CGDebugInfo::Checksum(const ObjCInterfaceDecl *ID) {
Adrian Prantl817bbb32013-06-07 01:10:48 +00002066 // The assumption is that the number of ivars can only increase
2067 // monotonically, so it is safe to just use their current number as
2068 // a checksum.
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002069 unsigned Sum = 0;
2070 for (const ObjCIvarDecl *Ivar = ID->all_declared_ivar_begin();
Craig Topper8a13c412014-05-21 05:09:00 +00002071 Ivar != nullptr; Ivar = Ivar->getNextIvar())
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002072 ++Sum;
2073
2074 return Sum;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002075}
2076
2077ObjCInterfaceDecl *CGDebugInfo::getObjCInterfaceDecl(QualType Ty) {
2078 switch (Ty->getTypeClass()) {
2079 case Type::ObjCObjectPointer:
Eric Christophere7b87e52014-10-26 23:40:33 +00002080 return getObjCInterfaceDecl(
2081 cast<ObjCObjectPointerType>(Ty)->getPointeeType());
Adrian Prantla03a85a2013-03-06 22:03:30 +00002082 case Type::ObjCInterface:
2083 return cast<ObjCInterfaceType>(Ty)->getDecl();
2084 default:
Craig Topper8a13c412014-05-21 05:09:00 +00002085 return nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002086 }
2087}
2088
Guy Benyei11169dd2012-12-18 14:30:41 +00002089/// CreateTypeNode - Create a new debug type node.
David Blaikie99dab3b2013-09-04 22:03:57 +00002090llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002091 // Handle qualifiers, which recursively handles what they refer to.
2092 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002093 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002094
Guy Benyei11169dd2012-12-18 14:30:41 +00002095 // Work out details of type.
2096 switch (Ty->getTypeClass()) {
2097#define TYPE(Class, Base)
2098#define ABSTRACT_TYPE(Class, Base)
2099#define NON_CANONICAL_TYPE(Class, Base)
2100#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2101#include "clang/AST/TypeNodes.def"
2102 llvm_unreachable("Dependent types cannot show up in debug information");
2103
2104 case Type::ExtVector:
2105 case Type::Vector:
2106 return CreateType(cast<VectorType>(Ty), Unit);
2107 case Type::ObjCObjectPointer:
2108 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2109 case Type::ObjCObject:
2110 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2111 case Type::ObjCInterface:
2112 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2113 case Type::Builtin:
2114 return CreateType(cast<BuiltinType>(Ty));
2115 case Type::Complex:
2116 return CreateType(cast<ComplexType>(Ty));
2117 case Type::Pointer:
2118 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002119 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002120 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002121 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002122 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002123 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002124 case Type::BlockPointer:
2125 return CreateType(cast<BlockPointerType>(Ty), Unit);
2126 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002127 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002128 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002129 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002130 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002131 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002132 case Type::FunctionProto:
2133 case Type::FunctionNoProto:
2134 return CreateType(cast<FunctionType>(Ty), Unit);
2135 case Type::ConstantArray:
2136 case Type::VariableArray:
2137 case Type::IncompleteArray:
2138 return CreateType(cast<ArrayType>(Ty), Unit);
2139
2140 case Type::LValueReference:
2141 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2142 case Type::RValueReference:
2143 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2144
2145 case Type::MemberPointer:
2146 return CreateType(cast<MemberPointerType>(Ty), Unit);
2147
2148 case Type::Atomic:
2149 return CreateType(cast<AtomicType>(Ty), Unit);
2150
Guy Benyei11169dd2012-12-18 14:30:41 +00002151 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002152 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2153
David Blaikie42edade2014-11-11 20:44:45 +00002154 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002155 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002156 case Type::Elaborated:
2157 case Type::Paren:
2158 case Type::SubstTemplateTypeParm:
2159 case Type::TypeOfExpr:
2160 case Type::TypeOf:
2161 case Type::Decltype:
2162 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002163 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002164 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002165 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002166
David Blaikie42edade2014-11-11 20:44:45 +00002167 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002168}
2169
2170/// getOrCreateLimitedType - Get the type from the cache or create a new
2171/// limited type if necessary.
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002172llvm::DIType CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
Eric Christopherc0c5d462013-02-21 22:35:08 +00002173 llvm::DIFile Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002174 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002175
David Blaikie8d5e1282013-08-20 21:03:29 +00002176 llvm::DICompositeType T(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002177
2178 // We may have cached a forward decl when we could have created
2179 // a non-forward decl. Go ahead and create a non-forward decl
2180 // now.
Eric Christophere7b87e52014-10-26 23:40:33 +00002181 if (T && !T.isForwardDecl())
2182 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002183
2184 // Otherwise create the type.
David Blaikie8d5e1282013-08-20 21:03:29 +00002185 llvm::DICompositeType Res = CreateLimitedType(Ty);
2186
2187 // Propagate members from the declaration to the definition
2188 // CreateType(const RecordType*) will overwrite this with the members in the
2189 // correct order if the full type is needed.
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002190 DBuilder.replaceArrays(Res, T.getElements());
Guy Benyei11169dd2012-12-18 14:30:41 +00002191
Guy Benyei11169dd2012-12-18 14:30:41 +00002192 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002193 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002194 return Res;
2195}
2196
2197// TODO: Currently used for context chains when limiting debug info.
David Blaikie8d5e1282013-08-20 21:03:29 +00002198llvm::DICompositeType CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002199 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002200
Guy Benyei11169dd2012-12-18 14:30:41 +00002201 // Get overall information about the record type for the debug info.
2202 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
2203 unsigned Line = getLineNumber(RD->getLocation());
2204 StringRef RDName = getClassName(RD);
2205
Eric Christopher07429ff2013-10-15 21:22:34 +00002206 llvm::DIDescriptor RDContext =
2207 getContextDescriptor(cast<Decl>(RD->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002208
David Blaikied2785892013-08-18 17:36:19 +00002209 // If we ended up creating the type during the context chain construction,
2210 // just return that.
David Blaikie8d5e1282013-08-20 21:03:29 +00002211 llvm::DICompositeType T(getTypeOrNull(CGM.getContext().getRecordType(RD)));
2212 if (T && (!T.isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002213 return T;
David Blaikied2785892013-08-18 17:36:19 +00002214
Adrian Prantl381e7552014-02-04 21:29:50 +00002215 // If this is just a forward or incomplete declaration, construct an
2216 // appropriately marked node and just return it.
2217 const RecordDecl *D = RD->getDefinition();
2218 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002219 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002220
2221 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2222 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
David Blaikie49ae6a72013-03-26 23:47:35 +00002223 llvm::DICompositeType RealDecl;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002224
Manman Rene0064d82013-08-29 23:19:58 +00002225 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2226
Guy Benyei11169dd2012-12-18 14:30:41 +00002227 if (RD->isUnion())
Eric Christophere7b87e52014-10-26 23:40:33 +00002228 RealDecl = DBuilder.createUnionType(RDContext, RDName, DefUnit, Line, Size,
2229 Align, 0, llvm::DIArray(), 0, FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002230 else if (RD->isClass()) {
2231 // FIXME: This could be a struct type giving a default visibility different
2232 // than C++ class type, but needs llvm metadata changes first.
Eric Christophere7b87e52014-10-26 23:40:33 +00002233 RealDecl = DBuilder.createClassType(
2234 RDContext, RDName, DefUnit, Line, Size, Align, 0, 0, llvm::DIType(),
2235 llvm::DIArray(), llvm::DIType(), llvm::DIArray(), FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002236 } else
Eric Christophere7b87e52014-10-26 23:40:33 +00002237 RealDecl = DBuilder.createStructType(
2238 RDContext, RDName, DefUnit, Line, Size, Align, 0, llvm::DIType(),
2239 llvm::DIArray(), 0, llvm::DIType(), FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002240
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002241 RegionMap[Ty->getDecl()].reset(RealDecl);
2242 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002243
David Blaikieadfbf992013-08-18 16:55:33 +00002244 if (const ClassTemplateSpecializationDecl *TSpecial =
2245 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002246 DBuilder.replaceArrays(RealDecl, llvm::DIArray(),
2247 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002248 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002249}
2250
David Blaikieadfbf992013-08-18 16:55:33 +00002251void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
2252 llvm::DICompositeType RealDecl) {
2253 // A class's primary base or the class itself contains the vtable.
2254 llvm::DICompositeType ContainingType;
2255 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2256 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002257 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002258 while (1) {
2259 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2260 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2261 if (PBT && !BRL.isPrimaryBaseVirtual())
2262 PBase = PBT;
2263 else
2264 break;
2265 }
2266 ContainingType = llvm::DICompositeType(
2267 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2268 getOrCreateFile(RD->getLocation())));
2269 } else if (RD->isDynamicClass())
2270 ContainingType = RealDecl;
2271
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002272 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002273}
2274
Guy Benyei11169dd2012-12-18 14:30:41 +00002275/// CreateMemberType - Create new member and increase Offset by FType's size.
2276llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
Eric Christophere7b87e52014-10-26 23:40:33 +00002277 StringRef Name, uint64_t *Offset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002278 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
2279 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2280 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Eric Christophere7b87e52014-10-26 23:40:33 +00002281 llvm::DIType Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
2282 FieldAlign, *Offset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002283 *Offset += FieldSize;
2284 return Ty;
2285}
2286
Frederic Riss9db79f12014-11-18 03:40:46 +00002287void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD,
2288 llvm::DIFile Unit,
2289 StringRef &Name, StringRef &LinkageName,
2290 llvm::DIDescriptor &FDContext,
2291 llvm::DIArray &TParamsArray,
2292 unsigned &Flags) {
2293 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2294 Name = getFunctionName(FD);
2295 // Use mangled name as linkage name for C/C++ functions.
2296 if (FD->hasPrototype()) {
2297 LinkageName = CGM.getMangledName(GD);
2298 Flags |= llvm::DIDescriptor::FlagPrototyped;
2299 }
2300 // No need to replicate the linkage name if it isn't different from the
2301 // subprogram name, no need to have it at all unless coverage is enabled or
2302 // debug is set to more than just line tables.
2303 if (LinkageName == Name ||
2304 (!CGM.getCodeGenOpts().EmitGcovArcs &&
2305 !CGM.getCodeGenOpts().EmitGcovNotes &&
2306 DebugKind <= CodeGenOptions::DebugLineTablesOnly))
2307 LinkageName = StringRef();
2308
2309 if (DebugKind >= CodeGenOptions::LimitedDebugInfo) {
2310 if (const NamespaceDecl *NSDecl =
2311 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2312 FDContext = getOrCreateNameSpace(NSDecl);
2313 else if (const RecordDecl *RDecl =
2314 dyn_cast_or_null<RecordDecl>(FD->getDeclContext()))
2315 FDContext = getContextDescriptor(cast<Decl>(RDecl));
2316 // Collect template parameters.
2317 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2318 }
2319}
2320
2321void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile &Unit,
2322 unsigned &LineNo, QualType &T,
2323 StringRef &Name, StringRef &LinkageName,
2324 llvm::DIDescriptor &VDContext) {
2325 Unit = getOrCreateFile(VD->getLocation());
2326 LineNo = getLineNumber(VD->getLocation());
2327
2328 setLocation(VD->getLocation());
2329
2330 T = VD->getType();
2331 if (T->isIncompleteArrayType()) {
2332 // CodeGen turns int[] into int[1] so we'll do the same here.
2333 llvm::APInt ConstVal(32, 1);
2334 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2335
2336 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2337 ArrayType::Normal, 0);
2338 }
2339
2340 Name = VD->getName();
2341 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2342 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2343 LinkageName = CGM.getMangledName(VD);
2344 if (LinkageName == Name)
2345 LinkageName = StringRef();
2346
2347 // Since we emit declarations (DW_AT_members) for static members, place the
2348 // definition of those static members in the namespace they were declared in
2349 // in the source code (the lexical decl context).
2350 // FIXME: Generalize this for even non-member global variables where the
2351 // declaration and definition may have different lexical decl contexts, once
2352 // we have support for emitting declarations of (non-member) global variables.
2353 VDContext = getContextDescriptor(
2354 dyn_cast<Decl>(VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2355 : VD->getDeclContext()));
2356}
2357
Frederic Rissd253ed62014-11-18 03:40:51 +00002358llvm::DISubprogram
2359CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
2360 llvm::DIArray TParamsArray;
2361 StringRef Name, LinkageName;
2362 unsigned Flags = 0;
2363 SourceLocation Loc = FD->getLocation();
2364 llvm::DIFile Unit = getOrCreateFile(Loc);
2365 llvm::DIDescriptor DContext(Unit);
2366 unsigned Line = getLineNumber(Loc);
2367
2368 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2369 TParamsArray, Flags);
2370 // Build function type.
2371 SmallVector<QualType, 16> ArgTypes;
2372 for (const ParmVarDecl *Parm: FD->parameters())
2373 ArgTypes.push_back(Parm->getType());
2374 QualType FnType =
2375 CGM.getContext().getFunctionType(FD->getReturnType(), ArgTypes,
2376 FunctionProtoType::ExtProtoInfo());
2377 llvm::DISubprogram SP =
2378 DBuilder.createTempFunctionFwdDecl(DContext, Name, LinkageName, Unit, Line,
2379 getOrCreateFunctionType(FD, FnType, Unit),
2380 !FD->isExternallyVisible(),
2381 false /*declaration*/, 0, Flags,
2382 CGM.getLangOpts().Optimize, nullptr,
2383 TParamsArray, getFunctionDeclaration(FD));
2384 const FunctionDecl *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002385 FwdDeclReplaceMap.emplace_back(
2386 std::piecewise_construct, std::make_tuple(CanonDecl),
2387 std::make_tuple(static_cast<llvm::Metadata *>(SP)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002388 return SP;
2389}
2390
2391llvm::DIGlobalVariable
2392CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2393 QualType T;
2394 StringRef Name, LinkageName;
2395 SourceLocation Loc = VD->getLocation();
2396 llvm::DIFile Unit = getOrCreateFile(Loc);
2397 llvm::DIDescriptor DContext(Unit);
2398 unsigned Line = getLineNumber(Loc);
2399
2400 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
2401 llvm::DIGlobalVariable GV =
2402 DBuilder.createTempGlobalVariableFwdDecl(DContext, Name, LinkageName, Unit,
2403 Line, getOrCreateType(T, Unit),
2404 !VD->isExternallyVisible(),
2405 nullptr, nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002406 FwdDeclReplaceMap.emplace_back(
2407 std::piecewise_construct,
2408 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2409 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002410 return GV;
2411}
2412
Frederic Riss442293e2014-11-06 21:12:06 +00002413llvm::DIDescriptor CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002414 // We only need a declaration (not a definition) of the type - so use whatever
2415 // we would otherwise do to get a type for a pointee. (forward declarations in
2416 // limited debug info, full definitions (if the type definition is available)
2417 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002418 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2419 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002420 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002421 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002422
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002423 if (I != DeclCache.end())
2424 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(I->second));
Frederic Rissd253ed62014-11-18 03:40:51 +00002425
2426 // No definition for now. Emit a forward definition that might be
2427 // merged with a potential upcoming definition.
2428 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
2429 return getFunctionForwardDeclaration(FD);
2430 else if (const auto *VD = dyn_cast<VarDecl>(D))
2431 return getGlobalVariableForwardDeclaration(VD);
2432
2433 return llvm::DIDescriptor();
David Blaikiebd483762013-05-20 04:58:53 +00002434}
2435
Guy Benyei11169dd2012-12-18 14:30:41 +00002436/// getFunctionDeclaration - Return debug info descriptor to describe method
2437/// declaration for the given method definition.
2438llvm::DISubprogram CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Diego Novillo913690c2014-06-24 17:02:17 +00002439 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
David Blaikie18cfbc52013-06-22 00:09:36 +00002440 return llvm::DISubprogram();
2441
Guy Benyei11169dd2012-12-18 14:30:41 +00002442 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002443 if (!FD)
2444 return llvm::DISubprogram();
Guy Benyei11169dd2012-12-18 14:30:41 +00002445
2446 // Setup context.
David Blaikiefd07c602013-08-09 17:20:05 +00002447 llvm::DIScope S = getContextDescriptor(cast<Decl>(D->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002448
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002449 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002450 if (MI == SPCache.end()) {
Eric Christopherf86c4052013-08-28 23:12:10 +00002451 if (const CXXMethodDecl *MD =
2452 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
David Blaikiefd07c602013-08-09 17:20:05 +00002453 llvm::DICompositeType T(S);
Eric Christopherf86c4052013-08-28 23:12:10 +00002454 llvm::DISubprogram SP =
2455 CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()), T);
David Blaikiefd07c602013-08-09 17:20:05 +00002456 return SP;
2457 }
2458 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002459 if (MI != SPCache.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002460 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(MI->second));
David Blaikie18cfbc52013-06-22 00:09:36 +00002461 if (SP.isSubprogram() && !SP.isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002462 return SP;
2463 }
2464
Aaron Ballman86c93902014-03-06 23:45:36 +00002465 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002466 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002467 if (MI != SPCache.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002468 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(MI->second));
David Blaikie18cfbc52013-06-22 00:09:36 +00002469 if (SP.isSubprogram() && !SP.isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002470 return SP;
2471 }
2472 }
2473 return llvm::DISubprogram();
2474}
2475
2476// getOrCreateFunctionType - Construct DIType. If it is a c++ method, include
2477// implicit parameter "this".
David Blaikie469f0792013-05-22 23:22:42 +00002478llvm::DICompositeType CGDebugInfo::getOrCreateFunctionType(const Decl *D,
2479 QualType FnType,
2480 llvm::DIFile F) {
Diego Novillo913690c2014-06-24 17:02:17 +00002481 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
David Blaikie18cfbc52013-06-22 00:09:36 +00002482 // Create fake but valid subroutine type. Otherwise
2483 // llvm::DISubprogram::Verify() would return false, and
2484 // subprogram DIE will miss DW_AT_decl_file and
2485 // DW_AT_decl_line fields.
Manman Ren67f005e2014-07-28 22:24:34 +00002486 return DBuilder.createSubroutineType(F,
2487 DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002488
2489 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2490 return getOrCreateMethodType(Method, F);
2491 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2492 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002493 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002494
2495 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002496 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002497
2498 // Replace the instancetype keyword with the actual type.
2499 if (ResultTy == CGM.getContext().getObjCInstanceType())
2500 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002501 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002502
Adrian Prantl7bec9032013-05-10 21:08:31 +00002503 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002504 // "self" pointer is always first argument.
Adrian Prantlde17db32013-03-29 19:20:29 +00002505 QualType SelfDeclTy = OMethod->getSelfDecl()->getType();
2506 llvm::DIType SelfTy = getOrCreateType(SelfDeclTy, F);
2507 Elts.push_back(CreateSelfType(SelfDeclTy, SelfTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002508 // "_cmd" pointer is always second argument.
2509 llvm::DIType CmdTy = getOrCreateType(OMethod->getCmdDecl()->getType(), F);
2510 Elts.push_back(DBuilder.createArtificialType(CmdTy));
2511 // Get rest of the arguments.
Aaron Ballman43b68be2014-03-07 17:50:17 +00002512 for (const auto *PI : OMethod->params())
2513 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002514 // Variadic methods need a special marker at the end of the type list.
2515 if (OMethod->isVariadic())
2516 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002517
Manman Ren67f005e2014-07-28 22:24:34 +00002518 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002519 return DBuilder.createSubroutineType(F, EltTypeArray);
2520 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002521
Adrian Prantl800faef2014-02-25 23:42:18 +00002522 // Handle variadic function types; they need an additional
2523 // unspecified parameter.
Adrian Prantld45ba252014-02-25 19:38:11 +00002524 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2525 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002526 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00002527 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2528 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2529 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2530 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2531 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Manman Ren67f005e2014-07-28 22:24:34 +00002532 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Adrian Prantld45ba252014-02-25 19:38:11 +00002533 return DBuilder.createSubroutineType(F, EltTypeArray);
2534 }
2535
David Blaikie469f0792013-05-22 23:22:42 +00002536 return llvm::DICompositeType(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002537}
2538
2539/// EmitFunctionStart - Constructs the debug code for entering a function.
Eric Christophere7b87e52014-10-26 23:40:33 +00002540void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2541 SourceLocation ScopeLoc, QualType FnType,
2542 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002543
2544 StringRef Name;
2545 StringRef LinkageName;
2546
2547 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2548
2549 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002550 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00002551
Guy Benyei11169dd2012-12-18 14:30:41 +00002552 unsigned Flags = 0;
2553 llvm::DIFile Unit = getOrCreateFile(Loc);
2554 llvm::DIDescriptor FDContext(Unit);
2555 llvm::DIArray TParamsArray;
2556 if (!HasDecl) {
2557 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00002558 LinkageName = Fn->getName();
Guy Benyei11169dd2012-12-18 14:30:41 +00002559 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2560 // If there is a DISubprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002561 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002562 if (FI != SPCache.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002563 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00002564 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
2565 llvm::MDNode *SPN = SP;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002566 LexicalBlockStack.emplace_back(SPN);
2567 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002568 return;
2569 }
2570 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002571 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2572 TParamsArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002573 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2574 Name = getObjCMethodName(OMD);
2575 Flags |= llvm::DIDescriptor::FlagPrototyped;
2576 } else {
2577 // Use llvm function name.
2578 Name = Fn->getName();
2579 Flags |= llvm::DIDescriptor::FlagPrototyped;
2580 }
2581 if (!Name.empty() && Name[0] == '\01')
2582 Name = Name.substr(1);
2583
Adrian Prantl42d71b92014-04-10 23:21:53 +00002584 if (!HasDecl || D->isImplicit()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002585 Flags |= llvm::DIDescriptor::FlagArtificial;
Adrian Prantl42d71b92014-04-10 23:21:53 +00002586 // Artificial functions without a location should not silently reuse CurLoc.
2587 if (Loc.isInvalid())
2588 CurLoc = SourceLocation();
2589 }
2590 unsigned LineNo = getLineNumber(Loc);
2591 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002592
Eric Christopher8018e412014-03-27 18:50:35 +00002593 // FIXME: The function declaration we're constructing here is mostly reusing
2594 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2595 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2596 // all subprograms instead of the actual context since subprogram definitions
2597 // are emitted as CU level entities by the backend.
Eric Christophere7b87e52014-10-26 23:40:33 +00002598 llvm::DISubprogram SP = DBuilder.createFunction(
2599 FDContext, Name, LinkageName, Unit, LineNo,
2600 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
2601 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize, Fn,
2602 TParamsArray, getFunctionDeclaration(D));
Frederic Rissb1ab28c2014-11-05 19:19:04 +00002603 // We might get here with a VarDecl in the case we're generating
2604 // code for the initialization of globals. Do not record these decls
2605 // as they will overwrite the actual VarDecl Decl in the cache.
2606 if (HasDecl && isa<FunctionDecl>(D))
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002607 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(SP));
Guy Benyei11169dd2012-12-18 14:30:41 +00002608
Adrian Prantlbebb8932014-03-21 21:01:58 +00002609 // Push the function onto the lexical block stack.
Guy Benyei11169dd2012-12-18 14:30:41 +00002610 llvm::MDNode *SPN = SP;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002611 LexicalBlockStack.emplace_back(SPN);
Adrian Prantlbebb8932014-03-21 21:01:58 +00002612
Guy Benyei11169dd2012-12-18 14:30:41 +00002613 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002614 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002615}
2616
2617/// EmitLocation - Emit metadata to indicate a change in line/column
Adrian Prantl02c0caa2013-07-18 00:27:59 +00002618/// information in the source file. If the location is invalid, the
2619/// previous location will be reused.
Adrian Prantlc7822422013-03-12 20:43:25 +00002620void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc,
Adrian Prantle83b1302014-01-07 22:05:52 +00002621 bool ForceColumnInfo) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002622 // Update our current location
2623 setLocation(Loc);
2624
Eric Christophere7b87e52014-10-26 23:40:33 +00002625 if (CurLoc.isInvalid() || CurLoc.isMacroID())
2626 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002627
2628 // Don't bother if things are the same as last time.
2629 SourceManager &SM = CGM.getContext().getSourceManager();
David Blaikie550d9002014-12-29 18:37:03 +00002630 assert(!LexicalBlockStack.empty());
Guy Benyei11169dd2012-12-18 14:30:41 +00002631 if (CurLoc == PrevLoc ||
2632 SM.getExpansionLoc(CurLoc) == SM.getExpansionLoc(PrevLoc))
2633 // New Builder may not be in sync with CGDebugInfo.
David Blaikie357aafb2013-02-01 19:09:49 +00002634 if (!Builder.getCurrentDebugLocation().isUnknown() &&
2635 Builder.getCurrentDebugLocation().getScope(CGM.getLLVMContext()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00002636 LexicalBlockStack.back())
Guy Benyei11169dd2012-12-18 14:30:41 +00002637 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002638
Guy Benyei11169dd2012-12-18 14:30:41 +00002639 // Update last state.
2640 PrevLoc = CurLoc;
2641
Adrian Prantle83b1302014-01-07 22:05:52 +00002642 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00002643 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2644 getLineNumber(CurLoc), getColumnNumber(CurLoc, ForceColumnInfo), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002645}
2646
2647/// CreateLexicalBlock - Creates a new lexical block node and pushes it on
2648/// the stack.
2649void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002650 llvm::MDNode *Back = nullptr;
2651 if (!LexicalBlockStack.empty())
2652 Back = LexicalBlockStack.back().get();
David Blaikief9ea2422014-06-02 16:32:05 +00002653 llvm::DIDescriptor D = DBuilder.createLexicalBlock(
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002654 llvm::DIDescriptor(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
2655 getColumnNumber(CurLoc));
Guy Benyei11169dd2012-12-18 14:30:41 +00002656 llvm::MDNode *DN = D;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002657 LexicalBlockStack.emplace_back(DN);
Guy Benyei11169dd2012-12-18 14:30:41 +00002658}
2659
2660/// EmitLexicalBlockStart - Constructs the debug code for entering a declarative
2661/// region - beginning of a DW_TAG_lexical_block.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002662void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2663 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002664 // Set our current location.
2665 setLocation(Loc);
2666
Guy Benyei11169dd2012-12-18 14:30:41 +00002667 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00002668 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2669 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00002670
2671 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2672 return;
2673
2674 // Create a new lexical block and push it on the stack.
2675 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002676}
2677
2678/// EmitLexicalBlockEnd - Constructs the debug code for exiting a declarative
2679/// region - end of a DW_TAG_lexical_block.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002680void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2681 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002682 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2683
2684 // Provide an entry in the line table for the end of the block.
2685 EmitLocation(Builder, Loc);
2686
David Blaikie60a877b2014-10-22 19:34:33 +00002687 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2688 return;
2689
Guy Benyei11169dd2012-12-18 14:30:41 +00002690 LexicalBlockStack.pop_back();
2691}
2692
2693/// EmitFunctionEnd - Constructs the debug code for exiting a function.
2694void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2695 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2696 unsigned RCount = FnBeginRegionCount.back();
2697 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2698
2699 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00002700 while (LexicalBlockStack.size() != RCount) {
2701 // Provide an entry in the line table for the end of the block.
2702 EmitLocation(Builder, CurLoc);
2703 LexicalBlockStack.pop_back();
2704 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002705 FnBeginRegionCount.pop_back();
2706}
2707
Eric Christopherb2a008c2013-05-16 00:45:12 +00002708// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
Guy Benyei11169dd2012-12-18 14:30:41 +00002709// See BuildByRefType.
2710llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
2711 uint64_t *XOffset) {
2712
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002713 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002714 QualType FType;
2715 uint64_t FieldSize, FieldOffset;
2716 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002717
Guy Benyei11169dd2012-12-18 14:30:41 +00002718 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002719 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002720
2721 FieldOffset = 0;
2722 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2723 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2724 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2725 FType = CGM.getContext().IntTy;
2726 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2727 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2728
2729 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2730 if (HasCopyAndDispose) {
2731 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002732 EltTys.push_back(
2733 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
2734 EltTys.push_back(
2735 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00002736 }
2737 bool HasByrefExtendedLayout;
2738 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00002739 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
2740 HasByrefExtendedLayout) &&
2741 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00002742 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002743 EltTys.push_back(
2744 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00002745 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002746
Guy Benyei11169dd2012-12-18 14:30:41 +00002747 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2748 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002749 CGM.getTarget().getPointerAlign(0))) {
2750 CharUnits FieldOffsetInBytes =
2751 CGM.getContext().toCharUnitsFromBits(FieldOffset);
2752 CharUnits AlignedOffsetInBytes =
2753 FieldOffsetInBytes.RoundUpToAlignment(Align);
2754 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002755
Guy Benyei11169dd2012-12-18 14:30:41 +00002756 if (NumPaddingBytes.isPositive()) {
2757 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
2758 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2759 pad, ArrayType::Normal, 0);
2760 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
2761 }
2762 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002763
Guy Benyei11169dd2012-12-18 14:30:41 +00002764 FType = Type;
David Blaikief427b002014-05-06 03:42:01 +00002765 llvm::DIType FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002766 FieldSize = CGM.getContext().getTypeSize(FType);
2767 FieldAlign = CGM.getContext().toBits(Align);
2768
Eric Christopherb2a008c2013-05-16 00:45:12 +00002769 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00002770 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
2771 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002772 EltTys.push_back(FieldTy);
2773 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002774
Guy Benyei11169dd2012-12-18 14:30:41 +00002775 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002776
Guy Benyei11169dd2012-12-18 14:30:41 +00002777 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002778
Guy Benyei11169dd2012-12-18 14:30:41 +00002779 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
David Blaikie6d4fe152013-02-25 01:07:08 +00002780 llvm::DIType(), Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002781}
2782
2783/// EmitDeclare - Emit local variable declaration debug info.
Ed Masteda706022014-05-07 12:49:30 +00002784void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::dwarf::LLVMConstants Tag,
Eric Christophere7b87e52014-10-26 23:40:33 +00002785 llvm::Value *Storage, unsigned ArgNo,
2786 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002787 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002788 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2789
David Blaikie7fceebf2013-08-19 03:37:48 +00002790 bool Unwritten =
2791 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
2792 cast<Decl>(VD->getDeclContext())->isImplicit());
2793 llvm::DIFile Unit;
2794 if (!Unwritten)
2795 Unit = getOrCreateFile(VD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002796 llvm::DIType Ty;
2797 uint64_t XOffset = 0;
2798 if (VD->hasAttr<BlocksAttr>())
2799 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002800 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002801 Ty = getOrCreateType(VD->getType(), Unit);
2802
2803 // If there is no debug info for this type then do not emit debug info
2804 // for this variable.
2805 if (!Ty)
2806 return;
2807
Guy Benyei11169dd2012-12-18 14:30:41 +00002808 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00002809 unsigned Line = 0;
2810 unsigned Column = 0;
2811 if (!Unwritten) {
2812 Line = getLineNumber(VD->getLocation());
2813 Column = getColumnNumber(VD->getLocation());
2814 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002815 unsigned Flags = 0;
2816 if (VD->isImplicit())
2817 Flags |= llvm::DIDescriptor::FlagArtificial;
2818 // If this is the first argument and it is implicit then
2819 // give it an object pointer flag.
2820 // FIXME: There has to be a better way to do this, but for static
2821 // functions there won't be an implicit param at arg1 and
2822 // otherwise it is 'self' or 'this'.
2823 if (isa<ImplicitParamDecl>(VD) && ArgNo == 1)
2824 Flags |= llvm::DIDescriptor::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00002825 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00002826 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
2827 !VD->getType()->isPointerType())
David Blaikieb9c667d2013-06-19 21:53:53 +00002828 Flags |= llvm::DIDescriptor::FlagIndirectVariable;
Guy Benyei11169dd2012-12-18 14:30:41 +00002829
2830 llvm::MDNode *Scope = LexicalBlockStack.back();
2831
2832 StringRef Name = VD->getName();
2833 if (!Name.empty()) {
2834 if (VD->hasAttr<BlocksAttr>()) {
2835 CharUnits offset = CharUnits::fromQuantity(32);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002836 SmallVector<int64_t, 9> addr;
2837 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002838 // offset of __forwarding field
2839 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002840 CGM.getTarget().getPointerWidth(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002841 addr.push_back(offset.getQuantity());
2842 addr.push_back(llvm::dwarf::DW_OP_deref);
2843 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002844 // offset of x field
2845 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002846 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002847
2848 // Create the descriptor for the variable.
Eric Christophere7b87e52014-10-26 23:40:33 +00002849 llvm::DIVariable D = DBuilder.createLocalVariable(
2850 Tag, llvm::DIDescriptor(Scope), VD->getName(), Unit, Line, Ty, ArgNo);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002851
Guy Benyei11169dd2012-12-18 14:30:41 +00002852 // Insert an llvm.dbg.declare into the current block.
2853 llvm::Instruction *Call =
Eric Christophere7b87e52014-10-26 23:40:33 +00002854 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr),
2855 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002856 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
2857 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00002858 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl0315f382013-09-18 22:08:57 +00002859 Flags |= llvm::DIDescriptor::FlagIndirectVariable;
David Blaikiea76a7c92013-01-05 05:58:35 +00002860 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2861 // If VD is an anonymous union then Storage represents value for
2862 // all union fields.
Guy Benyei11169dd2012-12-18 14:30:41 +00002863 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
David Blaikie219c7d92013-01-05 20:03:07 +00002864 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00002865 for (const auto *Field : RD->fields()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002866 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
2867 StringRef FieldName = Field->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002868
Guy Benyei11169dd2012-12-18 14:30:41 +00002869 // Ignore unnamed fields. Do not ignore unnamed records.
2870 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2871 continue;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002872
Guy Benyei11169dd2012-12-18 14:30:41 +00002873 // Use VarDecl's Tag, Scope and Line number.
Eric Christophere7b87e52014-10-26 23:40:33 +00002874 llvm::DIVariable D = DBuilder.createLocalVariable(
2875 Tag, llvm::DIDescriptor(Scope), FieldName, Unit, Line, FieldTy,
2876 CGM.getLangOpts().Optimize, Flags, ArgNo);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002877
Guy Benyei11169dd2012-12-18 14:30:41 +00002878 // Insert an llvm.dbg.declare into the current block.
Eric Christophere7b87e52014-10-26 23:40:33 +00002879 llvm::Instruction *Call = DBuilder.insertDeclare(
2880 Storage, D, DBuilder.createExpression(), Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002881 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
2882 }
David Blaikie219c7d92013-01-05 20:03:07 +00002883 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002884 }
2885 }
David Blaikiea76a7c92013-01-05 05:58:35 +00002886
2887 // Create the descriptor for the variable.
Eric Christophere7b87e52014-10-26 23:40:33 +00002888 llvm::DIVariable D = DBuilder.createLocalVariable(
2889 Tag, llvm::DIDescriptor(Scope), Name, Unit, Line, Ty,
2890 CGM.getLangOpts().Optimize, Flags, ArgNo);
David Blaikiea76a7c92013-01-05 05:58:35 +00002891
2892 // Insert an llvm.dbg.declare into the current block.
Eric Christophere7b87e52014-10-26 23:40:33 +00002893 llvm::Instruction *Call = DBuilder.insertDeclare(
2894 Storage, D, DBuilder.createExpression(), Builder.GetInsertBlock());
David Blaikiea76a7c92013-01-05 05:58:35 +00002895 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002896}
2897
2898void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
2899 llvm::Value *Storage,
2900 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002901 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002902 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, 0, Builder);
2903}
2904
Adrian Prantlde17db32013-03-29 19:20:29 +00002905/// Look up the completed type for a self pointer in the TypeCache and
2906/// create a copy of it with the ObjectPointer and Artificial flags
2907/// set. If the type is not cached, a new one is created. This should
2908/// never happen though, since creating a type for the implicit self
2909/// argument implies that we already parsed the interface definition
2910/// and the ivar declarations in the implementation.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002911llvm::DIType CGDebugInfo::CreateSelfType(const QualType &QualTy,
2912 llvm::DIType Ty) {
Adrian Prantlde17db32013-03-29 19:20:29 +00002913 llvm::DIType CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002914 if (CachedTy)
2915 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00002916 return DBuilder.createObjectPointerType(Ty);
2917}
2918
Eric Christophere7b87e52014-10-26 23:40:33 +00002919void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
2920 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00002921 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Eric Christopher75e17682013-05-16 00:45:23 +00002922 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002923 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00002924
Craig Topper8a13c412014-05-21 05:09:00 +00002925 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00002926 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002927
Guy Benyei11169dd2012-12-18 14:30:41 +00002928 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002929
Guy Benyei11169dd2012-12-18 14:30:41 +00002930 uint64_t XOffset = 0;
2931 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
2932 llvm::DIType Ty;
2933 if (isByRef)
2934 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002935 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002936 Ty = getOrCreateType(VD->getType(), Unit);
2937
2938 // Self is passed along as an implicit non-arg variable in a
2939 // block. Mark it as the object pointer.
2940 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00002941 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00002942
2943 // Get location information.
2944 unsigned Line = getLineNumber(VD->getLocation());
2945 unsigned Column = getColumnNumber(VD->getLocation());
2946
2947 const llvm::DataLayout &target = CGM.getDataLayout();
2948
2949 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00002950 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00002951 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
2952
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002953 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00002954 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002955 addr.push_back(llvm::dwarf::DW_OP_deref);
2956 addr.push_back(llvm::dwarf::DW_OP_plus);
2957 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002958 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002959 addr.push_back(llvm::dwarf::DW_OP_deref);
2960 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002961 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00002962 offset =
2963 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002964 addr.push_back(offset.getQuantity());
2965 addr.push_back(llvm::dwarf::DW_OP_deref);
2966 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002967 // offset of x field
2968 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002969 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002970 }
2971
2972 // Create the descriptor for the variable.
2973 llvm::DIVariable D =
Eric Christophere7b87e52014-10-26 23:40:33 +00002974 DBuilder.createLocalVariable(llvm::dwarf::DW_TAG_auto_variable,
2975 llvm::DIDescriptor(LexicalBlockStack.back()),
2976 VD->getName(), Unit, Line, Ty);
Adrian Prantl0f6df002013-03-29 19:20:35 +00002977
Guy Benyei11169dd2012-12-18 14:30:41 +00002978 // Insert an llvm.dbg.declare into the current block.
Adrian Prantl88eec392014-11-21 00:35:25 +00002979 llvm::Instruction *Call = InsertPoint ?
2980 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr),
2981 InsertPoint)
2982 : DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr),
2983 Builder.GetInsertBlock());
Eric Christophere7b87e52014-10-26 23:40:33 +00002984 Call->setDebugLoc(
2985 llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002986}
2987
2988/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
2989/// variable declaration.
2990void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
2991 unsigned ArgNo,
2992 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002993 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002994 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, ArgNo, Builder);
2995}
2996
2997namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00002998struct BlockLayoutChunk {
2999 uint64_t OffsetInBits;
3000 const BlockDecl::Capture *Capture;
3001};
3002bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3003 return l.OffsetInBits < r.OffsetInBits;
3004}
Guy Benyei11169dd2012-12-18 14:30:41 +00003005}
3006
3007void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003008 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003009 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003010 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003011 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003012 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003013 ASTContext &C = CGM.getContext();
3014 const BlockDecl *blockDecl = block.getBlockDecl();
3015
3016 // Collect some general information about the block's location.
3017 SourceLocation loc = blockDecl->getCaretLocation();
3018 llvm::DIFile tunit = getOrCreateFile(loc);
3019 unsigned line = getLineNumber(loc);
3020 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003021
Guy Benyei11169dd2012-12-18 14:30:41 +00003022 // Build the debug-info type for the block literal.
3023 getContextDescriptor(cast<Decl>(blockDecl->getDeclContext()));
3024
3025 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003026 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003027
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003028 SmallVector<llvm::Metadata *, 16> fields;
Guy Benyei11169dd2012-12-18 14:30:41 +00003029 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
3030 blockLayout->getElementOffsetInBits(0),
3031 tunit, tunit));
3032 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
3033 blockLayout->getElementOffsetInBits(1),
3034 tunit, tunit));
3035 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
3036 blockLayout->getElementOffsetInBits(2),
3037 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003038 auto *FnTy = block.getBlockExpr()->getFunctionType();
3039 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
3040 fields.push_back(createFieldType("__FuncPtr", FnPtrType, 0, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003041 blockLayout->getElementOffsetInBits(3),
3042 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003043 fields.push_back(createFieldType(
3044 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3045 ? C.getBlockDescriptorExtendedType()
3046 : C.getBlockDescriptorType()),
3047 0, loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003048
3049 // We want to sort the captures by offset, not because DWARF
3050 // requires this, but because we're paranoid about debuggers.
3051 SmallVector<BlockLayoutChunk, 8> chunks;
3052
3053 // 'this' capture.
3054 if (blockDecl->capturesCXXThis()) {
3055 BlockLayoutChunk chunk;
3056 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003057 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003058 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003059 chunks.push_back(chunk);
3060 }
3061
3062 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003063 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003064 const VarDecl *variable = capture.getVariable();
3065 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3066
3067 // Ignore constant captures.
3068 if (captureInfo.isConstant())
3069 continue;
3070
3071 BlockLayoutChunk chunk;
3072 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003073 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003074 chunk.Capture = &capture;
3075 chunks.push_back(chunk);
3076 }
3077
3078 // Sort by offset.
3079 llvm::array_pod_sort(chunks.begin(), chunks.end());
3080
Eric Christophere7b87e52014-10-26 23:40:33 +00003081 for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
3082 e = chunks.end();
3083 i != e; ++i) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003084 uint64_t offsetInBits = i->OffsetInBits;
3085 const BlockDecl::Capture *capture = i->Capture;
3086
3087 // If we have a null capture, this must be the C++ 'this' capture.
3088 if (!capture) {
3089 const CXXMethodDecl *method =
Eric Christophere7b87e52014-10-26 23:40:33 +00003090 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00003091 QualType type = method->getThisType(C);
3092
3093 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
3094 offsetInBits, tunit, tunit));
3095 continue;
3096 }
3097
3098 const VarDecl *variable = capture->getVariable();
3099 StringRef name = variable->getName();
3100
3101 llvm::DIType fieldType;
3102 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003103 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003104
3105 // FIXME: this creates a second copy of this type!
3106 uint64_t xoffset;
3107 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003108 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3109 fieldType =
3110 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3111 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003112 } else {
Eric Christophere7b87e52014-10-26 23:40:33 +00003113 fieldType = createFieldType(name, variable->getType(), 0, loc, AS_public,
3114 offsetInBits, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003115 }
3116 fields.push_back(fieldType);
3117 }
3118
3119 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003120 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3121 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003122
3123 llvm::DIArray fieldsArray = DBuilder.getOrCreateArray(fields);
3124
3125 llvm::DIType type =
Eric Christophere7b87e52014-10-26 23:40:33 +00003126 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
3127 CGM.getContext().toBits(block.BlockSize),
3128 CGM.getContext().toBits(block.BlockAlign), 0,
3129 llvm::DIType(), fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003130 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3131
3132 // Get overall information about the block.
3133 unsigned flags = llvm::DIDescriptor::FlagArtificial;
3134 llvm::MDNode *scope = LexicalBlockStack.back();
Guy Benyei11169dd2012-12-18 14:30:41 +00003135
3136 // Create the descriptor for the parameter.
Eric Christophere7b87e52014-10-26 23:40:33 +00003137 llvm::DIVariable debugVar = DBuilder.createLocalVariable(
3138 llvm::dwarf::DW_TAG_arg_variable, llvm::DIDescriptor(scope),
3139 Arg->getName(), tunit, line, type, CGM.getLangOpts().Optimize, flags,
3140 ArgNo);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003141
Adrian Prantl616bef42013-03-14 21:52:59 +00003142 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003143 // Insert an llvm.dbg.value into the current block.
Eric Christophere7b87e52014-10-26 23:40:33 +00003144 llvm::Instruction *DbgVal = DBuilder.insertDbgValueIntrinsic(
3145 LocalAddr, 0, debugVar, DBuilder.createExpression(),
3146 Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003147 DbgVal->setDebugLoc(llvm::DebugLoc::get(line, column, scope));
3148 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003149
Adrian Prantl616bef42013-03-14 21:52:59 +00003150 // Insert an llvm.dbg.declare into the current block.
Eric Christophere7b87e52014-10-26 23:40:33 +00003151 llvm::Instruction *DbgDecl = DBuilder.insertDeclare(
3152 Arg, debugVar, DBuilder.createExpression(), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003153 DbgDecl->setDebugLoc(llvm::DebugLoc::get(line, column, scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00003154}
3155
David Blaikie6943dea2013-08-20 01:28:15 +00003156/// If D is an out-of-class definition of a static data member of a class, find
3157/// its corresponding in-class declaration.
3158llvm::DIDerivedType
3159CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3160 if (!D->isStaticDataMember())
3161 return llvm::DIDerivedType();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003162 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003163 if (MI != StaticDataMemberCache.end()) {
3164 assert(MI->second && "Static data member declaration should still exist");
3165 return llvm::DIDerivedType(cast<llvm::MDNode>(MI->second));
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003166 }
David Blaikiece763042013-08-20 21:49:21 +00003167
3168 // If the member wasn't found in the cache, lazily construct and add it to the
3169 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003170 auto DC = D->getDeclContext();
3171 llvm::DICompositeType Ctxt(getContextDescriptor(cast<Decl>(DC)));
3172 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003173}
3174
Eric Christophercab9fae2014-04-10 05:20:00 +00003175/// Recursively collect all of the member fields of a global anonymous decl and
3176/// create static variables for them. The first time this is called it needs
3177/// to be on a union and then from there we can have additional unnamed fields.
3178llvm::DIGlobalVariable
3179CGDebugInfo::CollectAnonRecordDecls(const RecordDecl *RD, llvm::DIFile Unit,
3180 unsigned LineNo, StringRef LinkageName,
3181 llvm::GlobalVariable *Var,
3182 llvm::DIDescriptor DContext) {
3183 llvm::DIGlobalVariable GV;
3184
3185 for (const auto *Field : RD->fields()) {
3186 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
3187 StringRef FieldName = Field->getName();
3188
3189 // Ignore unnamed fields, but recurse into anonymous records.
3190 if (FieldName.empty()) {
3191 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3192 if (RT)
3193 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3194 Var, DContext);
3195 continue;
3196 }
3197 // Use VarDecl's Tag, Scope and Line number.
Eric Christophere7b87e52014-10-26 23:40:33 +00003198 GV = DBuilder.createGlobalVariable(
3199 DContext, FieldName, LinkageName, Unit, LineNo, FieldTy,
3200 Var->hasInternalLinkage(), Var, llvm::DIDerivedType());
Eric Christophercab9fae2014-04-10 05:20:00 +00003201 }
3202 return GV;
3203}
3204
Guy Benyei11169dd2012-12-18 14:30:41 +00003205/// EmitGlobalVariable - Emit information about a global variable.
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003206void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003207 const VarDecl *D) {
Eric Christopher75e17682013-05-16 00:45:23 +00003208 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003209 // Create global variable debug descriptor.
Frederic Riss9db79f12014-11-18 03:40:46 +00003210 llvm::DIFile Unit;
3211 llvm::DIDescriptor DContext;
3212 unsigned LineNo;
3213 StringRef DeclName, LinkageName;
3214 QualType T;
3215 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003216
3217 // Attempt to store one global variable for the declaration - even if we
3218 // emit a lot of fields.
3219 llvm::DIGlobalVariable GV;
3220
3221 // If this is an anonymous union then we'll want to emit a global
3222 // variable for each member of the anonymous union so that it's possible
3223 // to find the name of any field in the union.
3224 if (T->isUnionType() && DeclName.empty()) {
3225 const RecordDecl *RD = cast<RecordType>(T)->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003226 assert(RD->isAnonymousStructOrUnion() &&
3227 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003228 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3229 } else {
David Blaikie7550b112014-10-20 17:42:23 +00003230 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003231 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3232 Var->hasInternalLinkage(), Var,
3233 getOrCreateStaticDataMemberDeclarationOrNull(D));
3234 }
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003235 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(GV));
Guy Benyei11169dd2012-12-18 14:30:41 +00003236}
3237
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003238/// EmitGlobalVariable - Emit global variable's debug info.
3239void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3240 llvm::Constant *Init) {
Eric Christopher75e17682013-05-16 00:45:23 +00003241 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003242 // Create the descriptor for the variable.
3243 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
3244 StringRef Name = VD->getName();
3245 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
3246 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3247 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3248 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3249 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3250 }
3251 // Do not use DIGlobalVariable for enums.
3252 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
3253 return;
David Blaikiea15565562014-04-04 20:56:17 +00003254 // Do not emit separate definitions for function local const/statics.
3255 if (isa<FunctionDecl>(VD->getDeclContext()))
3256 return;
David Blaikiebb113912014-04-05 07:23:17 +00003257 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003258 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003259 if (VarD->isStaticDataMember()) {
3260 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
3261 getContextDescriptor(RD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003262 // Ensure that the type is retained even though it's otherwise unreferenced.
3263 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003264 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003265 return;
3266 }
3267
David Blaikieaf080852014-11-21 00:20:58 +00003268 llvm::DIDescriptor DContext =
3269 getContextDescriptor(dyn_cast<Decl>(VD->getDeclContext()));
3270
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003271 auto &GV = DeclCache[VD];
3272 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003273 return;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003274 GV.reset(DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003275 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003276 true, Init, getOrCreateStaticDataMemberDeclarationOrNull(VarD)));
David Blaikiebd483762013-05-20 04:58:53 +00003277}
3278
3279llvm::DIScope CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
3280 if (!LexicalBlockStack.empty())
3281 return llvm::DIScope(LexicalBlockStack.back());
3282 return getContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00003283}
3284
David Blaikie9f88fe82013-04-22 06:13:21 +00003285void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
David Blaikiebd483762013-05-20 04:58:53 +00003286 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3287 return;
David Blaikie9f88fe82013-04-22 06:13:21 +00003288 DBuilder.createImportedModule(
David Blaikiebd483762013-05-20 04:58:53 +00003289 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3290 getOrCreateNameSpace(UD.getNominatedNamespace()),
David Blaikie9f88fe82013-04-22 06:13:21 +00003291 getLineNumber(UD.getLocation()));
3292}
3293
David Blaikiebd483762013-05-20 04:58:53 +00003294void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
3295 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3296 return;
3297 assert(UD.shadow_size() &&
3298 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3299 // Emitting one decl is sufficient - debuggers can detect that this is an
3300 // overloaded name & provide lookup for all the overloads.
3301 const UsingShadowDecl &USD = **UD.shadow_begin();
Frederic Riss442293e2014-11-06 21:12:06 +00003302 if (llvm::DIDescriptor Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003303 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003304 DBuilder.createImportedDeclaration(
3305 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3306 getLineNumber(USD.getLocation()));
3307}
3308
David Blaikief121b932013-05-20 22:50:41 +00003309llvm::DIImportedEntity
3310CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
3311 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
Craig Topper8a13c412014-05-21 05:09:00 +00003312 return llvm::DIImportedEntity(nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003313 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003314 if (VH)
3315 return llvm::DIImportedEntity(cast<llvm::MDNode>(VH));
Craig Topper8a13c412014-05-21 05:09:00 +00003316 llvm::DIImportedEntity R(nullptr);
David Blaikief121b932013-05-20 22:50:41 +00003317 if (const NamespaceAliasDecl *Underlying =
3318 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3319 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003320 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003321 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3322 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3323 NA.getName());
3324 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003325 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003326 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3327 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3328 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003329 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003330 return R;
3331}
3332
Guy Benyei11169dd2012-12-18 14:30:41 +00003333/// getOrCreateNamesSpace - Return namespace descriptor for the given
3334/// namespace decl.
Eric Christopherb2a008c2013-05-16 00:45:12 +00003335llvm::DINameSpace
Guy Benyei11169dd2012-12-18 14:30:41 +00003336CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003337 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003338 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003339 if (I != NameSpaceCache.end())
3340 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
Eric Christopherb2a008c2013-05-16 00:45:12 +00003341
Guy Benyei11169dd2012-12-18 14:30:41 +00003342 unsigned LineNo = getLineNumber(NSDecl->getLocation());
3343 llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003344 llvm::DIDescriptor Context =
Guy Benyei11169dd2012-12-18 14:30:41 +00003345 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()));
3346 llvm::DINameSpace NS =
3347 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003348 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003349 return NS;
3350}
3351
3352void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003353 // Creating types might create further types - invalidating the current
3354 // element and the size(), so don't cache/reference them.
3355 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3356 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
3357 E.Decl.replaceAllUsesWith(CGM.getLLVMContext(),
3358 E.Type->getDecl()->getDefinition()
3359 ? CreateTypeDefinition(E.Type, E.Unit)
3360 : E.Decl);
3361 }
3362
David Blaikief427b002014-05-06 03:42:01 +00003363 for (auto p : ReplaceMap) {
3364 assert(p.second);
3365 llvm::DIType Ty(cast<llvm::MDNode>(p.second));
David Blaikieb8149042014-05-05 21:21:39 +00003366 assert(Ty.isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003367
David Blaikief427b002014-05-06 03:42:01 +00003368 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003369 assert(it != TypeCache.end());
3370 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003371
David Blaikief427b002014-05-06 03:42:01 +00003372 llvm::DIType RepTy(cast<llvm::MDNode>(it->second));
3373 Ty.replaceAllUsesWith(CGM.getLLVMContext(), RepTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003374 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003375
Frederic Rissd253ed62014-11-18 03:40:51 +00003376 for (const auto &p : FwdDeclReplaceMap) {
3377 assert(p.second);
3378 llvm::DIDescriptor FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003379 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003380
3381 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003382 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003383 // with ourselves, that will destroy the temporary MDNode and
3384 // replace it with a standard one, avoiding leaking memory.
3385 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003386 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003387 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003388 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003389
Frederic Rissd253ed62014-11-18 03:40:51 +00003390 FwdDecl.replaceAllUsesWith(CGM.getLLVMContext(),
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003391 llvm::DIDescriptor(cast<llvm::MDNode>(Repl)));
Frederic Rissd253ed62014-11-18 03:40:51 +00003392 }
3393
Adrian Prantl73409ce2013-03-11 18:33:46 +00003394 // We keep our own list of retained types, because we need to look
3395 // up the final type in the type cache.
3396 for (std::vector<void *>::const_iterator RI = RetainedTypes.begin(),
3397 RE = RetainedTypes.end(); RI != RE; ++RI)
David Blaikie0856f662014-03-04 22:01:08 +00003398 DBuilder.retainType(llvm::DIType(cast<llvm::MDNode>(TypeCache[*RI])));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003399
Guy Benyei11169dd2012-12-18 14:30:41 +00003400 DBuilder.finalize();
3401}
David Blaikie66088d52014-09-24 17:01:27 +00003402
3403void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
3404 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3405 return;
3406 llvm::DIType DieTy = getOrCreateType(Ty, getOrCreateMainFile());
3407 // Don't ignore in case of explicit cast where it is referenced indirectly.
3408 DBuilder.retainType(DieTy);
3409}