blob: 61993f8d542745937b02b25c813e0873b16a78ac [file] [log] [blame]
Guy Benyei11169dd2012-12-18 14:30:41 +00001//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the debug information generation while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGDebugInfo.h"
15#include "CGBlocks.h"
David Blaikie38079fd2013-05-10 21:53:14 +000016#include "CGCXXABI.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000017#include "CGObjCRuntime.h"
18#include "CodeGenFunction.h"
19#include "CodeGenModule.h"
20#include "clang/AST/ASTContext.h"
21#include "clang/AST/DeclFriend.h"
22#include "clang/AST/DeclObjC.h"
23#include "clang/AST/DeclTemplate.h"
24#include "clang/AST/Expr.h"
25#include "clang/AST/RecordLayout.h"
26#include "clang/Basic/FileManager.h"
27#include "clang/Basic/SourceManager.h"
28#include "clang/Basic/Version.h"
29#include "clang/Frontend/CodeGenOptions.h"
Adrian Prantlc4bb47e2015-06-30 17:39:51 +000030#include "clang/Lex/HeaderSearchOptions.h"
31#include "clang/Lex/PreprocessorOptions.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000032#include "llvm/ADT/SmallVector.h"
33#include "llvm/ADT/StringExtras.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000034#include "llvm/IR/Constants.h"
35#include "llvm/IR/DataLayout.h"
36#include "llvm/IR/DerivedTypes.h"
37#include "llvm/IR/Instructions.h"
38#include "llvm/IR/Intrinsics.h"
39#include "llvm/IR/Module.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000040#include "llvm/Support/FileSystem.h"
Adrian Prantl0630eb72013-12-18 21:48:18 +000041#include "llvm/Support/Path.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000042using namespace clang;
43using namespace clang::CodeGen;
44
45CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Eric Christopher324bbbd2013-07-14 21:12:44 +000046 : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
Adrian Prantl6b21ab22015-08-27 19:46:20 +000047 DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs),
Eric Christopher324bbbd2013-07-14 21:12:44 +000048 DBuilder(CGM.getModule()) {
Guy Benyei11169dd2012-12-18 14:30:41 +000049 CreateCompileUnit();
50}
51
52CGDebugInfo::~CGDebugInfo() {
53 assert(LexicalBlockStack.empty() &&
54 "Region stack mismatch, stack not empty!");
55}
56
David Blaikie66e41972015-01-14 07:38:27 +000057ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
David Blaikie835afb22015-01-21 23:08:17 +000058 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000059 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +000060 init(TemporaryLocation);
61}
62
Adrian Prantl39428e72015-02-03 18:40:42 +000063ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
Adrian Prantl95b24e92015-02-03 20:00:54 +000064 bool DefaultToEmpty,
Adrian Prantl39428e72015-02-03 18:40:42 +000065 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000066 : CGF(&CGF) {
Adrian Prantl95b24e92015-02-03 20:00:54 +000067 init(TemporaryLocation, DefaultToEmpty);
Adrian Prantl39428e72015-02-03 18:40:42 +000068}
69
70void ApplyDebugLocation::init(SourceLocation TemporaryLocation,
Adrian Prantl95b24e92015-02-03 20:00:54 +000071 bool DefaultToEmpty) {
David Blaikied7057d92015-08-12 23:49:57 +000072 auto *DI = CGF->getDebugInfo();
73 if (!DI) {
74 CGF = nullptr;
75 return;
David Blaikie66e41972015-01-14 07:38:27 +000076 }
David Blaikied7057d92015-08-12 23:49:57 +000077
78 OriginalLocation = CGF->Builder.getCurrentDebugLocation();
79 if (TemporaryLocation.isValid()) {
80 DI->EmitLocation(CGF->Builder, TemporaryLocation);
81 return;
82 }
83
84 if (DefaultToEmpty) {
85 CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc());
86 return;
87 }
88
89 // Construct a location that has a valid scope, but no line info.
90 assert(!DI->LexicalBlockStack.empty());
91 CGF->Builder.SetCurrentDebugLocation(
92 llvm::DebugLoc::get(0, 0, DI->LexicalBlockStack.back()));
Adrian Prantl2e0637f2013-07-18 00:28:02 +000093}
94
David Blaikie9b479662015-01-25 01:19:10 +000095ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E)
David Blaikied7057d92015-08-12 23:49:57 +000096 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +000097 init(E->getExprLoc());
98}
99
David Blaikie66e41972015-01-14 07:38:27 +0000100ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc)
David Blaikied7057d92015-08-12 23:49:57 +0000101 : CGF(&CGF) {
102 if (!CGF.getDebugInfo()) {
103 this->CGF = nullptr;
104 return;
David Blaikie66e41972015-01-14 07:38:27 +0000105 }
David Blaikied7057d92015-08-12 23:49:57 +0000106 OriginalLocation = CGF.Builder.getCurrentDebugLocation();
107 if (Loc)
108 CGF.Builder.SetCurrentDebugLocation(std::move(Loc));
David Blaikie66e41972015-01-14 07:38:27 +0000109}
110
111ApplyDebugLocation::~ApplyDebugLocation() {
112 // Query CGF so the location isn't overwritten when location updates are
113 // temporarily disabled (for C++ default function arguments)
David Blaikied7057d92015-08-12 23:49:57 +0000114 if (CGF)
115 CGF->Builder.SetCurrentDebugLocation(std::move(OriginalLocation));
David Blaikie66e41972015-01-14 07:38:27 +0000116}
117
Guy Benyei11169dd2012-12-18 14:30:41 +0000118void CGDebugInfo::setLocation(SourceLocation Loc) {
119 // If the new location isn't valid return.
Eric Christophere7b87e52014-10-26 23:40:33 +0000120 if (Loc.isInvalid())
121 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000122
123 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
124
125 // If we've changed files in the middle of a lexical scope go ahead
126 // and create a new lexical scope with file node if it's different
127 // from the one in the scope.
Eric Christophere7b87e52014-10-26 23:40:33 +0000128 if (LexicalBlockStack.empty())
129 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000130
131 SourceManager &SM = CGM.getContext().getSourceManager();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000132 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +0000133 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000134
Duncan P. N. Exon Smith373ee852015-04-16 01:36:36 +0000135 if (PCLoc.isInvalid() || Scope->getFilename() == PCLoc.getFilename())
Guy Benyei11169dd2012-12-18 14:30:41 +0000136 return;
137
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000138 if (auto *LBF = dyn_cast<llvm::DILexicalBlockFile>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000139 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000140 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlockFile(
141 LBF->getScope(), getOrCreateFile(CurLoc)));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000142 } else if (isa<llvm::DILexicalBlock>(Scope) ||
143 isa<llvm::DISubprogram>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000144 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000145 LexicalBlockStack.emplace_back(
146 DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000147 }
148}
149
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000150llvm::DIScope *CGDebugInfo::getDeclContextDescriptor(const Decl *D) {
151 return getContextDescriptor(cast<Decl>(D->getDeclContext()), TheCU);
152}
153
154llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context,
155 llvm::DIScope *Default) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000156 if (!Context)
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000157 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000158
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000159 auto I = RegionMap.find(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +0000160 if (I != RegionMap.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000161 llvm::Metadata *V = I->second;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000162 return dyn_cast_or_null<llvm::DIScope>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000163 }
164
165 // Check namespace.
166 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000167 return getOrCreateNameSpace(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +0000168
David Blaikiebfa52742013-04-19 06:56:38 +0000169 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context))
170 if (!RDecl->isDependentType())
171 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Eric Christophere7b87e52014-10-26 23:40:33 +0000172 getOrCreateMainFile());
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000173 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000174}
175
Guy Benyei11169dd2012-12-18 14:30:41 +0000176StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000177 assert(FD && "Invalid FunctionDecl!");
Guy Benyei11169dd2012-12-18 14:30:41 +0000178 IdentifierInfo *FII = FD->getIdentifier();
Eric Christophere7b87e52014-10-26 23:40:33 +0000179 FunctionTemplateSpecializationInfo *Info =
180 FD->getTemplateSpecializationInfo();
Guy Benyei11169dd2012-12-18 14:30:41 +0000181 if (!Info && FII)
182 return FII->getName();
183
184 // Otherwise construct human readable name for debug info.
Benjamin Kramer9170e912013-02-22 15:46:01 +0000185 SmallString<128> NS;
186 llvm::raw_svector_ostream OS(NS);
187 FD->printName(OS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000188
189 // Add any template specialization args.
190 if (Info) {
191 const TemplateArgumentList *TArgs = Info->TemplateArguments;
192 const TemplateArgument *Args = TArgs->data();
193 unsigned NumArgs = TArgs->size();
194 PrintingPolicy Policy(CGM.getLangOpts());
Benjamin Kramer9170e912013-02-22 15:46:01 +0000195 TemplateSpecializationType::PrintTemplateArgumentList(OS, Args, NumArgs,
196 Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000197 }
198
199 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000200 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000201}
202
203StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
204 SmallString<256> MethodName;
205 llvm::raw_svector_ostream OS(MethodName);
206 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
207 const DeclContext *DC = OMD->getDeclContext();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000208 if (const ObjCImplementationDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000209 dyn_cast<const ObjCImplementationDecl>(DC)) {
210 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000211 } else if (const ObjCInterfaceDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000212 dyn_cast<const ObjCInterfaceDecl>(DC)) {
213 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000214 } else if (const ObjCCategoryImplDecl *OCD =
Eric Christophere7b87e52014-10-26 23:40:33 +0000215 dyn_cast<const ObjCCategoryImplDecl>(DC)) {
216 OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '('
217 << OCD->getIdentifier()->getNameStart() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000218 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000219 // We can extract the type of the class from the self pointer.
Eric Christophere7b87e52014-10-26 23:40:33 +0000220 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000221 QualType ClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000222 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000223 ClassTy.print(OS, PrintingPolicy(LangOptions()));
224 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000225 }
226 OS << ' ' << OMD->getSelector().getAsString() << ']';
227
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000228 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000229}
230
Guy Benyei11169dd2012-12-18 14:30:41 +0000231StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000232 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000233}
234
Eric Christophere7b87e52014-10-26 23:40:33 +0000235StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
David Blaikie65813a32014-04-02 18:21:09 +0000236 // quick optimization to avoid having to intern strings that are already
237 // stored reliably elsewhere
238 if (!isa<ClassTemplateSpecializationDecl>(RD))
Guy Benyei11169dd2012-12-18 14:30:41 +0000239 return RD->getName();
240
David Blaikie65813a32014-04-02 18:21:09 +0000241 SmallString<128> Name;
Benjamin Kramer9170e912013-02-22 15:46:01 +0000242 {
David Blaikie65813a32014-04-02 18:21:09 +0000243 llvm::raw_svector_ostream OS(Name);
244 RD->getNameForDiagnostic(OS, CGM.getContext().getPrintingPolicy(),
245 /*Qualified*/ false);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000246 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000247
248 // Copy this name on the side and use its reference.
David Blaikie65813a32014-04-02 18:21:09 +0000249 return internString(Name);
Guy Benyei11169dd2012-12-18 14:30:41 +0000250}
251
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000252llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000253 if (!Loc.isValid())
254 // If Location is not valid then use main input file.
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +0000255 return DBuilder.createFile(TheCU->getFilename(), TheCU->getDirectory());
Guy Benyei11169dd2012-12-18 14:30:41 +0000256
257 SourceManager &SM = CGM.getContext().getSourceManager();
258 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
259
260 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
261 // If the location is not valid then use main input file.
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +0000262 return DBuilder.createFile(TheCU->getFilename(), TheCU->getDirectory());
Guy Benyei11169dd2012-12-18 14:30:41 +0000263
264 // Cache the results.
265 const char *fname = PLoc.getFilename();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000266 auto it = DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000267
268 if (it != DIFileCache.end()) {
269 // Verify that the information still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000270 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000271 return cast<llvm::DIFile>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000272 }
273
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000274 llvm::DIFile *F =
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +0000275 DBuilder.createFile(PLoc.getFilename(), getCurrentDirname());
Guy Benyei11169dd2012-12-18 14:30:41 +0000276
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000277 DIFileCache[fname].reset(F);
Guy Benyei11169dd2012-12-18 14:30:41 +0000278 return F;
279}
280
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000281llvm::DIFile *CGDebugInfo::getOrCreateMainFile() {
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +0000282 return DBuilder.createFile(TheCU->getFilename(), TheCU->getDirectory());
Guy Benyei11169dd2012-12-18 14:30:41 +0000283}
284
Guy Benyei11169dd2012-12-18 14:30:41 +0000285unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
286 if (Loc.isInvalid() && CurLoc.isInvalid())
287 return 0;
288 SourceManager &SM = CGM.getContext().getSourceManager();
289 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000290 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000291}
292
Adrian Prantlc7822422013-03-12 20:43:25 +0000293unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000294 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000295 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000296 return 0;
297
298 // If the location is invalid then use the current column.
299 if (Loc.isInvalid() && CurLoc.isInvalid())
300 return 0;
301 SourceManager &SM = CGM.getContext().getSourceManager();
302 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000303 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000304}
305
306StringRef CGDebugInfo::getCurrentDirname() {
307 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
308 return CGM.getCodeGenOpts().DebugCompilationDir;
309
310 if (!CWDName.empty())
311 return CWDName;
312 SmallString<256> CWD;
313 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000314 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000315}
316
Guy Benyei11169dd2012-12-18 14:30:41 +0000317void CGDebugInfo::CreateCompileUnit() {
318
David Blaikieaabde052014-05-14 00:29:00 +0000319 // Should we be asking the SourceManager for the main file name, instead of
320 // accepting it as an argument? This just causes the main file name to
321 // mismatch with source locations and create extra lexical scopes or
322 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
323 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
324 // because that's what the SourceManager says)
325
Guy Benyei11169dd2012-12-18 14:30:41 +0000326 // Get absolute path name.
327 SourceManager &SM = CGM.getContext().getSourceManager();
328 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
329 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000330 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000331
332 // The main file name provided via the "-main-file-name" option contains just
333 // the file name itself with no path information. This file name may have had
334 // a relative path, so we look into the actual file entry for the main
335 // file to determine the real absolute path for the file.
336 std::string MainFileDir;
337 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
338 MainFileDir = MainFile->getDir()->getName();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000339 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000340 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
341 llvm::sys::path::append(MainFileDirSS, MainFileName);
342 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000343 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000344 }
345
346 // Save filename string.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000347 StringRef Filename = internString(MainFileName);
Eric Christopherf1545832013-02-22 23:50:16 +0000348
349 // Save split dwarf file string.
350 std::string SplitDwarfFile = CGM.getCodeGenOpts().SplitDwarfFile;
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000351 StringRef SplitDwarfFilename = internString(SplitDwarfFile);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000352
Ed Masteda706022014-05-07 12:49:30 +0000353 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000354 const LangOptions &LO = CGM.getLangOpts();
355 if (LO.CPlusPlus) {
356 if (LO.ObjC1)
357 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
358 else
359 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
360 } else if (LO.ObjC1) {
361 LangTag = llvm::dwarf::DW_LANG_ObjC;
362 } else if (LO.C99) {
363 LangTag = llvm::dwarf::DW_LANG_C99;
364 } else {
365 LangTag = llvm::dwarf::DW_LANG_C89;
366 }
367
368 std::string Producer = getClangFullVersion();
369
370 // Figure out which version of the ObjC runtime we have.
371 unsigned RuntimeVers = 0;
372 if (LO.ObjC1)
373 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
374
375 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000376 // FIXME - Eliminate TheCU.
Eric Christophere4200a22014-02-27 01:25:08 +0000377 TheCU = DBuilder.createCompileUnit(
378 LangTag, Filename, getCurrentDirname(), Producer, LO.Optimize,
379 CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers, SplitDwarfFilename,
Diego Novillo913690c2014-06-24 17:02:17 +0000380 DebugKind <= CodeGenOptions::DebugLineTablesOnly
Eric Christophere4200a22014-02-27 01:25:08 +0000381 ? llvm::DIBuilder::LineTablesOnly
Diego Novillo913690c2014-06-24 17:02:17 +0000382 : llvm::DIBuilder::FullDebug,
Adrian Prantlbe81cf52015-05-21 20:37:26 +0000383 0 /* DWOid */,
Diego Novillo913690c2014-06-24 17:02:17 +0000384 DebugKind != CodeGenOptions::LocTrackingOnly);
Guy Benyei11169dd2012-12-18 14:30:41 +0000385}
386
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000387llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000388 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000389 StringRef BTName;
390 switch (BT->getKind()) {
391#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000392#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000393#include "clang/AST/BuiltinTypes.def"
394 case BuiltinType::Dependent:
395 llvm_unreachable("Unexpected builtin type");
396 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000397 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000398 case BuiltinType::Void:
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000399 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +0000400 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000401 if (!ClassTy)
402 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
403 "objc_class", TheCU,
404 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000405 return ClassTy;
406 case BuiltinType::ObjCId: {
407 // typedef struct objc_class *Class;
408 // typedef struct objc_object {
409 // Class isa;
410 // } *id;
411
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000412 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000413 return ObjTy;
414
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000415 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000416 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
417 "objc_class", TheCU,
418 getOrCreateMainFile(), 0);
419
420 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000421
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000422 auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000423
Eric Christopher5c7ee8b2013-04-02 22:59:11 +0000424 ObjTy =
David Blaikie6d4fe152013-02-25 01:07:08 +0000425 DBuilder.createStructType(TheCU, "objc_object", getOrCreateMainFile(),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000426 0, 0, 0, 0, nullptr, llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000427
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000428 DBuilder.replaceArrays(
429 ObjTy,
430 DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
431 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0, 0, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000432 return ObjTy;
433 }
434 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000435 if (!SelTy)
436 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
437 "objc_selector", TheCU,
438 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000439 return SelTy;
440 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000441
442 case BuiltinType::OCLImage1d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000443 return getOrCreateStructPtrType("opencl_image1d_t", OCLImage1dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000444 case BuiltinType::OCLImage1dArray:
Eric Christopherb2a008c2013-05-16 00:45:12 +0000445 return getOrCreateStructPtrType("opencl_image1d_array_t",
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000446 OCLImage1dArrayDITy);
447 case BuiltinType::OCLImage1dBuffer:
448 return getOrCreateStructPtrType("opencl_image1d_buffer_t",
449 OCLImage1dBufferDITy);
450 case BuiltinType::OCLImage2d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000451 return getOrCreateStructPtrType("opencl_image2d_t", OCLImage2dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000452 case BuiltinType::OCLImage2dArray:
453 return getOrCreateStructPtrType("opencl_image2d_array_t",
454 OCLImage2dArrayDITy);
455 case BuiltinType::OCLImage3d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000456 return getOrCreateStructPtrType("opencl_image3d_t", OCLImage3dDITy);
Guy Benyei61054192013-02-07 10:55:47 +0000457 case BuiltinType::OCLSampler:
Eric Christophere7b87e52014-10-26 23:40:33 +0000458 return DBuilder.createBasicType(
459 "opencl_sampler_t", CGM.getContext().getTypeSize(BT),
460 CGM.getContext().getTypeAlign(BT), llvm::dwarf::DW_ATE_unsigned);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000461 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000462 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000463
Guy Benyei11169dd2012-12-18 14:30:41 +0000464 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000465 case BuiltinType::Char_U:
466 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
467 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000468 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000469 case BuiltinType::SChar:
470 Encoding = llvm::dwarf::DW_ATE_signed_char;
471 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000472 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000473 case BuiltinType::Char32:
474 Encoding = llvm::dwarf::DW_ATE_UTF;
475 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000476 case BuiltinType::UShort:
477 case BuiltinType::UInt:
478 case BuiltinType::UInt128:
479 case BuiltinType::ULong:
480 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000481 case BuiltinType::ULongLong:
482 Encoding = llvm::dwarf::DW_ATE_unsigned;
483 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000484 case BuiltinType::Short:
485 case BuiltinType::Int:
486 case BuiltinType::Int128:
487 case BuiltinType::Long:
488 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000489 case BuiltinType::LongLong:
490 Encoding = llvm::dwarf::DW_ATE_signed;
491 break;
492 case BuiltinType::Bool:
493 Encoding = llvm::dwarf::DW_ATE_boolean;
494 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000495 case BuiltinType::Half:
496 case BuiltinType::Float:
497 case BuiltinType::LongDouble:
Eric Christophere7b87e52014-10-26 23:40:33 +0000498 case BuiltinType::Double:
499 Encoding = llvm::dwarf::DW_ATE_float;
500 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000501 }
502
503 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000504 case BuiltinType::Long:
505 BTName = "long int";
506 break;
507 case BuiltinType::LongLong:
508 BTName = "long long int";
509 break;
510 case BuiltinType::ULong:
511 BTName = "long unsigned int";
512 break;
513 case BuiltinType::ULongLong:
514 BTName = "long long unsigned int";
515 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000516 default:
517 BTName = BT->getName(CGM.getLangOpts());
518 break;
519 }
520 // Bit size, align and offset of the type.
521 uint64_t Size = CGM.getContext().getTypeSize(BT);
522 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000523 return DBuilder.createBasicType(BTName, Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000524}
525
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000526llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000527 // Bit size, align and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000528 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000529 if (Ty->isComplexIntegerType())
530 Encoding = llvm::dwarf::DW_ATE_lo_user;
531
532 uint64_t Size = CGM.getContext().getTypeSize(Ty);
533 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000534 return DBuilder.createBasicType("complex", Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000535}
536
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000537llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
538 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000539 QualifierCollector Qc;
540 const Type *T = Qc.strip(Ty);
541
542 // Ignore these qualifiers for now.
543 Qc.removeObjCGCAttr();
544 Qc.removeAddressSpace();
545 Qc.removeObjCLifetime();
546
547 // We will create one Derived type for one qualifier and recurse to handle any
548 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000549 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000550 if (Qc.hasConst()) {
551 Tag = llvm::dwarf::DW_TAG_const_type;
552 Qc.removeConst();
553 } else if (Qc.hasVolatile()) {
554 Tag = llvm::dwarf::DW_TAG_volatile_type;
555 Qc.removeVolatile();
556 } else if (Qc.hasRestrict()) {
557 Tag = llvm::dwarf::DW_TAG_restrict_type;
558 Qc.removeRestrict();
559 } else {
560 assert(Qc.empty() && "Unknown type qualifier for debug info");
561 return getOrCreateType(QualType(T, 0), Unit);
562 }
563
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000564 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000565
566 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
567 // CVR derived types.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000568 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000569}
570
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000571llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
572 llvm::DIFile *Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000573
574 // The frontend treats 'id' as a typedef to an ObjCObjectType,
575 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
576 // debug info, we want to emit 'id' in both cases.
577 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000578 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000579
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000580 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
581 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000582}
583
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000584llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
585 llvm::DIFile *Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000586 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000587 Ty->getPointeeType(), Unit);
588}
589
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000590/// \return whether a C++ mangling exists for the type defined by TD.
591static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
592 switch (TheCU->getSourceLanguage()) {
593 case llvm::dwarf::DW_LANG_C_plus_plus:
594 return true;
595 case llvm::dwarf::DW_LANG_ObjC_plus_plus:
596 return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
597 default:
598 return false;
599 }
600}
601
Manman Rene0064d82013-08-29 23:19:58 +0000602/// In C++ mode, types have linkage, so we can rely on the ODR and
603/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000604static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
605 CodeGenModule &CGM,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000606 llvm::DICompileUnit *TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000607 SmallString<256> FullName;
Manman Rene0064d82013-08-29 23:19:58 +0000608 const TagDecl *TD = Ty->getDecl();
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000609
610 if (!hasCXXMangling(TD, TheCU) || !TD->isExternallyVisible())
Manman Rene0064d82013-08-29 23:19:58 +0000611 return FullName;
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000612
Manman Rene0064d82013-08-29 23:19:58 +0000613 // Microsoft Mangler does not have support for mangleCXXRTTIName yet.
614 if (CGM.getTarget().getCXXABI().isMicrosoft())
615 return FullName;
616
617 // TODO: This is using the RTTI name. Is there a better way to get
618 // a unique string for a type?
619 llvm::raw_svector_ostream Out(FullName);
620 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
Manman Rene0064d82013-08-29 23:19:58 +0000621 return FullName;
622}
623
Adrian Prantl3e8bad42015-07-08 21:18:34 +0000624/// \return the approproate DWARF tag for a composite type.
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000625static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
626 llvm::dwarf::Tag Tag;
627 if (RD->isStruct() || RD->isInterface())
628 Tag = llvm::dwarf::DW_TAG_structure_type;
629 else if (RD->isUnion())
630 Tag = llvm::dwarf::DW_TAG_union_type;
631 else {
632 // FIXME: This could be a struct type giving a default visibility different
633 // than C++ class type, but needs llvm metadata changes first.
634 assert(RD->isClass());
635 Tag = llvm::dwarf::DW_TAG_class_type;
636 }
637 return Tag;
638}
639
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000640llvm::DICompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000641CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000642 llvm::DIScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000643 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000644 if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
645 return cast<llvm::DICompositeType>(T);
646 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +0000647 unsigned Line = getLineNumber(RD->getLocation());
648 StringRef RDName = getClassName(RD);
649
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000650 uint64_t Size = 0;
651 uint64_t Align = 0;
652
653 const RecordDecl *D = RD->getDefinition();
654 if (D && D->isCompleteDefinition()) {
655 Size = CGM.getContext().getTypeSize(Ty);
656 Align = CGM.getContext().getTypeAlign(Ty);
657 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000658
659 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000660 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000661 llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000662 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000663 llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000664 ReplaceMap.emplace_back(
665 std::piecewise_construct, std::make_tuple(Ty),
666 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000667 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000668}
669
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000670llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000671 const Type *Ty,
672 QualType PointeeTy,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000673 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000674 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
675 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
David Blaikie99dab3b2013-09-04 22:03:57 +0000676 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit));
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000677
Guy Benyei11169dd2012-12-18 14:30:41 +0000678 // Bit size, align and offset of the type.
679 // Size is always the size of a pointer. We can't use getTypeSize here
680 // because that does not return the correct value for references.
681 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000682 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000683 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
684
David Blaikie99dab3b2013-09-04 22:03:57 +0000685 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
686 Align);
Guy Benyei11169dd2012-12-18 14:30:41 +0000687}
688
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000689llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
690 llvm::DIType *&Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000691 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000692 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000693 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
694 TheCU, getOrCreateMainFile(), 0);
695 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
696 Cache = DBuilder.createPointerType(Cache, Size);
697 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000698}
699
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000700llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
701 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000702 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000703 QualType FType;
704 uint64_t FieldSize, FieldOffset;
705 unsigned FieldAlign;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000706 llvm::DINodeArray Elements;
Guy Benyei11169dd2012-12-18 14:30:41 +0000707
708 FieldOffset = 0;
709 FType = CGM.getContext().UnsignedLongTy;
710 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
711 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
712
713 Elements = DBuilder.getOrCreateArray(EltTys);
714 EltTys.clear();
715
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000716 unsigned Flags = llvm::DINode::FlagAppleBlock;
Adrian Prantl498fff62015-07-06 21:31:35 +0000717 unsigned LineNo = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000718
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000719 auto *EltTy =
Adrian Prantl498fff62015-07-06 21:31:35 +0000720 DBuilder.createStructType(Unit, "__block_descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000721 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000722
723 // Bit size, align and offset of the type.
724 uint64_t Size = CGM.getContext().getTypeSize(Ty);
725
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000726 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000727
728 FieldOffset = 0;
729 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
730 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
731 FType = CGM.getContext().IntTy;
732 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
733 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000734 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000735 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
736
737 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000738 FieldSize = CGM.getContext().getTypeSize(Ty);
739 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Adrian Prantl498fff62015-07-06 21:31:35 +0000740 EltTys.push_back(DBuilder.createMemberType(Unit, "__descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000741 FieldSize, FieldAlign, FieldOffset,
742 0, DescTy));
Guy Benyei11169dd2012-12-18 14:30:41 +0000743
744 FieldOffset += FieldSize;
745 Elements = DBuilder.getOrCreateArray(EltTys);
746
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000747 // The __block_literal_generic structs are marked with a special
748 // DW_AT_APPLE_BLOCK attribute and are an implementation detail only
749 // the debugger needs to know about. To allow type uniquing, emit
750 // them without a name or a location.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000751 EltTy =
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000752 DBuilder.createStructType(Unit, "", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000753 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000754
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000755 return DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000756}
757
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000758llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
759 llvm::DIFile *Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000760 assert(Ty->isTypeAlias());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000761 llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000762
763 SmallString<128> NS;
764 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000765 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
766 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000767
768 TemplateSpecializationType::PrintTemplateArgumentList(
769 OS, Ty->getArgs(), Ty->getNumArgs(),
770 CGM.getContext().getPrintingPolicy());
771
Eric Christophere7b87e52014-10-26 23:40:33 +0000772 TypeAliasDecl *AliasDecl = cast<TypeAliasTemplateDecl>(
773 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000774
775 SourceLocation Loc = AliasDecl->getLocation();
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000776 return DBuilder.createTypedef(
777 Src, internString(OS.str()), getOrCreateFile(Loc), getLineNumber(Loc),
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000778 getDeclContextDescriptor(AliasDecl));
David Blaikief1b382e2014-04-06 17:14:06 +0000779}
780
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000781llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
782 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000783 // We don't set size information, but do specify where the typedef was
784 // declared.
David Blaikiebe822ed2015-07-01 18:07:22 +0000785 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000786
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000787 // Typedefs are derived from some other type.
788 return DBuilder.createTypedef(
789 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
790 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000791 getDeclContextDescriptor(Ty->getDecl()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000792}
793
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000794llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
795 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000796 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000797
798 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000799 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000800
801 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000802 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000803 if (isa<FunctionNoProtoType>(Ty))
804 EltTys.push_back(DBuilder.createUnspecifiedParameter());
805 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
Alp Toker9cacbab2014-01-20 20:26:09 +0000806 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
807 EltTys.push_back(getOrCreateType(FPT->getParamType(i), Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000808 if (FPT->isVariadic())
809 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000810 }
811
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000812 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Guy Benyei11169dd2012-12-18 14:30:41 +0000813 return DBuilder.createSubroutineType(Unit, EltTypeArray);
814}
815
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000816/// Convert an AccessSpecifier into the corresponding DINode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +0000817/// As an optimization, return 0 if the access specifier equals the
818/// default for the containing type.
819static unsigned getAccessFlag(AccessSpecifier Access, const RecordDecl *RD) {
820 AccessSpecifier Default = clang::AS_none;
821 if (RD && RD->isClass())
822 Default = clang::AS_private;
823 else if (RD && (RD->isStruct() || RD->isUnion()))
824 Default = clang::AS_public;
825
826 if (Access == Default)
827 return 0;
828
Eric Christophere7b87e52014-10-26 23:40:33 +0000829 switch (Access) {
830 case clang::AS_private:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000831 return llvm::DINode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +0000832 case clang::AS_protected:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000833 return llvm::DINode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +0000834 case clang::AS_public:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000835 return llvm::DINode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +0000836 case clang::AS_none:
837 return 0;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000838 }
839 llvm_unreachable("unexpected access enumerator");
840}
Guy Benyei11169dd2012-12-18 14:30:41 +0000841
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000842llvm::DIType *CGDebugInfo::createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000843 StringRef name, QualType type, uint64_t sizeInBitsOverride,
844 SourceLocation loc, AccessSpecifier AS, uint64_t offsetInBits,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000845 llvm::DIFile *tunit, llvm::DIScope *scope, const RecordDecl *RD) {
846 llvm::DIType *debugType = getOrCreateType(type, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000847
848 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000849 llvm::DIFile *file = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000850 unsigned line = getLineNumber(loc);
851
David Majnemer34b57492014-07-30 01:30:47 +0000852 uint64_t SizeInBits = 0;
853 unsigned AlignInBits = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000854 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +0000855 TypeInfo TI = CGM.getContext().getTypeInfo(type);
856 SizeInBits = TI.Width;
857 AlignInBits = TI.Align;
Guy Benyei11169dd2012-12-18 14:30:41 +0000858
859 if (sizeInBitsOverride)
David Majnemer34b57492014-07-30 01:30:47 +0000860 SizeInBits = sizeInBitsOverride;
Guy Benyei11169dd2012-12-18 14:30:41 +0000861 }
862
Adrian Prantl21361fb2014-08-29 22:44:27 +0000863 unsigned flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +0000864 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
865 AlignInBits, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +0000866}
867
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000868void CGDebugInfo::CollectRecordLambdaFields(
869 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000870 llvm::DIType *RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +0000871 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
872 // has the name and the location of the variable so we should iterate over
873 // both concurrently.
874 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
875 RecordDecl::field_iterator Field = CXXDecl->field_begin();
876 unsigned fieldno = 0;
877 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +0000878 E = CXXDecl->captures_end();
879 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000880 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +0000881 if (C.capturesVariable()) {
882 VarDecl *V = C.getCapturedVar();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000883 llvm::DIFile *VUnit = getOrCreateFile(C.getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000884 StringRef VName = V->getName();
885 uint64_t SizeInBitsOverride = 0;
886 if (Field->isBitField()) {
887 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
888 assert(SizeInBitsOverride && "found named 0-width bitfield");
889 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000890 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000891 VName, Field->getType(), SizeInBitsOverride, C.getLocation(),
892 Field->getAccess(), layout.getFieldOffset(fieldno), VUnit, RecordTy,
893 CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000894 elements.push_back(fieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +0000895 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +0000896 // TODO: Need to handle 'this' in some way by probably renaming the
897 // this of the lambda class and having a field member of 'this' or
898 // by using AT_object_pointer for the function and having that be
899 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +0000900 FieldDecl *f = *Field;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000901 llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000902 QualType type = f->getType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000903 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000904 "this", type, 0, f->getLocation(), f->getAccess(),
905 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000906
907 elements.push_back(fieldType);
908 }
909 }
910}
911
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000912llvm::DIDerivedType *
913CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +0000914 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000915 // Create the descriptor for the static variable, with or without
916 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +0000917 Var = Var->getCanonicalDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000918 llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
919 llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher91a31902013-01-16 01:22:32 +0000920
Eric Christopher91a31902013-01-16 01:22:32 +0000921 unsigned LineNumber = getLineNumber(Var->getLocation());
922 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +0000923 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +0000924 if (Var->getInit()) {
925 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +0000926 if (Value) {
927 if (Value->isInt())
928 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
929 if (Value->isFloat())
930 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
931 }
Eric Christopher91a31902013-01-16 01:22:32 +0000932 }
933
Adrian Prantl21361fb2014-08-29 22:44:27 +0000934 unsigned Flags = getAccessFlag(Var->getAccess(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000935 llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
David Blaikieae019462013-08-15 22:50:29 +0000936 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000937 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +0000938 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +0000939}
940
Eric Christophere7b87e52014-10-26 23:40:33 +0000941void CGDebugInfo::CollectRecordNormalField(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000942 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
943 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +0000944 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000945 StringRef name = field->getName();
946 QualType type = field->getType();
947
948 // Ignore unnamed fields unless they're anonymous structs/unions.
949 if (name.empty() && !type->isRecordType())
950 return;
951
952 uint64_t SizeInBitsOverride = 0;
953 if (field->isBitField()) {
954 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
955 assert(SizeInBitsOverride && "found named 0-width bitfield");
956 }
957
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000958 llvm::DIType *fieldType =
Eric Christophere7b87e52014-10-26 23:40:33 +0000959 createFieldType(name, type, SizeInBitsOverride, field->getLocation(),
960 field->getAccess(), OffsetInBits, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +0000961
962 elements.push_back(fieldType);
963}
964
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000965void CGDebugInfo::CollectRecordFields(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000966 const RecordDecl *record, llvm::DIFile *tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000967 SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000968 llvm::DICompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000969 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
970
Eric Christopher91a31902013-01-16 01:22:32 +0000971 if (CXXDecl && CXXDecl->isLambda())
972 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
973 else {
974 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +0000975
Eric Christopher91a31902013-01-16 01:22:32 +0000976 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +0000977 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +0000978
Eric Christopher91a31902013-01-16 01:22:32 +0000979 // Static and non-static members should appear in the same order as
980 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +0000981 for (const auto *I : record->decls())
982 if (const auto *V = dyn_cast<VarDecl>(I)) {
David Blaikiece763042013-08-20 21:49:21 +0000983 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000984 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +0000985 if (MI != StaticDataMemberCache.end()) {
986 assert(MI->second &&
987 "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +0000988 elements.push_back(MI->second);
Adrian Prantl21361fb2014-08-29 22:44:27 +0000989 } else {
990 auto Field = CreateRecordStaticField(V, RecordTy, record);
991 elements.push_back(Field);
992 }
Aaron Ballman629afae2014-03-07 19:56:05 +0000993 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000994 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
995 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +0000996
997 // Bump field number for next field.
998 ++fieldNo;
Guy Benyei11169dd2012-12-18 14:30:41 +0000999 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001000 }
1001}
1002
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001003llvm::DISubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +00001004CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001005 llvm::DIFile *Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001006 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001007 if (Method->isStatic())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001008 return cast_or_null<llvm::DISubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001009 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001010 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1011 Func, Unit);
1012}
David Blaikie2aaf0652013-01-07 22:24:59 +00001013
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001014llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1015 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001016 // Add "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001017 llvm::DITypeRefArray Args(
1018 cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001019 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001020 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001021
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001022 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001023
1024 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001025 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001026
David Blaikie2aaf0652013-01-07 22:24:59 +00001027 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001028 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001029 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1030 // Create pointer type directly in this case.
1031 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1032 QualType PointeeTy = ThisPtrTy->getPointeeType();
1033 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001034 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie2aaf0652013-01-07 22:24:59 +00001035 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001036 llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1037 llvm::DIType *ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001038 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001039 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001040 // TODO: This and the artificial type below are misleading, the
1041 // types aren't artificial the argument is, but the current
1042 // metadata doesn't represent that.
1043 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1044 Elts.push_back(ThisPtrType);
1045 } else {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001046 llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001047 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001048 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1049 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001050 }
1051
1052 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001053 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1054 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001055
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001056 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001057
Adrian Prantl0630eb72013-12-18 21:48:18 +00001058 unsigned Flags = 0;
1059 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001060 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001061 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001062 Flags |= llvm::DINode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001063
1064 return DBuilder.createSubroutineType(Unit, EltTypeArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001065}
1066
Eric Christopherb2a008c2013-05-16 00:45:12 +00001067/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001068/// inside a function.
1069static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1070 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1071 return isFunctionLocalClass(NRD);
1072 if (isa<FunctionDecl>(RD->getDeclContext()))
1073 return true;
1074 return false;
1075}
1076
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001077llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1078 const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001079 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001080 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001081
Guy Benyei11169dd2012-12-18 14:30:41 +00001082 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001083 llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001084
1085 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1086 // make sense to give a single ctor/dtor a linkage name.
1087 StringRef MethodLinkageName;
1088 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1089 MethodLinkageName = CGM.getMangledName(Method);
1090
1091 // Get the location for the method.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001092 llvm::DIFile *MethodDefUnit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00001093 unsigned MethodLine = 0;
1094 if (!Method->isImplicit()) {
1095 MethodDefUnit = getOrCreateFile(Method->getLocation());
1096 MethodLine = getLineNumber(Method->getLocation());
1097 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001098
1099 // Collect virtual method info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001100 llvm::DIType *ContainingType = nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001101 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001102 unsigned VIndex = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001103
Guy Benyei11169dd2012-12-18 14:30:41 +00001104 if (Method->isVirtual()) {
1105 if (Method->isPure())
1106 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1107 else
1108 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001109
Guy Benyei11169dd2012-12-18 14:30:41 +00001110 // It doesn't make sense to give a virtual destructor a vtable index,
1111 // since a single destructor has two entries in the vtable.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001112 // FIXME: Add proper support for debug info for virtual calls in
1113 // the Microsoft ABI, where we may use multiple vptrs to make a vftable
1114 // lookup if we have multiple or virtual inheritance.
1115 if (!isa<CXXDestructorDecl>(Method) &&
1116 !CGM.getTarget().getCXXABI().isMicrosoft())
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001117 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
Guy Benyei11169dd2012-12-18 14:30:41 +00001118 ContainingType = RecordTy;
1119 }
1120
1121 unsigned Flags = 0;
1122 if (Method->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001123 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001124 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
Guy Benyei11169dd2012-12-18 14:30:41 +00001125 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1126 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001127 Flags |= llvm::DINode::FlagExplicit;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001128 } else if (const CXXConversionDecl *CXXC =
Eric Christophere7b87e52014-10-26 23:40:33 +00001129 dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001130 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001131 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001132 }
1133 if (Method->hasPrototype())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001134 Flags |= llvm::DINode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001135 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001136 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001137 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001138 Flags |= llvm::DINode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001139
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001140 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1141 llvm::DISubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001142 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
1143 MethodTy, /*isLocalToUnit=*/false,
1144 /* isDefinition=*/false, Virtuality, VIndex, ContainingType, Flags,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00001145 CGM.getLangOpts().Optimize, nullptr, TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001146
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001147 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001148
1149 return SP;
1150}
1151
Eric Christophere7b87e52014-10-26 23:40:33 +00001152void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001153 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1154 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001155
1156 // Since we want more than just the individual member decls if we
1157 // have templated functions iterate over every declaration to gather
1158 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001159 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001160 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1161 // If the member is implicit, don't add it to the member list. This avoids
1162 // the member being added to type units by LLVM, while still allowing it
1163 // to be emitted into the type declaration/reference inside the compile
1164 // unit.
Paul Robinson6a7511b2015-06-25 17:50:43 +00001165 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
David Blaikie6dddfe32014-10-06 05:52:27 +00001166 // FIXME: Handle Using(Shadow?)Decls here to create
1167 // DW_TAG_imported_declarations inside the class for base decls brought into
1168 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1169 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1170 // referenced)
Paul Robinson6a7511b2015-06-25 17:50:43 +00001171 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
David Blaikiefd580722014-10-06 05:18:55 +00001172 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001173
1174 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1175 continue;
1176
David Blaikiefd580722014-10-06 05:18:55 +00001177 // Reuse the existing member function declaration if it exists.
1178 // It may be associated with the declaration of the type & should be
1179 // reused as we're building the definition.
1180 //
1181 // This situation can arise in the vtable-based debug info reduction where
1182 // implicit members are emitted in a non-vtable TU.
1183 auto MI = SPCache.find(Method->getCanonicalDecl());
1184 EltTys.push_back(MI == SPCache.end()
1185 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001186 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001187 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001188}
Guy Benyei11169dd2012-12-18 14:30:41 +00001189
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001190void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001191 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001192 llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001193 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Aaron Ballman574705e2014-03-13 15:41:46 +00001194 for (const auto &BI : RD->bases()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001195 unsigned BFlags = 0;
1196 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001197
Guy Benyei11169dd2012-12-18 14:30:41 +00001198 const CXXRecordDecl *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001199 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001200
Aaron Ballman574705e2014-03-13 15:41:46 +00001201 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001202 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1203 // virtual base offset offset is -ve. The code generator emits dwarf
1204 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001205 BaseOffset = 0 - CGM.getItaniumVTableContext()
1206 .getVirtualBaseOffsetOffset(RD, Base)
1207 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001208 } else {
1209 // In the MS ABI, store the vbtable offset, which is analogous to the
1210 // vbase offset offset in Itanium.
1211 BaseOffset =
1212 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1213 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001214 BFlags = llvm::DINode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001215 } else
1216 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1217 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1218 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001219
Adrian Prantl21361fb2014-08-29 22:44:27 +00001220 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001221 llvm::DIType *DTy = DBuilder.createInheritance(
Eric Christophere7b87e52014-10-26 23:40:33 +00001222 RecordTy, getOrCreateType(BI.getType(), Unit), BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001223 EltTys.push_back(DTy);
1224 }
1225}
1226
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001227llvm::DINodeArray
Eric Christophere7b87e52014-10-26 23:40:33 +00001228CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1229 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001230 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001231 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001232 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1233 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001234 StringRef Name;
1235 if (TPList)
1236 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001237 switch (TA.getKind()) {
1238 case TemplateArgument::Type: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001239 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001240 TemplateParams.push_back(
1241 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001242 } break;
1243 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001244 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001245 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1246 TheCU, Name, TTy,
1247 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001248 } break;
1249 case TemplateArgument::Declaration: {
1250 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001251 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001252 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001253 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001254 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001255 // Variable pointer template parameters have a value that is the address
1256 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001257 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001258 V = CGM.GetAddrOfGlobalVar(VD);
1259 // Member function pointers have special support for building them, though
1260 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001261 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Majnemere2be95b2015-06-23 07:31:01 +00001262 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001263 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001264 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001265 // Member data pointers have special handling too to compute the fixed
1266 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001267 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001268 // These five lines (& possibly the above member function pointer
1269 // handling) might be able to be refactored to use similar code in
1270 // CodeGenModule::getMemberPointerConstant
1271 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1272 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001273 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001274 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001275 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001276 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1277 TheCU, Name, TTy,
1278 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001279 } break;
1280 case TemplateArgument::NullPtr: {
1281 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001282 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001283 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001284 // Special case member data pointer null values since they're actually -1
1285 // instead of zero.
1286 if (const MemberPointerType *MPT =
1287 dyn_cast<MemberPointerType>(T.getTypePtr()))
1288 // But treat member function pointers as simple zero integers because
1289 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1290 // CodeGen grows handling for values of non-null member function
1291 // pointers then perhaps we could remove this special case and rely on
1292 // EmitNullMemberPointer for member function pointers.
1293 if (MPT->isMemberDataPointer())
1294 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1295 if (!V)
1296 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001297 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1298 TheCU, Name, TTy, cast<llvm::Constant>(V)));
David Blaikie38079fd2013-05-10 21:53:14 +00001299 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001300 case TemplateArgument::Template:
1301 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001302 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001303 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1304 break;
1305 case TemplateArgument::Pack:
1306 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1307 TheCU, Name, nullptr,
1308 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1309 break;
David Majnemer5559d472013-08-24 08:21:10 +00001310 case TemplateArgument::Expression: {
1311 const Expr *E = TA.getAsExpr();
1312 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001313 if (E->isGLValue())
1314 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001315 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001316 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001317 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001318 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1319 TheCU, Name, TTy, cast<llvm::Constant>(V->stripPointerCasts())));
David Majnemer5559d472013-08-24 08:21:10 +00001320 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001321 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001322 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001323 case TemplateArgument::Null:
1324 llvm_unreachable(
1325 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001326 }
1327 }
1328 return DBuilder.getOrCreateArray(TemplateParams);
1329}
1330
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001331llvm::DINodeArray
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001332CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001333 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001334 if (FD->getTemplatedKind() ==
1335 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001336 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1337 ->getTemplate()
1338 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001339 return CollectTemplateParams(
1340 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001341 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001342 return llvm::DINodeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001343}
1344
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001345llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1346 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001347 // Always get the full list of parameters, not just the ones from
1348 // the specialization.
1349 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001350 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001351 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001352 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001353}
1354
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001355llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001356 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001357 return VTablePtrType;
1358
1359 ASTContext &Context = CGM.getContext();
1360
1361 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001362 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001363 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
1364 llvm::DIType *SubTy = DBuilder.createSubroutineType(Unit, SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001365 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001366 llvm::DIType *vtbl_ptr_type =
Eric Christophere7b87e52014-10-26 23:40:33 +00001367 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001368 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1369 return VTablePtrType;
1370}
1371
Guy Benyei11169dd2012-12-18 14:30:41 +00001372StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001373 // Copy the gdb compatible name on the side and use its reference.
1374 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001375}
1376
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001377void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001378 SmallVectorImpl<llvm::Metadata *> &EltTys) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001379 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1380
1381 // If there is a primary base then it will hold vtable info.
1382 if (RL.getPrimaryBase())
1383 return;
1384
1385 // If this class is not dynamic then there is not any vtable info to collect.
1386 if (!RD->isDynamicClass())
1387 return;
1388
1389 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001390 llvm::DIType *VPTR = DBuilder.createMemberType(
Eric Christophere7b87e52014-10-26 23:40:33 +00001391 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001392 llvm::DINode::FlagArtificial, getOrCreateVTablePtrType(Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001393 EltTys.push_back(VPTR);
1394}
1395
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001396llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001397 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001398 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001399 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001400 return T;
1401}
1402
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001403llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001404 SourceLocation Loc) {
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001405 return getOrCreateStandaloneType(D, Loc);
1406}
1407
1408llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
1409 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001410 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001411 assert(!D.isNull() && "null type");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001412 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001413 assert(T && "could not create debug info for type");
Adrian Prantl3a884fa2015-08-27 22:56:46 +00001414
1415 // Composite types with UIDs were already retained by DIBuilder
1416 // because they are only referenced by name in the IR.
1417 if (auto *CTy = dyn_cast<llvm::DICompositeType>(T))
1418 if (!CTy->getIdentifier().empty())
1419 return T;
Adrian Prantl73409ce2013-03-11 18:33:46 +00001420 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001421 return T;
1422}
1423
David Blaikie483a9da2014-05-06 18:35:21 +00001424void CGDebugInfo::completeType(const EnumDecl *ED) {
1425 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1426 return;
1427 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001428 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001429 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001430 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001431 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001432 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001433 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001434 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001435}
1436
David Blaikieb2e86eb2013-08-15 20:49:17 +00001437void CGDebugInfo::completeType(const RecordDecl *RD) {
1438 if (DebugKind > CodeGenOptions::LimitedDebugInfo ||
1439 !CGM.getLangOpts().CPlusPlus)
1440 completeRequiredType(RD);
1441}
1442
1443void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
David Blaikie0856f662014-03-04 22:01:08 +00001444 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1445 return;
1446
David Blaikie6943dea2013-08-20 01:28:15 +00001447 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1448 if (CXXDecl->isDynamicClass())
1449 return;
1450
David Blaikieb2e86eb2013-08-15 20:49:17 +00001451 QualType Ty = CGM.getContext().getRecordType(RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001452 llvm::DIType *T = getTypeOrNull(Ty);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001453 if (T && T->isForwardDecl())
David Blaikie6943dea2013-08-20 01:28:15 +00001454 completeClassData(RD);
1455}
1456
1457void CGDebugInfo::completeClassData(const RecordDecl *RD) {
1458 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001459 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001460 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001461 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001462 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001463 if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001464 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001465 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001466 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001467 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001468}
1469
David Blaikie0e716b42014-03-03 23:48:23 +00001470static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1471 CXXRecordDecl::method_iterator End) {
1472 for (; I != End; ++I)
1473 if (FunctionDecl *Tmpl = I->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001474 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
1475 !I->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001476 return true;
1477 return false;
1478}
1479
1480static bool shouldOmitDefinition(CodeGenOptions::DebugInfoKind DebugKind,
1481 const RecordDecl *RD,
1482 const LangOptions &LangOpts) {
1483 if (DebugKind > CodeGenOptions::LimitedDebugInfo)
1484 return false;
1485
1486 if (!LangOpts.CPlusPlus)
1487 return false;
1488
1489 if (!RD->isCompleteDefinitionRequired())
1490 return true;
1491
1492 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1493
1494 if (!CXXDecl)
1495 return false;
1496
1497 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass())
1498 return true;
1499
1500 TemplateSpecializationKind Spec = TSK_Undeclared;
1501 if (const ClassTemplateSpecializationDecl *SD =
1502 dyn_cast<ClassTemplateSpecializationDecl>(RD))
1503 Spec = SD->getSpecializationKind();
1504
1505 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1506 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1507 CXXDecl->method_end()))
1508 return true;
1509
1510 return false;
1511}
1512
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001513llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001514 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001515 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
David Blaikie0e716b42014-03-03 23:48:23 +00001516 if (T || shouldOmitDefinition(DebugKind, RD, CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001517 if (!T)
Adrian Prantl6ec370a2015-09-10 18:39:45 +00001518 T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001519 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001520 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001521
David Blaikieb2e86eb2013-08-15 20:49:17 +00001522 return CreateTypeDefinition(Ty);
1523}
1524
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001525llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00001526 RecordDecl *RD = Ty->getDecl();
1527
Guy Benyei11169dd2012-12-18 14:30:41 +00001528 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001529 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001530
1531 // Records and classes and unions can all be recursive. To handle them, we
1532 // first generate a debug descriptor for the struct as a forward declaration.
1533 // Then (if it is a definition) we go through and get debug info for all of
1534 // its members. Finally, we create a descriptor for the complete type (which
1535 // may refer to the forward decl if the struct is recursive) and replace all
1536 // uses of the forward declaration with the final definition.
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00001537 llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001538
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001539 const RecordDecl *D = RD->getDefinition();
1540 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001541 return FwdDecl;
1542
David Blaikieadfbf992013-08-18 16:55:33 +00001543 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1544 CollectContainingType(CXXDecl, FwdDecl);
1545
Guy Benyei11169dd2012-12-18 14:30:41 +00001546 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001547 LexicalBlockStack.emplace_back(&*FwdDecl);
1548 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001549
Guy Benyei11169dd2012-12-18 14:30:41 +00001550 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001551 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001552 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001553
1554 // Note: The split of CXXDecl information here is intentional, the
1555 // gdb tests will depend on a certain ordering at printout. The debug
1556 // information offsets are still correct if we merge them all together
1557 // though.
1558 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1559 if (CXXDecl) {
1560 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1561 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1562 }
1563
Eric Christopher91a31902013-01-16 01:22:32 +00001564 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001565 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001566 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001567 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001568
1569 LexicalBlockStack.pop_back();
1570 RegionMap.erase(Ty->getDecl());
1571
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001572 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001573 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001574
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001575 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001576 FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001577 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001578
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001579 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001580 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001581}
1582
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001583llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1584 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001585 // Ignore protocols.
1586 return getOrCreateType(Ty->getBaseType(), Unit);
1587}
1588
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001589/// \return true if Getter has the default name for the property PD.
1590static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1591 const ObjCMethodDecl *Getter) {
1592 assert(PD);
1593 if (!Getter)
1594 return true;
1595
1596 assert(Getter->getDeclName().isObjCZeroArgSelector());
1597 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001598 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001599}
1600
1601/// \return true if Setter has the default name for the property PD.
1602static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1603 const ObjCMethodDecl *Setter) {
1604 assert(PD);
1605 if (!Setter)
1606 return true;
1607
1608 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001609 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001610 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001611}
1612
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001613llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1614 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001615 ObjCInterfaceDecl *ID = Ty->getDecl();
1616 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001617 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001618
1619 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001620 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001621 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001622 auto RuntimeLang =
1623 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00001624
1625 // If this is just a forward declaration return a special forward-declaration
1626 // debug type since we won't be able to lay out the entire type.
1627 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001628 if (!Def || !Def->getImplementation()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001629 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
David Blaikief427b002014-05-06 03:42:01 +00001630 llvm::dwarf::DW_TAG_structure_type, ID->getName(), TheCU, DefUnit, Line,
1631 RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001632 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001633 return FwdDecl;
1634 }
1635
David Blaikieef8a9512014-05-05 23:23:53 +00001636 return CreateTypeDefinition(Ty, Unit);
1637}
1638
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001639llvm::DIModule *
1640CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod) {
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001641 auto it = ModuleRefCache.find(Mod.Signature);
1642 if (it != ModuleRefCache.end())
Adrian Prantl2388ead2015-06-30 18:01:05 +00001643 return it->second;
1644
1645 // Macro definitions that were defined with "-D" on the command line.
1646 SmallString<128> ConfigMacros;
1647 {
1648 llvm::raw_svector_ostream OS(ConfigMacros);
1649 const auto &PPOpts = CGM.getPreprocessorOpts();
1650 unsigned I = 0;
1651 // Translate the macro definitions back into a commmand line.
1652 for (auto &M : PPOpts.Macros) {
1653 if (++I > 1)
1654 OS << " ";
1655 const std::string &Macro = M.first;
1656 bool Undef = M.second;
1657 OS << "\"-" << (Undef ? 'U' : 'D');
1658 for (char c : Macro)
1659 switch (c) {
1660 case '\\' : OS << "\\\\"; break;
1661 case '"' : OS << "\\\""; break;
1662 default: OS << c;
1663 }
1664 OS << '\"';
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001665 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001666 }
Adrian Prantl2388ead2015-06-30 18:01:05 +00001667 llvm::DIBuilder DIB(CGM.getModule());
1668 auto *CU = DIB.createCompileUnit(
1669 TheCU->getSourceLanguage(), internString(Mod.ModuleName),
1670 internString(Mod.Path), TheCU->getProducer(), true, StringRef(), 0,
1671 internString(Mod.ASTFile), llvm::DIBuilder::FullDebug, Mod.Signature);
1672 llvm::DIModule *ModuleRef =
1673 DIB.createModule(CU, Mod.ModuleName, ConfigMacros, internString(Mod.Path),
1674 internString(CGM.getHeaderSearchOpts().Sysroot));
1675 DIB.finalize();
1676 ModuleRefCache.insert(std::make_pair(Mod.Signature, ModuleRef));
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001677 return ModuleRef;
1678}
1679
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001680llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
1681 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00001682 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001683 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00001684 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001685 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001686
1687 // Bit size, align and offset of the type.
1688 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1689 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1690
1691 unsigned Flags = 0;
1692 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001693 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00001694
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001695 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001696 Unit, ID->getName(), DefUnit, Line, Size, Align, Flags, nullptr,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001697 llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001698
David Blaikieef8a9512014-05-05 23:23:53 +00001699 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001700 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001701
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001702 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001703 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001704 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001705
1706 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001707 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001708
1709 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1710 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001711 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001712 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001713 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001714 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001715
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001716 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001717 EltTys.push_back(InhTag);
1718 }
1719
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001720 // Create entries for all of the properties.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001721 for (const auto *PD : ID->properties()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001722 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001723 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001724 unsigned PLine = getLineNumber(Loc);
1725 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1726 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001727 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
1728 PD->getName(), PUnit, PLine,
1729 hasDefaultGetterName(PD, Getter) ? ""
1730 : getSelectorName(PD->getGetterName()),
1731 hasDefaultSetterName(PD, Setter) ? ""
1732 : getSelectorName(PD->getSetterName()),
1733 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001734 EltTys.push_back(PropertyNode);
1735 }
1736
1737 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1738 unsigned FieldNo = 0;
1739 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1740 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001741 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001742 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001743 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001744
Guy Benyei11169dd2012-12-18 14:30:41 +00001745 StringRef FieldName = Field->getName();
1746
1747 // Ignore unnamed fields.
1748 if (FieldName.empty())
1749 continue;
1750
1751 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001752 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001753 unsigned FieldLine = getLineNumber(Field->getLocation());
1754 QualType FType = Field->getType();
1755 uint64_t FieldSize = 0;
1756 unsigned FieldAlign = 0;
1757
1758 if (!FType->isIncompleteArrayType()) {
1759
1760 // Bit size, align and offset of the type.
1761 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001762 ? Field->getBitWidthValue(CGM.getContext())
1763 : CGM.getContext().getTypeSize(FType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001764 FieldAlign = CGM.getContext().getTypeAlign(FType);
1765 }
1766
1767 uint64_t FieldOffset;
1768 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1769 // We don't know the runtime offset of an ivar if we're using the
1770 // non-fragile ABI. For bitfields, use the bit offset into the first
1771 // byte of storage of the bitfield. For other fields, use zero.
1772 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001773 FieldOffset =
1774 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00001775 FieldOffset %= CGM.getContext().getCharWidth();
1776 } else {
1777 FieldOffset = 0;
1778 }
1779 } else {
1780 FieldOffset = RL.getFieldOffset(FieldNo);
1781 }
1782
1783 unsigned Flags = 0;
1784 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001785 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00001786 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001787 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001788 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001789 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00001790
Craig Topper8a13c412014-05-21 05:09:00 +00001791 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001792 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001793 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00001794 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001795 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00001796 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001797 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00001798 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001799 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1800 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001801 PropertyNode = DBuilder.createObjCProperty(
1802 PD->getName(), PUnit, PLine,
1803 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
1804 PD->getGetterName()),
1805 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
1806 PD->getSetterName()),
1807 PD->getPropertyAttributes(),
1808 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001809 }
1810 }
1811 }
Eric Christophere7b87e52014-10-26 23:40:33 +00001812 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
1813 FieldSize, FieldAlign, FieldOffset, Flags,
1814 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00001815 EltTys.push_back(FieldTy);
1816 }
1817
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001818 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001819 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00001820
Guy Benyei11169dd2012-12-18 14:30:41 +00001821 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001822 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001823}
1824
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001825llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
1826 llvm::DIFile *Unit) {
1827 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001828 int64_t Count = Ty->getNumElements();
1829 if (Count == 0)
1830 // If number of elements are not known then this is an unbounded array.
1831 // Use Count == -1 to express such arrays.
1832 Count = -1;
1833
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001834 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001835 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00001836
1837 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1838 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1839
1840 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
1841}
1842
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001843llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001844 uint64_t Size;
1845 uint64_t Align;
1846
1847 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
1848 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
1849 Size = 0;
1850 Align =
Eric Christophere7b87e52014-10-26 23:40:33 +00001851 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Guy Benyei11169dd2012-12-18 14:30:41 +00001852 } else if (Ty->isIncompleteArrayType()) {
1853 Size = 0;
1854 if (Ty->getElementType()->isIncompleteType())
1855 Align = 0;
1856 else
1857 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikief03b2e82013-05-09 20:48:12 +00001858 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001859 Size = 0;
1860 Align = 0;
1861 } else {
1862 // Size and align of the whole array, not the element type.
1863 Size = CGM.getContext().getTypeSize(Ty);
1864 Align = CGM.getContext().getTypeAlign(Ty);
1865 }
1866
1867 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1868 // interior arrays, do we care? Why aren't nested arrays represented the
1869 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001870 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001871 QualType EltTy(Ty, 0);
1872 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1873 // If the number of elements is known, then count is that number. Otherwise,
1874 // it's -1. This allows us to represent a subrange with an array of 0
1875 // elements, like this:
1876 //
1877 // struct foo {
1878 // int x[0];
1879 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00001880 int64_t Count = -1; // Count == -1 is an unbounded array.
Guy Benyei11169dd2012-12-18 14:30:41 +00001881 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1882 Count = CAT->getSize().getZExtValue();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001883
Guy Benyei11169dd2012-12-18 14:30:41 +00001884 // FIXME: Verify this is right for VLAs.
1885 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
1886 EltTy = Ty->getElementType();
1887 }
1888
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001889 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001890
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001891 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
1892 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00001893}
1894
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001895llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
1896 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001897 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
1898 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001899}
1900
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001901llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
1902 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001903 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
1904 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001905}
1906
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001907llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
1908 llvm::DIFile *U) {
David Majnemer34568bc2015-05-26 21:54:24 +00001909 uint64_t Size = CGM.getCXXABI().isTypeInfoCalculable(QualType(Ty, 0))
1910 ? CGM.getContext().getTypeSize(Ty)
1911 : 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001912 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00001913 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00001914 return DBuilder.createMemberPointerType(
David Majnemer34568bc2015-05-26 21:54:24 +00001915 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size);
Adrian Prantl0866acd2013-12-19 01:38:47 +00001916
1917 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00001918 Ty->getPointeeType()->getAs<FunctionProtoType>();
1919 return DBuilder.createMemberPointerType(
1920 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
1921 Ty->getClass(), FPT->getTypeQuals())),
1922 FPT, U),
David Majnemer34568bc2015-05-26 21:54:24 +00001923 ClassType, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +00001924}
1925
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001926llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001927 // Ignore the atomic wrapping
1928 // FIXME: What is the correct representation?
1929 return getOrCreateType(Ty->getValueType(), U);
1930}
1931
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001932llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00001933 const EnumDecl *ED = Ty->getDecl();
Guy Benyei11169dd2012-12-18 14:30:41 +00001934 uint64_t Size = 0;
1935 uint64_t Align = 0;
1936 if (!ED->getTypeForDecl()->isIncompleteType()) {
1937 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1938 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1939 }
1940
Manman Rene0064d82013-08-29 23:19:58 +00001941 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1942
Guy Benyei11169dd2012-12-18 14:30:41 +00001943 // If this is just a forward declaration, construct an appropriately
1944 // marked node and just return it.
1945 if (!ED->getDefinition()) {
Adrian Prantl6ec370a2015-09-10 18:39:45 +00001946 llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001947 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001948 unsigned Line = getLineNumber(ED->getLocation());
1949 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001950 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
David Blaikief427b002014-05-06 03:42:01 +00001951 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001952 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001953 ReplaceMap.emplace_back(
1954 std::piecewise_construct, std::make_tuple(Ty),
1955 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00001956 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00001957 }
1958
David Blaikie483a9da2014-05-06 18:35:21 +00001959 return CreateTypeDefinition(Ty);
1960}
1961
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001962llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00001963 const EnumDecl *ED = Ty->getDecl();
1964 uint64_t Size = 0;
1965 uint64_t Align = 0;
1966 if (!ED->getTypeForDecl()->isIncompleteType()) {
1967 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1968 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1969 }
1970
1971 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1972
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001973 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001974 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00001975 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00001976 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001977 Enumerators.push_back(DBuilder.createEnumerator(
1978 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001979 }
1980
1981 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001982 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00001983
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001984 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001985 unsigned Line = getLineNumber(ED->getLocation());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00001986 llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001987 llvm::DIType *ClassTy =
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001988 ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
1989 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
1990 Line, Size, Align, EltArray, ClassTy,
1991 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00001992}
1993
David Blaikie05491062013-01-21 04:37:12 +00001994static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
1995 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00001996 do {
Adrian Prantl179af902013-09-26 21:35:50 +00001997 Qualifiers InnerQuals = T.getLocalQualifiers();
1998 // Qualifiers::operator+() doesn't like it if you add a Qualifier
1999 // that is already there.
2000 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2001 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002002 QualType LastT = T;
2003 switch (T->getTypeClass()) {
2004 default:
David Blaikie05491062013-01-21 04:37:12 +00002005 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002006 case Type::TemplateSpecialization: {
2007 const auto *Spec = cast<TemplateSpecializationType>(T);
2008 if (Spec->isTypeAlias())
2009 return C.getQualifiedType(T.getTypePtr(), Quals);
2010 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002011 break;
2012 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002013 case Type::TypeOfExpr:
2014 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2015 break;
2016 case Type::TypeOf:
2017 T = cast<TypeOfType>(T)->getUnderlyingType();
2018 break;
2019 case Type::Decltype:
2020 T = cast<DecltypeType>(T)->getUnderlyingType();
2021 break;
2022 case Type::UnaryTransform:
2023 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2024 break;
2025 case Type::Attributed:
2026 T = cast<AttributedType>(T)->getEquivalentType();
2027 break;
2028 case Type::Elaborated:
2029 T = cast<ElaboratedType>(T)->getNamedType();
2030 break;
2031 case Type::Paren:
2032 T = cast<ParenType>(T)->getInnerType();
2033 break;
David Blaikie05491062013-01-21 04:37:12 +00002034 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002035 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002036 break;
2037 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00002038 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002039 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002040 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002041 break;
2042 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002043
Guy Benyei11169dd2012-12-18 14:30:41 +00002044 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002045 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002046 } while (true);
2047}
2048
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002049llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002050
2051 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002052 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002053
David Blaikief427b002014-05-06 03:42:01 +00002054 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002055 if (it != TypeCache.end()) {
2056 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002057 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002058 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002059 }
2060
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002061 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002062}
2063
David Blaikie0e716b42014-03-03 23:48:23 +00002064void CGDebugInfo::completeTemplateDefinition(
2065 const ClassTemplateSpecializationDecl &SD) {
David Blaikie0856f662014-03-04 22:01:08 +00002066 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2067 return;
2068
David Blaikie0e716b42014-03-03 23:48:23 +00002069 completeClassData(&SD);
2070 // In case this type has no member function definitions being emitted, ensure
2071 // it is retained
2072 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2073}
2074
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002075llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002076 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002077 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002078
2079 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002080 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002081
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002082 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002083 return T;
2084
2085 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002086 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Eric Christophere7b87e52014-10-26 23:40:33 +00002087 void *TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002088
2089 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002090 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002091
Guy Benyei11169dd2012-12-18 14:30:41 +00002092 return Res;
2093}
2094
Eric Christopher1ecc5632013-06-07 22:54:39 +00002095unsigned CGDebugInfo::Checksum(const ObjCInterfaceDecl *ID) {
Adrian Prantl817bbb32013-06-07 01:10:48 +00002096 // The assumption is that the number of ivars can only increase
2097 // monotonically, so it is safe to just use their current number as
2098 // a checksum.
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002099 unsigned Sum = 0;
2100 for (const ObjCIvarDecl *Ivar = ID->all_declared_ivar_begin();
Craig Topper8a13c412014-05-21 05:09:00 +00002101 Ivar != nullptr; Ivar = Ivar->getNextIvar())
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002102 ++Sum;
2103
2104 return Sum;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002105}
2106
2107ObjCInterfaceDecl *CGDebugInfo::getObjCInterfaceDecl(QualType Ty) {
2108 switch (Ty->getTypeClass()) {
2109 case Type::ObjCObjectPointer:
Eric Christophere7b87e52014-10-26 23:40:33 +00002110 return getObjCInterfaceDecl(
2111 cast<ObjCObjectPointerType>(Ty)->getPointeeType());
Adrian Prantla03a85a2013-03-06 22:03:30 +00002112 case Type::ObjCInterface:
2113 return cast<ObjCInterfaceType>(Ty)->getDecl();
2114 default:
Craig Topper8a13c412014-05-21 05:09:00 +00002115 return nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002116 }
2117}
2118
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002119llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002120 // Handle qualifiers, which recursively handles what they refer to.
2121 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002122 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002123
Guy Benyei11169dd2012-12-18 14:30:41 +00002124 // Work out details of type.
2125 switch (Ty->getTypeClass()) {
2126#define TYPE(Class, Base)
2127#define ABSTRACT_TYPE(Class, Base)
2128#define NON_CANONICAL_TYPE(Class, Base)
2129#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2130#include "clang/AST/TypeNodes.def"
2131 llvm_unreachable("Dependent types cannot show up in debug information");
2132
2133 case Type::ExtVector:
2134 case Type::Vector:
2135 return CreateType(cast<VectorType>(Ty), Unit);
2136 case Type::ObjCObjectPointer:
2137 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2138 case Type::ObjCObject:
2139 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2140 case Type::ObjCInterface:
2141 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2142 case Type::Builtin:
2143 return CreateType(cast<BuiltinType>(Ty));
2144 case Type::Complex:
2145 return CreateType(cast<ComplexType>(Ty));
2146 case Type::Pointer:
2147 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002148 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002149 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002150 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002151 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002152 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002153 case Type::BlockPointer:
2154 return CreateType(cast<BlockPointerType>(Ty), Unit);
2155 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002156 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002157 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002158 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002159 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002160 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002161 case Type::FunctionProto:
2162 case Type::FunctionNoProto:
2163 return CreateType(cast<FunctionType>(Ty), Unit);
2164 case Type::ConstantArray:
2165 case Type::VariableArray:
2166 case Type::IncompleteArray:
2167 return CreateType(cast<ArrayType>(Ty), Unit);
2168
2169 case Type::LValueReference:
2170 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2171 case Type::RValueReference:
2172 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2173
2174 case Type::MemberPointer:
2175 return CreateType(cast<MemberPointerType>(Ty), Unit);
2176
2177 case Type::Atomic:
2178 return CreateType(cast<AtomicType>(Ty), Unit);
2179
Guy Benyei11169dd2012-12-18 14:30:41 +00002180 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002181 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2182
David Blaikie42edade2014-11-11 20:44:45 +00002183 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002184 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002185 case Type::Elaborated:
2186 case Type::Paren:
2187 case Type::SubstTemplateTypeParm:
2188 case Type::TypeOfExpr:
2189 case Type::TypeOf:
2190 case Type::Decltype:
2191 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002192 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002193 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002194 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002195
David Blaikie42edade2014-11-11 20:44:45 +00002196 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002197}
2198
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002199llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2200 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002201 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002202
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002203 auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002204
2205 // We may have cached a forward decl when we could have created
2206 // a non-forward decl. Go ahead and create a non-forward decl
2207 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002208 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002209 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002210
2211 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002212 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002213
2214 // Propagate members from the declaration to the definition
2215 // CreateType(const RecordType*) will overwrite this with the members in the
2216 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002217 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002218
Guy Benyei11169dd2012-12-18 14:30:41 +00002219 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002220 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002221 return Res;
2222}
2223
2224// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002225llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002226 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002227
Guy Benyei11169dd2012-12-18 14:30:41 +00002228 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002229 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002230 unsigned Line = getLineNumber(RD->getLocation());
2231 StringRef RDName = getClassName(RD);
2232
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002233 llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002234
David Blaikied2785892013-08-18 17:36:19 +00002235 // If we ended up creating the type during the context chain construction,
2236 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002237 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002238 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002239 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002240 return T;
David Blaikied2785892013-08-18 17:36:19 +00002241
Adrian Prantl381e7552014-02-04 21:29:50 +00002242 // If this is just a forward or incomplete declaration, construct an
2243 // appropriately marked node and just return it.
2244 const RecordDecl *D = RD->getDefinition();
2245 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002246 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002247
2248 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2249 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002250
Manman Rene0064d82013-08-29 23:19:58 +00002251 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2252
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002253 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002254 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align, 0,
2255 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002256
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002257 RegionMap[Ty->getDecl()].reset(RealDecl);
2258 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002259
David Blaikieadfbf992013-08-18 16:55:33 +00002260 if (const ClassTemplateSpecializationDecl *TSpecial =
2261 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002262 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002263 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002264 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002265}
2266
David Blaikieadfbf992013-08-18 16:55:33 +00002267void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002268 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002269 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002270 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002271 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2272 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002273 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002274 while (1) {
2275 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2276 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2277 if (PBT && !BRL.isPrimaryBaseVirtual())
2278 PBase = PBT;
2279 else
2280 break;
2281 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002282 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002283 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2284 getOrCreateFile(RD->getLocation())));
2285 } else if (RD->isDynamicClass())
2286 ContainingType = RealDecl;
2287
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002288 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002289}
2290
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002291llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002292 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002293 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002294 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2295 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002296 llvm::DIType *Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002297 FieldAlign, *Offset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002298 *Offset += FieldSize;
2299 return Ty;
2300}
2301
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002302void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002303 StringRef &Name,
2304 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002305 llvm::DIScope *&FDContext,
2306 llvm::DINodeArray &TParamsArray,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002307 unsigned &Flags) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002308 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2309 Name = getFunctionName(FD);
2310 // Use mangled name as linkage name for C/C++ functions.
2311 if (FD->hasPrototype()) {
2312 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002313 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002314 }
2315 // No need to replicate the linkage name if it isn't different from the
2316 // subprogram name, no need to have it at all unless coverage is enabled or
2317 // debug is set to more than just line tables.
2318 if (LinkageName == Name ||
2319 (!CGM.getCodeGenOpts().EmitGcovArcs &&
2320 !CGM.getCodeGenOpts().EmitGcovNotes &&
2321 DebugKind <= CodeGenOptions::DebugLineTablesOnly))
2322 LinkageName = StringRef();
2323
2324 if (DebugKind >= CodeGenOptions::LimitedDebugInfo) {
2325 if (const NamespaceDecl *NSDecl =
2326 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2327 FDContext = getOrCreateNameSpace(NSDecl);
2328 else if (const RecordDecl *RDecl =
2329 dyn_cast_or_null<RecordDecl>(FD->getDeclContext()))
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002330 FDContext = getContextDescriptor(RDecl, TheCU);
Frederic Riss9db79f12014-11-18 03:40:46 +00002331 // Collect template parameters.
2332 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2333 }
2334}
2335
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002336void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002337 unsigned &LineNo, QualType &T,
2338 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002339 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002340 Unit = getOrCreateFile(VD->getLocation());
2341 LineNo = getLineNumber(VD->getLocation());
2342
2343 setLocation(VD->getLocation());
2344
2345 T = VD->getType();
2346 if (T->isIncompleteArrayType()) {
2347 // CodeGen turns int[] into int[1] so we'll do the same here.
2348 llvm::APInt ConstVal(32, 1);
2349 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2350
2351 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2352 ArrayType::Normal, 0);
2353 }
2354
2355 Name = VD->getName();
2356 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2357 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2358 LinkageName = CGM.getMangledName(VD);
2359 if (LinkageName == Name)
2360 LinkageName = StringRef();
2361
2362 // Since we emit declarations (DW_AT_members) for static members, place the
2363 // definition of those static members in the namespace they were declared in
2364 // in the source code (the lexical decl context).
2365 // FIXME: Generalize this for even non-member global variables where the
2366 // declaration and definition may have different lexical decl contexts, once
2367 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002368 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2369 : VD->getDeclContext();
2370 // When a record type contains an in-line initialization of a static data
2371 // member, and the record type is marked as __declspec(dllexport), an implicit
2372 // definition of the member will be created in the record context. DWARF
2373 // doesn't seem to have a nice way to describe this in a form that consumers
2374 // are likely to understand, so fake the "normal" situation of a definition
2375 // outside the class by putting it in the global scope.
2376 if (DC->isRecord())
2377 DC = CGM.getContext().getTranslationUnitDecl();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002378 VDContext = getContextDescriptor(cast<Decl>(DC), TheCU);
Frederic Riss9db79f12014-11-18 03:40:46 +00002379}
2380
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002381llvm::DISubprogram *
Frederic Rissd253ed62014-11-18 03:40:51 +00002382CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002383 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00002384 StringRef Name, LinkageName;
2385 unsigned Flags = 0;
2386 SourceLocation Loc = FD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002387 llvm::DIFile *Unit = getOrCreateFile(Loc);
2388 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002389 unsigned Line = getLineNumber(Loc);
2390
2391 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2392 TParamsArray, Flags);
2393 // Build function type.
2394 SmallVector<QualType, 16> ArgTypes;
2395 for (const ParmVarDecl *Parm: FD->parameters())
2396 ArgTypes.push_back(Parm->getType());
2397 QualType FnType =
2398 CGM.getContext().getFunctionType(FD->getReturnType(), ArgTypes,
2399 FunctionProtoType::ExtProtoInfo());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002400 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002401 DContext, Name, LinkageName, Unit, Line,
2402 getOrCreateFunctionType(FD, FnType, Unit), !FD->isExternallyVisible(),
Duncan P. N. Exon Smith31d38f72015-08-26 22:21:09 +00002403 /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize, nullptr,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002404 TParamsArray.get(), getFunctionDeclaration(FD));
Frederic Rissd253ed62014-11-18 03:40:51 +00002405 const FunctionDecl *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002406 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2407 std::make_tuple(CanonDecl),
2408 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00002409 return SP;
2410}
2411
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002412llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00002413CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2414 QualType T;
2415 StringRef Name, LinkageName;
2416 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002417 llvm::DIFile *Unit = getOrCreateFile(Loc);
2418 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002419 unsigned Line = getLineNumber(Loc);
2420
2421 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002422 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
2423 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
2424 !VD->isExternallyVisible(), nullptr, nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002425 FwdDeclReplaceMap.emplace_back(
2426 std::piecewise_construct,
2427 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2428 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002429 return GV;
2430}
2431
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002432llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002433 // We only need a declaration (not a definition) of the type - so use whatever
2434 // we would otherwise do to get a type for a pointee. (forward declarations in
2435 // limited debug info, full definitions (if the type definition is available)
2436 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002437 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2438 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002439 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002440 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002441
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002442 if (I != DeclCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002443 return dyn_cast_or_null<llvm::DINode>(I->second);
Frederic Rissd253ed62014-11-18 03:40:51 +00002444
2445 // No definition for now. Emit a forward definition that might be
2446 // merged with a potential upcoming definition.
2447 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
2448 return getFunctionForwardDeclaration(FD);
2449 else if (const auto *VD = dyn_cast<VarDecl>(D))
2450 return getGlobalVariableForwardDeclaration(VD);
2451
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002452 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00002453}
2454
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002455llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Diego Novillo913690c2014-06-24 17:02:17 +00002456 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002457 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00002458
Guy Benyei11169dd2012-12-18 14:30:41 +00002459 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002460 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002461 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002462
2463 // Setup context.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002464 auto *S = getDeclContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00002465
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002466 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002467 if (MI == SPCache.end()) {
Eric Christopherf86c4052013-08-28 23:12:10 +00002468 if (const CXXMethodDecl *MD =
2469 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00002470 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002471 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00002472 }
2473 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002474 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002475 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002476 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002477 return SP;
2478 }
2479
Aaron Ballman86c93902014-03-06 23:45:36 +00002480 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002481 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002482 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002483 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002484 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002485 return SP;
2486 }
2487 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002488 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002489}
2490
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002491// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00002492// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002493llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002494 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002495 llvm::DIFile *F) {
Diego Novillo913690c2014-06-24 17:02:17 +00002496 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002497 // Create fake but valid subroutine type. Otherwise -verify would fail, and
2498 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Manman Ren67f005e2014-07-28 22:24:34 +00002499 return DBuilder.createSubroutineType(F,
2500 DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002501
2502 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2503 return getOrCreateMethodType(Method, F);
2504 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2505 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002506 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002507
2508 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002509 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002510
2511 // Replace the instancetype keyword with the actual type.
2512 if (ResultTy == CGM.getContext().getObjCInstanceType())
2513 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002514 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002515
Adrian Prantl7bec9032013-05-10 21:08:31 +00002516 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002517 // "self" pointer is always first argument.
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002518 QualType SelfDeclTy;
2519 if (auto *SelfDecl = OMethod->getSelfDecl())
2520 SelfDeclTy = SelfDecl->getType();
2521 else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType))
2522 if (FPT->getNumParams() > 1)
2523 SelfDeclTy = FPT->getParamType(0);
2524 if (!SelfDeclTy.isNull())
2525 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002526 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002527 Elts.push_back(DBuilder.createArtificialType(
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002528 getOrCreateType(CGM.getContext().getObjCSelType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002529 // Get rest of the arguments.
Aaron Ballman43b68be2014-03-07 17:50:17 +00002530 for (const auto *PI : OMethod->params())
2531 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002532 // Variadic methods need a special marker at the end of the type list.
2533 if (OMethod->isVariadic())
2534 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002535
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002536 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002537 return DBuilder.createSubroutineType(F, EltTypeArray);
2538 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002539
Adrian Prantl800faef2014-02-25 23:42:18 +00002540 // Handle variadic function types; they need an additional
2541 // unspecified parameter.
Adrian Prantld45ba252014-02-25 19:38:11 +00002542 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2543 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002544 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00002545 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2546 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2547 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2548 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2549 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002550 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Adrian Prantld45ba252014-02-25 19:38:11 +00002551 return DBuilder.createSubroutineType(F, EltTypeArray);
2552 }
2553
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002554 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002555}
2556
Eric Christophere7b87e52014-10-26 23:40:33 +00002557void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2558 SourceLocation ScopeLoc, QualType FnType,
2559 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002560
2561 StringRef Name;
2562 StringRef LinkageName;
2563
2564 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2565
2566 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002567 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00002568
Guy Benyei11169dd2012-12-18 14:30:41 +00002569 unsigned Flags = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002570 llvm::DIFile *Unit = getOrCreateFile(Loc);
2571 llvm::DIScope *FDContext = Unit;
2572 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00002573 if (!HasDecl) {
2574 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00002575 LinkageName = Fn->getName();
Guy Benyei11169dd2012-12-18 14:30:41 +00002576 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002577 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002578 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002579 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002580 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002581 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002582 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002583 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002584 return;
2585 }
2586 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002587 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2588 TParamsArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002589 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2590 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002591 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002592 } else {
2593 // Use llvm function name.
2594 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002595 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002596 }
2597 if (!Name.empty() && Name[0] == '\01')
2598 Name = Name.substr(1);
2599
Adrian Prantl42d71b92014-04-10 23:21:53 +00002600 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002601 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl42d71b92014-04-10 23:21:53 +00002602 // Artificial functions without a location should not silently reuse CurLoc.
2603 if (Loc.isInvalid())
2604 CurLoc = SourceLocation();
2605 }
2606 unsigned LineNo = getLineNumber(Loc);
2607 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002608
Eric Christopher8018e412014-03-27 18:50:35 +00002609 // FIXME: The function declaration we're constructing here is mostly reusing
2610 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2611 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2612 // all subprograms instead of the actual context since subprogram definitions
2613 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002614 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00002615 FDContext, Name, LinkageName, Unit, LineNo,
2616 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
2617 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize, Fn,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002618 TParamsArray.get(), getFunctionDeclaration(D));
Frederic Rissb1ab28c2014-11-05 19:19:04 +00002619 // We might get here with a VarDecl in the case we're generating
2620 // code for the initialization of globals. Do not record these decls
2621 // as they will overwrite the actual VarDecl Decl in the cache.
2622 if (HasDecl && isa<FunctionDecl>(D))
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002623 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(SP));
Guy Benyei11169dd2012-12-18 14:30:41 +00002624
Adrian Prantlbebb8932014-03-21 21:01:58 +00002625 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002626 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00002627
Guy Benyei11169dd2012-12-18 14:30:41 +00002628 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002629 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002630}
2631
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002632void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
2633 QualType FnType) {
2634 StringRef Name;
2635 StringRef LinkageName;
2636
2637 const Decl *D = GD.getDecl();
2638 if (!D)
2639 return;
2640
2641 unsigned Flags = 0;
2642 llvm::DIFile *Unit = getOrCreateFile(Loc);
2643 llvm::DIScope *FDContext = Unit;
2644 llvm::DINodeArray TParamsArray;
2645 if (isa<FunctionDecl>(D)) {
2646 // If there is a DISubprogram for this function available then use it.
2647 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2648 TParamsArray, Flags);
2649 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2650 Name = getObjCMethodName(OMD);
2651 Flags |= llvm::DINode::FlagPrototyped;
2652 } else {
2653 llvm_unreachable("not a function or ObjC method");
2654 }
2655 if (!Name.empty() && Name[0] == '\01')
2656 Name = Name.substr(1);
2657
2658 if (D->isImplicit()) {
2659 Flags |= llvm::DINode::FlagArtificial;
2660 // Artificial functions without a location should not silently reuse CurLoc.
2661 if (Loc.isInvalid())
2662 CurLoc = SourceLocation();
2663 }
2664 unsigned LineNo = getLineNumber(Loc);
2665 unsigned ScopeLine = 0;
2666
2667 DBuilder.createFunction(FDContext, Name, LinkageName, Unit, LineNo,
2668 getOrCreateFunctionType(D, FnType, Unit),
2669 false /*internalLinkage*/, true /*definition*/,
2670 ScopeLine, Flags, CGM.getLangOpts().Optimize, nullptr,
2671 TParamsArray.get(),
2672 getFunctionDeclaration(D));
2673}
2674
David Blaikie835afb22015-01-21 23:08:17 +00002675void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002676 // Update our current location
2677 setLocation(Loc);
2678
Eric Christophere7b87e52014-10-26 23:40:33 +00002679 if (CurLoc.isInvalid() || CurLoc.isMacroID())
2680 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002681
Adrian Prantle83b1302014-01-07 22:05:52 +00002682 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00002683 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
David Blaikie835afb22015-01-21 23:08:17 +00002684 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002685}
2686
Guy Benyei11169dd2012-12-18 14:30:41 +00002687void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002688 llvm::MDNode *Back = nullptr;
2689 if (!LexicalBlockStack.empty())
2690 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002691 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002692 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002693 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002694}
2695
Eric Christopher0fdcb312013-05-16 00:52:20 +00002696void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2697 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002698 // Set our current location.
2699 setLocation(Loc);
2700
Guy Benyei11169dd2012-12-18 14:30:41 +00002701 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00002702 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2703 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00002704
2705 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2706 return;
2707
2708 // Create a new lexical block and push it on the stack.
2709 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002710}
2711
Eric Christopher0fdcb312013-05-16 00:52:20 +00002712void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2713 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002714 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2715
2716 // Provide an entry in the line table for the end of the block.
2717 EmitLocation(Builder, Loc);
2718
David Blaikie60a877b2014-10-22 19:34:33 +00002719 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2720 return;
2721
Guy Benyei11169dd2012-12-18 14:30:41 +00002722 LexicalBlockStack.pop_back();
2723}
2724
Guy Benyei11169dd2012-12-18 14:30:41 +00002725void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2726 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2727 unsigned RCount = FnBeginRegionCount.back();
2728 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2729
2730 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00002731 while (LexicalBlockStack.size() != RCount) {
2732 // Provide an entry in the line table for the end of the block.
2733 EmitLocation(Builder, CurLoc);
2734 LexicalBlockStack.pop_back();
2735 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002736 FnBeginRegionCount.pop_back();
2737}
2738
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002739llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002740 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002741
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002742 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002743 QualType FType;
2744 uint64_t FieldSize, FieldOffset;
2745 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002746
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002747 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002748 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002749
2750 FieldOffset = 0;
2751 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2752 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2753 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2754 FType = CGM.getContext().IntTy;
2755 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2756 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2757
2758 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2759 if (HasCopyAndDispose) {
2760 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002761 EltTys.push_back(
2762 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
2763 EltTys.push_back(
2764 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00002765 }
2766 bool HasByrefExtendedLayout;
2767 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00002768 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
2769 HasByrefExtendedLayout) &&
2770 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00002771 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002772 EltTys.push_back(
2773 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00002774 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002775
Guy Benyei11169dd2012-12-18 14:30:41 +00002776 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2777 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002778 CGM.getTarget().getPointerAlign(0))) {
2779 CharUnits FieldOffsetInBytes =
2780 CGM.getContext().toCharUnitsFromBits(FieldOffset);
2781 CharUnits AlignedOffsetInBytes =
2782 FieldOffsetInBytes.RoundUpToAlignment(Align);
2783 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002784
Guy Benyei11169dd2012-12-18 14:30:41 +00002785 if (NumPaddingBytes.isPositive()) {
2786 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
2787 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2788 pad, ArrayType::Normal, 0);
2789 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
2790 }
2791 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002792
Guy Benyei11169dd2012-12-18 14:30:41 +00002793 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002794 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002795 FieldSize = CGM.getContext().getTypeSize(FType);
2796 FieldAlign = CGM.getContext().toBits(Align);
2797
Eric Christopherb2a008c2013-05-16 00:45:12 +00002798 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00002799 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
2800 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002801 EltTys.push_back(FieldTy);
2802 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002803
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002804 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002805
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002806 unsigned Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002807
Guy Benyei11169dd2012-12-18 14:30:41 +00002808 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002809 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002810}
2811
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002812void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::Value *Storage,
2813 llvm::Optional<unsigned> ArgNo,
Eric Christophere7b87e52014-10-26 23:40:33 +00002814 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002815 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002816 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2817
David Blaikie7fceebf2013-08-19 03:37:48 +00002818 bool Unwritten =
2819 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
2820 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002821 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00002822 if (!Unwritten)
2823 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002824 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00002825 uint64_t XOffset = 0;
2826 if (VD->hasAttr<BlocksAttr>())
2827 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002828 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002829 Ty = getOrCreateType(VD->getType(), Unit);
2830
2831 // If there is no debug info for this type then do not emit debug info
2832 // for this variable.
2833 if (!Ty)
2834 return;
2835
Guy Benyei11169dd2012-12-18 14:30:41 +00002836 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00002837 unsigned Line = 0;
2838 unsigned Column = 0;
2839 if (!Unwritten) {
2840 Line = getLineNumber(VD->getLocation());
2841 Column = getColumnNumber(VD->getLocation());
2842 }
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002843 SmallVector<int64_t, 9> Expr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002844 unsigned Flags = 0;
2845 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002846 Flags |= llvm::DINode::FlagArtificial;
Guy Benyei11169dd2012-12-18 14:30:41 +00002847 // If this is the first argument and it is implicit then
2848 // give it an object pointer flag.
2849 // FIXME: There has to be a better way to do this, but for static
2850 // functions there won't be an implicit param at arg1 and
2851 // otherwise it is 'self' or 'this'.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002852 if (isa<ImplicitParamDecl>(VD) && ArgNo && *ArgNo == 1)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002853 Flags |= llvm::DINode::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00002854 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00002855 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
2856 !VD->getType()->isPointerType())
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002857 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei11169dd2012-12-18 14:30:41 +00002858
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002859 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00002860
2861 StringRef Name = VD->getName();
2862 if (!Name.empty()) {
2863 if (VD->hasAttr<BlocksAttr>()) {
2864 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002865 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002866 // offset of __forwarding field
2867 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002868 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002869 Expr.push_back(offset.getQuantity());
2870 Expr.push_back(llvm::dwarf::DW_OP_deref);
2871 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002872 // offset of x field
2873 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002874 Expr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002875
2876 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002877 auto *D = ArgNo
2878 ? DBuilder.createParameterVariable(Scope, VD->getName(),
2879 *ArgNo, Unit, Line, Ty)
2880 : DBuilder.createAutoVariable(Scope, VD->getName(), Unit,
2881 Line, Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002882
Guy Benyei11169dd2012-12-18 14:30:41 +00002883 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002884 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2885 llvm::DebugLoc::get(Line, Column, Scope),
2886 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002887 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00002888 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002889 Expr.push_back(llvm::dwarf::DW_OP_deref);
Adrian Prantl0d820892015-04-29 15:05:50 +00002890 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2891 // If VD is an anonymous union then Storage represents value for
2892 // all union fields.
2893 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
2894 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00002895 // GDB has trouble finding local variables in anonymous unions, so we emit
2896 // artifical local variables for each of the members.
2897 //
2898 // FIXME: Remove this code as soon as GDB supports this.
2899 // The debug info verifier in LLVM operates based on the assumption that a
2900 // variable has the same size as its storage and we had to disable the check
2901 // for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00002902 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002903 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00002904 StringRef FieldName = Field->getName();
2905
2906 // Ignore unnamed fields. Do not ignore unnamed records.
2907 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2908 continue;
2909
2910 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002911 auto *D = DBuilder.createAutoVariable(
2912 Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
2913 Flags | llvm::DINode::FlagArtificial);
Adrian Prantl0d820892015-04-29 15:05:50 +00002914
2915 // Insert an llvm.dbg.declare into the current block.
2916 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2917 llvm::DebugLoc::get(Line, Column, Scope),
2918 Builder.GetInsertBlock());
2919 }
Adrian Prantl0d820892015-04-29 15:05:50 +00002920 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002921 }
David Blaikiea76a7c92013-01-05 05:58:35 +00002922
2923 // Create the descriptor for the variable.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002924 auto *D =
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002925 ArgNo
2926 ? DBuilder.createParameterVariable(Scope, Name, *ArgNo, Unit, Line,
2927 Ty, CGM.getLangOpts().Optimize,
2928 Flags)
2929 : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
2930 CGM.getLangOpts().Optimize, Flags);
David Blaikiea76a7c92013-01-05 05:58:35 +00002931
2932 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002933 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2934 llvm::DebugLoc::get(Line, Column, Scope),
2935 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002936}
2937
2938void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
2939 llvm::Value *Storage,
2940 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002941 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002942 EmitDeclare(VD, Storage, llvm::None, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00002943}
2944
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002945llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
2946 llvm::DIType *Ty) {
2947 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002948 if (CachedTy)
2949 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00002950 return DBuilder.createObjectPointerType(Ty);
2951}
2952
Eric Christophere7b87e52014-10-26 23:40:33 +00002953void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
2954 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00002955 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Eric Christopher75e17682013-05-16 00:45:23 +00002956 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002957 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00002958
Craig Topper8a13c412014-05-21 05:09:00 +00002959 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00002960 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002961
Guy Benyei11169dd2012-12-18 14:30:41 +00002962 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002963
Guy Benyei11169dd2012-12-18 14:30:41 +00002964 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002965 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
2966 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00002967 if (isByRef)
2968 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002969 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002970 Ty = getOrCreateType(VD->getType(), Unit);
2971
2972 // Self is passed along as an implicit non-arg variable in a
2973 // block. Mark it as the object pointer.
2974 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00002975 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00002976
2977 // Get location information.
2978 unsigned Line = getLineNumber(VD->getLocation());
2979 unsigned Column = getColumnNumber(VD->getLocation());
2980
2981 const llvm::DataLayout &target = CGM.getDataLayout();
2982
2983 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00002984 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00002985 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
2986
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002987 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00002988 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002989 addr.push_back(llvm::dwarf::DW_OP_deref);
2990 addr.push_back(llvm::dwarf::DW_OP_plus);
2991 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002992 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002993 addr.push_back(llvm::dwarf::DW_OP_deref);
2994 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002995 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00002996 offset =
2997 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002998 addr.push_back(offset.getQuantity());
2999 addr.push_back(llvm::dwarf::DW_OP_deref);
3000 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003001 // offset of x field
3002 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003003 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003004 }
3005
3006 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003007 auto *D = DBuilder.createAutoVariable(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003008 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003009 Line, Ty);
Adrian Prantl0f6df002013-03-29 19:20:35 +00003010
Guy Benyei11169dd2012-12-18 14:30:41 +00003011 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003012 auto DL = llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back());
3013 if (InsertPoint)
3014 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3015 InsertPoint);
3016 else
3017 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3018 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003019}
3020
Guy Benyei11169dd2012-12-18 14:30:41 +00003021void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3022 unsigned ArgNo,
3023 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003024 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003025 EmitDeclare(VD, AI, ArgNo, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003026}
3027
3028namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003029struct BlockLayoutChunk {
3030 uint64_t OffsetInBits;
3031 const BlockDecl::Capture *Capture;
3032};
3033bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3034 return l.OffsetInBits < r.OffsetInBits;
3035}
Guy Benyei11169dd2012-12-18 14:30:41 +00003036}
3037
3038void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003039 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003040 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003041 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003042 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003043 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003044 ASTContext &C = CGM.getContext();
3045 const BlockDecl *blockDecl = block.getBlockDecl();
3046
3047 // Collect some general information about the block's location.
3048 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003049 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003050 unsigned line = getLineNumber(loc);
3051 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003052
Guy Benyei11169dd2012-12-18 14:30:41 +00003053 // Build the debug-info type for the block literal.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003054 getDeclContextDescriptor(blockDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003055
3056 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003057 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003058
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003059 SmallVector<llvm::Metadata *, 16> fields;
Guy Benyei11169dd2012-12-18 14:30:41 +00003060 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
3061 blockLayout->getElementOffsetInBits(0),
3062 tunit, tunit));
3063 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
3064 blockLayout->getElementOffsetInBits(1),
3065 tunit, tunit));
3066 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
3067 blockLayout->getElementOffsetInBits(2),
3068 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003069 auto *FnTy = block.getBlockExpr()->getFunctionType();
3070 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
3071 fields.push_back(createFieldType("__FuncPtr", FnPtrType, 0, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003072 blockLayout->getElementOffsetInBits(3),
3073 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003074 fields.push_back(createFieldType(
3075 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3076 ? C.getBlockDescriptorExtendedType()
3077 : C.getBlockDescriptorType()),
3078 0, loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003079
3080 // We want to sort the captures by offset, not because DWARF
3081 // requires this, but because we're paranoid about debuggers.
3082 SmallVector<BlockLayoutChunk, 8> chunks;
3083
3084 // 'this' capture.
3085 if (blockDecl->capturesCXXThis()) {
3086 BlockLayoutChunk chunk;
3087 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003088 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003089 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003090 chunks.push_back(chunk);
3091 }
3092
3093 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003094 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003095 const VarDecl *variable = capture.getVariable();
3096 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3097
3098 // Ignore constant captures.
3099 if (captureInfo.isConstant())
3100 continue;
3101
3102 BlockLayoutChunk chunk;
3103 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003104 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003105 chunk.Capture = &capture;
3106 chunks.push_back(chunk);
3107 }
3108
3109 // Sort by offset.
3110 llvm::array_pod_sort(chunks.begin(), chunks.end());
3111
Eric Christophere7b87e52014-10-26 23:40:33 +00003112 for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
3113 e = chunks.end();
3114 i != e; ++i) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003115 uint64_t offsetInBits = i->OffsetInBits;
3116 const BlockDecl::Capture *capture = i->Capture;
3117
3118 // If we have a null capture, this must be the C++ 'this' capture.
3119 if (!capture) {
3120 const CXXMethodDecl *method =
Eric Christophere7b87e52014-10-26 23:40:33 +00003121 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00003122 QualType type = method->getThisType(C);
3123
3124 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
3125 offsetInBits, tunit, tunit));
3126 continue;
3127 }
3128
3129 const VarDecl *variable = capture->getVariable();
3130 StringRef name = variable->getName();
3131
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003132 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003133 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003134 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003135
3136 // FIXME: this creates a second copy of this type!
3137 uint64_t xoffset;
3138 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003139 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3140 fieldType =
3141 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3142 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003143 } else {
Eric Christophere7b87e52014-10-26 23:40:33 +00003144 fieldType = createFieldType(name, variable->getType(), 0, loc, AS_public,
3145 offsetInBits, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003146 }
3147 fields.push_back(fieldType);
3148 }
3149
3150 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003151 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3152 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003153
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003154 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003155
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003156 llvm::DIType *type = DBuilder.createStructType(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003157 tunit, typeName.str(), tunit, line,
3158 CGM.getContext().toBits(block.BlockSize),
3159 CGM.getContext().toBits(block.BlockAlign), 0, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003160 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3161
3162 // Get overall information about the block.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003163 unsigned flags = llvm::DINode::FlagArtificial;
3164 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003165
3166 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003167 auto *debugVar = DBuilder.createParameterVariable(
3168 scope, Arg->getName(), ArgNo, tunit, line, type,
3169 CGM.getLangOpts().Optimize, flags);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003170
Adrian Prantl616bef42013-03-14 21:52:59 +00003171 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003172 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003173 DBuilder.insertDbgValueIntrinsic(
Eric Christophere7b87e52014-10-26 23:40:33 +00003174 LocalAddr, 0, debugVar, DBuilder.createExpression(),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003175 llvm::DebugLoc::get(line, column, scope), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003176 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003177
Adrian Prantl616bef42013-03-14 21:52:59 +00003178 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003179 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
3180 llvm::DebugLoc::get(line, column, scope),
3181 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003182}
3183
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003184llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003185CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3186 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003187 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003188
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003189 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003190 if (MI != StaticDataMemberCache.end()) {
3191 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00003192 return MI->second;
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003193 }
David Blaikiece763042013-08-20 21:49:21 +00003194
3195 // If the member wasn't found in the cache, lazily construct and add it to the
3196 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003197 auto DC = D->getDeclContext();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003198 auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003199 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003200}
3201
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003202llvm::DIGlobalVariable *CGDebugInfo::CollectAnonRecordDecls(
3203 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3204 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
3205 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003206
3207 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003208 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003209 StringRef FieldName = Field->getName();
3210
3211 // Ignore unnamed fields, but recurse into anonymous records.
3212 if (FieldName.empty()) {
3213 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3214 if (RT)
3215 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3216 Var, DContext);
3217 continue;
3218 }
3219 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003220 GV = DBuilder.createGlobalVariable(DContext, FieldName, LinkageName, Unit,
3221 LineNo, FieldTy,
3222 Var->hasInternalLinkage(), Var, nullptr);
Eric Christophercab9fae2014-04-10 05:20:00 +00003223 }
3224 return GV;
3225}
3226
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003227void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003228 const VarDecl *D) {
Eric Christopher75e17682013-05-16 00:45:23 +00003229 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003230 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003231 llvm::DIFile *Unit = nullptr;
3232 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003233 unsigned LineNo;
3234 StringRef DeclName, LinkageName;
3235 QualType T;
3236 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003237
3238 // Attempt to store one global variable for the declaration - even if we
3239 // emit a lot of fields.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003240 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003241
3242 // If this is an anonymous union then we'll want to emit a global
3243 // variable for each member of the anonymous union so that it's possible
3244 // to find the name of any field in the union.
3245 if (T->isUnionType() && DeclName.empty()) {
3246 const RecordDecl *RD = cast<RecordType>(T)->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003247 assert(RD->isAnonymousStructOrUnion() &&
3248 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003249 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3250 } else {
David Blaikie7550b112014-10-20 17:42:23 +00003251 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003252 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3253 Var->hasInternalLinkage(), Var,
3254 getOrCreateStaticDataMemberDeclarationOrNull(D));
3255 }
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003256 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(GV));
Guy Benyei11169dd2012-12-18 14:30:41 +00003257}
3258
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003259void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3260 llvm::Constant *Init) {
Eric Christopher75e17682013-05-16 00:45:23 +00003261 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003262 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003263 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003264 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003265 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003266 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3267 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3268 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3269 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3270 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003271 // Do not use global variables for enums.
3272 //
3273 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003274 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003275 return;
David Blaikiea15565562014-04-04 20:56:17 +00003276 // Do not emit separate definitions for function local const/statics.
3277 if (isa<FunctionDecl>(VD->getDeclContext()))
3278 return;
David Blaikiebb113912014-04-05 07:23:17 +00003279 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003280 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003281 if (VarD->isStaticDataMember()) {
3282 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003283 getDeclContextDescriptor(VarD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003284 // Ensure that the type is retained even though it's otherwise unreferenced.
3285 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003286 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003287 return;
3288 }
3289
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003290 llvm::DIScope *DContext = getDeclContextDescriptor(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003291
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003292 auto &GV = DeclCache[VD];
3293 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003294 return;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003295 GV.reset(DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003296 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003297 true, Init, getOrCreateStaticDataMemberDeclarationOrNull(VarD)));
David Blaikiebd483762013-05-20 04:58:53 +00003298}
3299
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003300llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003301 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003302 return LexicalBlockStack.back();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003303 return getContextDescriptor(D, TheCU);
Guy Benyei11169dd2012-12-18 14:30:41 +00003304}
3305
David Blaikie9f88fe82013-04-22 06:13:21 +00003306void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
David Blaikiebd483762013-05-20 04:58:53 +00003307 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3308 return;
David Blaikie9f88fe82013-04-22 06:13:21 +00003309 DBuilder.createImportedModule(
David Blaikiebd483762013-05-20 04:58:53 +00003310 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3311 getOrCreateNameSpace(UD.getNominatedNamespace()),
David Blaikie9f88fe82013-04-22 06:13:21 +00003312 getLineNumber(UD.getLocation()));
3313}
3314
David Blaikiebd483762013-05-20 04:58:53 +00003315void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
3316 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3317 return;
3318 assert(UD.shadow_size() &&
3319 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3320 // Emitting one decl is sufficient - debuggers can detect that this is an
3321 // overloaded name & provide lookup for all the overloads.
3322 const UsingShadowDecl &USD = **UD.shadow_begin();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003323 if (llvm::DINode *Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003324 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003325 DBuilder.createImportedDeclaration(
3326 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3327 getLineNumber(USD.getLocation()));
3328}
3329
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003330void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
3331 auto *Reader = CGM.getContext().getExternalSource();
3332 auto Info = Reader->getSourceDescriptor(*ID.getImportedModule());
3333 DBuilder.createImportedDeclaration(
3334 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
3335 getOrCreateModuleRef(Info),
3336 getLineNumber(ID.getLocation()));
3337}
3338
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003339llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00003340CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
3341 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003342 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003343 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003344 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003345 return cast<llvm::DIImportedEntity>(VH);
3346 llvm::DIImportedEntity *R;
David Blaikief121b932013-05-20 22:50:41 +00003347 if (const NamespaceAliasDecl *Underlying =
3348 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3349 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003350 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003351 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3352 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3353 NA.getName());
3354 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003355 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003356 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3357 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3358 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003359 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003360 return R;
3361}
3362
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003363llvm::DINamespace *
Guy Benyei11169dd2012-12-18 14:30:41 +00003364CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003365 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003366 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003367 if (I != NameSpaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003368 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003369
Guy Benyei11169dd2012-12-18 14:30:41 +00003370 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003371 llvm::DIFile *FileD = getOrCreateFile(NSDecl->getLocation());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003372 llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003373 llvm::DINamespace *NS =
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003374 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003375 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003376 return NS;
3377}
3378
3379void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003380 // Creating types might create further types - invalidating the current
3381 // element and the size(), so don't cache/reference them.
3382 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3383 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003384 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003385 ? CreateTypeDefinition(E.Type, E.Unit)
3386 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003387 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00003388 }
3389
David Blaikief427b002014-05-06 03:42:01 +00003390 for (auto p : ReplaceMap) {
3391 assert(p.second);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003392 auto *Ty = cast<llvm::DIType>(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003393 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003394
David Blaikief427b002014-05-06 03:42:01 +00003395 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003396 assert(it != TypeCache.end());
3397 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003398
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003399 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
3400 cast<llvm::DIType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00003401 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003402
Frederic Rissd253ed62014-11-18 03:40:51 +00003403 for (const auto &p : FwdDeclReplaceMap) {
3404 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003405 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003406 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003407
3408 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003409 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003410 // with ourselves, that will destroy the temporary MDNode and
3411 // replace it with a standard one, avoiding leaking memory.
3412 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003413 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003414 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003415 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003416
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003417 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00003418 }
3419
Adrian Prantl73409ce2013-03-11 18:33:46 +00003420 // We keep our own list of retained types, because we need to look
3421 // up the final type in the type cache.
Adrian Prantl3a884fa2015-08-27 22:56:46 +00003422 for (auto &RT : RetainedTypes)
Adrian Prantlad9a195e2015-08-27 21:21:19 +00003423 if (auto MD = TypeCache[RT])
3424 DBuilder.retainType(cast<llvm::DIType>(MD));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003425
Guy Benyei11169dd2012-12-18 14:30:41 +00003426 DBuilder.finalize();
3427}
David Blaikie66088d52014-09-24 17:01:27 +00003428
3429void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
3430 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3431 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003432
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003433 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003434 // Don't ignore in case of explicit cast where it is referenced indirectly.
3435 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00003436}