blob: 978e1bb5b81fc36d7bdea015e293d556e656e988 [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 Blaikie84fe79c2014-12-30 19:39:33 +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 Blaikie84fe79c2014-12-30 19:39:33 +000061 CGF.Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(0, 0, Scope));
Adrian Prantl2e0637f2013-07-18 00:28:02 +000062 }
63}
64
David Blaikie84fe79c2014-12-30 19:39:33 +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 Blaikieb9a23c92015-01-02 22:07:26 +000078ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc)
79 : CGF(CGF) {
80 if (CGF.getDebugInfo()) {
81 OriginalLocation = CGF.Builder.getCurrentDebugLocation();
82 if (!Loc.isUnknown())
83 CGF.Builder.SetCurrentDebugLocation(Loc);
84 }
85}
86
David Blaikie84fe79c2014-12-30 19:39:33 +000087ApplyDebugLocation::~ApplyDebugLocation() {
88 // Query CGF so the location isn't overwritten when location updates are
89 // temporarily disabled (for C++ default function arguments)
90 if (CGF.getDebugInfo())
91 CGF.Builder.SetCurrentDebugLocation(OriginalLocation);
92}
93
94/// ArtificialLocation - An RAII object that temporarily switches to
95/// an artificial debug location that has a valid scope, but no line
Guy Benyei11169dd2012-12-18 14:30:41 +000096void CGDebugInfo::setLocation(SourceLocation Loc) {
97 // If the new location isn't valid return.
Eric Christophere7b87e52014-10-26 23:40:33 +000098 if (Loc.isInvalid())
99 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000100
101 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
102
103 // If we've changed files in the middle of a lexical scope go ahead
104 // and create a new lexical scope with file node if it's different
105 // from the one in the scope.
Eric Christophere7b87e52014-10-26 23:40:33 +0000106 if (LexicalBlockStack.empty())
107 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000108
109 SourceManager &SM = CGM.getContext().getSourceManager();
David Blaikieaabde052014-05-14 00:29:00 +0000110 llvm::DIScope Scope(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +0000111 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000112
David Blaikieaabde052014-05-14 00:29:00 +0000113 if (PCLoc.isInvalid() || Scope.getFilename() == PCLoc.getFilename())
Guy Benyei11169dd2012-12-18 14:30:41 +0000114 return;
115
Guy Benyei11169dd2012-12-18 14:30:41 +0000116 if (Scope.isLexicalBlockFile()) {
David Blaikieaabde052014-05-14 00:29:00 +0000117 llvm::DILexicalBlockFile LBF = llvm::DILexicalBlockFile(Scope);
Eric Christophere7b87e52014-10-26 23:40:33 +0000118 llvm::DIDescriptor D = DBuilder.createLexicalBlockFile(
119 LBF.getScope(), getOrCreateFile(CurLoc));
Guy Benyei11169dd2012-12-18 14:30:41 +0000120 llvm::MDNode *N = D;
121 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000122 LexicalBlockStack.emplace_back(N);
David Blaikie0a21d0d2013-01-26 22:16:26 +0000123 } else if (Scope.isLexicalBlock() || Scope.isSubprogram()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000124 llvm::DIDescriptor D =
125 DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc));
Guy Benyei11169dd2012-12-18 14:30:41 +0000126 llvm::MDNode *N = D;
127 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000128 LexicalBlockStack.emplace_back(N);
Guy Benyei11169dd2012-12-18 14:30:41 +0000129 }
130}
131
132/// getContextDescriptor - Get context info for the decl.
David Blaikiebfa52742013-04-19 06:56:38 +0000133llvm::DIScope CGDebugInfo::getContextDescriptor(const Decl *Context) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000134 if (!Context)
135 return TheCU;
136
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000137 auto I = RegionMap.find(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +0000138 if (I != RegionMap.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000139 llvm::Metadata *V = I->second;
David Blaikiebfa52742013-04-19 06:56:38 +0000140 return llvm::DIScope(dyn_cast_or_null<llvm::MDNode>(V));
Guy Benyei11169dd2012-12-18 14:30:41 +0000141 }
142
143 // Check namespace.
144 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000145 return getOrCreateNameSpace(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +0000146
David Blaikiebfa52742013-04-19 06:56:38 +0000147 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context))
148 if (!RDecl->isDependentType())
149 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Eric Christophere7b87e52014-10-26 23:40:33 +0000150 getOrCreateMainFile());
Guy Benyei11169dd2012-12-18 14:30:41 +0000151 return TheCU;
152}
153
154/// getFunctionName - Get function name for the given FunctionDecl. If the
Benjamin Kramer60509af2013-09-09 14:48:42 +0000155/// name is constructed on demand (e.g. C++ destructor) then the name
Guy Benyei11169dd2012-12-18 14:30:41 +0000156/// is stored on the side.
157StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000158 assert(FD && "Invalid FunctionDecl!");
Guy Benyei11169dd2012-12-18 14:30:41 +0000159 IdentifierInfo *FII = FD->getIdentifier();
Eric Christophere7b87e52014-10-26 23:40:33 +0000160 FunctionTemplateSpecializationInfo *Info =
161 FD->getTemplateSpecializationInfo();
Guy Benyei11169dd2012-12-18 14:30:41 +0000162 if (!Info && FII)
163 return FII->getName();
164
165 // Otherwise construct human readable name for debug info.
Benjamin Kramer9170e912013-02-22 15:46:01 +0000166 SmallString<128> NS;
167 llvm::raw_svector_ostream OS(NS);
168 FD->printName(OS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000169
170 // Add any template specialization args.
171 if (Info) {
172 const TemplateArgumentList *TArgs = Info->TemplateArguments;
173 const TemplateArgument *Args = TArgs->data();
174 unsigned NumArgs = TArgs->size();
175 PrintingPolicy Policy(CGM.getLangOpts());
Benjamin Kramer9170e912013-02-22 15:46:01 +0000176 TemplateSpecializationType::PrintTemplateArgumentList(OS, Args, NumArgs,
177 Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000178 }
179
180 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000181 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000182}
183
184StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
185 SmallString<256> MethodName;
186 llvm::raw_svector_ostream OS(MethodName);
187 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
188 const DeclContext *DC = OMD->getDeclContext();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000189 if (const ObjCImplementationDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000190 dyn_cast<const ObjCImplementationDecl>(DC)) {
191 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000192 } else if (const ObjCInterfaceDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000193 dyn_cast<const ObjCInterfaceDecl>(DC)) {
194 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000195 } else if (const ObjCCategoryImplDecl *OCD =
Eric Christophere7b87e52014-10-26 23:40:33 +0000196 dyn_cast<const ObjCCategoryImplDecl>(DC)) {
197 OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '('
198 << OCD->getIdentifier()->getNameStart() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000199 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000200 // We can extract the type of the class from the self pointer.
Eric Christophere7b87e52014-10-26 23:40:33 +0000201 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000202 QualType ClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000203 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000204 ClassTy.print(OS, PrintingPolicy(LangOptions()));
205 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000206 }
207 OS << ' ' << OMD->getSelector().getAsString() << ']';
208
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000209 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000210}
211
212/// getSelectorName - Return selector name. This is used for debugging
213/// info.
214StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000215 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000216}
217
218/// getClassName - Get class name including template argument list.
Eric Christophere7b87e52014-10-26 23:40:33 +0000219StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
David Blaikie65813a32014-04-02 18:21:09 +0000220 // quick optimization to avoid having to intern strings that are already
221 // stored reliably elsewhere
222 if (!isa<ClassTemplateSpecializationDecl>(RD))
Guy Benyei11169dd2012-12-18 14:30:41 +0000223 return RD->getName();
224
David Blaikie65813a32014-04-02 18:21:09 +0000225 SmallString<128> Name;
Benjamin Kramer9170e912013-02-22 15:46:01 +0000226 {
David Blaikie65813a32014-04-02 18:21:09 +0000227 llvm::raw_svector_ostream OS(Name);
228 RD->getNameForDiagnostic(OS, CGM.getContext().getPrintingPolicy(),
229 /*Qualified*/ false);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000230 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000231
232 // Copy this name on the side and use its reference.
David Blaikie65813a32014-04-02 18:21:09 +0000233 return internString(Name);
Guy Benyei11169dd2012-12-18 14:30:41 +0000234}
235
236/// getOrCreateFile - Get the file debug info descriptor for the input location.
237llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
238 if (!Loc.isValid())
239 // If Location is not valid then use main input file.
240 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
241
242 SourceManager &SM = CGM.getContext().getSourceManager();
243 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
244
245 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
246 // If the location is not valid then use main input file.
247 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
248
249 // Cache the results.
250 const char *fname = PLoc.getFilename();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000251 auto it = DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000252
253 if (it != DIFileCache.end()) {
254 // Verify that the information still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000255 if (llvm::Metadata *V = it->second)
Guy Benyei11169dd2012-12-18 14:30:41 +0000256 return llvm::DIFile(cast<llvm::MDNode>(V));
257 }
258
259 llvm::DIFile F = DBuilder.createFile(PLoc.getFilename(), getCurrentDirname());
260
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000261 DIFileCache[fname].reset(F);
Guy Benyei11169dd2012-12-18 14:30:41 +0000262 return F;
263}
264
265/// getOrCreateMainFile - Get the file info for main compile unit.
266llvm::DIFile CGDebugInfo::getOrCreateMainFile() {
267 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
268}
269
270/// getLineNumber - Get line number for the location. If location is invalid
271/// then use current location.
272unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
273 if (Loc.isInvalid() && CurLoc.isInvalid())
274 return 0;
275 SourceManager &SM = CGM.getContext().getSourceManager();
276 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000277 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000278}
279
280/// getColumnNumber - Get column number for the location.
Adrian Prantlc7822422013-03-12 20:43:25 +0000281unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000282 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000283 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000284 return 0;
285
286 // If the location is invalid then use the current column.
287 if (Loc.isInvalid() && CurLoc.isInvalid())
288 return 0;
289 SourceManager &SM = CGM.getContext().getSourceManager();
290 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000291 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000292}
293
294StringRef CGDebugInfo::getCurrentDirname() {
295 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
296 return CGM.getCodeGenOpts().DebugCompilationDir;
297
298 if (!CWDName.empty())
299 return CWDName;
300 SmallString<256> CWD;
301 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000302 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000303}
304
305/// CreateCompileUnit - Create new compile unit.
306void CGDebugInfo::CreateCompileUnit() {
307
David Blaikieaabde052014-05-14 00:29:00 +0000308 // Should we be asking the SourceManager for the main file name, instead of
309 // accepting it as an argument? This just causes the main file name to
310 // mismatch with source locations and create extra lexical scopes or
311 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
312 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
313 // because that's what the SourceManager says)
314
Guy Benyei11169dd2012-12-18 14:30:41 +0000315 // Get absolute path name.
316 SourceManager &SM = CGM.getContext().getSourceManager();
317 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
318 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000319 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000320
321 // The main file name provided via the "-main-file-name" option contains just
322 // the file name itself with no path information. This file name may have had
323 // a relative path, so we look into the actual file entry for the main
324 // file to determine the real absolute path for the file.
325 std::string MainFileDir;
326 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
327 MainFileDir = MainFile->getDir()->getName();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000328 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000329 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
330 llvm::sys::path::append(MainFileDirSS, MainFileName);
331 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000332 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000333 }
334
335 // Save filename string.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000336 StringRef Filename = internString(MainFileName);
Eric Christopherf1545832013-02-22 23:50:16 +0000337
338 // Save split dwarf file string.
339 std::string SplitDwarfFile = CGM.getCodeGenOpts().SplitDwarfFile;
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000340 StringRef SplitDwarfFilename = internString(SplitDwarfFile);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000341
Ed Masteda706022014-05-07 12:49:30 +0000342 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000343 const LangOptions &LO = CGM.getLangOpts();
344 if (LO.CPlusPlus) {
345 if (LO.ObjC1)
346 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
347 else
348 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
349 } else if (LO.ObjC1) {
350 LangTag = llvm::dwarf::DW_LANG_ObjC;
351 } else if (LO.C99) {
352 LangTag = llvm::dwarf::DW_LANG_C99;
353 } else {
354 LangTag = llvm::dwarf::DW_LANG_C89;
355 }
356
357 std::string Producer = getClangFullVersion();
358
359 // Figure out which version of the ObjC runtime we have.
360 unsigned RuntimeVers = 0;
361 if (LO.ObjC1)
362 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
363
364 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000365 // FIXME - Eliminate TheCU.
Eric Christophere4200a22014-02-27 01:25:08 +0000366 TheCU = DBuilder.createCompileUnit(
367 LangTag, Filename, getCurrentDirname(), Producer, LO.Optimize,
368 CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers, SplitDwarfFilename,
Diego Novillo913690c2014-06-24 17:02:17 +0000369 DebugKind <= CodeGenOptions::DebugLineTablesOnly
Eric Christophere4200a22014-02-27 01:25:08 +0000370 ? llvm::DIBuilder::LineTablesOnly
Diego Novillo913690c2014-06-24 17:02:17 +0000371 : llvm::DIBuilder::FullDebug,
372 DebugKind != CodeGenOptions::LocTrackingOnly);
Guy Benyei11169dd2012-12-18 14:30:41 +0000373}
374
375/// CreateType - Get the Basic type from the cache or create a new
376/// one if necessary.
377llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000378 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000379 StringRef BTName;
380 switch (BT->getKind()) {
381#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000382#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000383#include "clang/AST/BuiltinTypes.def"
384 case BuiltinType::Dependent:
385 llvm_unreachable("Unexpected builtin type");
386 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000387 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000388 case BuiltinType::Void:
389 return llvm::DIType();
390 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000391 if (!ClassTy)
392 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
393 "objc_class", TheCU,
394 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000395 return ClassTy;
396 case BuiltinType::ObjCId: {
397 // typedef struct objc_class *Class;
398 // typedef struct objc_object {
399 // Class isa;
400 // } *id;
401
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000402 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000403 return ObjTy;
404
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000405 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000406 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
407 "objc_class", TheCU,
408 getOrCreateMainFile(), 0);
409
410 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000411
Guy Benyei11169dd2012-12-18 14:30:41 +0000412 llvm::DIType ISATy = DBuilder.createPointerType(ClassTy, Size);
413
Eric Christopher5c7ee8b2013-04-02 22:59:11 +0000414 ObjTy =
David Blaikie6d4fe152013-02-25 01:07:08 +0000415 DBuilder.createStructType(TheCU, "objc_object", getOrCreateMainFile(),
416 0, 0, 0, 0, llvm::DIType(), llvm::DIArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000417
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000418 DBuilder.replaceArrays(
419 ObjTy,
420 DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
421 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0, 0, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000422 return ObjTy;
423 }
424 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000425 if (!SelTy)
426 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
427 "objc_selector", TheCU,
428 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000429 return SelTy;
430 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000431
432 case BuiltinType::OCLImage1d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000433 return getOrCreateStructPtrType("opencl_image1d_t", OCLImage1dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000434 case BuiltinType::OCLImage1dArray:
Eric Christopherb2a008c2013-05-16 00:45:12 +0000435 return getOrCreateStructPtrType("opencl_image1d_array_t",
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000436 OCLImage1dArrayDITy);
437 case BuiltinType::OCLImage1dBuffer:
438 return getOrCreateStructPtrType("opencl_image1d_buffer_t",
439 OCLImage1dBufferDITy);
440 case BuiltinType::OCLImage2d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000441 return getOrCreateStructPtrType("opencl_image2d_t", OCLImage2dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000442 case BuiltinType::OCLImage2dArray:
443 return getOrCreateStructPtrType("opencl_image2d_array_t",
444 OCLImage2dArrayDITy);
445 case BuiltinType::OCLImage3d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000446 return getOrCreateStructPtrType("opencl_image3d_t", OCLImage3dDITy);
Guy Benyei61054192013-02-07 10:55:47 +0000447 case BuiltinType::OCLSampler:
Eric Christophere7b87e52014-10-26 23:40:33 +0000448 return DBuilder.createBasicType(
449 "opencl_sampler_t", CGM.getContext().getTypeSize(BT),
450 CGM.getContext().getTypeAlign(BT), llvm::dwarf::DW_ATE_unsigned);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000451 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000452 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000453
Guy Benyei11169dd2012-12-18 14:30:41 +0000454 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000455 case BuiltinType::Char_U:
456 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
457 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000458 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000459 case BuiltinType::SChar:
460 Encoding = llvm::dwarf::DW_ATE_signed_char;
461 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000462 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000463 case BuiltinType::Char32:
464 Encoding = llvm::dwarf::DW_ATE_UTF;
465 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000466 case BuiltinType::UShort:
467 case BuiltinType::UInt:
468 case BuiltinType::UInt128:
469 case BuiltinType::ULong:
470 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000471 case BuiltinType::ULongLong:
472 Encoding = llvm::dwarf::DW_ATE_unsigned;
473 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000474 case BuiltinType::Short:
475 case BuiltinType::Int:
476 case BuiltinType::Int128:
477 case BuiltinType::Long:
478 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000479 case BuiltinType::LongLong:
480 Encoding = llvm::dwarf::DW_ATE_signed;
481 break;
482 case BuiltinType::Bool:
483 Encoding = llvm::dwarf::DW_ATE_boolean;
484 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000485 case BuiltinType::Half:
486 case BuiltinType::Float:
487 case BuiltinType::LongDouble:
Eric Christophere7b87e52014-10-26 23:40:33 +0000488 case BuiltinType::Double:
489 Encoding = llvm::dwarf::DW_ATE_float;
490 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000491 }
492
493 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000494 case BuiltinType::Long:
495 BTName = "long int";
496 break;
497 case BuiltinType::LongLong:
498 BTName = "long long int";
499 break;
500 case BuiltinType::ULong:
501 BTName = "long unsigned int";
502 break;
503 case BuiltinType::ULongLong:
504 BTName = "long long unsigned int";
505 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000506 default:
507 BTName = BT->getName(CGM.getLangOpts());
508 break;
509 }
510 // Bit size, align and offset of the type.
511 uint64_t Size = CGM.getContext().getTypeSize(BT);
512 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Eric Christophere7b87e52014-10-26 23:40:33 +0000513 llvm::DIType DbgTy = DBuilder.createBasicType(BTName, Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000514 return DbgTy;
515}
516
517llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty) {
518 // Bit size, align and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000519 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000520 if (Ty->isComplexIntegerType())
521 Encoding = llvm::dwarf::DW_ATE_lo_user;
522
523 uint64_t Size = CGM.getContext().getTypeSize(Ty);
524 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000525 llvm::DIType DbgTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000526 DBuilder.createBasicType("complex", Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000527
528 return DbgTy;
529}
530
531/// CreateCVRType - Get the qualified type from the cache or create
532/// a new one if necessary.
David Blaikie99dab3b2013-09-04 22:03:57 +0000533llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000534 QualifierCollector Qc;
535 const Type *T = Qc.strip(Ty);
536
537 // Ignore these qualifiers for now.
538 Qc.removeObjCGCAttr();
539 Qc.removeAddressSpace();
540 Qc.removeObjCLifetime();
541
542 // We will create one Derived type for one qualifier and recurse to handle any
543 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000544 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000545 if (Qc.hasConst()) {
546 Tag = llvm::dwarf::DW_TAG_const_type;
547 Qc.removeConst();
548 } else if (Qc.hasVolatile()) {
549 Tag = llvm::dwarf::DW_TAG_volatile_type;
550 Qc.removeVolatile();
551 } else if (Qc.hasRestrict()) {
552 Tag = llvm::dwarf::DW_TAG_restrict_type;
553 Qc.removeRestrict();
554 } else {
555 assert(Qc.empty() && "Unknown type qualifier for debug info");
556 return getOrCreateType(QualType(T, 0), Unit);
557 }
558
David Blaikie99dab3b2013-09-04 22:03:57 +0000559 llvm::DIType FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000560
561 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
562 // CVR derived types.
563 llvm::DIType DbgTy = DBuilder.createQualifiedType(Tag, FromTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000564
Guy Benyei11169dd2012-12-18 14:30:41 +0000565 return DbgTy;
566}
567
568llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
569 llvm::DIFile Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000570
571 // The frontend treats 'id' as a typedef to an ObjCObjectType,
572 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
573 // debug info, we want to emit 'id' in both cases.
574 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000575 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000576
Eric Christophere7b87e52014-10-26 23:40:33 +0000577 llvm::DIType DbgTy = CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type,
578 Ty, Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000579 return DbgTy;
580}
581
Eric Christophere7b87e52014-10-26 23:40:33 +0000582llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty, llvm::DIFile Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000583 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000584 Ty->getPointeeType(), Unit);
585}
586
Manman Rene0064d82013-08-29 23:19:58 +0000587/// In C++ mode, types have linkage, so we can rely on the ODR and
588/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000589static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
590 CodeGenModule &CGM,
591 llvm::DICompileUnit TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000592 SmallString<256> FullName;
593 // FIXME: ODR should apply to ObjC++ exactly the same wasy it does to C++.
594 // For now, only apply ODR with C++.
595 const TagDecl *TD = Ty->getDecl();
596 if (TheCU.getLanguage() != llvm::dwarf::DW_LANG_C_plus_plus ||
597 !TD->isExternallyVisible())
598 return FullName;
599 // Microsoft Mangler does not have support for mangleCXXRTTIName yet.
600 if (CGM.getTarget().getCXXABI().isMicrosoft())
601 return FullName;
602
603 // TODO: This is using the RTTI name. Is there a better way to get
604 // a unique string for a type?
605 llvm::raw_svector_ostream Out(FullName);
606 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
607 Out.flush();
608 return FullName;
609}
610
Guy Benyei11169dd2012-12-18 14:30:41 +0000611// Creates a forward declaration for a RecordDecl in the given context.
David Blaikie8d5e1282013-08-20 21:03:29 +0000612llvm::DICompositeType
Manman Ren1b457022013-08-28 21:20:28 +0000613CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
David Blaikie8d5e1282013-08-20 21:03:29 +0000614 llvm::DIDescriptor Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000615 const RecordDecl *RD = Ty->getDecl();
David Blaikie4e7ef802013-08-15 20:17:25 +0000616 if (llvm::DIType T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
David Blaikie8d5e1282013-08-20 21:03:29 +0000617 return llvm::DICompositeType(T);
Guy Benyei11169dd2012-12-18 14:30:41 +0000618 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
619 unsigned Line = getLineNumber(RD->getLocation());
620 StringRef RDName = getClassName(RD);
621
Ed Masteda706022014-05-07 12:49:30 +0000622 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000623 if (RD->isStruct() || RD->isInterface())
624 Tag = llvm::dwarf::DW_TAG_structure_type;
625 else if (RD->isUnion())
626 Tag = llvm::dwarf::DW_TAG_union_type;
627 else {
628 assert(RD->isClass());
629 Tag = llvm::dwarf::DW_TAG_class_type;
630 }
631
632 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000633 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
David Blaikief427b002014-05-06 03:42:01 +0000634 llvm::DICompositeType RetTy = DBuilder.createReplaceableForwardDecl(
635 Tag, RDName, Ctx, DefUnit, Line, 0, 0, 0, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000636 ReplaceMap.emplace_back(
637 std::piecewise_construct, std::make_tuple(Ty),
638 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000639 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000640}
641
Ed Masteda706022014-05-07 12:49:30 +0000642llvm::DIType CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Eric Christopherb2a008c2013-05-16 00:45:12 +0000643 const Type *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000644 QualType PointeeTy,
645 llvm::DIFile Unit) {
646 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
647 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
David Blaikie99dab3b2013-09-04 22:03:57 +0000648 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit));
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000649
Guy Benyei11169dd2012-12-18 14:30:41 +0000650 // Bit size, align and offset of the type.
651 // Size is always the size of a pointer. We can't use getTypeSize here
652 // because that does not return the correct value for references.
653 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000654 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000655 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
656
David Blaikie99dab3b2013-09-04 22:03:57 +0000657 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
658 Align);
Guy Benyei11169dd2012-12-18 14:30:41 +0000659}
660
Eric Christopher0fdcb312013-05-16 00:52:20 +0000661llvm::DIType CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
662 llvm::DIType &Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000663 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000664 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000665 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
666 TheCU, getOrCreateMainFile(), 0);
667 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
668 Cache = DBuilder.createPointerType(Cache, Size);
669 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000670}
671
Guy Benyei11169dd2012-12-18 14:30:41 +0000672llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
673 llvm::DIFile Unit) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000674 if (BlockLiteralGeneric)
Guy Benyei11169dd2012-12-18 14:30:41 +0000675 return BlockLiteralGeneric;
676
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000677 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000678 llvm::DIType FieldTy;
679 QualType FType;
680 uint64_t FieldSize, FieldOffset;
681 unsigned FieldAlign;
682 llvm::DIArray Elements;
683 llvm::DIType EltTy, DescTy;
684
685 FieldOffset = 0;
686 FType = CGM.getContext().UnsignedLongTy;
687 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
688 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
689
690 Elements = DBuilder.getOrCreateArray(EltTys);
691 EltTys.clear();
692
693 unsigned Flags = llvm::DIDescriptor::FlagAppleBlock;
694 unsigned LineNo = getLineNumber(CurLoc);
695
Eric Christophere7b87e52014-10-26 23:40:33 +0000696 EltTy = DBuilder.createStructType(Unit, "__block_descriptor", Unit, LineNo,
697 FieldOffset, 0, Flags, llvm::DIType(),
698 Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000699
700 // Bit size, align and offset of the type.
701 uint64_t Size = CGM.getContext().getTypeSize(Ty);
702
703 DescTy = DBuilder.createPointerType(EltTy, Size);
704
705 FieldOffset = 0;
706 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
707 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
708 FType = CGM.getContext().IntTy;
709 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
710 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000711 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000712 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
713
714 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
715 FieldTy = DescTy;
716 FieldSize = CGM.getContext().getTypeSize(Ty);
717 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Eric Christophere7b87e52014-10-26 23:40:33 +0000718 FieldTy =
719 DBuilder.createMemberType(Unit, "__descriptor", Unit, LineNo, FieldSize,
720 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000721 EltTys.push_back(FieldTy);
722
723 FieldOffset += FieldSize;
724 Elements = DBuilder.getOrCreateArray(EltTys);
725
Eric Christophere7b87e52014-10-26 23:40:33 +0000726 EltTy = DBuilder.createStructType(Unit, "__block_literal_generic", Unit,
727 LineNo, FieldOffset, 0, Flags,
728 llvm::DIType(), Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000729
Guy Benyei11169dd2012-12-18 14:30:41 +0000730 BlockLiteralGeneric = DBuilder.createPointerType(EltTy, Size);
731 return BlockLiteralGeneric;
732}
733
Eric Christophere7b87e52014-10-26 23:40:33 +0000734llvm::DIType CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
735 llvm::DIFile Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000736 assert(Ty->isTypeAlias());
737 llvm::DIType Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000738
739 SmallString<128> NS;
740 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000741 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
742 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000743
744 TemplateSpecializationType::PrintTemplateArgumentList(
745 OS, Ty->getArgs(), Ty->getNumArgs(),
746 CGM.getContext().getPrintingPolicy());
747
Eric Christophere7b87e52014-10-26 23:40:33 +0000748 TypeAliasDecl *AliasDecl = cast<TypeAliasTemplateDecl>(
749 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000750
751 SourceLocation Loc = AliasDecl->getLocation();
752 llvm::DIFile File = getOrCreateFile(Loc);
753 unsigned Line = getLineNumber(Loc);
754
Eric Christophere7b87e52014-10-26 23:40:33 +0000755 llvm::DIDescriptor Ctxt =
756 getContextDescriptor(cast<Decl>(AliasDecl->getDeclContext()));
David Blaikief1b382e2014-04-06 17:14:06 +0000757
758 return DBuilder.createTypedef(Src, internString(OS.str()), File, Line, Ctxt);
759}
760
David Blaikie99dab3b2013-09-04 22:03:57 +0000761llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000762 // Typedefs are derived from some other type. If we have a typedef of a
763 // typedef, make sure to emit the whole chain.
David Blaikie99dab3b2013-09-04 22:03:57 +0000764 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000765 // We don't set size information, but do specify where the typedef was
766 // declared.
Adrian Prantl3eff2252014-01-21 18:42:27 +0000767 SourceLocation Loc = Ty->getDecl()->getLocation();
768 llvm::DIFile File = getOrCreateFile(Loc);
769 unsigned Line = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000770 const TypedefNameDecl *TyDecl = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000771
Guy Benyei11169dd2012-12-18 14:30:41 +0000772 llvm::DIDescriptor TypedefContext =
Eric Christophere7b87e52014-10-26 23:40:33 +0000773 getContextDescriptor(cast<Decl>(Ty->getDecl()->getDeclContext()));
Eric Christopherb2a008c2013-05-16 00:45:12 +0000774
Eric Christophere7b87e52014-10-26 23:40:33 +0000775 return DBuilder.createTypedef(Src, TyDecl->getName(), File, Line,
776 TypedefContext);
Guy Benyei11169dd2012-12-18 14:30:41 +0000777}
778
779llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
780 llvm::DIFile Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000781 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000782
783 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000784 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000785
786 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000787 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000788 if (isa<FunctionNoProtoType>(Ty))
789 EltTys.push_back(DBuilder.createUnspecifiedParameter());
790 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
Alp Toker9cacbab2014-01-20 20:26:09 +0000791 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
792 EltTys.push_back(getOrCreateType(FPT->getParamType(i), Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000793 if (FPT->isVariadic())
794 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000795 }
796
Manman Ren67f005e2014-07-28 22:24:34 +0000797 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Guy Benyei11169dd2012-12-18 14:30:41 +0000798 return DBuilder.createSubroutineType(Unit, EltTypeArray);
799}
800
Adrian Prantl21361fb2014-08-29 22:44:27 +0000801/// Convert an AccessSpecifier into the corresponding DIDescriptor flag.
802/// As an optimization, return 0 if the access specifier equals the
803/// default for the containing type.
804static unsigned getAccessFlag(AccessSpecifier Access, const RecordDecl *RD) {
805 AccessSpecifier Default = clang::AS_none;
806 if (RD && RD->isClass())
807 Default = clang::AS_private;
808 else if (RD && (RD->isStruct() || RD->isUnion()))
809 Default = clang::AS_public;
810
811 if (Access == Default)
812 return 0;
813
Eric Christophere7b87e52014-10-26 23:40:33 +0000814 switch (Access) {
815 case clang::AS_private:
816 return llvm::DIDescriptor::FlagPrivate;
817 case clang::AS_protected:
818 return llvm::DIDescriptor::FlagProtected;
819 case clang::AS_public:
820 return llvm::DIDescriptor::FlagPublic;
821 case clang::AS_none:
822 return 0;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000823 }
824 llvm_unreachable("unexpected access enumerator");
825}
Guy Benyei11169dd2012-12-18 14:30:41 +0000826
Eric Christophere7b87e52014-10-26 23:40:33 +0000827llvm::DIType CGDebugInfo::createFieldType(
828 StringRef name, QualType type, uint64_t sizeInBitsOverride,
829 SourceLocation loc, AccessSpecifier AS, uint64_t offsetInBits,
830 llvm::DIFile tunit, llvm::DIScope scope, const RecordDecl *RD) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000831 llvm::DIType debugType = getOrCreateType(type, tunit);
832
833 // Get the location for the field.
834 llvm::DIFile file = getOrCreateFile(loc);
835 unsigned line = getLineNumber(loc);
836
David Majnemer34b57492014-07-30 01:30:47 +0000837 uint64_t SizeInBits = 0;
838 unsigned AlignInBits = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000839 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +0000840 TypeInfo TI = CGM.getContext().getTypeInfo(type);
841 SizeInBits = TI.Width;
842 AlignInBits = TI.Align;
Guy Benyei11169dd2012-12-18 14:30:41 +0000843
844 if (sizeInBitsOverride)
David Majnemer34b57492014-07-30 01:30:47 +0000845 SizeInBits = sizeInBitsOverride;
Guy Benyei11169dd2012-12-18 14:30:41 +0000846 }
847
Adrian Prantl21361fb2014-08-29 22:44:27 +0000848 unsigned flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +0000849 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
850 AlignInBits, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +0000851}
852
Eric Christopher91a31902013-01-16 01:22:32 +0000853/// CollectRecordLambdaFields - Helper for CollectRecordFields.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000854void CGDebugInfo::CollectRecordLambdaFields(
855 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
856 llvm::DIType RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +0000857 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
858 // has the name and the location of the variable so we should iterate over
859 // both concurrently.
860 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
861 RecordDecl::field_iterator Field = CXXDecl->field_begin();
862 unsigned fieldno = 0;
863 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +0000864 E = CXXDecl->captures_end();
865 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000866 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +0000867 if (C.capturesVariable()) {
868 VarDecl *V = C.getCapturedVar();
869 llvm::DIFile VUnit = getOrCreateFile(C.getLocation());
870 StringRef VName = V->getName();
871 uint64_t SizeInBitsOverride = 0;
872 if (Field->isBitField()) {
873 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
874 assert(SizeInBitsOverride && "found named 0-width bitfield");
875 }
Eric Christophere7b87e52014-10-26 23:40:33 +0000876 llvm::DIType fieldType = createFieldType(
877 VName, Field->getType(), SizeInBitsOverride, C.getLocation(),
878 Field->getAccess(), layout.getFieldOffset(fieldno), VUnit, RecordTy,
879 CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000880 elements.push_back(fieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +0000881 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +0000882 // TODO: Need to handle 'this' in some way by probably renaming the
883 // this of the lambda class and having a field member of 'this' or
884 // by using AT_object_pointer for the function and having that be
885 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +0000886 FieldDecl *f = *Field;
887 llvm::DIFile VUnit = getOrCreateFile(f->getLocation());
888 QualType type = f->getType();
Eric Christophere7b87e52014-10-26 23:40:33 +0000889 llvm::DIType fieldType = createFieldType(
890 "this", type, 0, f->getLocation(), f->getAccess(),
891 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000892
893 elements.push_back(fieldType);
894 }
895 }
896}
897
David Blaikie6943dea2013-08-20 01:28:15 +0000898/// Helper for CollectRecordFields.
Eric Christophere7b87e52014-10-26 23:40:33 +0000899llvm::DIDerivedType CGDebugInfo::CreateRecordStaticField(const VarDecl *Var,
900 llvm::DIType RecordTy,
901 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000902 // Create the descriptor for the static variable, with or without
903 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +0000904 Var = Var->getCanonicalDecl();
Eric Christopher91a31902013-01-16 01:22:32 +0000905 llvm::DIFile VUnit = getOrCreateFile(Var->getLocation());
906 llvm::DIType VTy = getOrCreateType(Var->getType(), VUnit);
907
Eric Christopher91a31902013-01-16 01:22:32 +0000908 unsigned LineNumber = getLineNumber(Var->getLocation());
909 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +0000910 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +0000911 if (Var->getInit()) {
912 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +0000913 if (Value) {
914 if (Value->isInt())
915 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
916 if (Value->isFloat())
917 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
918 }
Eric Christopher91a31902013-01-16 01:22:32 +0000919 }
920
Adrian Prantl21361fb2014-08-29 22:44:27 +0000921 unsigned Flags = getAccessFlag(Var->getAccess(), RD);
David Blaikieae019462013-08-15 22:50:29 +0000922 llvm::DIDerivedType GV = DBuilder.createStaticMemberType(
923 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000924 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +0000925 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +0000926}
927
928/// CollectRecordNormalField - Helper for CollectRecordFields.
Eric Christophere7b87e52014-10-26 23:40:33 +0000929void CGDebugInfo::CollectRecordNormalField(
930 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000931 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +0000932 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000933 StringRef name = field->getName();
934 QualType type = field->getType();
935
936 // Ignore unnamed fields unless they're anonymous structs/unions.
937 if (name.empty() && !type->isRecordType())
938 return;
939
940 uint64_t SizeInBitsOverride = 0;
941 if (field->isBitField()) {
942 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
943 assert(SizeInBitsOverride && "found named 0-width bitfield");
944 }
945
Eric Christophere7b87e52014-10-26 23:40:33 +0000946 llvm::DIType fieldType =
947 createFieldType(name, type, SizeInBitsOverride, field->getLocation(),
948 field->getAccess(), OffsetInBits, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +0000949
950 elements.push_back(fieldType);
951}
952
Guy Benyei11169dd2012-12-18 14:30:41 +0000953/// CollectRecordFields - A helper function to collect debug info for
954/// record fields. This is used while creating debug info entry for a Record.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000955void CGDebugInfo::CollectRecordFields(
956 const RecordDecl *record, llvm::DIFile tunit,
957 SmallVectorImpl<llvm::Metadata *> &elements,
958 llvm::DICompositeType RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000959 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
960
Eric Christopher91a31902013-01-16 01:22:32 +0000961 if (CXXDecl && CXXDecl->isLambda())
962 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
963 else {
964 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +0000965
Eric Christopher91a31902013-01-16 01:22:32 +0000966 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +0000967 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +0000968
Eric Christopher91a31902013-01-16 01:22:32 +0000969 // Static and non-static members should appear in the same order as
970 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +0000971 for (const auto *I : record->decls())
972 if (const auto *V = dyn_cast<VarDecl>(I)) {
David Blaikiece763042013-08-20 21:49:21 +0000973 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000974 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +0000975 if (MI != StaticDataMemberCache.end()) {
976 assert(MI->second &&
977 "Static data member declaration should still exist");
978 elements.push_back(
979 llvm::DIDerivedType(cast<llvm::MDNode>(MI->second)));
Adrian Prantl21361fb2014-08-29 22:44:27 +0000980 } else {
981 auto Field = CreateRecordStaticField(V, RecordTy, record);
982 elements.push_back(Field);
983 }
Aaron Ballman629afae2014-03-07 19:56:05 +0000984 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000985 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
986 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +0000987
988 // Bump field number for next field.
989 ++fieldNo;
Guy Benyei11169dd2012-12-18 14:30:41 +0000990 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000991 }
992}
993
994/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
995/// function type is not updated to include implicit "this" pointer. Use this
996/// routine to get a method type which includes "this" pointer.
David Blaikie469f0792013-05-22 23:22:42 +0000997llvm::DICompositeType
Guy Benyei11169dd2012-12-18 14:30:41 +0000998CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
999 llvm::DIFile Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001000 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001001 if (Method->isStatic())
David Blaikie469f0792013-05-22 23:22:42 +00001002 return llvm::DICompositeType(getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001003 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1004 Func, Unit);
1005}
David Blaikie2aaf0652013-01-07 22:24:59 +00001006
David Blaikie469f0792013-05-22 23:22:42 +00001007llvm::DICompositeType CGDebugInfo::getOrCreateInstanceMethodType(
David Blaikie7eb06852013-01-07 23:06:35 +00001008 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001009 // Add "this" pointer.
Manman Ren67f005e2014-07-28 22:24:34 +00001010 llvm::DITypeArray Args = llvm::DISubroutineType(
1011 getOrCreateType(QualType(Func, 0), Unit)).getTypeArray();
Eric Christophere7b87e52014-10-26 23:40:33 +00001012 assert(Args.getNumElements() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001013
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001014 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001015
1016 // First element is always return type. For 'void' functions it is NULL.
1017 Elts.push_back(Args.getElement(0));
1018
David Blaikie2aaf0652013-01-07 22:24:59 +00001019 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001020 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001021 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1022 // Create pointer type directly in this case.
1023 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1024 QualType PointeeTy = ThisPtrTy->getPointeeType();
1025 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001026 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie2aaf0652013-01-07 22:24:59 +00001027 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
1028 llvm::DIType PointeeType = getOrCreateType(PointeeTy, Unit);
Eric Christopher0fdcb312013-05-16 00:52:20 +00001029 llvm::DIType ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001030 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001031 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001032 // TODO: This and the artificial type below are misleading, the
1033 // types aren't artificial the argument is, but the current
1034 // metadata doesn't represent that.
1035 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1036 Elts.push_back(ThisPtrType);
1037 } else {
1038 llvm::DIType ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001039 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001040 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1041 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001042 }
1043
1044 // Copy rest of the arguments.
1045 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
1046 Elts.push_back(Args.getElement(i));
1047
Manman Ren67f005e2014-07-28 22:24:34 +00001048 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001049
Adrian Prantl0630eb72013-12-18 21:48:18 +00001050 unsigned Flags = 0;
1051 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
1052 Flags |= llvm::DIDescriptor::FlagLValueReference;
1053 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
1054 Flags |= llvm::DIDescriptor::FlagRValueReference;
1055
1056 return DBuilder.createSubroutineType(Unit, EltTypeArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001057}
1058
Eric Christopherb2a008c2013-05-16 00:45:12 +00001059/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001060/// inside a function.
1061static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1062 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1063 return isFunctionLocalClass(NRD);
1064 if (isa<FunctionDecl>(RD->getDeclContext()))
1065 return true;
1066 return false;
1067}
1068
1069/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
1070/// a single member function GlobalDecl.
1071llvm::DISubprogram
1072CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Eric Christophere7b87e52014-10-26 23:40:33 +00001073 llvm::DIFile Unit, llvm::DIType RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001074 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001075 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001076
Guy Benyei11169dd2012-12-18 14:30:41 +00001077 StringRef MethodName = getFunctionName(Method);
David Blaikie469f0792013-05-22 23:22:42 +00001078 llvm::DICompositeType MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001079
1080 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1081 // make sense to give a single ctor/dtor a linkage name.
1082 StringRef MethodLinkageName;
1083 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1084 MethodLinkageName = CGM.getMangledName(Method);
1085
1086 // Get the location for the method.
David Blaikie7fceebf2013-08-19 03:37:48 +00001087 llvm::DIFile MethodDefUnit;
1088 unsigned MethodLine = 0;
1089 if (!Method->isImplicit()) {
1090 MethodDefUnit = getOrCreateFile(Method->getLocation());
1091 MethodLine = getLineNumber(Method->getLocation());
1092 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001093
1094 // Collect virtual method info.
1095 llvm::DIType ContainingType;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001096 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001097 unsigned VIndex = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001098
Guy Benyei11169dd2012-12-18 14:30:41 +00001099 if (Method->isVirtual()) {
1100 if (Method->isPure())
1101 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1102 else
1103 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001104
Guy Benyei11169dd2012-12-18 14:30:41 +00001105 // It doesn't make sense to give a virtual destructor a vtable index,
1106 // since a single destructor has two entries in the vtable.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001107 // FIXME: Add proper support for debug info for virtual calls in
1108 // the Microsoft ABI, where we may use multiple vptrs to make a vftable
1109 // lookup if we have multiple or virtual inheritance.
1110 if (!isa<CXXDestructorDecl>(Method) &&
1111 !CGM.getTarget().getCXXABI().isMicrosoft())
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001112 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
Guy Benyei11169dd2012-12-18 14:30:41 +00001113 ContainingType = RecordTy;
1114 }
1115
1116 unsigned Flags = 0;
1117 if (Method->isImplicit())
1118 Flags |= llvm::DIDescriptor::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001119 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
Guy Benyei11169dd2012-12-18 14:30:41 +00001120 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1121 if (CXXC->isExplicit())
1122 Flags |= llvm::DIDescriptor::FlagExplicit;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001123 } else if (const CXXConversionDecl *CXXC =
Eric Christophere7b87e52014-10-26 23:40:33 +00001124 dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001125 if (CXXC->isExplicit())
1126 Flags |= llvm::DIDescriptor::FlagExplicit;
1127 }
1128 if (Method->hasPrototype())
1129 Flags |= llvm::DIDescriptor::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001130 if (Method->getRefQualifier() == RQ_LValue)
1131 Flags |= llvm::DIDescriptor::FlagLValueReference;
1132 if (Method->getRefQualifier() == RQ_RValue)
1133 Flags |= llvm::DIDescriptor::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001134
1135 llvm::DIArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
Eric Christophere7b87e52014-10-26 23:40:33 +00001136 llvm::DISubprogram SP = DBuilder.createMethod(
1137 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
1138 MethodTy, /*isLocalToUnit=*/false,
1139 /* isDefinition=*/false, Virtuality, VIndex, ContainingType, Flags,
1140 CGM.getLangOpts().Optimize, nullptr, TParamsArray);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001141
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001142 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001143
1144 return SP;
1145}
1146
1147/// CollectCXXMemberFunctions - A helper function to collect debug info for
Eric Christopherb2a008c2013-05-16 00:45:12 +00001148/// C++ member functions. This is used while creating debug info entry for
Guy Benyei11169dd2012-12-18 14:30:41 +00001149/// a Record.
Eric Christophere7b87e52014-10-26 23:40:33 +00001150void CGDebugInfo::CollectCXXMemberFunctions(
1151 const CXXRecordDecl *RD, llvm::DIFile Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001152 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001153
1154 // Since we want more than just the individual member decls if we
1155 // have templated functions iterate over every declaration to gather
1156 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001157 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001158 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1159 // If the member is implicit, don't add it to the member list. This avoids
1160 // the member being added to type units by LLVM, while still allowing it
1161 // to be emitted into the type declaration/reference inside the compile
1162 // unit.
David Blaikie6dddfe32014-10-06 05:52:27 +00001163 // FIXME: Handle Using(Shadow?)Decls here to create
1164 // DW_TAG_imported_declarations inside the class for base decls brought into
1165 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1166 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1167 // referenced)
David Blaikiefd580722014-10-06 05:18:55 +00001168 if (!Method || Method->isImplicit())
1169 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001170
1171 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1172 continue;
1173
David Blaikiefd580722014-10-06 05:18:55 +00001174 // Reuse the existing member function declaration if it exists.
1175 // It may be associated with the declaration of the type & should be
1176 // reused as we're building the definition.
1177 //
1178 // This situation can arise in the vtable-based debug info reduction where
1179 // implicit members are emitted in a non-vtable TU.
1180 auto MI = SPCache.find(Method->getCanonicalDecl());
1181 EltTys.push_back(MI == SPCache.end()
1182 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001183 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001184 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001185}
Guy Benyei11169dd2012-12-18 14:30:41 +00001186
Guy Benyei11169dd2012-12-18 14:30:41 +00001187/// CollectCXXBases - A helper function to collect debug info for
Eric Christopherb2a008c2013-05-16 00:45:12 +00001188/// C++ base classes. This is used while creating debug info entry for
Guy Benyei11169dd2012-12-18 14:30:41 +00001189/// a Record.
Eric Christophere7b87e52014-10-26 23:40:33 +00001190void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001191 SmallVectorImpl<llvm::Metadata *> &EltTys,
Eric Christophere7b87e52014-10-26 23:40:33 +00001192 llvm::DIType RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001193
1194 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Aaron Ballman574705e2014-03-13 15:41:46 +00001195 for (const auto &BI : RD->bases()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001196 unsigned BFlags = 0;
1197 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001198
Guy Benyei11169dd2012-12-18 14:30:41 +00001199 const CXXRecordDecl *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001200 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001201
Aaron Ballman574705e2014-03-13 15:41:46 +00001202 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001203 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1204 // virtual base offset offset is -ve. The code generator emits dwarf
1205 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001206 BaseOffset = 0 - CGM.getItaniumVTableContext()
1207 .getVirtualBaseOffsetOffset(RD, Base)
1208 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001209 } else {
1210 // In the MS ABI, store the vbtable offset, which is analogous to the
1211 // vbase offset offset in Itanium.
1212 BaseOffset =
1213 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1214 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001215 BFlags = llvm::DIDescriptor::FlagVirtual;
1216 } else
1217 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1218 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1219 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001220
Adrian Prantl21361fb2014-08-29 22:44:27 +00001221 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001222 llvm::DIType DTy = DBuilder.createInheritance(
1223 RecordTy, getOrCreateType(BI.getType(), Unit), BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001224 EltTys.push_back(DTy);
1225 }
1226}
1227
1228/// CollectTemplateParams - A helper function to collect template parameters.
Eric Christophere7b87e52014-10-26 23:40:33 +00001229llvm::DIArray
1230CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1231 ArrayRef<TemplateArgument> TAList,
1232 llvm::DIFile Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001233 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001234 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1235 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001236 StringRef Name;
1237 if (TPList)
1238 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001239 switch (TA.getKind()) {
1240 case TemplateArgument::Type: {
Guy Benyei11169dd2012-12-18 14:30:41 +00001241 llvm::DIType TTy = getOrCreateType(TA.getAsType(), Unit);
1242 llvm::DITemplateTypeParameter TTP =
David Blaikie47c11502013-06-22 18:59:18 +00001243 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00001244 TemplateParams.push_back(TTP);
David Blaikie38079fd2013-05-10 21:53:14 +00001245 } break;
1246 case TemplateArgument::Integral: {
Guy Benyei11169dd2012-12-18 14:30:41 +00001247 llvm::DIType TTy = getOrCreateType(TA.getIntegralType(), Unit);
1248 llvm::DITemplateValueParameter TVP =
David Blaikie38079fd2013-05-10 21:53:14 +00001249 DBuilder.createTemplateValueParameter(
David Blaikie47c11502013-06-22 18:59:18 +00001250 TheCU, Name, TTy,
David Blaikie38079fd2013-05-10 21:53:14 +00001251 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral()));
1252 TemplateParams.push_back(TVP);
1253 } break;
1254 case TemplateArgument::Declaration: {
1255 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001256 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
David Blaikie38079fd2013-05-10 21:53:14 +00001257 llvm::DIType TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001258 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001259 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001260 // Variable pointer template parameters have a value that is the address
1261 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001262 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001263 V = CGM.GetAddrOfGlobalVar(VD);
1264 // Member function pointers have special support for building them, though
1265 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001266 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Blaikie0a7c9d52014-10-20 20:29:35 +00001267 V = CGM.getCXXABI().EmitMemberPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001268 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001269 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001270 // Member data pointers have special handling too to compute the fixed
1271 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001272 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001273 // These five lines (& possibly the above member function pointer
1274 // handling) might be able to be refactored to use similar code in
1275 // CodeGenModule::getMemberPointerConstant
1276 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1277 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001278 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001279 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001280 }
1281 llvm::DITemplateValueParameter TVP =
Duncan P. N. Exon Smith2f68dad2014-11-15 00:24:50 +00001282 DBuilder.createTemplateValueParameter(
1283 TheCU, Name, TTy,
1284 cast_or_null<llvm::Constant>(V->stripPointerCasts()));
David Blaikie38079fd2013-05-10 21:53:14 +00001285 TemplateParams.push_back(TVP);
1286 } break;
1287 case TemplateArgument::NullPtr: {
1288 QualType T = TA.getNullPtrType();
1289 llvm::DIType TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001290 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001291 // Special case member data pointer null values since they're actually -1
1292 // instead of zero.
1293 if (const MemberPointerType *MPT =
1294 dyn_cast<MemberPointerType>(T.getTypePtr()))
1295 // But treat member function pointers as simple zero integers because
1296 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1297 // CodeGen grows handling for values of non-null member function
1298 // pointers then perhaps we could remove this special case and rely on
1299 // EmitNullMemberPointer for member function pointers.
1300 if (MPT->isMemberDataPointer())
1301 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1302 if (!V)
1303 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
1304 llvm::DITemplateValueParameter TVP =
Duncan P. N. Exon Smith2f68dad2014-11-15 00:24:50 +00001305 DBuilder.createTemplateValueParameter(TheCU, Name, TTy,
1306 cast<llvm::Constant>(V));
David Blaikie38079fd2013-05-10 21:53:14 +00001307 TemplateParams.push_back(TVP);
1308 } break;
David Blaikie47c11502013-06-22 18:59:18 +00001309 case TemplateArgument::Template: {
Eric Christophere7b87e52014-10-26 23:40:33 +00001310 llvm::DITemplateValueParameter
1311 TVP = DBuilder.createTemplateTemplateParameter(
1312 TheCU, Name, llvm::DIType(),
1313 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString());
David Blaikie47c11502013-06-22 18:59:18 +00001314 TemplateParams.push_back(TVP);
1315 } break;
1316 case TemplateArgument::Pack: {
Eric Christophere7b87e52014-10-26 23:40:33 +00001317 llvm::DITemplateValueParameter TVP = DBuilder.createTemplateParameterPack(
1318 TheCU, Name, llvm::DIType(),
1319 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit));
David Blaikie47c11502013-06-22 18:59:18 +00001320 TemplateParams.push_back(TVP);
1321 } break;
David Majnemer5559d472013-08-24 08:21:10 +00001322 case TemplateArgument::Expression: {
1323 const Expr *E = TA.getAsExpr();
1324 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001325 if (E->isGLValue())
1326 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001327 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001328 assert(V && "Expression in template argument isn't constant");
1329 llvm::DIType TTy = getOrCreateType(T, Unit);
1330 llvm::DITemplateValueParameter TVP =
Duncan P. N. Exon Smith2f68dad2014-11-15 00:24:50 +00001331 DBuilder.createTemplateValueParameter(
1332 TheCU, Name, TTy, cast<llvm::Constant>(V->stripPointerCasts()));
David Majnemer5559d472013-08-24 08:21:10 +00001333 TemplateParams.push_back(TVP);
1334 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001335 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001336 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001337 case TemplateArgument::Null:
1338 llvm_unreachable(
1339 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001340 }
1341 }
1342 return DBuilder.getOrCreateArray(TemplateParams);
1343}
1344
1345/// CollectFunctionTemplateParams - A helper function to collect debug
1346/// info for function template parameters.
Eric Christophere7b87e52014-10-26 23:40:33 +00001347llvm::DIArray CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
1348 llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001349 if (FD->getTemplatedKind() ==
1350 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001351 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1352 ->getTemplate()
1353 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001354 return CollectTemplateParams(
1355 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001356 }
1357 return llvm::DIArray();
1358}
1359
1360/// CollectCXXTemplateParams - A helper function to collect debug info for
1361/// template parameters.
Eric Christophere7b87e52014-10-26 23:40:33 +00001362llvm::DIArray CGDebugInfo::CollectCXXTemplateParams(
1363 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001364 // Always get the full list of parameters, not just the ones from
1365 // the specialization.
1366 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001367 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001368 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001369 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001370}
1371
1372/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
1373llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
1374 if (VTablePtrType.isValid())
1375 return VTablePtrType;
1376
1377 ASTContext &Context = CGM.getContext();
1378
1379 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001380 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Manman Ren67f005e2014-07-28 22:24:34 +00001381 llvm::DITypeArray SElements = DBuilder.getOrCreateTypeArray(STy);
Guy Benyei11169dd2012-12-18 14:30:41 +00001382 llvm::DIType SubTy = DBuilder.createSubroutineType(Unit, SElements);
1383 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00001384 llvm::DIType vtbl_ptr_type =
1385 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001386 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1387 return VTablePtrType;
1388}
1389
1390/// getVTableName - Get vtable name for the given Class.
1391StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001392 // Copy the gdb compatible name on the side and use its reference.
1393 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001394}
1395
Guy Benyei11169dd2012-12-18 14:30:41 +00001396/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
1397/// debug info entry in EltTys vector.
Eric Christophere7b87e52014-10-26 23:40:33 +00001398void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001399 SmallVectorImpl<llvm::Metadata *> &EltTys) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001400 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1401
1402 // If there is a primary base then it will hold vtable info.
1403 if (RL.getPrimaryBase())
1404 return;
1405
1406 // If this class is not dynamic then there is not any vtable info to collect.
1407 if (!RD->isDynamicClass())
1408 return;
1409
1410 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00001411 llvm::DIType VPTR = DBuilder.createMemberType(
1412 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
1413 llvm::DIDescriptor::FlagArtificial, getOrCreateVTablePtrType(Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001414 EltTys.push_back(VPTR);
1415}
1416
Eric Christopherb2a008c2013-05-16 00:45:12 +00001417/// getOrCreateRecordType - Emit record type's standalone debug info.
1418llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
Guy Benyei11169dd2012-12-18 14:30:41 +00001419 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001420 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00001421 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
1422 return T;
1423}
1424
1425/// getOrCreateInterfaceType - Emit an objective c interface type standalone
1426/// debug info.
1427llvm::DIType CGDebugInfo::getOrCreateInterfaceType(QualType D,
Eric Christopherc0c5d462013-02-21 22:35:08 +00001428 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001429 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00001430 llvm::DIType T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantl73409ce2013-03-11 18:33:46 +00001431 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001432 return T;
1433}
1434
David Blaikie483a9da2014-05-06 18:35:21 +00001435void CGDebugInfo::completeType(const EnumDecl *ED) {
1436 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1437 return;
1438 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001439 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001440 auto I = TypeCache.find(TyPtr);
1441 if (I == TypeCache.end() ||
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001442 !llvm::DIType(cast<llvm::MDNode>(I->second)).isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001443 return;
1444 llvm::DIType Res = CreateTypeDefinition(Ty->castAs<EnumType>());
1445 assert(!Res.isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001446 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001447}
1448
David Blaikieb2e86eb2013-08-15 20:49:17 +00001449void CGDebugInfo::completeType(const RecordDecl *RD) {
1450 if (DebugKind > CodeGenOptions::LimitedDebugInfo ||
1451 !CGM.getLangOpts().CPlusPlus)
1452 completeRequiredType(RD);
1453}
1454
1455void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
David Blaikie0856f662014-03-04 22:01:08 +00001456 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1457 return;
1458
David Blaikie6943dea2013-08-20 01:28:15 +00001459 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1460 if (CXXDecl->isDynamicClass())
1461 return;
1462
David Blaikieb2e86eb2013-08-15 20:49:17 +00001463 QualType Ty = CGM.getContext().getRecordType(RD);
1464 llvm::DIType T = getTypeOrNull(Ty);
David Blaikie6943dea2013-08-20 01:28:15 +00001465 if (T && T.isForwardDecl())
1466 completeClassData(RD);
1467}
1468
1469void CGDebugInfo::completeClassData(const RecordDecl *RD) {
1470 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001471 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001472 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001473 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001474 auto I = TypeCache.find(TyPtr);
1475 if (I != TypeCache.end() &&
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001476 !llvm::DIType(cast<llvm::MDNode>(I->second)).isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001477 return;
1478 llvm::DIType Res = CreateTypeDefinition(Ty->castAs<RecordType>());
1479 assert(!Res.isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001480 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001481}
1482
David Blaikie0e716b42014-03-03 23:48:23 +00001483static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1484 CXXRecordDecl::method_iterator End) {
1485 for (; I != End; ++I)
1486 if (FunctionDecl *Tmpl = I->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001487 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
1488 !I->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001489 return true;
1490 return false;
1491}
1492
1493static bool shouldOmitDefinition(CodeGenOptions::DebugInfoKind DebugKind,
1494 const RecordDecl *RD,
1495 const LangOptions &LangOpts) {
1496 if (DebugKind > CodeGenOptions::LimitedDebugInfo)
1497 return false;
1498
1499 if (!LangOpts.CPlusPlus)
1500 return false;
1501
1502 if (!RD->isCompleteDefinitionRequired())
1503 return true;
1504
1505 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1506
1507 if (!CXXDecl)
1508 return false;
1509
1510 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass())
1511 return true;
1512
1513 TemplateSpecializationKind Spec = TSK_Undeclared;
1514 if (const ClassTemplateSpecializationDecl *SD =
1515 dyn_cast<ClassTemplateSpecializationDecl>(RD))
1516 Spec = SD->getSpecializationKind();
1517
1518 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1519 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1520 CXXDecl->method_end()))
1521 return true;
1522
1523 return false;
1524}
1525
Guy Benyei11169dd2012-12-18 14:30:41 +00001526/// CreateType - get structure or union type.
David Blaikie99dab3b2013-09-04 22:03:57 +00001527llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001528 RecordDecl *RD = Ty->getDecl();
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001529 llvm::DICompositeType T(getTypeOrNull(QualType(Ty, 0)));
David Blaikie0e716b42014-03-03 23:48:23 +00001530 if (T || shouldOmitDefinition(DebugKind, RD, CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001531 if (!T)
David Blaikie65ec94e2014-02-18 20:52:05 +00001532 T = getOrCreateRecordFwdDecl(
1533 Ty, getContextDescriptor(cast<Decl>(RD->getDeclContext())));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001534 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001535 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001536
David Blaikieb2e86eb2013-08-15 20:49:17 +00001537 return CreateTypeDefinition(Ty);
1538}
1539
1540llvm::DIType CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
1541 RecordDecl *RD = Ty->getDecl();
1542
Guy Benyei11169dd2012-12-18 14:30:41 +00001543 // Get overall information about the record type for the debug info.
1544 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
1545
1546 // Records and classes and unions can all be recursive. To handle them, we
1547 // first generate a debug descriptor for the struct as a forward declaration.
1548 // Then (if it is a definition) we go through and get debug info for all of
1549 // its members. Finally, we create a descriptor for the complete type (which
1550 // may refer to the forward decl if the struct is recursive) and replace all
1551 // uses of the forward declaration with the final definition.
1552
David Blaikie4a2b5ef2013-08-12 22:24:20 +00001553 llvm::DICompositeType FwdDecl(getOrCreateLimitedType(Ty, DefUnit));
Manman Ren0d441f12013-07-02 19:01:53 +00001554 assert(FwdDecl.isCompositeType() &&
David Blaikie469f0792013-05-22 23:22:42 +00001555 "The debug type of a RecordType should be a llvm::DICompositeType");
Guy Benyei11169dd2012-12-18 14:30:41 +00001556
1557 if (FwdDecl.isForwardDecl())
1558 return FwdDecl;
1559
David Blaikieadfbf992013-08-18 16:55:33 +00001560 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1561 CollectContainingType(CXXDecl, FwdDecl);
1562
Guy Benyei11169dd2012-12-18 14:30:41 +00001563 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001564 LexicalBlockStack.emplace_back(&*FwdDecl);
1565 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001566
Guy Benyei11169dd2012-12-18 14:30:41 +00001567 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001568 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001569 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001570
1571 // Note: The split of CXXDecl information here is intentional, the
1572 // gdb tests will depend on a certain ordering at printout. The debug
1573 // information offsets are still correct if we merge them all together
1574 // though.
1575 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1576 if (CXXDecl) {
1577 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1578 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1579 }
1580
Eric Christopher91a31902013-01-16 01:22:32 +00001581 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001582 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001583 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001584 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001585
1586 LexicalBlockStack.pop_back();
1587 RegionMap.erase(Ty->getDecl());
1588
1589 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001590 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001591
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001592 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001593 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001594}
1595
1596/// CreateType - get objective-c object type.
1597llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1598 llvm::DIFile Unit) {
1599 // Ignore protocols.
1600 return getOrCreateType(Ty->getBaseType(), Unit);
1601}
1602
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001603/// \return true if Getter has the default name for the property PD.
1604static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1605 const ObjCMethodDecl *Getter) {
1606 assert(PD);
1607 if (!Getter)
1608 return true;
1609
1610 assert(Getter->getDeclName().isObjCZeroArgSelector());
1611 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001612 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001613}
1614
1615/// \return true if Setter has the default name for the property PD.
1616static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1617 const ObjCMethodDecl *Setter) {
1618 assert(PD);
1619 if (!Setter)
1620 return true;
1621
1622 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001623 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001624 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001625}
1626
Guy Benyei11169dd2012-12-18 14:30:41 +00001627/// CreateType - get objective-c interface type.
1628llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1629 llvm::DIFile Unit) {
1630 ObjCInterfaceDecl *ID = Ty->getDecl();
1631 if (!ID)
1632 return llvm::DIType();
1633
1634 // Get overall information about the record type for the debug info.
1635 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
1636 unsigned Line = getLineNumber(ID->getLocation());
Ed Masteda706022014-05-07 12:49:30 +00001637 llvm::dwarf::SourceLanguage RuntimeLang = TheCU.getLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001638
1639 // If this is just a forward declaration return a special forward-declaration
1640 // debug type since we won't be able to lay out the entire type.
1641 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001642 if (!Def || !Def->getImplementation()) {
David Blaikief427b002014-05-06 03:42:01 +00001643 llvm::DIType FwdDecl = DBuilder.createReplaceableForwardDecl(
1644 llvm::dwarf::DW_TAG_structure_type, ID->getName(), TheCU, DefUnit, Line,
1645 RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001646 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001647 return FwdDecl;
1648 }
1649
David Blaikieef8a9512014-05-05 23:23:53 +00001650 return CreateTypeDefinition(Ty, Unit);
1651}
1652
Eric Christophere7b87e52014-10-26 23:40:33 +00001653llvm::DIType CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
1654 llvm::DIFile Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00001655 ObjCInterfaceDecl *ID = Ty->getDecl();
1656 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
1657 unsigned Line = getLineNumber(ID->getLocation());
1658 unsigned RuntimeLang = TheCU.getLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001659
1660 // Bit size, align and offset of the type.
1661 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1662 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1663
1664 unsigned Flags = 0;
1665 if (ID->getImplementation())
1666 Flags |= llvm::DIDescriptor::FlagObjcClassComplete;
1667
Eric Christophere7b87e52014-10-26 23:40:33 +00001668 llvm::DICompositeType RealDecl = DBuilder.createStructType(
1669 Unit, ID->getName(), DefUnit, Line, Size, Align, Flags, llvm::DIType(),
1670 llvm::DIArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001671
David Blaikieef8a9512014-05-05 23:23:53 +00001672 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001673 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001674
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001675 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001676 LexicalBlockStack.emplace_back(static_cast<llvm::MDNode *>(RealDecl));
1677 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001678
1679 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001680 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001681
1682 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1683 if (SClass) {
1684 llvm::DIType SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001685 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001686 if (!SClassTy.isValid())
1687 return llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001688
Eric Christophere7b87e52014-10-26 23:40:33 +00001689 llvm::DIType InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001690 EltTys.push_back(InhTag);
1691 }
1692
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001693 // Create entries for all of the properties.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001694 for (const auto *PD : ID->properties()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001695 SourceLocation Loc = PD->getLocation();
1696 llvm::DIFile PUnit = getOrCreateFile(Loc);
1697 unsigned PLine = getLineNumber(Loc);
1698 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1699 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001700 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
1701 PD->getName(), PUnit, PLine,
1702 hasDefaultGetterName(PD, Getter) ? ""
1703 : getSelectorName(PD->getGetterName()),
1704 hasDefaultSetterName(PD, Setter) ? ""
1705 : getSelectorName(PD->getSetterName()),
1706 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001707 EltTys.push_back(PropertyNode);
1708 }
1709
1710 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1711 unsigned FieldNo = 0;
1712 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1713 Field = Field->getNextIvar(), ++FieldNo) {
1714 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1715 if (!FieldTy.isValid())
1716 return llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001717
Guy Benyei11169dd2012-12-18 14:30:41 +00001718 StringRef FieldName = Field->getName();
1719
1720 // Ignore unnamed fields.
1721 if (FieldName.empty())
1722 continue;
1723
1724 // Get the location for the field.
1725 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1726 unsigned FieldLine = getLineNumber(Field->getLocation());
1727 QualType FType = Field->getType();
1728 uint64_t FieldSize = 0;
1729 unsigned FieldAlign = 0;
1730
1731 if (!FType->isIncompleteArrayType()) {
1732
1733 // Bit size, align and offset of the type.
1734 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001735 ? Field->getBitWidthValue(CGM.getContext())
1736 : CGM.getContext().getTypeSize(FType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001737 FieldAlign = CGM.getContext().getTypeAlign(FType);
1738 }
1739
1740 uint64_t FieldOffset;
1741 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1742 // We don't know the runtime offset of an ivar if we're using the
1743 // non-fragile ABI. For bitfields, use the bit offset into the first
1744 // byte of storage of the bitfield. For other fields, use zero.
1745 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001746 FieldOffset =
1747 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00001748 FieldOffset %= CGM.getContext().getCharWidth();
1749 } else {
1750 FieldOffset = 0;
1751 }
1752 } else {
1753 FieldOffset = RL.getFieldOffset(FieldNo);
1754 }
1755
1756 unsigned Flags = 0;
1757 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
1758 Flags = llvm::DIDescriptor::FlagProtected;
1759 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
1760 Flags = llvm::DIDescriptor::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001761 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
1762 Flags = llvm::DIDescriptor::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00001763
Craig Topper8a13c412014-05-21 05:09:00 +00001764 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001765 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001766 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00001767 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001768 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00001769 SourceLocation Loc = PD->getLocation();
1770 llvm::DIFile PUnit = getOrCreateFile(Loc);
1771 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001772 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1773 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001774 PropertyNode = DBuilder.createObjCProperty(
1775 PD->getName(), PUnit, PLine,
1776 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
1777 PD->getGetterName()),
1778 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
1779 PD->getSetterName()),
1780 PD->getPropertyAttributes(),
1781 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001782 }
1783 }
1784 }
Eric Christophere7b87e52014-10-26 23:40:33 +00001785 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
1786 FieldSize, FieldAlign, FieldOffset, Flags,
1787 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00001788 EltTys.push_back(FieldTy);
1789 }
1790
1791 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001792 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00001793
Guy Benyei11169dd2012-12-18 14:30:41 +00001794 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001795 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001796}
1797
1798llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty, llvm::DIFile Unit) {
1799 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1800 int64_t Count = Ty->getNumElements();
1801 if (Count == 0)
1802 // If number of elements are not known then this is an unbounded array.
1803 // Use Count == -1 to express such arrays.
1804 Count = -1;
1805
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001806 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Guy Benyei11169dd2012-12-18 14:30:41 +00001807 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
1808
1809 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1810 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1811
1812 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
1813}
1814
Eric Christophere7b87e52014-10-26 23:40:33 +00001815llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001816 uint64_t Size;
1817 uint64_t Align;
1818
1819 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
1820 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
1821 Size = 0;
1822 Align =
Eric Christophere7b87e52014-10-26 23:40:33 +00001823 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Guy Benyei11169dd2012-12-18 14:30:41 +00001824 } else if (Ty->isIncompleteArrayType()) {
1825 Size = 0;
1826 if (Ty->getElementType()->isIncompleteType())
1827 Align = 0;
1828 else
1829 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikief03b2e82013-05-09 20:48:12 +00001830 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001831 Size = 0;
1832 Align = 0;
1833 } else {
1834 // Size and align of the whole array, not the element type.
1835 Size = CGM.getContext().getTypeSize(Ty);
1836 Align = CGM.getContext().getTypeAlign(Ty);
1837 }
1838
1839 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1840 // interior arrays, do we care? Why aren't nested arrays represented the
1841 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001842 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001843 QualType EltTy(Ty, 0);
1844 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1845 // If the number of elements is known, then count is that number. Otherwise,
1846 // it's -1. This allows us to represent a subrange with an array of 0
1847 // elements, like this:
1848 //
1849 // struct foo {
1850 // int x[0];
1851 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00001852 int64_t Count = -1; // Count == -1 is an unbounded array.
Guy Benyei11169dd2012-12-18 14:30:41 +00001853 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1854 Count = CAT->getSize().getZExtValue();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001855
Guy Benyei11169dd2012-12-18 14:30:41 +00001856 // FIXME: Verify this is right for VLAs.
1857 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
1858 EltTy = Ty->getElementType();
1859 }
1860
1861 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
1862
Eric Christophere7b87e52014-10-26 23:40:33 +00001863 llvm::DIType DbgTy = DBuilder.createArrayType(
1864 Size, Align, getOrCreateType(EltTy, Unit), SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00001865 return DbgTy;
1866}
1867
Eric Christopherb2a008c2013-05-16 00:45:12 +00001868llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001869 llvm::DIFile Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001870 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
1871 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001872}
1873
Eric Christopherb2a008c2013-05-16 00:45:12 +00001874llvm::DIType CGDebugInfo::CreateType(const RValueReferenceType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001875 llvm::DIFile Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001876 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
1877 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001878}
1879
Eric Christopherb2a008c2013-05-16 00:45:12 +00001880llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001881 llvm::DIFile U) {
David Blaikie2c705ca2013-01-19 19:20:56 +00001882 llvm::DIType ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
1883 if (!Ty->getPointeeType()->isFunctionType())
1884 return DBuilder.createMemberPointerType(
Adrian Prantlee24e142014-12-23 19:11:54 +00001885 getOrCreateType(Ty->getPointeeType(), U), ClassType,
Adrian Prantl0772dbd2015-01-07 17:49:30 +00001886 CGM.getContext().getTypeSize(Ty));
Adrian Prantl0866acd2013-12-19 01:38:47 +00001887
1888 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00001889 Ty->getPointeeType()->getAs<FunctionProtoType>();
1890 return DBuilder.createMemberPointerType(
1891 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
1892 Ty->getClass(), FPT->getTypeQuals())),
1893 FPT, U),
Adrian Prantl0772dbd2015-01-07 17:49:30 +00001894 ClassType, CGM.getContext().getTypeSize(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00001895}
1896
Eric Christophere7b87e52014-10-26 23:40:33 +00001897llvm::DIType CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile U) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001898 // Ignore the atomic wrapping
1899 // FIXME: What is the correct representation?
1900 return getOrCreateType(Ty->getValueType(), U);
1901}
1902
1903/// CreateEnumType - get enumeration type.
Manman Ren501ecf92013-08-28 21:46:36 +00001904llvm::DIType CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00001905 const EnumDecl *ED = Ty->getDecl();
Guy Benyei11169dd2012-12-18 14:30:41 +00001906 uint64_t Size = 0;
1907 uint64_t Align = 0;
1908 if (!ED->getTypeForDecl()->isIncompleteType()) {
1909 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1910 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1911 }
1912
Manman Rene0064d82013-08-29 23:19:58 +00001913 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1914
Guy Benyei11169dd2012-12-18 14:30:41 +00001915 // If this is just a forward declaration, construct an appropriately
1916 // marked node and just return it.
1917 if (!ED->getDefinition()) {
1918 llvm::DIDescriptor EDContext;
1919 EDContext = getContextDescriptor(cast<Decl>(ED->getDeclContext()));
1920 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1921 unsigned Line = getLineNumber(ED->getLocation());
1922 StringRef EDName = ED->getName();
David Blaikief427b002014-05-06 03:42:01 +00001923 llvm::DIType RetTy = DBuilder.createReplaceableForwardDecl(
1924 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
1925 0, Size, Align, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001926 ReplaceMap.emplace_back(
1927 std::piecewise_construct, std::make_tuple(Ty),
1928 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00001929 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00001930 }
1931
David Blaikie483a9da2014-05-06 18:35:21 +00001932 return CreateTypeDefinition(Ty);
1933}
1934
1935llvm::DIType CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
1936 const EnumDecl *ED = Ty->getDecl();
1937 uint64_t Size = 0;
1938 uint64_t Align = 0;
1939 if (!ED->getTypeForDecl()->isIncompleteType()) {
1940 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1941 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1942 }
1943
1944 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1945
Guy Benyei11169dd2012-12-18 14:30:41 +00001946 // Create DIEnumerator elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001947 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00001948 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00001949 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001950 Enumerators.push_back(DBuilder.createEnumerator(
1951 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001952 }
1953
1954 // Return a CompositeType for the enum itself.
1955 llvm::DIArray EltArray = DBuilder.getOrCreateArray(Enumerators);
1956
1957 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1958 unsigned Line = getLineNumber(ED->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001959 llvm::DIDescriptor EnumContext =
Eric Christophere7b87e52014-10-26 23:40:33 +00001960 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
1961 llvm::DIType ClassTy = ED->isFixed()
1962 ? getOrCreateType(ED->getIntegerType(), DefUnit)
1963 : llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001964 llvm::DIType DbgTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001965 DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit, Line,
1966 Size, Align, EltArray, ClassTy, FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00001967 return DbgTy;
1968}
1969
David Blaikie05491062013-01-21 04:37:12 +00001970static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
1971 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00001972 do {
Adrian Prantl179af902013-09-26 21:35:50 +00001973 Qualifiers InnerQuals = T.getLocalQualifiers();
1974 // Qualifiers::operator+() doesn't like it if you add a Qualifier
1975 // that is already there.
1976 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
1977 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00001978 QualType LastT = T;
1979 switch (T->getTypeClass()) {
1980 default:
David Blaikie05491062013-01-21 04:37:12 +00001981 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00001982 case Type::TemplateSpecialization: {
1983 const auto *Spec = cast<TemplateSpecializationType>(T);
1984 if (Spec->isTypeAlias())
1985 return C.getQualifiedType(T.getTypePtr(), Quals);
1986 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00001987 break;
1988 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001989 case Type::TypeOfExpr:
1990 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
1991 break;
1992 case Type::TypeOf:
1993 T = cast<TypeOfType>(T)->getUnderlyingType();
1994 break;
1995 case Type::Decltype:
1996 T = cast<DecltypeType>(T)->getUnderlyingType();
1997 break;
1998 case Type::UnaryTransform:
1999 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2000 break;
2001 case Type::Attributed:
2002 T = cast<AttributedType>(T)->getEquivalentType();
2003 break;
2004 case Type::Elaborated:
2005 T = cast<ElaboratedType>(T)->getNamedType();
2006 break;
2007 case Type::Paren:
2008 T = cast<ParenType>(T)->getInnerType();
2009 break;
David Blaikie05491062013-01-21 04:37:12 +00002010 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002011 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002012 break;
2013 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00002014 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002015 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002016 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002017 break;
2018 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002019
Guy Benyei11169dd2012-12-18 14:30:41 +00002020 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002021 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002022 } while (true);
2023}
2024
Eric Christopher0fdcb312013-05-16 00:52:20 +00002025/// getType - Get the type from the cache or return null type if it doesn't
2026/// exist.
Guy Benyei11169dd2012-12-18 14:30:41 +00002027llvm::DIType CGDebugInfo::getTypeOrNull(QualType Ty) {
2028
2029 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002030 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002031
David Blaikief427b002014-05-06 03:42:01 +00002032 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002033 if (it != TypeCache.end()) {
2034 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002035 if (llvm::Metadata *V = it->second)
Guy Benyei11169dd2012-12-18 14:30:41 +00002036 return llvm::DIType(cast<llvm::MDNode>(V));
2037 }
2038
2039 return llvm::DIType();
2040}
2041
David Blaikie0e716b42014-03-03 23:48:23 +00002042void CGDebugInfo::completeTemplateDefinition(
2043 const ClassTemplateSpecializationDecl &SD) {
David Blaikie0856f662014-03-04 22:01:08 +00002044 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2045 return;
2046
David Blaikie0e716b42014-03-03 23:48:23 +00002047 completeClassData(&SD);
2048 // In case this type has no member function definitions being emitted, ensure
2049 // it is retained
2050 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2051}
2052
Guy Benyei11169dd2012-12-18 14:30:41 +00002053/// getOrCreateType - Get the type from the cache or create a new
2054/// one if necessary.
David Blaikie99dab3b2013-09-04 22:03:57 +00002055llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002056 if (Ty.isNull())
2057 return llvm::DIType();
2058
2059 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002060 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002061
David Blaikieef8a9512014-05-05 23:23:53 +00002062 if (llvm::DIType T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002063 return T;
2064
2065 // Otherwise create the type.
David Blaikie99dab3b2013-09-04 22:03:57 +00002066 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Eric Christophere7b87e52014-10-26 23:40:33 +00002067 void *TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002068
2069 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002070 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002071
Guy Benyei11169dd2012-12-18 14:30:41 +00002072 return Res;
2073}
2074
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002075/// Currently the checksum of an interface includes the number of
2076/// ivars and property accessors.
Eric Christopher1ecc5632013-06-07 22:54:39 +00002077unsigned CGDebugInfo::Checksum(const ObjCInterfaceDecl *ID) {
Adrian Prantl817bbb32013-06-07 01:10:48 +00002078 // The assumption is that the number of ivars can only increase
2079 // monotonically, so it is safe to just use their current number as
2080 // a checksum.
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002081 unsigned Sum = 0;
2082 for (const ObjCIvarDecl *Ivar = ID->all_declared_ivar_begin();
Craig Topper8a13c412014-05-21 05:09:00 +00002083 Ivar != nullptr; Ivar = Ivar->getNextIvar())
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002084 ++Sum;
2085
2086 return Sum;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002087}
2088
2089ObjCInterfaceDecl *CGDebugInfo::getObjCInterfaceDecl(QualType Ty) {
2090 switch (Ty->getTypeClass()) {
2091 case Type::ObjCObjectPointer:
Eric Christophere7b87e52014-10-26 23:40:33 +00002092 return getObjCInterfaceDecl(
2093 cast<ObjCObjectPointerType>(Ty)->getPointeeType());
Adrian Prantla03a85a2013-03-06 22:03:30 +00002094 case Type::ObjCInterface:
2095 return cast<ObjCInterfaceType>(Ty)->getDecl();
2096 default:
Craig Topper8a13c412014-05-21 05:09:00 +00002097 return nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002098 }
2099}
2100
Guy Benyei11169dd2012-12-18 14:30:41 +00002101/// CreateTypeNode - Create a new debug type node.
David Blaikie99dab3b2013-09-04 22:03:57 +00002102llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002103 // Handle qualifiers, which recursively handles what they refer to.
2104 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002105 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002106
Guy Benyei11169dd2012-12-18 14:30:41 +00002107 // Work out details of type.
2108 switch (Ty->getTypeClass()) {
2109#define TYPE(Class, Base)
2110#define ABSTRACT_TYPE(Class, Base)
2111#define NON_CANONICAL_TYPE(Class, Base)
2112#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2113#include "clang/AST/TypeNodes.def"
2114 llvm_unreachable("Dependent types cannot show up in debug information");
2115
2116 case Type::ExtVector:
2117 case Type::Vector:
2118 return CreateType(cast<VectorType>(Ty), Unit);
2119 case Type::ObjCObjectPointer:
2120 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2121 case Type::ObjCObject:
2122 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2123 case Type::ObjCInterface:
2124 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2125 case Type::Builtin:
2126 return CreateType(cast<BuiltinType>(Ty));
2127 case Type::Complex:
2128 return CreateType(cast<ComplexType>(Ty));
2129 case Type::Pointer:
2130 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002131 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002132 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002133 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002134 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002135 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002136 case Type::BlockPointer:
2137 return CreateType(cast<BlockPointerType>(Ty), Unit);
2138 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002139 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002140 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002141 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002142 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002143 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002144 case Type::FunctionProto:
2145 case Type::FunctionNoProto:
2146 return CreateType(cast<FunctionType>(Ty), Unit);
2147 case Type::ConstantArray:
2148 case Type::VariableArray:
2149 case Type::IncompleteArray:
2150 return CreateType(cast<ArrayType>(Ty), Unit);
2151
2152 case Type::LValueReference:
2153 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2154 case Type::RValueReference:
2155 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2156
2157 case Type::MemberPointer:
2158 return CreateType(cast<MemberPointerType>(Ty), Unit);
2159
2160 case Type::Atomic:
2161 return CreateType(cast<AtomicType>(Ty), Unit);
2162
Guy Benyei11169dd2012-12-18 14:30:41 +00002163 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002164 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2165
David Blaikie42edade2014-11-11 20:44:45 +00002166 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002167 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002168 case Type::Elaborated:
2169 case Type::Paren:
2170 case Type::SubstTemplateTypeParm:
2171 case Type::TypeOfExpr:
2172 case Type::TypeOf:
2173 case Type::Decltype:
2174 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002175 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002176 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002177 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002178
David Blaikie42edade2014-11-11 20:44:45 +00002179 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002180}
2181
2182/// getOrCreateLimitedType - Get the type from the cache or create a new
2183/// limited type if necessary.
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002184llvm::DIType CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
Eric Christopherc0c5d462013-02-21 22:35:08 +00002185 llvm::DIFile Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002186 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002187
David Blaikie8d5e1282013-08-20 21:03:29 +00002188 llvm::DICompositeType T(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002189
2190 // We may have cached a forward decl when we could have created
2191 // a non-forward decl. Go ahead and create a non-forward decl
2192 // now.
Eric Christophere7b87e52014-10-26 23:40:33 +00002193 if (T && !T.isForwardDecl())
2194 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002195
2196 // Otherwise create the type.
David Blaikie8d5e1282013-08-20 21:03:29 +00002197 llvm::DICompositeType Res = CreateLimitedType(Ty);
2198
2199 // Propagate members from the declaration to the definition
2200 // CreateType(const RecordType*) will overwrite this with the members in the
2201 // correct order if the full type is needed.
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002202 DBuilder.replaceArrays(Res, T.getElements());
Guy Benyei11169dd2012-12-18 14:30:41 +00002203
Guy Benyei11169dd2012-12-18 14:30:41 +00002204 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002205 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002206 return Res;
2207}
2208
2209// TODO: Currently used for context chains when limiting debug info.
David Blaikie8d5e1282013-08-20 21:03:29 +00002210llvm::DICompositeType CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002211 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002212
Guy Benyei11169dd2012-12-18 14:30:41 +00002213 // Get overall information about the record type for the debug info.
2214 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
2215 unsigned Line = getLineNumber(RD->getLocation());
2216 StringRef RDName = getClassName(RD);
2217
Eric Christopher07429ff2013-10-15 21:22:34 +00002218 llvm::DIDescriptor RDContext =
2219 getContextDescriptor(cast<Decl>(RD->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002220
David Blaikied2785892013-08-18 17:36:19 +00002221 // If we ended up creating the type during the context chain construction,
2222 // just return that.
David Blaikie8d5e1282013-08-20 21:03:29 +00002223 llvm::DICompositeType T(getTypeOrNull(CGM.getContext().getRecordType(RD)));
2224 if (T && (!T.isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002225 return T;
David Blaikied2785892013-08-18 17:36:19 +00002226
Adrian Prantl381e7552014-02-04 21:29:50 +00002227 // If this is just a forward or incomplete declaration, construct an
2228 // appropriately marked node and just return it.
2229 const RecordDecl *D = RD->getDefinition();
2230 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002231 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002232
2233 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2234 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
David Blaikie49ae6a72013-03-26 23:47:35 +00002235 llvm::DICompositeType RealDecl;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002236
Manman Rene0064d82013-08-29 23:19:58 +00002237 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2238
Guy Benyei11169dd2012-12-18 14:30:41 +00002239 if (RD->isUnion())
Eric Christophere7b87e52014-10-26 23:40:33 +00002240 RealDecl = DBuilder.createUnionType(RDContext, RDName, DefUnit, Line, Size,
2241 Align, 0, llvm::DIArray(), 0, FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002242 else if (RD->isClass()) {
2243 // FIXME: This could be a struct type giving a default visibility different
2244 // than C++ class type, but needs llvm metadata changes first.
Eric Christophere7b87e52014-10-26 23:40:33 +00002245 RealDecl = DBuilder.createClassType(
2246 RDContext, RDName, DefUnit, Line, Size, Align, 0, 0, llvm::DIType(),
2247 llvm::DIArray(), llvm::DIType(), llvm::DIArray(), FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002248 } else
Eric Christophere7b87e52014-10-26 23:40:33 +00002249 RealDecl = DBuilder.createStructType(
2250 RDContext, RDName, DefUnit, Line, Size, Align, 0, llvm::DIType(),
2251 llvm::DIArray(), 0, llvm::DIType(), FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002252
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002253 RegionMap[Ty->getDecl()].reset(RealDecl);
2254 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002255
David Blaikieadfbf992013-08-18 16:55:33 +00002256 if (const ClassTemplateSpecializationDecl *TSpecial =
2257 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002258 DBuilder.replaceArrays(RealDecl, llvm::DIArray(),
2259 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002260 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002261}
2262
David Blaikieadfbf992013-08-18 16:55:33 +00002263void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
2264 llvm::DICompositeType RealDecl) {
2265 // A class's primary base or the class itself contains the vtable.
2266 llvm::DICompositeType ContainingType;
2267 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2268 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002269 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002270 while (1) {
2271 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2272 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2273 if (PBT && !BRL.isPrimaryBaseVirtual())
2274 PBase = PBT;
2275 else
2276 break;
2277 }
2278 ContainingType = llvm::DICompositeType(
2279 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2280 getOrCreateFile(RD->getLocation())));
2281 } else if (RD->isDynamicClass())
2282 ContainingType = RealDecl;
2283
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002284 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002285}
2286
Guy Benyei11169dd2012-12-18 14:30:41 +00002287/// CreateMemberType - Create new member and increase Offset by FType's size.
2288llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
Eric Christophere7b87e52014-10-26 23:40:33 +00002289 StringRef Name, uint64_t *Offset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002290 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
2291 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2292 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Eric Christophere7b87e52014-10-26 23:40:33 +00002293 llvm::DIType Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
2294 FieldAlign, *Offset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002295 *Offset += FieldSize;
2296 return Ty;
2297}
2298
Frederic Riss9db79f12014-11-18 03:40:46 +00002299void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD,
2300 llvm::DIFile Unit,
2301 StringRef &Name, StringRef &LinkageName,
2302 llvm::DIDescriptor &FDContext,
2303 llvm::DIArray &TParamsArray,
2304 unsigned &Flags) {
2305 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2306 Name = getFunctionName(FD);
2307 // Use mangled name as linkage name for C/C++ functions.
2308 if (FD->hasPrototype()) {
2309 LinkageName = CGM.getMangledName(GD);
2310 Flags |= llvm::DIDescriptor::FlagPrototyped;
2311 }
2312 // No need to replicate the linkage name if it isn't different from the
2313 // subprogram name, no need to have it at all unless coverage is enabled or
2314 // debug is set to more than just line tables.
2315 if (LinkageName == Name ||
2316 (!CGM.getCodeGenOpts().EmitGcovArcs &&
2317 !CGM.getCodeGenOpts().EmitGcovNotes &&
2318 DebugKind <= CodeGenOptions::DebugLineTablesOnly))
2319 LinkageName = StringRef();
2320
2321 if (DebugKind >= CodeGenOptions::LimitedDebugInfo) {
2322 if (const NamespaceDecl *NSDecl =
2323 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2324 FDContext = getOrCreateNameSpace(NSDecl);
2325 else if (const RecordDecl *RDecl =
2326 dyn_cast_or_null<RecordDecl>(FD->getDeclContext()))
2327 FDContext = getContextDescriptor(cast<Decl>(RDecl));
2328 // Collect template parameters.
2329 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2330 }
2331}
2332
2333void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile &Unit,
2334 unsigned &LineNo, QualType &T,
2335 StringRef &Name, StringRef &LinkageName,
2336 llvm::DIDescriptor &VDContext) {
2337 Unit = getOrCreateFile(VD->getLocation());
2338 LineNo = getLineNumber(VD->getLocation());
2339
2340 setLocation(VD->getLocation());
2341
2342 T = VD->getType();
2343 if (T->isIncompleteArrayType()) {
2344 // CodeGen turns int[] into int[1] so we'll do the same here.
2345 llvm::APInt ConstVal(32, 1);
2346 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2347
2348 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2349 ArrayType::Normal, 0);
2350 }
2351
2352 Name = VD->getName();
2353 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2354 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2355 LinkageName = CGM.getMangledName(VD);
2356 if (LinkageName == Name)
2357 LinkageName = StringRef();
2358
2359 // Since we emit declarations (DW_AT_members) for static members, place the
2360 // definition of those static members in the namespace they were declared in
2361 // in the source code (the lexical decl context).
2362 // FIXME: Generalize this for even non-member global variables where the
2363 // declaration and definition may have different lexical decl contexts, once
2364 // we have support for emitting declarations of (non-member) global variables.
2365 VDContext = getContextDescriptor(
2366 dyn_cast<Decl>(VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2367 : VD->getDeclContext()));
2368}
2369
Frederic Rissd253ed62014-11-18 03:40:51 +00002370llvm::DISubprogram
2371CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
2372 llvm::DIArray TParamsArray;
2373 StringRef Name, LinkageName;
2374 unsigned Flags = 0;
2375 SourceLocation Loc = FD->getLocation();
2376 llvm::DIFile Unit = getOrCreateFile(Loc);
2377 llvm::DIDescriptor DContext(Unit);
2378 unsigned Line = getLineNumber(Loc);
2379
2380 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2381 TParamsArray, Flags);
2382 // Build function type.
2383 SmallVector<QualType, 16> ArgTypes;
2384 for (const ParmVarDecl *Parm: FD->parameters())
2385 ArgTypes.push_back(Parm->getType());
2386 QualType FnType =
2387 CGM.getContext().getFunctionType(FD->getReturnType(), ArgTypes,
2388 FunctionProtoType::ExtProtoInfo());
2389 llvm::DISubprogram SP =
2390 DBuilder.createTempFunctionFwdDecl(DContext, Name, LinkageName, Unit, Line,
2391 getOrCreateFunctionType(FD, FnType, Unit),
2392 !FD->isExternallyVisible(),
2393 false /*declaration*/, 0, Flags,
2394 CGM.getLangOpts().Optimize, nullptr,
2395 TParamsArray, getFunctionDeclaration(FD));
2396 const FunctionDecl *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002397 FwdDeclReplaceMap.emplace_back(
2398 std::piecewise_construct, std::make_tuple(CanonDecl),
2399 std::make_tuple(static_cast<llvm::Metadata *>(SP)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002400 return SP;
2401}
2402
2403llvm::DIGlobalVariable
2404CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2405 QualType T;
2406 StringRef Name, LinkageName;
2407 SourceLocation Loc = VD->getLocation();
2408 llvm::DIFile Unit = getOrCreateFile(Loc);
2409 llvm::DIDescriptor DContext(Unit);
2410 unsigned Line = getLineNumber(Loc);
2411
2412 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
2413 llvm::DIGlobalVariable GV =
2414 DBuilder.createTempGlobalVariableFwdDecl(DContext, Name, LinkageName, Unit,
2415 Line, getOrCreateType(T, Unit),
2416 !VD->isExternallyVisible(),
2417 nullptr, nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002418 FwdDeclReplaceMap.emplace_back(
2419 std::piecewise_construct,
2420 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2421 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002422 return GV;
2423}
2424
Frederic Riss442293e2014-11-06 21:12:06 +00002425llvm::DIDescriptor CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002426 // We only need a declaration (not a definition) of the type - so use whatever
2427 // we would otherwise do to get a type for a pointee. (forward declarations in
2428 // limited debug info, full definitions (if the type definition is available)
2429 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002430 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2431 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002432 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002433 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002434
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002435 if (I != DeclCache.end())
2436 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(I->second));
Frederic Rissd253ed62014-11-18 03:40:51 +00002437
2438 // No definition for now. Emit a forward definition that might be
2439 // merged with a potential upcoming definition.
2440 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
2441 return getFunctionForwardDeclaration(FD);
2442 else if (const auto *VD = dyn_cast<VarDecl>(D))
2443 return getGlobalVariableForwardDeclaration(VD);
2444
2445 return llvm::DIDescriptor();
David Blaikiebd483762013-05-20 04:58:53 +00002446}
2447
Guy Benyei11169dd2012-12-18 14:30:41 +00002448/// getFunctionDeclaration - Return debug info descriptor to describe method
2449/// declaration for the given method definition.
2450llvm::DISubprogram CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Diego Novillo913690c2014-06-24 17:02:17 +00002451 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
David Blaikie18cfbc52013-06-22 00:09:36 +00002452 return llvm::DISubprogram();
2453
Guy Benyei11169dd2012-12-18 14:30:41 +00002454 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002455 if (!FD)
2456 return llvm::DISubprogram();
Guy Benyei11169dd2012-12-18 14:30:41 +00002457
2458 // Setup context.
David Blaikiefd07c602013-08-09 17:20:05 +00002459 llvm::DIScope S = getContextDescriptor(cast<Decl>(D->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002460
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002461 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002462 if (MI == SPCache.end()) {
Eric Christopherf86c4052013-08-28 23:12:10 +00002463 if (const CXXMethodDecl *MD =
2464 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
David Blaikiefd07c602013-08-09 17:20:05 +00002465 llvm::DICompositeType T(S);
Eric Christopherf86c4052013-08-28 23:12:10 +00002466 llvm::DISubprogram SP =
2467 CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()), T);
David Blaikiefd07c602013-08-09 17:20:05 +00002468 return SP;
2469 }
2470 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002471 if (MI != SPCache.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002472 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(MI->second));
David Blaikie18cfbc52013-06-22 00:09:36 +00002473 if (SP.isSubprogram() && !SP.isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002474 return SP;
2475 }
2476
Aaron Ballman86c93902014-03-06 23:45:36 +00002477 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002478 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002479 if (MI != SPCache.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002480 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(MI->second));
David Blaikie18cfbc52013-06-22 00:09:36 +00002481 if (SP.isSubprogram() && !SP.isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002482 return SP;
2483 }
2484 }
2485 return llvm::DISubprogram();
2486}
2487
2488// getOrCreateFunctionType - Construct DIType. If it is a c++ method, include
2489// implicit parameter "this".
David Blaikie469f0792013-05-22 23:22:42 +00002490llvm::DICompositeType CGDebugInfo::getOrCreateFunctionType(const Decl *D,
2491 QualType FnType,
2492 llvm::DIFile F) {
Diego Novillo913690c2014-06-24 17:02:17 +00002493 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
David Blaikie18cfbc52013-06-22 00:09:36 +00002494 // Create fake but valid subroutine type. Otherwise
2495 // llvm::DISubprogram::Verify() would return false, and
2496 // subprogram DIE will miss DW_AT_decl_file and
2497 // DW_AT_decl_line fields.
Manman Ren67f005e2014-07-28 22:24:34 +00002498 return DBuilder.createSubroutineType(F,
2499 DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002500
2501 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2502 return getOrCreateMethodType(Method, F);
2503 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2504 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002505 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002506
2507 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002508 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002509
2510 // Replace the instancetype keyword with the actual type.
2511 if (ResultTy == CGM.getContext().getObjCInstanceType())
2512 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002513 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002514
Adrian Prantl7bec9032013-05-10 21:08:31 +00002515 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002516 // "self" pointer is always first argument.
Adrian Prantlde17db32013-03-29 19:20:29 +00002517 QualType SelfDeclTy = OMethod->getSelfDecl()->getType();
2518 llvm::DIType SelfTy = getOrCreateType(SelfDeclTy, F);
2519 Elts.push_back(CreateSelfType(SelfDeclTy, SelfTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002520 // "_cmd" pointer is always second argument.
2521 llvm::DIType CmdTy = getOrCreateType(OMethod->getCmdDecl()->getType(), F);
2522 Elts.push_back(DBuilder.createArtificialType(CmdTy));
2523 // Get rest of the arguments.
Aaron Ballman43b68be2014-03-07 17:50:17 +00002524 for (const auto *PI : OMethod->params())
2525 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002526 // Variadic methods need a special marker at the end of the type list.
2527 if (OMethod->isVariadic())
2528 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002529
Manman Ren67f005e2014-07-28 22:24:34 +00002530 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002531 return DBuilder.createSubroutineType(F, EltTypeArray);
2532 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002533
Adrian Prantl800faef2014-02-25 23:42:18 +00002534 // Handle variadic function types; they need an additional
2535 // unspecified parameter.
Adrian Prantld45ba252014-02-25 19:38:11 +00002536 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2537 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002538 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00002539 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2540 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2541 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2542 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2543 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Manman Ren67f005e2014-07-28 22:24:34 +00002544 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Adrian Prantld45ba252014-02-25 19:38:11 +00002545 return DBuilder.createSubroutineType(F, EltTypeArray);
2546 }
2547
David Blaikie469f0792013-05-22 23:22:42 +00002548 return llvm::DICompositeType(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002549}
2550
2551/// EmitFunctionStart - Constructs the debug code for entering a function.
Eric Christophere7b87e52014-10-26 23:40:33 +00002552void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2553 SourceLocation ScopeLoc, QualType FnType,
2554 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002555
2556 StringRef Name;
2557 StringRef LinkageName;
2558
2559 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2560
2561 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002562 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00002563
Guy Benyei11169dd2012-12-18 14:30:41 +00002564 unsigned Flags = 0;
2565 llvm::DIFile Unit = getOrCreateFile(Loc);
2566 llvm::DIDescriptor FDContext(Unit);
2567 llvm::DIArray TParamsArray;
2568 if (!HasDecl) {
2569 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00002570 LinkageName = Fn->getName();
Guy Benyei11169dd2012-12-18 14:30:41 +00002571 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2572 // If there is a DISubprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002573 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002574 if (FI != SPCache.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002575 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00002576 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
2577 llvm::MDNode *SPN = SP;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002578 LexicalBlockStack.emplace_back(SPN);
2579 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002580 return;
2581 }
2582 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002583 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2584 TParamsArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002585 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2586 Name = getObjCMethodName(OMD);
2587 Flags |= llvm::DIDescriptor::FlagPrototyped;
2588 } else {
2589 // Use llvm function name.
2590 Name = Fn->getName();
2591 Flags |= llvm::DIDescriptor::FlagPrototyped;
2592 }
2593 if (!Name.empty() && Name[0] == '\01')
2594 Name = Name.substr(1);
2595
Adrian Prantl42d71b92014-04-10 23:21:53 +00002596 if (!HasDecl || D->isImplicit()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002597 Flags |= llvm::DIDescriptor::FlagArtificial;
Adrian Prantl42d71b92014-04-10 23:21:53 +00002598 // Artificial functions without a location should not silently reuse CurLoc.
2599 if (Loc.isInvalid())
2600 CurLoc = SourceLocation();
2601 }
2602 unsigned LineNo = getLineNumber(Loc);
2603 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002604
Eric Christopher8018e412014-03-27 18:50:35 +00002605 // FIXME: The function declaration we're constructing here is mostly reusing
2606 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2607 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2608 // all subprograms instead of the actual context since subprogram definitions
2609 // are emitted as CU level entities by the backend.
Eric Christophere7b87e52014-10-26 23:40:33 +00002610 llvm::DISubprogram SP = DBuilder.createFunction(
2611 FDContext, Name, LinkageName, Unit, LineNo,
2612 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
2613 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize, Fn,
2614 TParamsArray, getFunctionDeclaration(D));
Frederic Rissb1ab28c2014-11-05 19:19:04 +00002615 // We might get here with a VarDecl in the case we're generating
2616 // code for the initialization of globals. Do not record these decls
2617 // as they will overwrite the actual VarDecl Decl in the cache.
2618 if (HasDecl && isa<FunctionDecl>(D))
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002619 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(SP));
Guy Benyei11169dd2012-12-18 14:30:41 +00002620
Adrian Prantlbebb8932014-03-21 21:01:58 +00002621 // Push the function onto the lexical block stack.
Guy Benyei11169dd2012-12-18 14:30:41 +00002622 llvm::MDNode *SPN = SP;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002623 LexicalBlockStack.emplace_back(SPN);
Adrian Prantlbebb8932014-03-21 21:01:58 +00002624
Guy Benyei11169dd2012-12-18 14:30:41 +00002625 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002626 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002627}
2628
2629/// EmitLocation - Emit metadata to indicate a change in line/column
Adrian Prantl02c0caa2013-07-18 00:27:59 +00002630/// information in the source file. If the location is invalid, the
2631/// previous location will be reused.
Adrian Prantlc7822422013-03-12 20:43:25 +00002632void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc,
Adrian Prantle83b1302014-01-07 22:05:52 +00002633 bool ForceColumnInfo) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002634 // Update our current location
2635 setLocation(Loc);
2636
Eric Christophere7b87e52014-10-26 23:40:33 +00002637 if (CurLoc.isInvalid() || CurLoc.isMacroID())
2638 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002639
Adrian Prantle83b1302014-01-07 22:05:52 +00002640 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00002641 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2642 getLineNumber(CurLoc), getColumnNumber(CurLoc, ForceColumnInfo), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002643}
2644
2645/// CreateLexicalBlock - Creates a new lexical block node and pushes it on
2646/// the stack.
2647void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002648 llvm::MDNode *Back = nullptr;
2649 if (!LexicalBlockStack.empty())
2650 Back = LexicalBlockStack.back().get();
David Blaikief9ea2422014-06-02 16:32:05 +00002651 llvm::DIDescriptor D = DBuilder.createLexicalBlock(
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002652 llvm::DIDescriptor(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
2653 getColumnNumber(CurLoc));
Guy Benyei11169dd2012-12-18 14:30:41 +00002654 llvm::MDNode *DN = D;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002655 LexicalBlockStack.emplace_back(DN);
Guy Benyei11169dd2012-12-18 14:30:41 +00002656}
2657
2658/// EmitLexicalBlockStart - Constructs the debug code for entering a declarative
2659/// region - beginning of a DW_TAG_lexical_block.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002660void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2661 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002662 // Set our current location.
2663 setLocation(Loc);
2664
Guy Benyei11169dd2012-12-18 14:30:41 +00002665 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00002666 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2667 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00002668
2669 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2670 return;
2671
2672 // Create a new lexical block and push it on the stack.
2673 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002674}
2675
2676/// EmitLexicalBlockEnd - Constructs the debug code for exiting a declarative
2677/// region - end of a DW_TAG_lexical_block.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002678void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2679 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002680 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2681
2682 // Provide an entry in the line table for the end of the block.
2683 EmitLocation(Builder, Loc);
2684
David Blaikie60a877b2014-10-22 19:34:33 +00002685 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2686 return;
2687
Guy Benyei11169dd2012-12-18 14:30:41 +00002688 LexicalBlockStack.pop_back();
2689}
2690
2691/// EmitFunctionEnd - Constructs the debug code for exiting a function.
2692void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2693 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2694 unsigned RCount = FnBeginRegionCount.back();
2695 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2696
2697 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00002698 while (LexicalBlockStack.size() != RCount) {
2699 // Provide an entry in the line table for the end of the block.
2700 EmitLocation(Builder, CurLoc);
2701 LexicalBlockStack.pop_back();
2702 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002703 FnBeginRegionCount.pop_back();
2704}
2705
Eric Christopherb2a008c2013-05-16 00:45:12 +00002706// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
Guy Benyei11169dd2012-12-18 14:30:41 +00002707// See BuildByRefType.
2708llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
2709 uint64_t *XOffset) {
2710
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002711 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002712 QualType FType;
2713 uint64_t FieldSize, FieldOffset;
2714 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002715
Guy Benyei11169dd2012-12-18 14:30:41 +00002716 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002717 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002718
2719 FieldOffset = 0;
2720 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2721 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2722 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2723 FType = CGM.getContext().IntTy;
2724 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2725 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2726
2727 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2728 if (HasCopyAndDispose) {
2729 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002730 EltTys.push_back(
2731 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
2732 EltTys.push_back(
2733 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00002734 }
2735 bool HasByrefExtendedLayout;
2736 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00002737 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
2738 HasByrefExtendedLayout) &&
2739 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00002740 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002741 EltTys.push_back(
2742 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00002743 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002744
Guy Benyei11169dd2012-12-18 14:30:41 +00002745 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2746 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002747 CGM.getTarget().getPointerAlign(0))) {
2748 CharUnits FieldOffsetInBytes =
2749 CGM.getContext().toCharUnitsFromBits(FieldOffset);
2750 CharUnits AlignedOffsetInBytes =
2751 FieldOffsetInBytes.RoundUpToAlignment(Align);
2752 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002753
Guy Benyei11169dd2012-12-18 14:30:41 +00002754 if (NumPaddingBytes.isPositive()) {
2755 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
2756 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2757 pad, ArrayType::Normal, 0);
2758 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
2759 }
2760 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002761
Guy Benyei11169dd2012-12-18 14:30:41 +00002762 FType = Type;
David Blaikief427b002014-05-06 03:42:01 +00002763 llvm::DIType FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002764 FieldSize = CGM.getContext().getTypeSize(FType);
2765 FieldAlign = CGM.getContext().toBits(Align);
2766
Eric Christopherb2a008c2013-05-16 00:45:12 +00002767 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00002768 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
2769 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002770 EltTys.push_back(FieldTy);
2771 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002772
Guy Benyei11169dd2012-12-18 14:30:41 +00002773 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002774
Guy Benyei11169dd2012-12-18 14:30:41 +00002775 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002776
Guy Benyei11169dd2012-12-18 14:30:41 +00002777 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
David Blaikie6d4fe152013-02-25 01:07:08 +00002778 llvm::DIType(), Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002779}
2780
2781/// EmitDeclare - Emit local variable declaration debug info.
Ed Masteda706022014-05-07 12:49:30 +00002782void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::dwarf::LLVMConstants Tag,
Eric Christophere7b87e52014-10-26 23:40:33 +00002783 llvm::Value *Storage, unsigned ArgNo,
2784 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002785 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002786 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2787
David Blaikie7fceebf2013-08-19 03:37:48 +00002788 bool Unwritten =
2789 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
2790 cast<Decl>(VD->getDeclContext())->isImplicit());
2791 llvm::DIFile Unit;
2792 if (!Unwritten)
2793 Unit = getOrCreateFile(VD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002794 llvm::DIType Ty;
2795 uint64_t XOffset = 0;
2796 if (VD->hasAttr<BlocksAttr>())
2797 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002798 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002799 Ty = getOrCreateType(VD->getType(), Unit);
2800
2801 // If there is no debug info for this type then do not emit debug info
2802 // for this variable.
2803 if (!Ty)
2804 return;
2805
Guy Benyei11169dd2012-12-18 14:30:41 +00002806 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00002807 unsigned Line = 0;
2808 unsigned Column = 0;
2809 if (!Unwritten) {
2810 Line = getLineNumber(VD->getLocation());
2811 Column = getColumnNumber(VD->getLocation());
2812 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002813 unsigned Flags = 0;
2814 if (VD->isImplicit())
2815 Flags |= llvm::DIDescriptor::FlagArtificial;
2816 // If this is the first argument and it is implicit then
2817 // give it an object pointer flag.
2818 // FIXME: There has to be a better way to do this, but for static
2819 // functions there won't be an implicit param at arg1 and
2820 // otherwise it is 'self' or 'this'.
2821 if (isa<ImplicitParamDecl>(VD) && ArgNo == 1)
2822 Flags |= llvm::DIDescriptor::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00002823 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00002824 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
2825 !VD->getType()->isPointerType())
David Blaikieb9c667d2013-06-19 21:53:53 +00002826 Flags |= llvm::DIDescriptor::FlagIndirectVariable;
Guy Benyei11169dd2012-12-18 14:30:41 +00002827
2828 llvm::MDNode *Scope = LexicalBlockStack.back();
2829
2830 StringRef Name = VD->getName();
2831 if (!Name.empty()) {
2832 if (VD->hasAttr<BlocksAttr>()) {
2833 CharUnits offset = CharUnits::fromQuantity(32);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002834 SmallVector<int64_t, 9> addr;
2835 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002836 // offset of __forwarding field
2837 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002838 CGM.getTarget().getPointerWidth(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002839 addr.push_back(offset.getQuantity());
2840 addr.push_back(llvm::dwarf::DW_OP_deref);
2841 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002842 // offset of x field
2843 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002844 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002845
2846 // Create the descriptor for the variable.
Eric Christophere7b87e52014-10-26 23:40:33 +00002847 llvm::DIVariable D = DBuilder.createLocalVariable(
2848 Tag, llvm::DIDescriptor(Scope), VD->getName(), Unit, Line, Ty, ArgNo);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002849
Guy Benyei11169dd2012-12-18 14:30:41 +00002850 // Insert an llvm.dbg.declare into the current block.
2851 llvm::Instruction *Call =
Eric Christophere7b87e52014-10-26 23:40:33 +00002852 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr),
2853 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002854 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
2855 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00002856 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl0315f382013-09-18 22:08:57 +00002857 Flags |= llvm::DIDescriptor::FlagIndirectVariable;
David Blaikiea76a7c92013-01-05 05:58:35 +00002858 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2859 // If VD is an anonymous union then Storage represents value for
2860 // all union fields.
Guy Benyei11169dd2012-12-18 14:30:41 +00002861 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
David Blaikie219c7d92013-01-05 20:03:07 +00002862 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00002863 for (const auto *Field : RD->fields()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002864 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
2865 StringRef FieldName = Field->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002866
Guy Benyei11169dd2012-12-18 14:30:41 +00002867 // Ignore unnamed fields. Do not ignore unnamed records.
2868 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2869 continue;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002870
Guy Benyei11169dd2012-12-18 14:30:41 +00002871 // Use VarDecl's Tag, Scope and Line number.
Eric Christophere7b87e52014-10-26 23:40:33 +00002872 llvm::DIVariable D = DBuilder.createLocalVariable(
2873 Tag, llvm::DIDescriptor(Scope), FieldName, Unit, Line, FieldTy,
2874 CGM.getLangOpts().Optimize, Flags, ArgNo);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002875
Guy Benyei11169dd2012-12-18 14:30:41 +00002876 // Insert an llvm.dbg.declare into the current block.
Eric Christophere7b87e52014-10-26 23:40:33 +00002877 llvm::Instruction *Call = DBuilder.insertDeclare(
2878 Storage, D, DBuilder.createExpression(), Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002879 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
2880 }
David Blaikie219c7d92013-01-05 20:03:07 +00002881 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002882 }
2883 }
David Blaikiea76a7c92013-01-05 05:58:35 +00002884
2885 // Create the descriptor for the variable.
Eric Christophere7b87e52014-10-26 23:40:33 +00002886 llvm::DIVariable D = DBuilder.createLocalVariable(
2887 Tag, llvm::DIDescriptor(Scope), Name, Unit, Line, Ty,
2888 CGM.getLangOpts().Optimize, Flags, ArgNo);
David Blaikiea76a7c92013-01-05 05:58:35 +00002889
2890 // Insert an llvm.dbg.declare into the current block.
Eric Christophere7b87e52014-10-26 23:40:33 +00002891 llvm::Instruction *Call = DBuilder.insertDeclare(
2892 Storage, D, DBuilder.createExpression(), Builder.GetInsertBlock());
David Blaikiea76a7c92013-01-05 05:58:35 +00002893 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002894}
2895
2896void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
2897 llvm::Value *Storage,
2898 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002899 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002900 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, 0, Builder);
2901}
2902
Adrian Prantlde17db32013-03-29 19:20:29 +00002903/// Look up the completed type for a self pointer in the TypeCache and
2904/// create a copy of it with the ObjectPointer and Artificial flags
2905/// set. If the type is not cached, a new one is created. This should
2906/// never happen though, since creating a type for the implicit self
2907/// argument implies that we already parsed the interface definition
2908/// and the ivar declarations in the implementation.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002909llvm::DIType CGDebugInfo::CreateSelfType(const QualType &QualTy,
2910 llvm::DIType Ty) {
Adrian Prantlde17db32013-03-29 19:20:29 +00002911 llvm::DIType CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002912 if (CachedTy)
2913 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00002914 return DBuilder.createObjectPointerType(Ty);
2915}
2916
Eric Christophere7b87e52014-10-26 23:40:33 +00002917void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
2918 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00002919 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Eric Christopher75e17682013-05-16 00:45:23 +00002920 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002921 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00002922
Craig Topper8a13c412014-05-21 05:09:00 +00002923 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00002924 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002925
Guy Benyei11169dd2012-12-18 14:30:41 +00002926 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002927
Guy Benyei11169dd2012-12-18 14:30:41 +00002928 uint64_t XOffset = 0;
2929 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
2930 llvm::DIType Ty;
2931 if (isByRef)
2932 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002933 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002934 Ty = getOrCreateType(VD->getType(), Unit);
2935
2936 // Self is passed along as an implicit non-arg variable in a
2937 // block. Mark it as the object pointer.
2938 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00002939 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00002940
2941 // Get location information.
2942 unsigned Line = getLineNumber(VD->getLocation());
2943 unsigned Column = getColumnNumber(VD->getLocation());
2944
2945 const llvm::DataLayout &target = CGM.getDataLayout();
2946
2947 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00002948 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00002949 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
2950
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002951 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00002952 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002953 addr.push_back(llvm::dwarf::DW_OP_deref);
2954 addr.push_back(llvm::dwarf::DW_OP_plus);
2955 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002956 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002957 addr.push_back(llvm::dwarf::DW_OP_deref);
2958 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002959 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00002960 offset =
2961 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002962 addr.push_back(offset.getQuantity());
2963 addr.push_back(llvm::dwarf::DW_OP_deref);
2964 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002965 // offset of x field
2966 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002967 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002968 }
2969
2970 // Create the descriptor for the variable.
2971 llvm::DIVariable D =
Eric Christophere7b87e52014-10-26 23:40:33 +00002972 DBuilder.createLocalVariable(llvm::dwarf::DW_TAG_auto_variable,
2973 llvm::DIDescriptor(LexicalBlockStack.back()),
2974 VD->getName(), Unit, Line, Ty);
Adrian Prantl0f6df002013-03-29 19:20:35 +00002975
Guy Benyei11169dd2012-12-18 14:30:41 +00002976 // Insert an llvm.dbg.declare into the current block.
Adrian Prantl88eec392014-11-21 00:35:25 +00002977 llvm::Instruction *Call = InsertPoint ?
2978 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr),
2979 InsertPoint)
2980 : DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr),
2981 Builder.GetInsertBlock());
Eric Christophere7b87e52014-10-26 23:40:33 +00002982 Call->setDebugLoc(
2983 llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002984}
2985
2986/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
2987/// variable declaration.
2988void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
2989 unsigned ArgNo,
2990 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002991 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002992 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, ArgNo, Builder);
2993}
2994
2995namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00002996struct BlockLayoutChunk {
2997 uint64_t OffsetInBits;
2998 const BlockDecl::Capture *Capture;
2999};
3000bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3001 return l.OffsetInBits < r.OffsetInBits;
3002}
Guy Benyei11169dd2012-12-18 14:30:41 +00003003}
3004
3005void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003006 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003007 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003008 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003009 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003010 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003011 ASTContext &C = CGM.getContext();
3012 const BlockDecl *blockDecl = block.getBlockDecl();
3013
3014 // Collect some general information about the block's location.
3015 SourceLocation loc = blockDecl->getCaretLocation();
3016 llvm::DIFile tunit = getOrCreateFile(loc);
3017 unsigned line = getLineNumber(loc);
3018 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003019
Guy Benyei11169dd2012-12-18 14:30:41 +00003020 // Build the debug-info type for the block literal.
3021 getContextDescriptor(cast<Decl>(blockDecl->getDeclContext()));
3022
3023 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003024 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003025
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003026 SmallVector<llvm::Metadata *, 16> fields;
Guy Benyei11169dd2012-12-18 14:30:41 +00003027 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
3028 blockLayout->getElementOffsetInBits(0),
3029 tunit, tunit));
3030 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
3031 blockLayout->getElementOffsetInBits(1),
3032 tunit, tunit));
3033 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
3034 blockLayout->getElementOffsetInBits(2),
3035 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003036 auto *FnTy = block.getBlockExpr()->getFunctionType();
3037 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
3038 fields.push_back(createFieldType("__FuncPtr", FnPtrType, 0, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003039 blockLayout->getElementOffsetInBits(3),
3040 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003041 fields.push_back(createFieldType(
3042 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3043 ? C.getBlockDescriptorExtendedType()
3044 : C.getBlockDescriptorType()),
3045 0, loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003046
3047 // We want to sort the captures by offset, not because DWARF
3048 // requires this, but because we're paranoid about debuggers.
3049 SmallVector<BlockLayoutChunk, 8> chunks;
3050
3051 // 'this' capture.
3052 if (blockDecl->capturesCXXThis()) {
3053 BlockLayoutChunk chunk;
3054 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003055 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003056 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003057 chunks.push_back(chunk);
3058 }
3059
3060 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003061 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003062 const VarDecl *variable = capture.getVariable();
3063 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3064
3065 // Ignore constant captures.
3066 if (captureInfo.isConstant())
3067 continue;
3068
3069 BlockLayoutChunk chunk;
3070 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003071 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003072 chunk.Capture = &capture;
3073 chunks.push_back(chunk);
3074 }
3075
3076 // Sort by offset.
3077 llvm::array_pod_sort(chunks.begin(), chunks.end());
3078
Eric Christophere7b87e52014-10-26 23:40:33 +00003079 for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
3080 e = chunks.end();
3081 i != e; ++i) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003082 uint64_t offsetInBits = i->OffsetInBits;
3083 const BlockDecl::Capture *capture = i->Capture;
3084
3085 // If we have a null capture, this must be the C++ 'this' capture.
3086 if (!capture) {
3087 const CXXMethodDecl *method =
Eric Christophere7b87e52014-10-26 23:40:33 +00003088 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00003089 QualType type = method->getThisType(C);
3090
3091 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
3092 offsetInBits, tunit, tunit));
3093 continue;
3094 }
3095
3096 const VarDecl *variable = capture->getVariable();
3097 StringRef name = variable->getName();
3098
3099 llvm::DIType fieldType;
3100 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003101 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003102
3103 // FIXME: this creates a second copy of this type!
3104 uint64_t xoffset;
3105 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003106 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3107 fieldType =
3108 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3109 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003110 } else {
Eric Christophere7b87e52014-10-26 23:40:33 +00003111 fieldType = createFieldType(name, variable->getType(), 0, loc, AS_public,
3112 offsetInBits, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003113 }
3114 fields.push_back(fieldType);
3115 }
3116
3117 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003118 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3119 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003120
3121 llvm::DIArray fieldsArray = DBuilder.getOrCreateArray(fields);
3122
3123 llvm::DIType type =
Eric Christophere7b87e52014-10-26 23:40:33 +00003124 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
3125 CGM.getContext().toBits(block.BlockSize),
3126 CGM.getContext().toBits(block.BlockAlign), 0,
3127 llvm::DIType(), fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003128 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3129
3130 // Get overall information about the block.
3131 unsigned flags = llvm::DIDescriptor::FlagArtificial;
3132 llvm::MDNode *scope = LexicalBlockStack.back();
Guy Benyei11169dd2012-12-18 14:30:41 +00003133
3134 // Create the descriptor for the parameter.
Eric Christophere7b87e52014-10-26 23:40:33 +00003135 llvm::DIVariable debugVar = DBuilder.createLocalVariable(
3136 llvm::dwarf::DW_TAG_arg_variable, llvm::DIDescriptor(scope),
3137 Arg->getName(), tunit, line, type, CGM.getLangOpts().Optimize, flags,
3138 ArgNo);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003139
Adrian Prantl616bef42013-03-14 21:52:59 +00003140 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003141 // Insert an llvm.dbg.value into the current block.
Eric Christophere7b87e52014-10-26 23:40:33 +00003142 llvm::Instruction *DbgVal = DBuilder.insertDbgValueIntrinsic(
3143 LocalAddr, 0, debugVar, DBuilder.createExpression(),
3144 Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003145 DbgVal->setDebugLoc(llvm::DebugLoc::get(line, column, scope));
3146 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003147
Adrian Prantl616bef42013-03-14 21:52:59 +00003148 // Insert an llvm.dbg.declare into the current block.
Eric Christophere7b87e52014-10-26 23:40:33 +00003149 llvm::Instruction *DbgDecl = DBuilder.insertDeclare(
3150 Arg, debugVar, DBuilder.createExpression(), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003151 DbgDecl->setDebugLoc(llvm::DebugLoc::get(line, column, scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00003152}
3153
David Blaikie6943dea2013-08-20 01:28:15 +00003154/// If D is an out-of-class definition of a static data member of a class, find
3155/// its corresponding in-class declaration.
3156llvm::DIDerivedType
3157CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3158 if (!D->isStaticDataMember())
3159 return llvm::DIDerivedType();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003160 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003161 if (MI != StaticDataMemberCache.end()) {
3162 assert(MI->second && "Static data member declaration should still exist");
3163 return llvm::DIDerivedType(cast<llvm::MDNode>(MI->second));
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003164 }
David Blaikiece763042013-08-20 21:49:21 +00003165
3166 // If the member wasn't found in the cache, lazily construct and add it to the
3167 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003168 auto DC = D->getDeclContext();
3169 llvm::DICompositeType Ctxt(getContextDescriptor(cast<Decl>(DC)));
3170 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003171}
3172
Eric Christophercab9fae2014-04-10 05:20:00 +00003173/// Recursively collect all of the member fields of a global anonymous decl and
3174/// create static variables for them. The first time this is called it needs
3175/// to be on a union and then from there we can have additional unnamed fields.
3176llvm::DIGlobalVariable
3177CGDebugInfo::CollectAnonRecordDecls(const RecordDecl *RD, llvm::DIFile Unit,
3178 unsigned LineNo, StringRef LinkageName,
3179 llvm::GlobalVariable *Var,
3180 llvm::DIDescriptor DContext) {
3181 llvm::DIGlobalVariable GV;
3182
3183 for (const auto *Field : RD->fields()) {
3184 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
3185 StringRef FieldName = Field->getName();
3186
3187 // Ignore unnamed fields, but recurse into anonymous records.
3188 if (FieldName.empty()) {
3189 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3190 if (RT)
3191 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3192 Var, DContext);
3193 continue;
3194 }
3195 // Use VarDecl's Tag, Scope and Line number.
Eric Christophere7b87e52014-10-26 23:40:33 +00003196 GV = DBuilder.createGlobalVariable(
3197 DContext, FieldName, LinkageName, Unit, LineNo, FieldTy,
3198 Var->hasInternalLinkage(), Var, llvm::DIDerivedType());
Eric Christophercab9fae2014-04-10 05:20:00 +00003199 }
3200 return GV;
3201}
3202
Guy Benyei11169dd2012-12-18 14:30:41 +00003203/// EmitGlobalVariable - Emit information about a global variable.
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003204void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003205 const VarDecl *D) {
Eric Christopher75e17682013-05-16 00:45:23 +00003206 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003207 // Create global variable debug descriptor.
Frederic Riss9db79f12014-11-18 03:40:46 +00003208 llvm::DIFile Unit;
3209 llvm::DIDescriptor DContext;
3210 unsigned LineNo;
3211 StringRef DeclName, LinkageName;
3212 QualType T;
3213 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003214
3215 // Attempt to store one global variable for the declaration - even if we
3216 // emit a lot of fields.
3217 llvm::DIGlobalVariable GV;
3218
3219 // If this is an anonymous union then we'll want to emit a global
3220 // variable for each member of the anonymous union so that it's possible
3221 // to find the name of any field in the union.
3222 if (T->isUnionType() && DeclName.empty()) {
3223 const RecordDecl *RD = cast<RecordType>(T)->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003224 assert(RD->isAnonymousStructOrUnion() &&
3225 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003226 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3227 } else {
David Blaikie7550b112014-10-20 17:42:23 +00003228 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003229 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3230 Var->hasInternalLinkage(), Var,
3231 getOrCreateStaticDataMemberDeclarationOrNull(D));
3232 }
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003233 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(GV));
Guy Benyei11169dd2012-12-18 14:30:41 +00003234}
3235
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003236/// EmitGlobalVariable - Emit global variable's debug info.
3237void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3238 llvm::Constant *Init) {
Eric Christopher75e17682013-05-16 00:45:23 +00003239 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003240 // Create the descriptor for the variable.
3241 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
3242 StringRef Name = VD->getName();
3243 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
3244 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3245 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3246 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3247 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3248 }
3249 // Do not use DIGlobalVariable for enums.
3250 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
3251 return;
David Blaikiea15565562014-04-04 20:56:17 +00003252 // Do not emit separate definitions for function local const/statics.
3253 if (isa<FunctionDecl>(VD->getDeclContext()))
3254 return;
David Blaikiebb113912014-04-05 07:23:17 +00003255 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003256 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003257 if (VarD->isStaticDataMember()) {
3258 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
3259 getContextDescriptor(RD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003260 // Ensure that the type is retained even though it's otherwise unreferenced.
3261 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003262 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003263 return;
3264 }
3265
David Blaikieaf080852014-11-21 00:20:58 +00003266 llvm::DIDescriptor DContext =
3267 getContextDescriptor(dyn_cast<Decl>(VD->getDeclContext()));
3268
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003269 auto &GV = DeclCache[VD];
3270 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003271 return;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003272 GV.reset(DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003273 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003274 true, Init, getOrCreateStaticDataMemberDeclarationOrNull(VarD)));
David Blaikiebd483762013-05-20 04:58:53 +00003275}
3276
3277llvm::DIScope CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
3278 if (!LexicalBlockStack.empty())
3279 return llvm::DIScope(LexicalBlockStack.back());
3280 return getContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00003281}
3282
David Blaikie9f88fe82013-04-22 06:13:21 +00003283void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
David Blaikiebd483762013-05-20 04:58:53 +00003284 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3285 return;
David Blaikie9f88fe82013-04-22 06:13:21 +00003286 DBuilder.createImportedModule(
David Blaikiebd483762013-05-20 04:58:53 +00003287 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3288 getOrCreateNameSpace(UD.getNominatedNamespace()),
David Blaikie9f88fe82013-04-22 06:13:21 +00003289 getLineNumber(UD.getLocation()));
3290}
3291
David Blaikiebd483762013-05-20 04:58:53 +00003292void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
3293 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3294 return;
3295 assert(UD.shadow_size() &&
3296 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3297 // Emitting one decl is sufficient - debuggers can detect that this is an
3298 // overloaded name & provide lookup for all the overloads.
3299 const UsingShadowDecl &USD = **UD.shadow_begin();
Frederic Riss442293e2014-11-06 21:12:06 +00003300 if (llvm::DIDescriptor Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003301 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003302 DBuilder.createImportedDeclaration(
3303 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3304 getLineNumber(USD.getLocation()));
3305}
3306
David Blaikief121b932013-05-20 22:50:41 +00003307llvm::DIImportedEntity
3308CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
3309 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
Craig Topper8a13c412014-05-21 05:09:00 +00003310 return llvm::DIImportedEntity(nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003311 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003312 if (VH)
3313 return llvm::DIImportedEntity(cast<llvm::MDNode>(VH));
Craig Topper8a13c412014-05-21 05:09:00 +00003314 llvm::DIImportedEntity R(nullptr);
David Blaikief121b932013-05-20 22:50:41 +00003315 if (const NamespaceAliasDecl *Underlying =
3316 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3317 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003318 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003319 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3320 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3321 NA.getName());
3322 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003323 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003324 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3325 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3326 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003327 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003328 return R;
3329}
3330
Guy Benyei11169dd2012-12-18 14:30:41 +00003331/// getOrCreateNamesSpace - Return namespace descriptor for the given
3332/// namespace decl.
Eric Christopherb2a008c2013-05-16 00:45:12 +00003333llvm::DINameSpace
Guy Benyei11169dd2012-12-18 14:30:41 +00003334CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003335 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003336 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003337 if (I != NameSpaceCache.end())
3338 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
Eric Christopherb2a008c2013-05-16 00:45:12 +00003339
Guy Benyei11169dd2012-12-18 14:30:41 +00003340 unsigned LineNo = getLineNumber(NSDecl->getLocation());
3341 llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003342 llvm::DIDescriptor Context =
Guy Benyei11169dd2012-12-18 14:30:41 +00003343 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()));
3344 llvm::DINameSpace NS =
3345 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003346 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003347 return NS;
3348}
3349
3350void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003351 // Creating types might create further types - invalidating the current
3352 // element and the size(), so don't cache/reference them.
3353 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3354 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
3355 E.Decl.replaceAllUsesWith(CGM.getLLVMContext(),
3356 E.Type->getDecl()->getDefinition()
3357 ? CreateTypeDefinition(E.Type, E.Unit)
3358 : E.Decl);
3359 }
3360
David Blaikief427b002014-05-06 03:42:01 +00003361 for (auto p : ReplaceMap) {
3362 assert(p.second);
3363 llvm::DIType Ty(cast<llvm::MDNode>(p.second));
David Blaikieb8149042014-05-05 21:21:39 +00003364 assert(Ty.isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003365
David Blaikief427b002014-05-06 03:42:01 +00003366 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003367 assert(it != TypeCache.end());
3368 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003369
David Blaikief427b002014-05-06 03:42:01 +00003370 llvm::DIType RepTy(cast<llvm::MDNode>(it->second));
3371 Ty.replaceAllUsesWith(CGM.getLLVMContext(), RepTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003372 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003373
Frederic Rissd253ed62014-11-18 03:40:51 +00003374 for (const auto &p : FwdDeclReplaceMap) {
3375 assert(p.second);
3376 llvm::DIDescriptor FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003377 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003378
3379 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003380 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003381 // with ourselves, that will destroy the temporary MDNode and
3382 // replace it with a standard one, avoiding leaking memory.
3383 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003384 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003385 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003386 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003387
Frederic Rissd253ed62014-11-18 03:40:51 +00003388 FwdDecl.replaceAllUsesWith(CGM.getLLVMContext(),
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003389 llvm::DIDescriptor(cast<llvm::MDNode>(Repl)));
Frederic Rissd253ed62014-11-18 03:40:51 +00003390 }
3391
Adrian Prantl73409ce2013-03-11 18:33:46 +00003392 // We keep our own list of retained types, because we need to look
3393 // up the final type in the type cache.
3394 for (std::vector<void *>::const_iterator RI = RetainedTypes.begin(),
3395 RE = RetainedTypes.end(); RI != RE; ++RI)
David Blaikie0856f662014-03-04 22:01:08 +00003396 DBuilder.retainType(llvm::DIType(cast<llvm::MDNode>(TypeCache[*RI])));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003397
Guy Benyei11169dd2012-12-18 14:30:41 +00003398 DBuilder.finalize();
3399}
David Blaikie66088d52014-09-24 17:01:27 +00003400
3401void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
3402 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3403 return;
3404 llvm::DIType DieTy = getOrCreateType(Ty, getOrCreateMainFile());
3405 // Don't ignore in case of explicit cast where it is referenced indirectly.
3406 DBuilder.retainType(DieTy);
3407}