blob: 83b5e01deefcafa17b3cd4cd6b4edc8486d513f1 [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 Majnemer9df56372015-09-10 21:52:00 +00001909 uint64_t Size =
1910 !Ty->isIncompleteType() ? CGM.getContext().getTypeSize(Ty) : 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001911 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00001912 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00001913 return DBuilder.createMemberPointerType(
David Majnemer34568bc2015-05-26 21:54:24 +00001914 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size);
Adrian Prantl0866acd2013-12-19 01:38:47 +00001915
1916 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00001917 Ty->getPointeeType()->getAs<FunctionProtoType>();
1918 return DBuilder.createMemberPointerType(
1919 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
1920 Ty->getClass(), FPT->getTypeQuals())),
1921 FPT, U),
David Majnemer34568bc2015-05-26 21:54:24 +00001922 ClassType, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +00001923}
1924
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001925llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001926 // Ignore the atomic wrapping
1927 // FIXME: What is the correct representation?
1928 return getOrCreateType(Ty->getValueType(), U);
1929}
1930
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001931llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00001932 const EnumDecl *ED = Ty->getDecl();
Guy Benyei11169dd2012-12-18 14:30:41 +00001933 uint64_t Size = 0;
1934 uint64_t Align = 0;
1935 if (!ED->getTypeForDecl()->isIncompleteType()) {
1936 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1937 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1938 }
1939
Manman Rene0064d82013-08-29 23:19:58 +00001940 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1941
Guy Benyei11169dd2012-12-18 14:30:41 +00001942 // If this is just a forward declaration, construct an appropriately
1943 // marked node and just return it.
1944 if (!ED->getDefinition()) {
Adrian Prantl6ec370a2015-09-10 18:39:45 +00001945 llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001946 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001947 unsigned Line = getLineNumber(ED->getLocation());
1948 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001949 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
David Blaikief427b002014-05-06 03:42:01 +00001950 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001951 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001952 ReplaceMap.emplace_back(
1953 std::piecewise_construct, std::make_tuple(Ty),
1954 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00001955 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00001956 }
1957
David Blaikie483a9da2014-05-06 18:35:21 +00001958 return CreateTypeDefinition(Ty);
1959}
1960
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001961llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00001962 const EnumDecl *ED = Ty->getDecl();
1963 uint64_t Size = 0;
1964 uint64_t Align = 0;
1965 if (!ED->getTypeForDecl()->isIncompleteType()) {
1966 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1967 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1968 }
1969
1970 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1971
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001972 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001973 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00001974 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00001975 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001976 Enumerators.push_back(DBuilder.createEnumerator(
1977 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001978 }
1979
1980 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001981 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00001982
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001983 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001984 unsigned Line = getLineNumber(ED->getLocation());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00001985 llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001986 llvm::DIType *ClassTy =
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001987 ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
1988 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
1989 Line, Size, Align, EltArray, ClassTy,
1990 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00001991}
1992
David Blaikie05491062013-01-21 04:37:12 +00001993static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
1994 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00001995 do {
Adrian Prantl179af902013-09-26 21:35:50 +00001996 Qualifiers InnerQuals = T.getLocalQualifiers();
1997 // Qualifiers::operator+() doesn't like it if you add a Qualifier
1998 // that is already there.
1999 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2000 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002001 QualType LastT = T;
2002 switch (T->getTypeClass()) {
2003 default:
David Blaikie05491062013-01-21 04:37:12 +00002004 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002005 case Type::TemplateSpecialization: {
2006 const auto *Spec = cast<TemplateSpecializationType>(T);
2007 if (Spec->isTypeAlias())
2008 return C.getQualifiedType(T.getTypePtr(), Quals);
2009 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002010 break;
2011 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002012 case Type::TypeOfExpr:
2013 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2014 break;
2015 case Type::TypeOf:
2016 T = cast<TypeOfType>(T)->getUnderlyingType();
2017 break;
2018 case Type::Decltype:
2019 T = cast<DecltypeType>(T)->getUnderlyingType();
2020 break;
2021 case Type::UnaryTransform:
2022 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2023 break;
2024 case Type::Attributed:
2025 T = cast<AttributedType>(T)->getEquivalentType();
2026 break;
2027 case Type::Elaborated:
2028 T = cast<ElaboratedType>(T)->getNamedType();
2029 break;
2030 case Type::Paren:
2031 T = cast<ParenType>(T)->getInnerType();
2032 break;
David Blaikie05491062013-01-21 04:37:12 +00002033 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002034 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002035 break;
2036 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00002037 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002038 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002039 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002040 break;
2041 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002042
Guy Benyei11169dd2012-12-18 14:30:41 +00002043 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002044 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002045 } while (true);
2046}
2047
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002048llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002049
2050 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002051 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002052
David Blaikief427b002014-05-06 03:42:01 +00002053 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002054 if (it != TypeCache.end()) {
2055 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002056 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002057 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002058 }
2059
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002060 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002061}
2062
David Blaikie0e716b42014-03-03 23:48:23 +00002063void CGDebugInfo::completeTemplateDefinition(
2064 const ClassTemplateSpecializationDecl &SD) {
David Blaikie0856f662014-03-04 22:01:08 +00002065 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2066 return;
2067
David Blaikie0e716b42014-03-03 23:48:23 +00002068 completeClassData(&SD);
2069 // In case this type has no member function definitions being emitted, ensure
2070 // it is retained
2071 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2072}
2073
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002074llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002075 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002076 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002077
2078 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002079 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002080
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002081 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002082 return T;
2083
2084 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002085 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Eric Christophere7b87e52014-10-26 23:40:33 +00002086 void *TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002087
2088 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002089 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002090
Guy Benyei11169dd2012-12-18 14:30:41 +00002091 return Res;
2092}
2093
Eric Christopher1ecc5632013-06-07 22:54:39 +00002094unsigned CGDebugInfo::Checksum(const ObjCInterfaceDecl *ID) {
Adrian Prantl817bbb32013-06-07 01:10:48 +00002095 // The assumption is that the number of ivars can only increase
2096 // monotonically, so it is safe to just use their current number as
2097 // a checksum.
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002098 unsigned Sum = 0;
2099 for (const ObjCIvarDecl *Ivar = ID->all_declared_ivar_begin();
Craig Topper8a13c412014-05-21 05:09:00 +00002100 Ivar != nullptr; Ivar = Ivar->getNextIvar())
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002101 ++Sum;
2102
2103 return Sum;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002104}
2105
2106ObjCInterfaceDecl *CGDebugInfo::getObjCInterfaceDecl(QualType Ty) {
2107 switch (Ty->getTypeClass()) {
2108 case Type::ObjCObjectPointer:
Eric Christophere7b87e52014-10-26 23:40:33 +00002109 return getObjCInterfaceDecl(
2110 cast<ObjCObjectPointerType>(Ty)->getPointeeType());
Adrian Prantla03a85a2013-03-06 22:03:30 +00002111 case Type::ObjCInterface:
2112 return cast<ObjCInterfaceType>(Ty)->getDecl();
2113 default:
Craig Topper8a13c412014-05-21 05:09:00 +00002114 return nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002115 }
2116}
2117
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002118llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002119 // Handle qualifiers, which recursively handles what they refer to.
2120 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002121 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002122
Guy Benyei11169dd2012-12-18 14:30:41 +00002123 // Work out details of type.
2124 switch (Ty->getTypeClass()) {
2125#define TYPE(Class, Base)
2126#define ABSTRACT_TYPE(Class, Base)
2127#define NON_CANONICAL_TYPE(Class, Base)
2128#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2129#include "clang/AST/TypeNodes.def"
2130 llvm_unreachable("Dependent types cannot show up in debug information");
2131
2132 case Type::ExtVector:
2133 case Type::Vector:
2134 return CreateType(cast<VectorType>(Ty), Unit);
2135 case Type::ObjCObjectPointer:
2136 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2137 case Type::ObjCObject:
2138 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2139 case Type::ObjCInterface:
2140 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2141 case Type::Builtin:
2142 return CreateType(cast<BuiltinType>(Ty));
2143 case Type::Complex:
2144 return CreateType(cast<ComplexType>(Ty));
2145 case Type::Pointer:
2146 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002147 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002148 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002149 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002150 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002151 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002152 case Type::BlockPointer:
2153 return CreateType(cast<BlockPointerType>(Ty), Unit);
2154 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002155 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002156 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002157 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002158 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002159 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002160 case Type::FunctionProto:
2161 case Type::FunctionNoProto:
2162 return CreateType(cast<FunctionType>(Ty), Unit);
2163 case Type::ConstantArray:
2164 case Type::VariableArray:
2165 case Type::IncompleteArray:
2166 return CreateType(cast<ArrayType>(Ty), Unit);
2167
2168 case Type::LValueReference:
2169 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2170 case Type::RValueReference:
2171 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2172
2173 case Type::MemberPointer:
2174 return CreateType(cast<MemberPointerType>(Ty), Unit);
2175
2176 case Type::Atomic:
2177 return CreateType(cast<AtomicType>(Ty), Unit);
2178
Guy Benyei11169dd2012-12-18 14:30:41 +00002179 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002180 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2181
David Blaikie42edade2014-11-11 20:44:45 +00002182 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002183 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002184 case Type::Elaborated:
2185 case Type::Paren:
2186 case Type::SubstTemplateTypeParm:
2187 case Type::TypeOfExpr:
2188 case Type::TypeOf:
2189 case Type::Decltype:
2190 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002191 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002192 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002193 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002194
David Blaikie42edade2014-11-11 20:44:45 +00002195 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002196}
2197
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002198llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2199 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002200 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002201
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002202 auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002203
2204 // We may have cached a forward decl when we could have created
2205 // a non-forward decl. Go ahead and create a non-forward decl
2206 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002207 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002208 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002209
2210 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002211 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002212
2213 // Propagate members from the declaration to the definition
2214 // CreateType(const RecordType*) will overwrite this with the members in the
2215 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002216 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002217
Guy Benyei11169dd2012-12-18 14:30:41 +00002218 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002219 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002220 return Res;
2221}
2222
2223// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002224llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002225 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002226
Guy Benyei11169dd2012-12-18 14:30:41 +00002227 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002228 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002229 unsigned Line = getLineNumber(RD->getLocation());
2230 StringRef RDName = getClassName(RD);
2231
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002232 llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002233
David Blaikied2785892013-08-18 17:36:19 +00002234 // If we ended up creating the type during the context chain construction,
2235 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002236 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002237 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002238 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002239 return T;
David Blaikied2785892013-08-18 17:36:19 +00002240
Adrian Prantl381e7552014-02-04 21:29:50 +00002241 // If this is just a forward or incomplete declaration, construct an
2242 // appropriately marked node and just return it.
2243 const RecordDecl *D = RD->getDefinition();
2244 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002245 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002246
2247 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2248 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002249
Manman Rene0064d82013-08-29 23:19:58 +00002250 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2251
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002252 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002253 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align, 0,
2254 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002255
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002256 RegionMap[Ty->getDecl()].reset(RealDecl);
2257 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002258
David Blaikieadfbf992013-08-18 16:55:33 +00002259 if (const ClassTemplateSpecializationDecl *TSpecial =
2260 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002261 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002262 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002263 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002264}
2265
David Blaikieadfbf992013-08-18 16:55:33 +00002266void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002267 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002268 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002269 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002270 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2271 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002272 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002273 while (1) {
2274 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2275 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2276 if (PBT && !BRL.isPrimaryBaseVirtual())
2277 PBase = PBT;
2278 else
2279 break;
2280 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002281 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002282 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2283 getOrCreateFile(RD->getLocation())));
2284 } else if (RD->isDynamicClass())
2285 ContainingType = RealDecl;
2286
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002287 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002288}
2289
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002290llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002291 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002292 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002293 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2294 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002295 llvm::DIType *Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002296 FieldAlign, *Offset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002297 *Offset += FieldSize;
2298 return Ty;
2299}
2300
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002301void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002302 StringRef &Name,
2303 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002304 llvm::DIScope *&FDContext,
2305 llvm::DINodeArray &TParamsArray,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002306 unsigned &Flags) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002307 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2308 Name = getFunctionName(FD);
2309 // Use mangled name as linkage name for C/C++ functions.
2310 if (FD->hasPrototype()) {
2311 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002312 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002313 }
2314 // No need to replicate the linkage name if it isn't different from the
2315 // subprogram name, no need to have it at all unless coverage is enabled or
2316 // debug is set to more than just line tables.
2317 if (LinkageName == Name ||
2318 (!CGM.getCodeGenOpts().EmitGcovArcs &&
2319 !CGM.getCodeGenOpts().EmitGcovNotes &&
2320 DebugKind <= CodeGenOptions::DebugLineTablesOnly))
2321 LinkageName = StringRef();
2322
2323 if (DebugKind >= CodeGenOptions::LimitedDebugInfo) {
2324 if (const NamespaceDecl *NSDecl =
2325 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2326 FDContext = getOrCreateNameSpace(NSDecl);
2327 else if (const RecordDecl *RDecl =
2328 dyn_cast_or_null<RecordDecl>(FD->getDeclContext()))
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002329 FDContext = getContextDescriptor(RDecl, TheCU);
Frederic Riss9db79f12014-11-18 03:40:46 +00002330 // Collect template parameters.
2331 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2332 }
2333}
2334
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002335void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002336 unsigned &LineNo, QualType &T,
2337 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002338 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002339 Unit = getOrCreateFile(VD->getLocation());
2340 LineNo = getLineNumber(VD->getLocation());
2341
2342 setLocation(VD->getLocation());
2343
2344 T = VD->getType();
2345 if (T->isIncompleteArrayType()) {
2346 // CodeGen turns int[] into int[1] so we'll do the same here.
2347 llvm::APInt ConstVal(32, 1);
2348 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2349
2350 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2351 ArrayType::Normal, 0);
2352 }
2353
2354 Name = VD->getName();
2355 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2356 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2357 LinkageName = CGM.getMangledName(VD);
2358 if (LinkageName == Name)
2359 LinkageName = StringRef();
2360
2361 // Since we emit declarations (DW_AT_members) for static members, place the
2362 // definition of those static members in the namespace they were declared in
2363 // in the source code (the lexical decl context).
2364 // FIXME: Generalize this for even non-member global variables where the
2365 // declaration and definition may have different lexical decl contexts, once
2366 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002367 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2368 : VD->getDeclContext();
2369 // When a record type contains an in-line initialization of a static data
2370 // member, and the record type is marked as __declspec(dllexport), an implicit
2371 // definition of the member will be created in the record context. DWARF
2372 // doesn't seem to have a nice way to describe this in a form that consumers
2373 // are likely to understand, so fake the "normal" situation of a definition
2374 // outside the class by putting it in the global scope.
2375 if (DC->isRecord())
2376 DC = CGM.getContext().getTranslationUnitDecl();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002377 VDContext = getContextDescriptor(cast<Decl>(DC), TheCU);
Frederic Riss9db79f12014-11-18 03:40:46 +00002378}
2379
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002380llvm::DISubprogram *
Frederic Rissd253ed62014-11-18 03:40:51 +00002381CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002382 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00002383 StringRef Name, LinkageName;
2384 unsigned Flags = 0;
2385 SourceLocation Loc = FD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002386 llvm::DIFile *Unit = getOrCreateFile(Loc);
2387 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002388 unsigned Line = getLineNumber(Loc);
2389
2390 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2391 TParamsArray, Flags);
2392 // Build function type.
2393 SmallVector<QualType, 16> ArgTypes;
2394 for (const ParmVarDecl *Parm: FD->parameters())
2395 ArgTypes.push_back(Parm->getType());
2396 QualType FnType =
2397 CGM.getContext().getFunctionType(FD->getReturnType(), ArgTypes,
2398 FunctionProtoType::ExtProtoInfo());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002399 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002400 DContext, Name, LinkageName, Unit, Line,
2401 getOrCreateFunctionType(FD, FnType, Unit), !FD->isExternallyVisible(),
Duncan P. N. Exon Smith31d38f72015-08-26 22:21:09 +00002402 /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize, nullptr,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002403 TParamsArray.get(), getFunctionDeclaration(FD));
Frederic Rissd253ed62014-11-18 03:40:51 +00002404 const FunctionDecl *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002405 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2406 std::make_tuple(CanonDecl),
2407 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00002408 return SP;
2409}
2410
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002411llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00002412CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2413 QualType T;
2414 StringRef Name, LinkageName;
2415 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002416 llvm::DIFile *Unit = getOrCreateFile(Loc);
2417 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002418 unsigned Line = getLineNumber(Loc);
2419
2420 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002421 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
2422 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
2423 !VD->isExternallyVisible(), nullptr, nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002424 FwdDeclReplaceMap.emplace_back(
2425 std::piecewise_construct,
2426 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2427 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002428 return GV;
2429}
2430
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002431llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002432 // We only need a declaration (not a definition) of the type - so use whatever
2433 // we would otherwise do to get a type for a pointee. (forward declarations in
2434 // limited debug info, full definitions (if the type definition is available)
2435 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002436 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2437 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002438 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002439 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002440
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002441 if (I != DeclCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002442 return dyn_cast_or_null<llvm::DINode>(I->second);
Frederic Rissd253ed62014-11-18 03:40:51 +00002443
2444 // No definition for now. Emit a forward definition that might be
2445 // merged with a potential upcoming definition.
2446 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
2447 return getFunctionForwardDeclaration(FD);
2448 else if (const auto *VD = dyn_cast<VarDecl>(D))
2449 return getGlobalVariableForwardDeclaration(VD);
2450
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002451 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00002452}
2453
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002454llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Diego Novillo913690c2014-06-24 17:02:17 +00002455 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002456 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00002457
Guy Benyei11169dd2012-12-18 14:30:41 +00002458 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002459 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002460 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002461
2462 // Setup context.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002463 auto *S = getDeclContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00002464
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002465 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002466 if (MI == SPCache.end()) {
Eric Christopherf86c4052013-08-28 23:12:10 +00002467 if (const CXXMethodDecl *MD =
2468 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00002469 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002470 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00002471 }
2472 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002473 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002474 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002475 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002476 return SP;
2477 }
2478
Aaron Ballman86c93902014-03-06 23:45:36 +00002479 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002480 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002481 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002482 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002483 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002484 return SP;
2485 }
2486 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002487 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002488}
2489
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002490// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00002491// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002492llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002493 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002494 llvm::DIFile *F) {
Diego Novillo913690c2014-06-24 17:02:17 +00002495 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002496 // Create fake but valid subroutine type. Otherwise -verify would fail, and
2497 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Manman Ren67f005e2014-07-28 22:24:34 +00002498 return DBuilder.createSubroutineType(F,
2499 DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002500
2501 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2502 return getOrCreateMethodType(Method, F);
2503 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2504 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002505 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002506
2507 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002508 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002509
2510 // Replace the instancetype keyword with the actual type.
2511 if (ResultTy == CGM.getContext().getObjCInstanceType())
2512 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002513 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002514
Adrian Prantl7bec9032013-05-10 21:08:31 +00002515 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002516 // "self" pointer is always first argument.
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002517 QualType SelfDeclTy;
2518 if (auto *SelfDecl = OMethod->getSelfDecl())
2519 SelfDeclTy = SelfDecl->getType();
2520 else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType))
2521 if (FPT->getNumParams() > 1)
2522 SelfDeclTy = FPT->getParamType(0);
2523 if (!SelfDeclTy.isNull())
2524 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002525 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002526 Elts.push_back(DBuilder.createArtificialType(
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002527 getOrCreateType(CGM.getContext().getObjCSelType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002528 // Get rest of the arguments.
Aaron Ballman43b68be2014-03-07 17:50:17 +00002529 for (const auto *PI : OMethod->params())
2530 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002531 // Variadic methods need a special marker at the end of the type list.
2532 if (OMethod->isVariadic())
2533 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002534
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002535 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002536 return DBuilder.createSubroutineType(F, EltTypeArray);
2537 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002538
Adrian Prantl800faef2014-02-25 23:42:18 +00002539 // Handle variadic function types; they need an additional
2540 // unspecified parameter.
Adrian Prantld45ba252014-02-25 19:38:11 +00002541 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2542 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002543 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00002544 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2545 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2546 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2547 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2548 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002549 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Adrian Prantld45ba252014-02-25 19:38:11 +00002550 return DBuilder.createSubroutineType(F, EltTypeArray);
2551 }
2552
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002553 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002554}
2555
Eric Christophere7b87e52014-10-26 23:40:33 +00002556void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2557 SourceLocation ScopeLoc, QualType FnType,
2558 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002559
2560 StringRef Name;
2561 StringRef LinkageName;
2562
2563 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2564
2565 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002566 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00002567
Guy Benyei11169dd2012-12-18 14:30:41 +00002568 unsigned Flags = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002569 llvm::DIFile *Unit = getOrCreateFile(Loc);
2570 llvm::DIScope *FDContext = Unit;
2571 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00002572 if (!HasDecl) {
2573 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00002574 LinkageName = Fn->getName();
Guy Benyei11169dd2012-12-18 14:30:41 +00002575 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002576 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002577 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002578 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002579 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002580 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002581 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002582 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002583 return;
2584 }
2585 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002586 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2587 TParamsArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002588 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2589 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002590 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002591 } else {
2592 // Use llvm function name.
2593 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002594 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002595 }
2596 if (!Name.empty() && Name[0] == '\01')
2597 Name = Name.substr(1);
2598
Adrian Prantl42d71b92014-04-10 23:21:53 +00002599 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002600 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl42d71b92014-04-10 23:21:53 +00002601 // Artificial functions without a location should not silently reuse CurLoc.
2602 if (Loc.isInvalid())
2603 CurLoc = SourceLocation();
2604 }
2605 unsigned LineNo = getLineNumber(Loc);
2606 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002607
Eric Christopher8018e412014-03-27 18:50:35 +00002608 // FIXME: The function declaration we're constructing here is mostly reusing
2609 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2610 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2611 // all subprograms instead of the actual context since subprogram definitions
2612 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002613 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00002614 FDContext, Name, LinkageName, Unit, LineNo,
2615 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
2616 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize, Fn,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002617 TParamsArray.get(), getFunctionDeclaration(D));
Frederic Rissb1ab28c2014-11-05 19:19:04 +00002618 // We might get here with a VarDecl in the case we're generating
2619 // code for the initialization of globals. Do not record these decls
2620 // as they will overwrite the actual VarDecl Decl in the cache.
2621 if (HasDecl && isa<FunctionDecl>(D))
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002622 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(SP));
Guy Benyei11169dd2012-12-18 14:30:41 +00002623
Adrian Prantlbebb8932014-03-21 21:01:58 +00002624 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002625 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00002626
Guy Benyei11169dd2012-12-18 14:30:41 +00002627 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002628 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002629}
2630
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002631void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
2632 QualType FnType) {
2633 StringRef Name;
2634 StringRef LinkageName;
2635
2636 const Decl *D = GD.getDecl();
2637 if (!D)
2638 return;
2639
2640 unsigned Flags = 0;
2641 llvm::DIFile *Unit = getOrCreateFile(Loc);
2642 llvm::DIScope *FDContext = Unit;
2643 llvm::DINodeArray TParamsArray;
2644 if (isa<FunctionDecl>(D)) {
2645 // If there is a DISubprogram for this function available then use it.
2646 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2647 TParamsArray, Flags);
2648 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2649 Name = getObjCMethodName(OMD);
2650 Flags |= llvm::DINode::FlagPrototyped;
2651 } else {
2652 llvm_unreachable("not a function or ObjC method");
2653 }
2654 if (!Name.empty() && Name[0] == '\01')
2655 Name = Name.substr(1);
2656
2657 if (D->isImplicit()) {
2658 Flags |= llvm::DINode::FlagArtificial;
2659 // Artificial functions without a location should not silently reuse CurLoc.
2660 if (Loc.isInvalid())
2661 CurLoc = SourceLocation();
2662 }
2663 unsigned LineNo = getLineNumber(Loc);
2664 unsigned ScopeLine = 0;
2665
2666 DBuilder.createFunction(FDContext, Name, LinkageName, Unit, LineNo,
2667 getOrCreateFunctionType(D, FnType, Unit),
2668 false /*internalLinkage*/, true /*definition*/,
2669 ScopeLine, Flags, CGM.getLangOpts().Optimize, nullptr,
2670 TParamsArray.get(),
2671 getFunctionDeclaration(D));
2672}
2673
David Blaikie835afb22015-01-21 23:08:17 +00002674void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002675 // Update our current location
2676 setLocation(Loc);
2677
Eric Christophere7b87e52014-10-26 23:40:33 +00002678 if (CurLoc.isInvalid() || CurLoc.isMacroID())
2679 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002680
Adrian Prantle83b1302014-01-07 22:05:52 +00002681 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00002682 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
David Blaikie835afb22015-01-21 23:08:17 +00002683 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002684}
2685
Guy Benyei11169dd2012-12-18 14:30:41 +00002686void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002687 llvm::MDNode *Back = nullptr;
2688 if (!LexicalBlockStack.empty())
2689 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002690 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002691 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002692 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002693}
2694
Eric Christopher0fdcb312013-05-16 00:52:20 +00002695void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2696 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002697 // Set our current location.
2698 setLocation(Loc);
2699
Guy Benyei11169dd2012-12-18 14:30:41 +00002700 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00002701 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2702 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00002703
2704 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2705 return;
2706
2707 // Create a new lexical block and push it on the stack.
2708 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002709}
2710
Eric Christopher0fdcb312013-05-16 00:52:20 +00002711void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2712 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002713 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2714
2715 // Provide an entry in the line table for the end of the block.
2716 EmitLocation(Builder, Loc);
2717
David Blaikie60a877b2014-10-22 19:34:33 +00002718 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2719 return;
2720
Guy Benyei11169dd2012-12-18 14:30:41 +00002721 LexicalBlockStack.pop_back();
2722}
2723
Guy Benyei11169dd2012-12-18 14:30:41 +00002724void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2725 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2726 unsigned RCount = FnBeginRegionCount.back();
2727 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2728
2729 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00002730 while (LexicalBlockStack.size() != RCount) {
2731 // Provide an entry in the line table for the end of the block.
2732 EmitLocation(Builder, CurLoc);
2733 LexicalBlockStack.pop_back();
2734 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002735 FnBeginRegionCount.pop_back();
2736}
2737
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002738llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002739 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002740
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002741 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002742 QualType FType;
2743 uint64_t FieldSize, FieldOffset;
2744 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002745
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002746 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002747 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002748
2749 FieldOffset = 0;
2750 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2751 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2752 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2753 FType = CGM.getContext().IntTy;
2754 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2755 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2756
2757 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2758 if (HasCopyAndDispose) {
2759 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002760 EltTys.push_back(
2761 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
2762 EltTys.push_back(
2763 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00002764 }
2765 bool HasByrefExtendedLayout;
2766 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00002767 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
2768 HasByrefExtendedLayout) &&
2769 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00002770 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002771 EltTys.push_back(
2772 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00002773 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002774
Guy Benyei11169dd2012-12-18 14:30:41 +00002775 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2776 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002777 CGM.getTarget().getPointerAlign(0))) {
2778 CharUnits FieldOffsetInBytes =
2779 CGM.getContext().toCharUnitsFromBits(FieldOffset);
2780 CharUnits AlignedOffsetInBytes =
2781 FieldOffsetInBytes.RoundUpToAlignment(Align);
2782 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002783
Guy Benyei11169dd2012-12-18 14:30:41 +00002784 if (NumPaddingBytes.isPositive()) {
2785 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
2786 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2787 pad, ArrayType::Normal, 0);
2788 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
2789 }
2790 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002791
Guy Benyei11169dd2012-12-18 14:30:41 +00002792 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002793 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002794 FieldSize = CGM.getContext().getTypeSize(FType);
2795 FieldAlign = CGM.getContext().toBits(Align);
2796
Eric Christopherb2a008c2013-05-16 00:45:12 +00002797 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00002798 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
2799 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002800 EltTys.push_back(FieldTy);
2801 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002802
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002803 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002804
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002805 unsigned Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002806
Guy Benyei11169dd2012-12-18 14:30:41 +00002807 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002808 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002809}
2810
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002811void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::Value *Storage,
2812 llvm::Optional<unsigned> ArgNo,
Eric Christophere7b87e52014-10-26 23:40:33 +00002813 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002814 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002815 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2816
David Blaikie7fceebf2013-08-19 03:37:48 +00002817 bool Unwritten =
2818 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
2819 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002820 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00002821 if (!Unwritten)
2822 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002823 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00002824 uint64_t XOffset = 0;
2825 if (VD->hasAttr<BlocksAttr>())
2826 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002827 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002828 Ty = getOrCreateType(VD->getType(), Unit);
2829
2830 // If there is no debug info for this type then do not emit debug info
2831 // for this variable.
2832 if (!Ty)
2833 return;
2834
Guy Benyei11169dd2012-12-18 14:30:41 +00002835 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00002836 unsigned Line = 0;
2837 unsigned Column = 0;
2838 if (!Unwritten) {
2839 Line = getLineNumber(VD->getLocation());
2840 Column = getColumnNumber(VD->getLocation());
2841 }
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002842 SmallVector<int64_t, 9> Expr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002843 unsigned Flags = 0;
2844 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002845 Flags |= llvm::DINode::FlagArtificial;
Guy Benyei11169dd2012-12-18 14:30:41 +00002846 // If this is the first argument and it is implicit then
2847 // give it an object pointer flag.
2848 // FIXME: There has to be a better way to do this, but for static
2849 // functions there won't be an implicit param at arg1 and
2850 // otherwise it is 'self' or 'this'.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002851 if (isa<ImplicitParamDecl>(VD) && ArgNo && *ArgNo == 1)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002852 Flags |= llvm::DINode::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00002853 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00002854 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
2855 !VD->getType()->isPointerType())
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002856 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei11169dd2012-12-18 14:30:41 +00002857
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002858 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00002859
2860 StringRef Name = VD->getName();
2861 if (!Name.empty()) {
2862 if (VD->hasAttr<BlocksAttr>()) {
2863 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002864 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002865 // offset of __forwarding field
2866 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002867 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002868 Expr.push_back(offset.getQuantity());
2869 Expr.push_back(llvm::dwarf::DW_OP_deref);
2870 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002871 // offset of x field
2872 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002873 Expr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002874
2875 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002876 auto *D = ArgNo
2877 ? DBuilder.createParameterVariable(Scope, VD->getName(),
2878 *ArgNo, Unit, Line, Ty)
2879 : DBuilder.createAutoVariable(Scope, VD->getName(), Unit,
2880 Line, Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002881
Guy Benyei11169dd2012-12-18 14:30:41 +00002882 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002883 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2884 llvm::DebugLoc::get(Line, Column, Scope),
2885 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002886 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00002887 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002888 Expr.push_back(llvm::dwarf::DW_OP_deref);
Adrian Prantl0d820892015-04-29 15:05:50 +00002889 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2890 // If VD is an anonymous union then Storage represents value for
2891 // all union fields.
2892 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
2893 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00002894 // GDB has trouble finding local variables in anonymous unions, so we emit
2895 // artifical local variables for each of the members.
2896 //
2897 // FIXME: Remove this code as soon as GDB supports this.
2898 // The debug info verifier in LLVM operates based on the assumption that a
2899 // variable has the same size as its storage and we had to disable the check
2900 // for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00002901 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002902 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00002903 StringRef FieldName = Field->getName();
2904
2905 // Ignore unnamed fields. Do not ignore unnamed records.
2906 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2907 continue;
2908
2909 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002910 auto *D = DBuilder.createAutoVariable(
2911 Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
2912 Flags | llvm::DINode::FlagArtificial);
Adrian Prantl0d820892015-04-29 15:05:50 +00002913
2914 // Insert an llvm.dbg.declare into the current block.
2915 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2916 llvm::DebugLoc::get(Line, Column, Scope),
2917 Builder.GetInsertBlock());
2918 }
Adrian Prantl0d820892015-04-29 15:05:50 +00002919 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002920 }
David Blaikiea76a7c92013-01-05 05:58:35 +00002921
2922 // Create the descriptor for the variable.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002923 auto *D =
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002924 ArgNo
2925 ? DBuilder.createParameterVariable(Scope, Name, *ArgNo, Unit, Line,
2926 Ty, CGM.getLangOpts().Optimize,
2927 Flags)
2928 : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
2929 CGM.getLangOpts().Optimize, Flags);
David Blaikiea76a7c92013-01-05 05:58:35 +00002930
2931 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002932 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2933 llvm::DebugLoc::get(Line, Column, Scope),
2934 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002935}
2936
2937void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
2938 llvm::Value *Storage,
2939 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002940 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002941 EmitDeclare(VD, Storage, llvm::None, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00002942}
2943
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002944llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
2945 llvm::DIType *Ty) {
2946 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002947 if (CachedTy)
2948 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00002949 return DBuilder.createObjectPointerType(Ty);
2950}
2951
Eric Christophere7b87e52014-10-26 23:40:33 +00002952void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
2953 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00002954 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Eric Christopher75e17682013-05-16 00:45:23 +00002955 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002956 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00002957
Craig Topper8a13c412014-05-21 05:09:00 +00002958 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00002959 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002960
Guy Benyei11169dd2012-12-18 14:30:41 +00002961 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002962
Guy Benyei11169dd2012-12-18 14:30:41 +00002963 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002964 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
2965 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00002966 if (isByRef)
2967 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002968 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002969 Ty = getOrCreateType(VD->getType(), Unit);
2970
2971 // Self is passed along as an implicit non-arg variable in a
2972 // block. Mark it as the object pointer.
2973 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00002974 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00002975
2976 // Get location information.
2977 unsigned Line = getLineNumber(VD->getLocation());
2978 unsigned Column = getColumnNumber(VD->getLocation());
2979
2980 const llvm::DataLayout &target = CGM.getDataLayout();
2981
2982 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00002983 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00002984 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
2985
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002986 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00002987 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002988 addr.push_back(llvm::dwarf::DW_OP_deref);
2989 addr.push_back(llvm::dwarf::DW_OP_plus);
2990 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002991 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002992 addr.push_back(llvm::dwarf::DW_OP_deref);
2993 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002994 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00002995 offset =
2996 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002997 addr.push_back(offset.getQuantity());
2998 addr.push_back(llvm::dwarf::DW_OP_deref);
2999 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003000 // offset of x field
3001 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003002 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003003 }
3004
3005 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003006 auto *D = DBuilder.createAutoVariable(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003007 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003008 Line, Ty);
Adrian Prantl0f6df002013-03-29 19:20:35 +00003009
Guy Benyei11169dd2012-12-18 14:30:41 +00003010 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003011 auto DL = llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back());
3012 if (InsertPoint)
3013 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3014 InsertPoint);
3015 else
3016 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3017 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003018}
3019
Guy Benyei11169dd2012-12-18 14:30:41 +00003020void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3021 unsigned ArgNo,
3022 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003023 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003024 EmitDeclare(VD, AI, ArgNo, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003025}
3026
3027namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003028struct BlockLayoutChunk {
3029 uint64_t OffsetInBits;
3030 const BlockDecl::Capture *Capture;
3031};
3032bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3033 return l.OffsetInBits < r.OffsetInBits;
3034}
Guy Benyei11169dd2012-12-18 14:30:41 +00003035}
3036
3037void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003038 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003039 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003040 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003041 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003042 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003043 ASTContext &C = CGM.getContext();
3044 const BlockDecl *blockDecl = block.getBlockDecl();
3045
3046 // Collect some general information about the block's location.
3047 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003048 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003049 unsigned line = getLineNumber(loc);
3050 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003051
Guy Benyei11169dd2012-12-18 14:30:41 +00003052 // Build the debug-info type for the block literal.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003053 getDeclContextDescriptor(blockDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003054
3055 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003056 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003057
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003058 SmallVector<llvm::Metadata *, 16> fields;
Guy Benyei11169dd2012-12-18 14:30:41 +00003059 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
3060 blockLayout->getElementOffsetInBits(0),
3061 tunit, tunit));
3062 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
3063 blockLayout->getElementOffsetInBits(1),
3064 tunit, tunit));
3065 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
3066 blockLayout->getElementOffsetInBits(2),
3067 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003068 auto *FnTy = block.getBlockExpr()->getFunctionType();
3069 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
3070 fields.push_back(createFieldType("__FuncPtr", FnPtrType, 0, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003071 blockLayout->getElementOffsetInBits(3),
3072 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003073 fields.push_back(createFieldType(
3074 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3075 ? C.getBlockDescriptorExtendedType()
3076 : C.getBlockDescriptorType()),
3077 0, loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003078
3079 // We want to sort the captures by offset, not because DWARF
3080 // requires this, but because we're paranoid about debuggers.
3081 SmallVector<BlockLayoutChunk, 8> chunks;
3082
3083 // 'this' capture.
3084 if (blockDecl->capturesCXXThis()) {
3085 BlockLayoutChunk chunk;
3086 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003087 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003088 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003089 chunks.push_back(chunk);
3090 }
3091
3092 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003093 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003094 const VarDecl *variable = capture.getVariable();
3095 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3096
3097 // Ignore constant captures.
3098 if (captureInfo.isConstant())
3099 continue;
3100
3101 BlockLayoutChunk chunk;
3102 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003103 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003104 chunk.Capture = &capture;
3105 chunks.push_back(chunk);
3106 }
3107
3108 // Sort by offset.
3109 llvm::array_pod_sort(chunks.begin(), chunks.end());
3110
Eric Christophere7b87e52014-10-26 23:40:33 +00003111 for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
3112 e = chunks.end();
3113 i != e; ++i) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003114 uint64_t offsetInBits = i->OffsetInBits;
3115 const BlockDecl::Capture *capture = i->Capture;
3116
3117 // If we have a null capture, this must be the C++ 'this' capture.
3118 if (!capture) {
3119 const CXXMethodDecl *method =
Eric Christophere7b87e52014-10-26 23:40:33 +00003120 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00003121 QualType type = method->getThisType(C);
3122
3123 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
3124 offsetInBits, tunit, tunit));
3125 continue;
3126 }
3127
3128 const VarDecl *variable = capture->getVariable();
3129 StringRef name = variable->getName();
3130
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003131 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003132 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003133 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003134
3135 // FIXME: this creates a second copy of this type!
3136 uint64_t xoffset;
3137 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003138 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3139 fieldType =
3140 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3141 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003142 } else {
Eric Christophere7b87e52014-10-26 23:40:33 +00003143 fieldType = createFieldType(name, variable->getType(), 0, loc, AS_public,
3144 offsetInBits, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003145 }
3146 fields.push_back(fieldType);
3147 }
3148
3149 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003150 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3151 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003152
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003153 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003154
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003155 llvm::DIType *type = DBuilder.createStructType(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003156 tunit, typeName.str(), tunit, line,
3157 CGM.getContext().toBits(block.BlockSize),
3158 CGM.getContext().toBits(block.BlockAlign), 0, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003159 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3160
3161 // Get overall information about the block.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003162 unsigned flags = llvm::DINode::FlagArtificial;
3163 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003164
3165 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003166 auto *debugVar = DBuilder.createParameterVariable(
3167 scope, Arg->getName(), ArgNo, tunit, line, type,
3168 CGM.getLangOpts().Optimize, flags);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003169
Adrian Prantl616bef42013-03-14 21:52:59 +00003170 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003171 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003172 DBuilder.insertDbgValueIntrinsic(
Eric Christophere7b87e52014-10-26 23:40:33 +00003173 LocalAddr, 0, debugVar, DBuilder.createExpression(),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003174 llvm::DebugLoc::get(line, column, scope), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003175 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003176
Adrian Prantl616bef42013-03-14 21:52:59 +00003177 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003178 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
3179 llvm::DebugLoc::get(line, column, scope),
3180 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003181}
3182
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003183llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003184CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3185 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003186 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003187
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003188 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003189 if (MI != StaticDataMemberCache.end()) {
3190 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00003191 return MI->second;
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003192 }
David Blaikiece763042013-08-20 21:49:21 +00003193
3194 // If the member wasn't found in the cache, lazily construct and add it to the
3195 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003196 auto DC = D->getDeclContext();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003197 auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003198 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003199}
3200
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003201llvm::DIGlobalVariable *CGDebugInfo::CollectAnonRecordDecls(
3202 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3203 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
3204 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003205
3206 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003207 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003208 StringRef FieldName = Field->getName();
3209
3210 // Ignore unnamed fields, but recurse into anonymous records.
3211 if (FieldName.empty()) {
3212 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3213 if (RT)
3214 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3215 Var, DContext);
3216 continue;
3217 }
3218 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003219 GV = DBuilder.createGlobalVariable(DContext, FieldName, LinkageName, Unit,
3220 LineNo, FieldTy,
3221 Var->hasInternalLinkage(), Var, nullptr);
Eric Christophercab9fae2014-04-10 05:20:00 +00003222 }
3223 return GV;
3224}
3225
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003226void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003227 const VarDecl *D) {
Eric Christopher75e17682013-05-16 00:45:23 +00003228 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003229 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003230 llvm::DIFile *Unit = nullptr;
3231 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003232 unsigned LineNo;
3233 StringRef DeclName, LinkageName;
3234 QualType T;
3235 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003236
3237 // Attempt to store one global variable for the declaration - even if we
3238 // emit a lot of fields.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003239 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003240
3241 // If this is an anonymous union then we'll want to emit a global
3242 // variable for each member of the anonymous union so that it's possible
3243 // to find the name of any field in the union.
3244 if (T->isUnionType() && DeclName.empty()) {
3245 const RecordDecl *RD = cast<RecordType>(T)->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003246 assert(RD->isAnonymousStructOrUnion() &&
3247 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003248 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3249 } else {
David Blaikie7550b112014-10-20 17:42:23 +00003250 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003251 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3252 Var->hasInternalLinkage(), Var,
3253 getOrCreateStaticDataMemberDeclarationOrNull(D));
3254 }
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003255 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(GV));
Guy Benyei11169dd2012-12-18 14:30:41 +00003256}
3257
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003258void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3259 llvm::Constant *Init) {
Eric Christopher75e17682013-05-16 00:45:23 +00003260 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003261 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003262 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003263 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003264 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003265 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3266 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3267 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3268 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3269 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003270 // Do not use global variables for enums.
3271 //
3272 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003273 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003274 return;
David Blaikiea15565562014-04-04 20:56:17 +00003275 // Do not emit separate definitions for function local const/statics.
3276 if (isa<FunctionDecl>(VD->getDeclContext()))
3277 return;
David Blaikiebb113912014-04-05 07:23:17 +00003278 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003279 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003280 if (VarD->isStaticDataMember()) {
3281 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003282 getDeclContextDescriptor(VarD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003283 // Ensure that the type is retained even though it's otherwise unreferenced.
3284 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003285 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003286 return;
3287 }
3288
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003289 llvm::DIScope *DContext = getDeclContextDescriptor(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003290
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003291 auto &GV = DeclCache[VD];
3292 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003293 return;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003294 GV.reset(DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003295 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003296 true, Init, getOrCreateStaticDataMemberDeclarationOrNull(VarD)));
David Blaikiebd483762013-05-20 04:58:53 +00003297}
3298
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003299llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003300 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003301 return LexicalBlockStack.back();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003302 return getContextDescriptor(D, TheCU);
Guy Benyei11169dd2012-12-18 14:30:41 +00003303}
3304
David Blaikie9f88fe82013-04-22 06:13:21 +00003305void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
David Blaikiebd483762013-05-20 04:58:53 +00003306 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3307 return;
David Blaikie9f88fe82013-04-22 06:13:21 +00003308 DBuilder.createImportedModule(
David Blaikiebd483762013-05-20 04:58:53 +00003309 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3310 getOrCreateNameSpace(UD.getNominatedNamespace()),
David Blaikie9f88fe82013-04-22 06:13:21 +00003311 getLineNumber(UD.getLocation()));
3312}
3313
David Blaikiebd483762013-05-20 04:58:53 +00003314void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
3315 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3316 return;
3317 assert(UD.shadow_size() &&
3318 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3319 // Emitting one decl is sufficient - debuggers can detect that this is an
3320 // overloaded name & provide lookup for all the overloads.
3321 const UsingShadowDecl &USD = **UD.shadow_begin();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003322 if (llvm::DINode *Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003323 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003324 DBuilder.createImportedDeclaration(
3325 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3326 getLineNumber(USD.getLocation()));
3327}
3328
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003329void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
3330 auto *Reader = CGM.getContext().getExternalSource();
3331 auto Info = Reader->getSourceDescriptor(*ID.getImportedModule());
3332 DBuilder.createImportedDeclaration(
3333 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
3334 getOrCreateModuleRef(Info),
3335 getLineNumber(ID.getLocation()));
3336}
3337
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003338llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00003339CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
3340 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003341 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003342 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003343 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003344 return cast<llvm::DIImportedEntity>(VH);
3345 llvm::DIImportedEntity *R;
David Blaikief121b932013-05-20 22:50:41 +00003346 if (const NamespaceAliasDecl *Underlying =
3347 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3348 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003349 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003350 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3351 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3352 NA.getName());
3353 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003354 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003355 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3356 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3357 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003358 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003359 return R;
3360}
3361
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003362llvm::DINamespace *
Guy Benyei11169dd2012-12-18 14:30:41 +00003363CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003364 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003365 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003366 if (I != NameSpaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003367 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003368
Guy Benyei11169dd2012-12-18 14:30:41 +00003369 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003370 llvm::DIFile *FileD = getOrCreateFile(NSDecl->getLocation());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003371 llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003372 llvm::DINamespace *NS =
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003373 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003374 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003375 return NS;
3376}
3377
3378void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003379 // Creating types might create further types - invalidating the current
3380 // element and the size(), so don't cache/reference them.
3381 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3382 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003383 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003384 ? CreateTypeDefinition(E.Type, E.Unit)
3385 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003386 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00003387 }
3388
David Blaikief427b002014-05-06 03:42:01 +00003389 for (auto p : ReplaceMap) {
3390 assert(p.second);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003391 auto *Ty = cast<llvm::DIType>(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003392 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003393
David Blaikief427b002014-05-06 03:42:01 +00003394 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003395 assert(it != TypeCache.end());
3396 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003397
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003398 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
3399 cast<llvm::DIType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00003400 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003401
Frederic Rissd253ed62014-11-18 03:40:51 +00003402 for (const auto &p : FwdDeclReplaceMap) {
3403 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003404 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003405 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003406
3407 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003408 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003409 // with ourselves, that will destroy the temporary MDNode and
3410 // replace it with a standard one, avoiding leaking memory.
3411 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003412 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003413 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003414 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003415
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003416 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00003417 }
3418
Adrian Prantl73409ce2013-03-11 18:33:46 +00003419 // We keep our own list of retained types, because we need to look
3420 // up the final type in the type cache.
Adrian Prantl3a884fa2015-08-27 22:56:46 +00003421 for (auto &RT : RetainedTypes)
Adrian Prantlad9a195e2015-08-27 21:21:19 +00003422 if (auto MD = TypeCache[RT])
3423 DBuilder.retainType(cast<llvm::DIType>(MD));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003424
Guy Benyei11169dd2012-12-18 14:30:41 +00003425 DBuilder.finalize();
3426}
David Blaikie66088d52014-09-24 17:01:27 +00003427
3428void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
3429 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3430 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003431
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003432 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003433 // Don't ignore in case of explicit cast where it is referenced indirectly.
3434 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00003435}