blob: dd3f783cf4ee51f5534cf4f75e90a975c86a7bd9 [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/Dwarf.h"
41#include "llvm/Support/FileSystem.h"
Adrian Prantl0630eb72013-12-18 21:48:18 +000042#include "llvm/Support/Path.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000043using namespace clang;
44using namespace clang::CodeGen;
45
46CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Eric Christopher324bbbd2013-07-14 21:12:44 +000047 : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
Adrian Prantl6b21ab22015-08-27 19:46:20 +000048 DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs),
Eric Christopher324bbbd2013-07-14 21:12:44 +000049 DBuilder(CGM.getModule()) {
Guy Benyei11169dd2012-12-18 14:30:41 +000050 CreateCompileUnit();
51}
52
53CGDebugInfo::~CGDebugInfo() {
54 assert(LexicalBlockStack.empty() &&
55 "Region stack mismatch, stack not empty!");
56}
57
David Blaikie66e41972015-01-14 07:38:27 +000058ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
David Blaikie835afb22015-01-21 23:08:17 +000059 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000060 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +000061 init(TemporaryLocation);
62}
63
Adrian Prantl39428e72015-02-03 18:40:42 +000064ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
Adrian Prantl95b24e92015-02-03 20:00:54 +000065 bool DefaultToEmpty,
Adrian Prantl39428e72015-02-03 18:40:42 +000066 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000067 : CGF(&CGF) {
Adrian Prantl95b24e92015-02-03 20:00:54 +000068 init(TemporaryLocation, DefaultToEmpty);
Adrian Prantl39428e72015-02-03 18:40:42 +000069}
70
71void ApplyDebugLocation::init(SourceLocation TemporaryLocation,
Adrian Prantl95b24e92015-02-03 20:00:54 +000072 bool DefaultToEmpty) {
David Blaikied7057d92015-08-12 23:49:57 +000073 auto *DI = CGF->getDebugInfo();
74 if (!DI) {
75 CGF = nullptr;
76 return;
David Blaikie66e41972015-01-14 07:38:27 +000077 }
David Blaikied7057d92015-08-12 23:49:57 +000078
79 OriginalLocation = CGF->Builder.getCurrentDebugLocation();
80 if (TemporaryLocation.isValid()) {
81 DI->EmitLocation(CGF->Builder, TemporaryLocation);
82 return;
83 }
84
85 if (DefaultToEmpty) {
86 CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc());
87 return;
88 }
89
90 // Construct a location that has a valid scope, but no line info.
91 assert(!DI->LexicalBlockStack.empty());
92 CGF->Builder.SetCurrentDebugLocation(
93 llvm::DebugLoc::get(0, 0, DI->LexicalBlockStack.back()));
Adrian Prantl2e0637f2013-07-18 00:28:02 +000094}
95
David Blaikie9b479662015-01-25 01:19:10 +000096ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E)
David Blaikied7057d92015-08-12 23:49:57 +000097 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +000098 init(E->getExprLoc());
99}
100
David Blaikie66e41972015-01-14 07:38:27 +0000101ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc)
David Blaikied7057d92015-08-12 23:49:57 +0000102 : CGF(&CGF) {
103 if (!CGF.getDebugInfo()) {
104 this->CGF = nullptr;
105 return;
David Blaikie66e41972015-01-14 07:38:27 +0000106 }
David Blaikied7057d92015-08-12 23:49:57 +0000107 OriginalLocation = CGF.Builder.getCurrentDebugLocation();
108 if (Loc)
109 CGF.Builder.SetCurrentDebugLocation(std::move(Loc));
David Blaikie66e41972015-01-14 07:38:27 +0000110}
111
112ApplyDebugLocation::~ApplyDebugLocation() {
113 // Query CGF so the location isn't overwritten when location updates are
114 // temporarily disabled (for C++ default function arguments)
David Blaikied7057d92015-08-12 23:49:57 +0000115 if (CGF)
116 CGF->Builder.SetCurrentDebugLocation(std::move(OriginalLocation));
David Blaikie66e41972015-01-14 07:38:27 +0000117}
118
Guy Benyei11169dd2012-12-18 14:30:41 +0000119void CGDebugInfo::setLocation(SourceLocation Loc) {
120 // If the new location isn't valid return.
Eric Christophere7b87e52014-10-26 23:40:33 +0000121 if (Loc.isInvalid())
122 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000123
124 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
125
126 // If we've changed files in the middle of a lexical scope go ahead
127 // and create a new lexical scope with file node if it's different
128 // from the one in the scope.
Eric Christophere7b87e52014-10-26 23:40:33 +0000129 if (LexicalBlockStack.empty())
130 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000131
132 SourceManager &SM = CGM.getContext().getSourceManager();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000133 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +0000134 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000135
Duncan P. N. Exon Smith373ee852015-04-16 01:36:36 +0000136 if (PCLoc.isInvalid() || Scope->getFilename() == PCLoc.getFilename())
Guy Benyei11169dd2012-12-18 14:30:41 +0000137 return;
138
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000139 if (auto *LBF = dyn_cast<llvm::DILexicalBlockFile>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000140 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000141 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlockFile(
142 LBF->getScope(), getOrCreateFile(CurLoc)));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000143 } else if (isa<llvm::DILexicalBlock>(Scope) ||
144 isa<llvm::DISubprogram>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000145 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000146 LexicalBlockStack.emplace_back(
147 DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000148 }
149}
150
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000151llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000152 if (!Context)
153 return TheCU;
154
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000155 auto I = RegionMap.find(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +0000156 if (I != RegionMap.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000157 llvm::Metadata *V = I->second;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000158 return dyn_cast_or_null<llvm::DIScope>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000159 }
160
161 // Check namespace.
162 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000163 return getOrCreateNameSpace(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +0000164
David Blaikiebfa52742013-04-19 06:56:38 +0000165 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context))
166 if (!RDecl->isDependentType())
167 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Eric Christophere7b87e52014-10-26 23:40:33 +0000168 getOrCreateMainFile());
Guy Benyei11169dd2012-12-18 14:30:41 +0000169 return TheCU;
170}
171
Guy Benyei11169dd2012-12-18 14:30:41 +0000172StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000173 assert(FD && "Invalid FunctionDecl!");
Guy Benyei11169dd2012-12-18 14:30:41 +0000174 IdentifierInfo *FII = FD->getIdentifier();
Eric Christophere7b87e52014-10-26 23:40:33 +0000175 FunctionTemplateSpecializationInfo *Info =
176 FD->getTemplateSpecializationInfo();
Guy Benyei11169dd2012-12-18 14:30:41 +0000177 if (!Info && FII)
178 return FII->getName();
179
180 // Otherwise construct human readable name for debug info.
Benjamin Kramer9170e912013-02-22 15:46:01 +0000181 SmallString<128> NS;
182 llvm::raw_svector_ostream OS(NS);
183 FD->printName(OS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000184
185 // Add any template specialization args.
186 if (Info) {
187 const TemplateArgumentList *TArgs = Info->TemplateArguments;
188 const TemplateArgument *Args = TArgs->data();
189 unsigned NumArgs = TArgs->size();
190 PrintingPolicy Policy(CGM.getLangOpts());
Benjamin Kramer9170e912013-02-22 15:46:01 +0000191 TemplateSpecializationType::PrintTemplateArgumentList(OS, Args, NumArgs,
192 Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000193 }
194
195 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000196 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000197}
198
199StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
200 SmallString<256> MethodName;
201 llvm::raw_svector_ostream OS(MethodName);
202 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
203 const DeclContext *DC = OMD->getDeclContext();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000204 if (const ObjCImplementationDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000205 dyn_cast<const ObjCImplementationDecl>(DC)) {
206 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000207 } else if (const ObjCInterfaceDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000208 dyn_cast<const ObjCInterfaceDecl>(DC)) {
209 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000210 } else if (const ObjCCategoryImplDecl *OCD =
Eric Christophere7b87e52014-10-26 23:40:33 +0000211 dyn_cast<const ObjCCategoryImplDecl>(DC)) {
212 OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '('
213 << OCD->getIdentifier()->getNameStart() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000214 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000215 // We can extract the type of the class from the self pointer.
Eric Christophere7b87e52014-10-26 23:40:33 +0000216 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000217 QualType ClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000218 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000219 ClassTy.print(OS, PrintingPolicy(LangOptions()));
220 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000221 }
222 OS << ' ' << OMD->getSelector().getAsString() << ']';
223
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000224 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000225}
226
Guy Benyei11169dd2012-12-18 14:30:41 +0000227StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000228 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000229}
230
Eric Christophere7b87e52014-10-26 23:40:33 +0000231StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
David Blaikie65813a32014-04-02 18:21:09 +0000232 // quick optimization to avoid having to intern strings that are already
233 // stored reliably elsewhere
234 if (!isa<ClassTemplateSpecializationDecl>(RD))
Guy Benyei11169dd2012-12-18 14:30:41 +0000235 return RD->getName();
236
David Blaikie65813a32014-04-02 18:21:09 +0000237 SmallString<128> Name;
Benjamin Kramer9170e912013-02-22 15:46:01 +0000238 {
David Blaikie65813a32014-04-02 18:21:09 +0000239 llvm::raw_svector_ostream OS(Name);
240 RD->getNameForDiagnostic(OS, CGM.getContext().getPrintingPolicy(),
241 /*Qualified*/ false);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000242 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000243
244 // Copy this name on the side and use its reference.
David Blaikie65813a32014-04-02 18:21:09 +0000245 return internString(Name);
Guy Benyei11169dd2012-12-18 14:30:41 +0000246}
247
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000248llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000249 if (!Loc.isValid())
250 // If Location is not valid then use main input file.
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +0000251 return DBuilder.createFile(TheCU->getFilename(), TheCU->getDirectory());
Guy Benyei11169dd2012-12-18 14:30:41 +0000252
253 SourceManager &SM = CGM.getContext().getSourceManager();
254 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
255
256 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
257 // If the location is not valid then use main input file.
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +0000258 return DBuilder.createFile(TheCU->getFilename(), TheCU->getDirectory());
Guy Benyei11169dd2012-12-18 14:30:41 +0000259
260 // Cache the results.
261 const char *fname = PLoc.getFilename();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000262 auto it = DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000263
264 if (it != DIFileCache.end()) {
265 // Verify that the information still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000266 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000267 return cast<llvm::DIFile>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000268 }
269
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000270 llvm::DIFile *F =
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +0000271 DBuilder.createFile(PLoc.getFilename(), getCurrentDirname());
Guy Benyei11169dd2012-12-18 14:30:41 +0000272
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000273 DIFileCache[fname].reset(F);
Guy Benyei11169dd2012-12-18 14:30:41 +0000274 return F;
275}
276
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000277llvm::DIFile *CGDebugInfo::getOrCreateMainFile() {
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +0000278 return DBuilder.createFile(TheCU->getFilename(), TheCU->getDirectory());
Guy Benyei11169dd2012-12-18 14:30:41 +0000279}
280
Guy Benyei11169dd2012-12-18 14:30:41 +0000281unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
282 if (Loc.isInvalid() && CurLoc.isInvalid())
283 return 0;
284 SourceManager &SM = CGM.getContext().getSourceManager();
285 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000286 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000287}
288
Adrian Prantlc7822422013-03-12 20:43:25 +0000289unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000290 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000291 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000292 return 0;
293
294 // If the location is invalid then use the current column.
295 if (Loc.isInvalid() && CurLoc.isInvalid())
296 return 0;
297 SourceManager &SM = CGM.getContext().getSourceManager();
298 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000299 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000300}
301
302StringRef CGDebugInfo::getCurrentDirname() {
303 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
304 return CGM.getCodeGenOpts().DebugCompilationDir;
305
306 if (!CWDName.empty())
307 return CWDName;
308 SmallString<256> CWD;
309 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000310 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000311}
312
Guy Benyei11169dd2012-12-18 14:30:41 +0000313void CGDebugInfo::CreateCompileUnit() {
314
David Blaikieaabde052014-05-14 00:29:00 +0000315 // Should we be asking the SourceManager for the main file name, instead of
316 // accepting it as an argument? This just causes the main file name to
317 // mismatch with source locations and create extra lexical scopes or
318 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
319 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
320 // because that's what the SourceManager says)
321
Guy Benyei11169dd2012-12-18 14:30:41 +0000322 // Get absolute path name.
323 SourceManager &SM = CGM.getContext().getSourceManager();
324 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
325 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000326 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000327
328 // The main file name provided via the "-main-file-name" option contains just
329 // the file name itself with no path information. This file name may have had
330 // a relative path, so we look into the actual file entry for the main
331 // file to determine the real absolute path for the file.
332 std::string MainFileDir;
333 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
334 MainFileDir = MainFile->getDir()->getName();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000335 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000336 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
337 llvm::sys::path::append(MainFileDirSS, MainFileName);
338 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000339 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000340 }
341
342 // Save filename string.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000343 StringRef Filename = internString(MainFileName);
Eric Christopherf1545832013-02-22 23:50:16 +0000344
345 // Save split dwarf file string.
346 std::string SplitDwarfFile = CGM.getCodeGenOpts().SplitDwarfFile;
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000347 StringRef SplitDwarfFilename = internString(SplitDwarfFile);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000348
Ed Masteda706022014-05-07 12:49:30 +0000349 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000350 const LangOptions &LO = CGM.getLangOpts();
351 if (LO.CPlusPlus) {
352 if (LO.ObjC1)
353 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
354 else
355 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
356 } else if (LO.ObjC1) {
357 LangTag = llvm::dwarf::DW_LANG_ObjC;
358 } else if (LO.C99) {
359 LangTag = llvm::dwarf::DW_LANG_C99;
360 } else {
361 LangTag = llvm::dwarf::DW_LANG_C89;
362 }
363
364 std::string Producer = getClangFullVersion();
365
366 // Figure out which version of the ObjC runtime we have.
367 unsigned RuntimeVers = 0;
368 if (LO.ObjC1)
369 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
370
371 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000372 // FIXME - Eliminate TheCU.
Eric Christophere4200a22014-02-27 01:25:08 +0000373 TheCU = DBuilder.createCompileUnit(
374 LangTag, Filename, getCurrentDirname(), Producer, LO.Optimize,
375 CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers, SplitDwarfFilename,
Diego Novillo913690c2014-06-24 17:02:17 +0000376 DebugKind <= CodeGenOptions::DebugLineTablesOnly
Eric Christophere4200a22014-02-27 01:25:08 +0000377 ? llvm::DIBuilder::LineTablesOnly
Diego Novillo913690c2014-06-24 17:02:17 +0000378 : llvm::DIBuilder::FullDebug,
Adrian Prantlbe81cf52015-05-21 20:37:26 +0000379 0 /* DWOid */,
Diego Novillo913690c2014-06-24 17:02:17 +0000380 DebugKind != CodeGenOptions::LocTrackingOnly);
Guy Benyei11169dd2012-12-18 14:30:41 +0000381}
382
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000383llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000384 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000385 StringRef BTName;
386 switch (BT->getKind()) {
387#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000388#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000389#include "clang/AST/BuiltinTypes.def"
390 case BuiltinType::Dependent:
391 llvm_unreachable("Unexpected builtin type");
392 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000393 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000394 case BuiltinType::Void:
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000395 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +0000396 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000397 if (!ClassTy)
398 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
399 "objc_class", TheCU,
400 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000401 return ClassTy;
402 case BuiltinType::ObjCId: {
403 // typedef struct objc_class *Class;
404 // typedef struct objc_object {
405 // Class isa;
406 // } *id;
407
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000408 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000409 return ObjTy;
410
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000411 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000412 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
413 "objc_class", TheCU,
414 getOrCreateMainFile(), 0);
415
416 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000417
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000418 auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000419
Eric Christopher5c7ee8b2013-04-02 22:59:11 +0000420 ObjTy =
David Blaikie6d4fe152013-02-25 01:07:08 +0000421 DBuilder.createStructType(TheCU, "objc_object", getOrCreateMainFile(),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000422 0, 0, 0, 0, nullptr, llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000423
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000424 DBuilder.replaceArrays(
425 ObjTy,
426 DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
427 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0, 0, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000428 return ObjTy;
429 }
430 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000431 if (!SelTy)
432 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
433 "objc_selector", TheCU,
434 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000435 return SelTy;
436 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000437
438 case BuiltinType::OCLImage1d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000439 return getOrCreateStructPtrType("opencl_image1d_t", OCLImage1dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000440 case BuiltinType::OCLImage1dArray:
Eric Christopherb2a008c2013-05-16 00:45:12 +0000441 return getOrCreateStructPtrType("opencl_image1d_array_t",
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000442 OCLImage1dArrayDITy);
443 case BuiltinType::OCLImage1dBuffer:
444 return getOrCreateStructPtrType("opencl_image1d_buffer_t",
445 OCLImage1dBufferDITy);
446 case BuiltinType::OCLImage2d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000447 return getOrCreateStructPtrType("opencl_image2d_t", OCLImage2dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000448 case BuiltinType::OCLImage2dArray:
449 return getOrCreateStructPtrType("opencl_image2d_array_t",
450 OCLImage2dArrayDITy);
451 case BuiltinType::OCLImage3d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000452 return getOrCreateStructPtrType("opencl_image3d_t", OCLImage3dDITy);
Guy Benyei61054192013-02-07 10:55:47 +0000453 case BuiltinType::OCLSampler:
Eric Christophere7b87e52014-10-26 23:40:33 +0000454 return DBuilder.createBasicType(
455 "opencl_sampler_t", CGM.getContext().getTypeSize(BT),
456 CGM.getContext().getTypeAlign(BT), llvm::dwarf::DW_ATE_unsigned);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000457 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000458 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000459
Guy Benyei11169dd2012-12-18 14:30:41 +0000460 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000461 case BuiltinType::Char_U:
462 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
463 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000464 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000465 case BuiltinType::SChar:
466 Encoding = llvm::dwarf::DW_ATE_signed_char;
467 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000468 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000469 case BuiltinType::Char32:
470 Encoding = llvm::dwarf::DW_ATE_UTF;
471 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000472 case BuiltinType::UShort:
473 case BuiltinType::UInt:
474 case BuiltinType::UInt128:
475 case BuiltinType::ULong:
476 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000477 case BuiltinType::ULongLong:
478 Encoding = llvm::dwarf::DW_ATE_unsigned;
479 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000480 case BuiltinType::Short:
481 case BuiltinType::Int:
482 case BuiltinType::Int128:
483 case BuiltinType::Long:
484 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000485 case BuiltinType::LongLong:
486 Encoding = llvm::dwarf::DW_ATE_signed;
487 break;
488 case BuiltinType::Bool:
489 Encoding = llvm::dwarf::DW_ATE_boolean;
490 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000491 case BuiltinType::Half:
492 case BuiltinType::Float:
493 case BuiltinType::LongDouble:
Eric Christophere7b87e52014-10-26 23:40:33 +0000494 case BuiltinType::Double:
495 Encoding = llvm::dwarf::DW_ATE_float;
496 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000497 }
498
499 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000500 case BuiltinType::Long:
501 BTName = "long int";
502 break;
503 case BuiltinType::LongLong:
504 BTName = "long long int";
505 break;
506 case BuiltinType::ULong:
507 BTName = "long unsigned int";
508 break;
509 case BuiltinType::ULongLong:
510 BTName = "long long unsigned int";
511 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000512 default:
513 BTName = BT->getName(CGM.getLangOpts());
514 break;
515 }
516 // Bit size, align and offset of the type.
517 uint64_t Size = CGM.getContext().getTypeSize(BT);
518 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000519 return DBuilder.createBasicType(BTName, Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000520}
521
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000522llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000523 // Bit size, align and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000524 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000525 if (Ty->isComplexIntegerType())
526 Encoding = llvm::dwarf::DW_ATE_lo_user;
527
528 uint64_t Size = CGM.getContext().getTypeSize(Ty);
529 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000530 return DBuilder.createBasicType("complex", Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000531}
532
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000533llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
534 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000535 QualifierCollector Qc;
536 const Type *T = Qc.strip(Ty);
537
538 // Ignore these qualifiers for now.
539 Qc.removeObjCGCAttr();
540 Qc.removeAddressSpace();
541 Qc.removeObjCLifetime();
542
543 // We will create one Derived type for one qualifier and recurse to handle any
544 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000545 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000546 if (Qc.hasConst()) {
547 Tag = llvm::dwarf::DW_TAG_const_type;
548 Qc.removeConst();
549 } else if (Qc.hasVolatile()) {
550 Tag = llvm::dwarf::DW_TAG_volatile_type;
551 Qc.removeVolatile();
552 } else if (Qc.hasRestrict()) {
553 Tag = llvm::dwarf::DW_TAG_restrict_type;
554 Qc.removeRestrict();
555 } else {
556 assert(Qc.empty() && "Unknown type qualifier for debug info");
557 return getOrCreateType(QualType(T, 0), Unit);
558 }
559
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000560 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000561
562 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
563 // CVR derived types.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000564 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000565}
566
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000567llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
568 llvm::DIFile *Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000569
570 // The frontend treats 'id' as a typedef to an ObjCObjectType,
571 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
572 // debug info, we want to emit 'id' in both cases.
573 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000574 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000575
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000576 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
577 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000578}
579
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000580llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
581 llvm::DIFile *Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000582 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000583 Ty->getPointeeType(), Unit);
584}
585
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000586/// \return whether a C++ mangling exists for the type defined by TD.
587static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
588 switch (TheCU->getSourceLanguage()) {
589 case llvm::dwarf::DW_LANG_C_plus_plus:
590 return true;
591 case llvm::dwarf::DW_LANG_ObjC_plus_plus:
592 return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
593 default:
594 return false;
595 }
596}
597
Manman Rene0064d82013-08-29 23:19:58 +0000598/// In C++ mode, types have linkage, so we can rely on the ODR and
599/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000600static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
601 CodeGenModule &CGM,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000602 llvm::DICompileUnit *TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000603 SmallString<256> FullName;
Manman Rene0064d82013-08-29 23:19:58 +0000604 const TagDecl *TD = Ty->getDecl();
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000605
606 if (!hasCXXMangling(TD, TheCU) || !TD->isExternallyVisible())
Manman Rene0064d82013-08-29 23:19:58 +0000607 return FullName;
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000608
Manman Rene0064d82013-08-29 23:19:58 +0000609 // Microsoft Mangler does not have support for mangleCXXRTTIName yet.
610 if (CGM.getTarget().getCXXABI().isMicrosoft())
611 return FullName;
612
613 // TODO: This is using the RTTI name. Is there a better way to get
614 // a unique string for a type?
615 llvm::raw_svector_ostream Out(FullName);
616 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
Manman Rene0064d82013-08-29 23:19:58 +0000617 return FullName;
618}
619
Adrian Prantl3e8bad42015-07-08 21:18:34 +0000620/// \return the approproate DWARF tag for a composite type.
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000621static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
622 llvm::dwarf::Tag Tag;
623 if (RD->isStruct() || RD->isInterface())
624 Tag = llvm::dwarf::DW_TAG_structure_type;
625 else if (RD->isUnion())
626 Tag = llvm::dwarf::DW_TAG_union_type;
627 else {
628 // FIXME: This could be a struct type giving a default visibility different
629 // than C++ class type, but needs llvm metadata changes first.
630 assert(RD->isClass());
631 Tag = llvm::dwarf::DW_TAG_class_type;
632 }
633 return Tag;
634}
635
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000636llvm::DICompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000637CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000638 llvm::DIScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000639 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000640 if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
641 return cast<llvm::DICompositeType>(T);
642 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +0000643 unsigned Line = getLineNumber(RD->getLocation());
644 StringRef RDName = getClassName(RD);
645
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000646 uint64_t Size = 0;
647 uint64_t Align = 0;
648
649 const RecordDecl *D = RD->getDefinition();
650 if (D && D->isCompleteDefinition()) {
651 Size = CGM.getContext().getTypeSize(Ty);
652 Align = CGM.getContext().getTypeAlign(Ty);
653 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000654
655 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000656 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000657 llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000658 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000659 llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000660 ReplaceMap.emplace_back(
661 std::piecewise_construct, std::make_tuple(Ty),
662 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000663 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000664}
665
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000666llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000667 const Type *Ty,
668 QualType PointeeTy,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000669 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000670 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
671 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
David Blaikie99dab3b2013-09-04 22:03:57 +0000672 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit));
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000673
Guy Benyei11169dd2012-12-18 14:30:41 +0000674 // Bit size, align and offset of the type.
675 // Size is always the size of a pointer. We can't use getTypeSize here
676 // because that does not return the correct value for references.
677 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000678 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000679 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
680
David Blaikie99dab3b2013-09-04 22:03:57 +0000681 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
682 Align);
Guy Benyei11169dd2012-12-18 14:30:41 +0000683}
684
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000685llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
686 llvm::DIType *&Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000687 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000688 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000689 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
690 TheCU, getOrCreateMainFile(), 0);
691 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
692 Cache = DBuilder.createPointerType(Cache, Size);
693 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000694}
695
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000696llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
697 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000698 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000699 QualType FType;
700 uint64_t FieldSize, FieldOffset;
701 unsigned FieldAlign;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000702 llvm::DINodeArray Elements;
Guy Benyei11169dd2012-12-18 14:30:41 +0000703
704 FieldOffset = 0;
705 FType = CGM.getContext().UnsignedLongTy;
706 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
707 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
708
709 Elements = DBuilder.getOrCreateArray(EltTys);
710 EltTys.clear();
711
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000712 unsigned Flags = llvm::DINode::FlagAppleBlock;
Adrian Prantl498fff62015-07-06 21:31:35 +0000713 unsigned LineNo = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000714
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000715 auto *EltTy =
Adrian Prantl498fff62015-07-06 21:31:35 +0000716 DBuilder.createStructType(Unit, "__block_descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000717 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000718
719 // Bit size, align and offset of the type.
720 uint64_t Size = CGM.getContext().getTypeSize(Ty);
721
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000722 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000723
724 FieldOffset = 0;
725 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
726 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
727 FType = CGM.getContext().IntTy;
728 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
729 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000730 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000731 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
732
733 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000734 FieldSize = CGM.getContext().getTypeSize(Ty);
735 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Adrian Prantl498fff62015-07-06 21:31:35 +0000736 EltTys.push_back(DBuilder.createMemberType(Unit, "__descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000737 FieldSize, FieldAlign, FieldOffset,
738 0, DescTy));
Guy Benyei11169dd2012-12-18 14:30:41 +0000739
740 FieldOffset += FieldSize;
741 Elements = DBuilder.getOrCreateArray(EltTys);
742
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000743 // The __block_literal_generic structs are marked with a special
744 // DW_AT_APPLE_BLOCK attribute and are an implementation detail only
745 // the debugger needs to know about. To allow type uniquing, emit
746 // them without a name or a location.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000747 EltTy =
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000748 DBuilder.createStructType(Unit, "", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000749 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000750
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000751 return DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000752}
753
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000754llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
755 llvm::DIFile *Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000756 assert(Ty->isTypeAlias());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000757 llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000758
759 SmallString<128> NS;
760 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000761 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
762 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000763
764 TemplateSpecializationType::PrintTemplateArgumentList(
765 OS, Ty->getArgs(), Ty->getNumArgs(),
766 CGM.getContext().getPrintingPolicy());
767
Eric Christophere7b87e52014-10-26 23:40:33 +0000768 TypeAliasDecl *AliasDecl = cast<TypeAliasTemplateDecl>(
769 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000770
771 SourceLocation Loc = AliasDecl->getLocation();
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000772 return DBuilder.createTypedef(
773 Src, internString(OS.str()), getOrCreateFile(Loc), getLineNumber(Loc),
774 getContextDescriptor(cast<Decl>(AliasDecl->getDeclContext())));
David Blaikief1b382e2014-04-06 17:14:06 +0000775}
776
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000777llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
778 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000779 // We don't set size information, but do specify where the typedef was
780 // declared.
David Blaikiebe822ed2015-07-01 18:07:22 +0000781 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000782
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000783 // Typedefs are derived from some other type.
784 return DBuilder.createTypedef(
785 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
786 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
David Blaikiebe822ed2015-07-01 18:07:22 +0000787 getContextDescriptor(cast<Decl>(Ty->getDecl()->getDeclContext())));
Guy Benyei11169dd2012-12-18 14:30:41 +0000788}
789
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000790llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
791 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000792 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000793
794 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000795 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000796
797 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000798 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000799 if (isa<FunctionNoProtoType>(Ty))
800 EltTys.push_back(DBuilder.createUnspecifiedParameter());
801 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
Alp Toker9cacbab2014-01-20 20:26:09 +0000802 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
803 EltTys.push_back(getOrCreateType(FPT->getParamType(i), Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000804 if (FPT->isVariadic())
805 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000806 }
807
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000808 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Guy Benyei11169dd2012-12-18 14:30:41 +0000809 return DBuilder.createSubroutineType(Unit, EltTypeArray);
810}
811
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000812/// Convert an AccessSpecifier into the corresponding DINode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +0000813/// As an optimization, return 0 if the access specifier equals the
814/// default for the containing type.
815static unsigned getAccessFlag(AccessSpecifier Access, const RecordDecl *RD) {
816 AccessSpecifier Default = clang::AS_none;
817 if (RD && RD->isClass())
818 Default = clang::AS_private;
819 else if (RD && (RD->isStruct() || RD->isUnion()))
820 Default = clang::AS_public;
821
822 if (Access == Default)
823 return 0;
824
Eric Christophere7b87e52014-10-26 23:40:33 +0000825 switch (Access) {
826 case clang::AS_private:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000827 return llvm::DINode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +0000828 case clang::AS_protected:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000829 return llvm::DINode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +0000830 case clang::AS_public:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000831 return llvm::DINode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +0000832 case clang::AS_none:
833 return 0;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000834 }
835 llvm_unreachable("unexpected access enumerator");
836}
Guy Benyei11169dd2012-12-18 14:30:41 +0000837
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000838llvm::DIType *CGDebugInfo::createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000839 StringRef name, QualType type, uint64_t sizeInBitsOverride,
840 SourceLocation loc, AccessSpecifier AS, uint64_t offsetInBits,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000841 llvm::DIFile *tunit, llvm::DIScope *scope, const RecordDecl *RD) {
842 llvm::DIType *debugType = getOrCreateType(type, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000843
844 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000845 llvm::DIFile *file = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000846 unsigned line = getLineNumber(loc);
847
David Majnemer34b57492014-07-30 01:30:47 +0000848 uint64_t SizeInBits = 0;
849 unsigned AlignInBits = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000850 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +0000851 TypeInfo TI = CGM.getContext().getTypeInfo(type);
852 SizeInBits = TI.Width;
853 AlignInBits = TI.Align;
Guy Benyei11169dd2012-12-18 14:30:41 +0000854
855 if (sizeInBitsOverride)
David Majnemer34b57492014-07-30 01:30:47 +0000856 SizeInBits = sizeInBitsOverride;
Guy Benyei11169dd2012-12-18 14:30:41 +0000857 }
858
Adrian Prantl21361fb2014-08-29 22:44:27 +0000859 unsigned flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +0000860 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
861 AlignInBits, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +0000862}
863
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000864void CGDebugInfo::CollectRecordLambdaFields(
865 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000866 llvm::DIType *RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +0000867 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
868 // has the name and the location of the variable so we should iterate over
869 // both concurrently.
870 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
871 RecordDecl::field_iterator Field = CXXDecl->field_begin();
872 unsigned fieldno = 0;
873 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +0000874 E = CXXDecl->captures_end();
875 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000876 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +0000877 if (C.capturesVariable()) {
878 VarDecl *V = C.getCapturedVar();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000879 llvm::DIFile *VUnit = getOrCreateFile(C.getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000880 StringRef VName = V->getName();
881 uint64_t SizeInBitsOverride = 0;
882 if (Field->isBitField()) {
883 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
884 assert(SizeInBitsOverride && "found named 0-width bitfield");
885 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000886 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000887 VName, Field->getType(), SizeInBitsOverride, C.getLocation(),
888 Field->getAccess(), layout.getFieldOffset(fieldno), VUnit, RecordTy,
889 CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000890 elements.push_back(fieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +0000891 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +0000892 // TODO: Need to handle 'this' in some way by probably renaming the
893 // this of the lambda class and having a field member of 'this' or
894 // by using AT_object_pointer for the function and having that be
895 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +0000896 FieldDecl *f = *Field;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000897 llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000898 QualType type = f->getType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000899 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000900 "this", type, 0, f->getLocation(), f->getAccess(),
901 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000902
903 elements.push_back(fieldType);
904 }
905 }
906}
907
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000908llvm::DIDerivedType *
909CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +0000910 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000911 // Create the descriptor for the static variable, with or without
912 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +0000913 Var = Var->getCanonicalDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000914 llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
915 llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher91a31902013-01-16 01:22:32 +0000916
Eric Christopher91a31902013-01-16 01:22:32 +0000917 unsigned LineNumber = getLineNumber(Var->getLocation());
918 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +0000919 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +0000920 if (Var->getInit()) {
921 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +0000922 if (Value) {
923 if (Value->isInt())
924 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
925 if (Value->isFloat())
926 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
927 }
Eric Christopher91a31902013-01-16 01:22:32 +0000928 }
929
Adrian Prantl21361fb2014-08-29 22:44:27 +0000930 unsigned Flags = getAccessFlag(Var->getAccess(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000931 llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
David Blaikieae019462013-08-15 22:50:29 +0000932 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000933 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +0000934 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +0000935}
936
Eric Christophere7b87e52014-10-26 23:40:33 +0000937void CGDebugInfo::CollectRecordNormalField(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000938 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
939 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +0000940 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000941 StringRef name = field->getName();
942 QualType type = field->getType();
943
944 // Ignore unnamed fields unless they're anonymous structs/unions.
945 if (name.empty() && !type->isRecordType())
946 return;
947
948 uint64_t SizeInBitsOverride = 0;
949 if (field->isBitField()) {
950 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
951 assert(SizeInBitsOverride && "found named 0-width bitfield");
952 }
953
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000954 llvm::DIType *fieldType =
Eric Christophere7b87e52014-10-26 23:40:33 +0000955 createFieldType(name, type, SizeInBitsOverride, field->getLocation(),
956 field->getAccess(), OffsetInBits, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +0000957
958 elements.push_back(fieldType);
959}
960
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000961void CGDebugInfo::CollectRecordFields(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000962 const RecordDecl *record, llvm::DIFile *tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000963 SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000964 llvm::DICompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000965 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
966
Eric Christopher91a31902013-01-16 01:22:32 +0000967 if (CXXDecl && CXXDecl->isLambda())
968 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
969 else {
970 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +0000971
Eric Christopher91a31902013-01-16 01:22:32 +0000972 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +0000973 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +0000974
Eric Christopher91a31902013-01-16 01:22:32 +0000975 // Static and non-static members should appear in the same order as
976 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +0000977 for (const auto *I : record->decls())
978 if (const auto *V = dyn_cast<VarDecl>(I)) {
David Blaikiece763042013-08-20 21:49:21 +0000979 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000980 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +0000981 if (MI != StaticDataMemberCache.end()) {
982 assert(MI->second &&
983 "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +0000984 elements.push_back(MI->second);
Adrian Prantl21361fb2014-08-29 22:44:27 +0000985 } else {
986 auto Field = CreateRecordStaticField(V, RecordTy, record);
987 elements.push_back(Field);
988 }
Aaron Ballman629afae2014-03-07 19:56:05 +0000989 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000990 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
991 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +0000992
993 // Bump field number for next field.
994 ++fieldNo;
Guy Benyei11169dd2012-12-18 14:30:41 +0000995 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000996 }
997}
998
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000999llvm::DISubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +00001000CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001001 llvm::DIFile *Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001002 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001003 if (Method->isStatic())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001004 return cast_or_null<llvm::DISubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001005 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001006 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1007 Func, Unit);
1008}
David Blaikie2aaf0652013-01-07 22:24:59 +00001009
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001010llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1011 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001012 // Add "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001013 llvm::DITypeRefArray Args(
1014 cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001015 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001016 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001017
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001018 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001019
1020 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001021 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001022
David Blaikie2aaf0652013-01-07 22:24:59 +00001023 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001024 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001025 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1026 // Create pointer type directly in this case.
1027 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1028 QualType PointeeTy = ThisPtrTy->getPointeeType();
1029 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001030 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie2aaf0652013-01-07 22:24:59 +00001031 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001032 llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1033 llvm::DIType *ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001034 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001035 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001036 // TODO: This and the artificial type below are misleading, the
1037 // types aren't artificial the argument is, but the current
1038 // metadata doesn't represent that.
1039 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1040 Elts.push_back(ThisPtrType);
1041 } else {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001042 llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001043 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001044 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1045 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001046 }
1047
1048 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001049 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1050 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001051
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001052 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001053
Adrian Prantl0630eb72013-12-18 21:48:18 +00001054 unsigned Flags = 0;
1055 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001056 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001057 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001058 Flags |= llvm::DINode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001059
1060 return DBuilder.createSubroutineType(Unit, EltTypeArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001061}
1062
Eric Christopherb2a008c2013-05-16 00:45:12 +00001063/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001064/// inside a function.
1065static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1066 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1067 return isFunctionLocalClass(NRD);
1068 if (isa<FunctionDecl>(RD->getDeclContext()))
1069 return true;
1070 return false;
1071}
1072
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001073llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1074 const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001075 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001076 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001077
Guy Benyei11169dd2012-12-18 14:30:41 +00001078 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001079 llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001080
1081 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1082 // make sense to give a single ctor/dtor a linkage name.
1083 StringRef MethodLinkageName;
1084 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1085 MethodLinkageName = CGM.getMangledName(Method);
1086
1087 // Get the location for the method.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001088 llvm::DIFile *MethodDefUnit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00001089 unsigned MethodLine = 0;
1090 if (!Method->isImplicit()) {
1091 MethodDefUnit = getOrCreateFile(Method->getLocation());
1092 MethodLine = getLineNumber(Method->getLocation());
1093 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001094
1095 // Collect virtual method info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001096 llvm::DIType *ContainingType = nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001097 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001098 unsigned VIndex = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001099
Guy Benyei11169dd2012-12-18 14:30:41 +00001100 if (Method->isVirtual()) {
1101 if (Method->isPure())
1102 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1103 else
1104 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001105
Guy Benyei11169dd2012-12-18 14:30:41 +00001106 // It doesn't make sense to give a virtual destructor a vtable index,
1107 // since a single destructor has two entries in the vtable.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001108 // FIXME: Add proper support for debug info for virtual calls in
1109 // the Microsoft ABI, where we may use multiple vptrs to make a vftable
1110 // lookup if we have multiple or virtual inheritance.
1111 if (!isa<CXXDestructorDecl>(Method) &&
1112 !CGM.getTarget().getCXXABI().isMicrosoft())
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001113 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
Guy Benyei11169dd2012-12-18 14:30:41 +00001114 ContainingType = RecordTy;
1115 }
1116
1117 unsigned Flags = 0;
1118 if (Method->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001119 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001120 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
Guy Benyei11169dd2012-12-18 14:30:41 +00001121 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1122 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001123 Flags |= llvm::DINode::FlagExplicit;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001124 } else if (const CXXConversionDecl *CXXC =
Eric Christophere7b87e52014-10-26 23:40:33 +00001125 dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001126 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001127 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001128 }
1129 if (Method->hasPrototype())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001130 Flags |= llvm::DINode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001131 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001132 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001133 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001134 Flags |= llvm::DINode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001135
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001136 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1137 llvm::DISubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001138 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
1139 MethodTy, /*isLocalToUnit=*/false,
1140 /* isDefinition=*/false, Virtuality, VIndex, ContainingType, Flags,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00001141 CGM.getLangOpts().Optimize, nullptr, TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001142
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001143 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001144
1145 return SP;
1146}
1147
Eric Christophere7b87e52014-10-26 23:40:33 +00001148void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001149 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1150 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001151
1152 // Since we want more than just the individual member decls if we
1153 // have templated functions iterate over every declaration to gather
1154 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001155 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001156 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1157 // If the member is implicit, don't add it to the member list. This avoids
1158 // the member being added to type units by LLVM, while still allowing it
1159 // to be emitted into the type declaration/reference inside the compile
1160 // unit.
Paul Robinson6a7511b2015-06-25 17:50:43 +00001161 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
David Blaikie6dddfe32014-10-06 05:52:27 +00001162 // FIXME: Handle Using(Shadow?)Decls here to create
1163 // DW_TAG_imported_declarations inside the class for base decls brought into
1164 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1165 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1166 // referenced)
Paul Robinson6a7511b2015-06-25 17:50:43 +00001167 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
David Blaikiefd580722014-10-06 05:18:55 +00001168 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001169
1170 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1171 continue;
1172
David Blaikiefd580722014-10-06 05:18:55 +00001173 // Reuse the existing member function declaration if it exists.
1174 // It may be associated with the declaration of the type & should be
1175 // reused as we're building the definition.
1176 //
1177 // This situation can arise in the vtable-based debug info reduction where
1178 // implicit members are emitted in a non-vtable TU.
1179 auto MI = SPCache.find(Method->getCanonicalDecl());
1180 EltTys.push_back(MI == SPCache.end()
1181 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001182 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001183 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001184}
Guy Benyei11169dd2012-12-18 14:30:41 +00001185
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001186void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001187 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001188 llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001189 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Aaron Ballman574705e2014-03-13 15:41:46 +00001190 for (const auto &BI : RD->bases()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001191 unsigned BFlags = 0;
1192 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001193
Guy Benyei11169dd2012-12-18 14:30:41 +00001194 const CXXRecordDecl *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001195 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001196
Aaron Ballman574705e2014-03-13 15:41:46 +00001197 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001198 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1199 // virtual base offset offset is -ve. The code generator emits dwarf
1200 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001201 BaseOffset = 0 - CGM.getItaniumVTableContext()
1202 .getVirtualBaseOffsetOffset(RD, Base)
1203 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001204 } else {
1205 // In the MS ABI, store the vbtable offset, which is analogous to the
1206 // vbase offset offset in Itanium.
1207 BaseOffset =
1208 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1209 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001210 BFlags = llvm::DINode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001211 } else
1212 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1213 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1214 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001215
Adrian Prantl21361fb2014-08-29 22:44:27 +00001216 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001217 llvm::DIType *DTy = DBuilder.createInheritance(
Eric Christophere7b87e52014-10-26 23:40:33 +00001218 RecordTy, getOrCreateType(BI.getType(), Unit), BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001219 EltTys.push_back(DTy);
1220 }
1221}
1222
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001223llvm::DINodeArray
Eric Christophere7b87e52014-10-26 23:40:33 +00001224CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1225 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001226 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001227 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001228 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1229 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001230 StringRef Name;
1231 if (TPList)
1232 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001233 switch (TA.getKind()) {
1234 case TemplateArgument::Type: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001235 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001236 TemplateParams.push_back(
1237 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001238 } break;
1239 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001240 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001241 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1242 TheCU, Name, TTy,
1243 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001244 } break;
1245 case TemplateArgument::Declaration: {
1246 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001247 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001248 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001249 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001250 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001251 // Variable pointer template parameters have a value that is the address
1252 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001253 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001254 V = CGM.GetAddrOfGlobalVar(VD);
1255 // Member function pointers have special support for building them, though
1256 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001257 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Majnemere2be95b2015-06-23 07:31:01 +00001258 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001259 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001260 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001261 // Member data pointers have special handling too to compute the fixed
1262 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001263 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001264 // These five lines (& possibly the above member function pointer
1265 // handling) might be able to be refactored to use similar code in
1266 // CodeGenModule::getMemberPointerConstant
1267 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1268 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001269 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001270 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001271 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001272 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1273 TheCU, Name, TTy,
1274 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001275 } break;
1276 case TemplateArgument::NullPtr: {
1277 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001278 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001279 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001280 // Special case member data pointer null values since they're actually -1
1281 // instead of zero.
1282 if (const MemberPointerType *MPT =
1283 dyn_cast<MemberPointerType>(T.getTypePtr()))
1284 // But treat member function pointers as simple zero integers because
1285 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1286 // CodeGen grows handling for values of non-null member function
1287 // pointers then perhaps we could remove this special case and rely on
1288 // EmitNullMemberPointer for member function pointers.
1289 if (MPT->isMemberDataPointer())
1290 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1291 if (!V)
1292 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001293 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1294 TheCU, Name, TTy, cast<llvm::Constant>(V)));
David Blaikie38079fd2013-05-10 21:53:14 +00001295 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001296 case TemplateArgument::Template:
1297 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001298 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001299 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1300 break;
1301 case TemplateArgument::Pack:
1302 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1303 TheCU, Name, nullptr,
1304 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1305 break;
David Majnemer5559d472013-08-24 08:21:10 +00001306 case TemplateArgument::Expression: {
1307 const Expr *E = TA.getAsExpr();
1308 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001309 if (E->isGLValue())
1310 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001311 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001312 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001313 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001314 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1315 TheCU, Name, TTy, cast<llvm::Constant>(V->stripPointerCasts())));
David Majnemer5559d472013-08-24 08:21:10 +00001316 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001317 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001318 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001319 case TemplateArgument::Null:
1320 llvm_unreachable(
1321 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001322 }
1323 }
1324 return DBuilder.getOrCreateArray(TemplateParams);
1325}
1326
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001327llvm::DINodeArray
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001328CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001329 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001330 if (FD->getTemplatedKind() ==
1331 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001332 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1333 ->getTemplate()
1334 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001335 return CollectTemplateParams(
1336 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001337 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001338 return llvm::DINodeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001339}
1340
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001341llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1342 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001343 // Always get the full list of parameters, not just the ones from
1344 // the specialization.
1345 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001346 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001347 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001348 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001349}
1350
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001351llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001352 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001353 return VTablePtrType;
1354
1355 ASTContext &Context = CGM.getContext();
1356
1357 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001358 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001359 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
1360 llvm::DIType *SubTy = DBuilder.createSubroutineType(Unit, SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001361 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001362 llvm::DIType *vtbl_ptr_type =
Eric Christophere7b87e52014-10-26 23:40:33 +00001363 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001364 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1365 return VTablePtrType;
1366}
1367
Guy Benyei11169dd2012-12-18 14:30:41 +00001368StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001369 // Copy the gdb compatible name on the side and use its reference.
1370 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001371}
1372
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001373void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001374 SmallVectorImpl<llvm::Metadata *> &EltTys) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001375 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1376
1377 // If there is a primary base then it will hold vtable info.
1378 if (RL.getPrimaryBase())
1379 return;
1380
1381 // If this class is not dynamic then there is not any vtable info to collect.
1382 if (!RD->isDynamicClass())
1383 return;
1384
1385 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001386 llvm::DIType *VPTR = DBuilder.createMemberType(
Eric Christophere7b87e52014-10-26 23:40:33 +00001387 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001388 llvm::DINode::FlagArtificial, getOrCreateVTablePtrType(Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001389 EltTys.push_back(VPTR);
1390}
1391
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001392llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001393 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001394 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001395 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001396 return T;
1397}
1398
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001399llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001400 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001401 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001402 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantl73409ce2013-03-11 18:33:46 +00001403 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001404 return T;
1405}
1406
David Blaikie483a9da2014-05-06 18:35:21 +00001407void CGDebugInfo::completeType(const EnumDecl *ED) {
1408 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1409 return;
1410 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001411 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001412 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001413 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001414 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001415 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001416 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001417 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001418}
1419
David Blaikieb2e86eb2013-08-15 20:49:17 +00001420void CGDebugInfo::completeType(const RecordDecl *RD) {
1421 if (DebugKind > CodeGenOptions::LimitedDebugInfo ||
1422 !CGM.getLangOpts().CPlusPlus)
1423 completeRequiredType(RD);
1424}
1425
1426void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
David Blaikie0856f662014-03-04 22:01:08 +00001427 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1428 return;
1429
David Blaikie6943dea2013-08-20 01:28:15 +00001430 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1431 if (CXXDecl->isDynamicClass())
1432 return;
1433
David Blaikieb2e86eb2013-08-15 20:49:17 +00001434 QualType Ty = CGM.getContext().getRecordType(RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001435 llvm::DIType *T = getTypeOrNull(Ty);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001436 if (T && T->isForwardDecl())
David Blaikie6943dea2013-08-20 01:28:15 +00001437 completeClassData(RD);
1438}
1439
1440void CGDebugInfo::completeClassData(const RecordDecl *RD) {
1441 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001442 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001443 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001444 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001445 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001446 if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001447 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001448 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001449 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001450 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001451}
1452
David Blaikie0e716b42014-03-03 23:48:23 +00001453static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1454 CXXRecordDecl::method_iterator End) {
1455 for (; I != End; ++I)
1456 if (FunctionDecl *Tmpl = I->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001457 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
1458 !I->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001459 return true;
1460 return false;
1461}
1462
1463static bool shouldOmitDefinition(CodeGenOptions::DebugInfoKind DebugKind,
1464 const RecordDecl *RD,
1465 const LangOptions &LangOpts) {
1466 if (DebugKind > CodeGenOptions::LimitedDebugInfo)
1467 return false;
1468
1469 if (!LangOpts.CPlusPlus)
1470 return false;
1471
1472 if (!RD->isCompleteDefinitionRequired())
1473 return true;
1474
1475 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1476
1477 if (!CXXDecl)
1478 return false;
1479
1480 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass())
1481 return true;
1482
1483 TemplateSpecializationKind Spec = TSK_Undeclared;
1484 if (const ClassTemplateSpecializationDecl *SD =
1485 dyn_cast<ClassTemplateSpecializationDecl>(RD))
1486 Spec = SD->getSpecializationKind();
1487
1488 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1489 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1490 CXXDecl->method_end()))
1491 return true;
1492
1493 return false;
1494}
1495
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001496llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001497 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001498 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
David Blaikie0e716b42014-03-03 23:48:23 +00001499 if (T || shouldOmitDefinition(DebugKind, RD, CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001500 if (!T)
David Blaikie65ec94e2014-02-18 20:52:05 +00001501 T = getOrCreateRecordFwdDecl(
1502 Ty, getContextDescriptor(cast<Decl>(RD->getDeclContext())));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001503 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001504 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001505
David Blaikieb2e86eb2013-08-15 20:49:17 +00001506 return CreateTypeDefinition(Ty);
1507}
1508
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001509llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00001510 RecordDecl *RD = Ty->getDecl();
1511
Guy Benyei11169dd2012-12-18 14:30:41 +00001512 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001513 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001514
1515 // Records and classes and unions can all be recursive. To handle them, we
1516 // first generate a debug descriptor for the struct as a forward declaration.
1517 // Then (if it is a definition) we go through and get debug info for all of
1518 // its members. Finally, we create a descriptor for the complete type (which
1519 // may refer to the forward decl if the struct is recursive) and replace all
1520 // uses of the forward declaration with the final definition.
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00001521 llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001522
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001523 const RecordDecl *D = RD->getDefinition();
1524 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001525 return FwdDecl;
1526
David Blaikieadfbf992013-08-18 16:55:33 +00001527 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1528 CollectContainingType(CXXDecl, FwdDecl);
1529
Guy Benyei11169dd2012-12-18 14:30:41 +00001530 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001531 LexicalBlockStack.emplace_back(&*FwdDecl);
1532 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001533
Guy Benyei11169dd2012-12-18 14:30:41 +00001534 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001535 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001536 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001537
1538 // Note: The split of CXXDecl information here is intentional, the
1539 // gdb tests will depend on a certain ordering at printout. The debug
1540 // information offsets are still correct if we merge them all together
1541 // though.
1542 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1543 if (CXXDecl) {
1544 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1545 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1546 }
1547
Eric Christopher91a31902013-01-16 01:22:32 +00001548 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001549 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001550 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001551 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001552
1553 LexicalBlockStack.pop_back();
1554 RegionMap.erase(Ty->getDecl());
1555
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001556 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001557 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001558
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001559 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001560 FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001561 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001562
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001563 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001564 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001565}
1566
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001567llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1568 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001569 // Ignore protocols.
1570 return getOrCreateType(Ty->getBaseType(), Unit);
1571}
1572
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001573/// \return true if Getter has the default name for the property PD.
1574static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1575 const ObjCMethodDecl *Getter) {
1576 assert(PD);
1577 if (!Getter)
1578 return true;
1579
1580 assert(Getter->getDeclName().isObjCZeroArgSelector());
1581 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001582 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001583}
1584
1585/// \return true if Setter has the default name for the property PD.
1586static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1587 const ObjCMethodDecl *Setter) {
1588 assert(PD);
1589 if (!Setter)
1590 return true;
1591
1592 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001593 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001594 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001595}
1596
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001597llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1598 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001599 ObjCInterfaceDecl *ID = Ty->getDecl();
1600 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001601 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001602
1603 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001604 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001605 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001606 auto RuntimeLang =
1607 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00001608
1609 // If this is just a forward declaration return a special forward-declaration
1610 // debug type since we won't be able to lay out the entire type.
1611 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001612 if (!Def || !Def->getImplementation()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001613 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
David Blaikief427b002014-05-06 03:42:01 +00001614 llvm::dwarf::DW_TAG_structure_type, ID->getName(), TheCU, DefUnit, Line,
1615 RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001616 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001617 return FwdDecl;
1618 }
1619
David Blaikieef8a9512014-05-05 23:23:53 +00001620 return CreateTypeDefinition(Ty, Unit);
1621}
1622
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001623llvm::DIModule *
1624CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod) {
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001625 auto it = ModuleRefCache.find(Mod.Signature);
1626 if (it != ModuleRefCache.end())
Adrian Prantl2388ead2015-06-30 18:01:05 +00001627 return it->second;
1628
1629 // Macro definitions that were defined with "-D" on the command line.
1630 SmallString<128> ConfigMacros;
1631 {
1632 llvm::raw_svector_ostream OS(ConfigMacros);
1633 const auto &PPOpts = CGM.getPreprocessorOpts();
1634 unsigned I = 0;
1635 // Translate the macro definitions back into a commmand line.
1636 for (auto &M : PPOpts.Macros) {
1637 if (++I > 1)
1638 OS << " ";
1639 const std::string &Macro = M.first;
1640 bool Undef = M.second;
1641 OS << "\"-" << (Undef ? 'U' : 'D');
1642 for (char c : Macro)
1643 switch (c) {
1644 case '\\' : OS << "\\\\"; break;
1645 case '"' : OS << "\\\""; break;
1646 default: OS << c;
1647 }
1648 OS << '\"';
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001649 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001650 }
Adrian Prantl2388ead2015-06-30 18:01:05 +00001651 llvm::DIBuilder DIB(CGM.getModule());
1652 auto *CU = DIB.createCompileUnit(
1653 TheCU->getSourceLanguage(), internString(Mod.ModuleName),
1654 internString(Mod.Path), TheCU->getProducer(), true, StringRef(), 0,
1655 internString(Mod.ASTFile), llvm::DIBuilder::FullDebug, Mod.Signature);
1656 llvm::DIModule *ModuleRef =
1657 DIB.createModule(CU, Mod.ModuleName, ConfigMacros, internString(Mod.Path),
1658 internString(CGM.getHeaderSearchOpts().Sysroot));
1659 DIB.finalize();
1660 ModuleRefCache.insert(std::make_pair(Mod.Signature, ModuleRef));
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001661 return ModuleRef;
1662}
1663
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001664llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
1665 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00001666 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001667 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00001668 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001669 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001670
1671 // Bit size, align and offset of the type.
1672 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1673 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1674
1675 unsigned Flags = 0;
1676 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001677 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00001678
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001679 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001680 Unit, ID->getName(), DefUnit, Line, Size, Align, Flags, nullptr,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001681 llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001682
David Blaikieef8a9512014-05-05 23:23:53 +00001683 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001684 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001685
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001686 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001687 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001688 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001689
1690 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001691 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001692
1693 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1694 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001695 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001696 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001697 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001698 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001699
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001700 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001701 EltTys.push_back(InhTag);
1702 }
1703
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001704 // Create entries for all of the properties.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001705 for (const auto *PD : ID->properties()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001706 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001707 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001708 unsigned PLine = getLineNumber(Loc);
1709 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1710 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001711 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
1712 PD->getName(), PUnit, PLine,
1713 hasDefaultGetterName(PD, Getter) ? ""
1714 : getSelectorName(PD->getGetterName()),
1715 hasDefaultSetterName(PD, Setter) ? ""
1716 : getSelectorName(PD->getSetterName()),
1717 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001718 EltTys.push_back(PropertyNode);
1719 }
1720
1721 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1722 unsigned FieldNo = 0;
1723 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1724 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001725 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001726 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001727 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001728
Guy Benyei11169dd2012-12-18 14:30:41 +00001729 StringRef FieldName = Field->getName();
1730
1731 // Ignore unnamed fields.
1732 if (FieldName.empty())
1733 continue;
1734
1735 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001736 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001737 unsigned FieldLine = getLineNumber(Field->getLocation());
1738 QualType FType = Field->getType();
1739 uint64_t FieldSize = 0;
1740 unsigned FieldAlign = 0;
1741
1742 if (!FType->isIncompleteArrayType()) {
1743
1744 // Bit size, align and offset of the type.
1745 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001746 ? Field->getBitWidthValue(CGM.getContext())
1747 : CGM.getContext().getTypeSize(FType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001748 FieldAlign = CGM.getContext().getTypeAlign(FType);
1749 }
1750
1751 uint64_t FieldOffset;
1752 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1753 // We don't know the runtime offset of an ivar if we're using the
1754 // non-fragile ABI. For bitfields, use the bit offset into the first
1755 // byte of storage of the bitfield. For other fields, use zero.
1756 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001757 FieldOffset =
1758 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00001759 FieldOffset %= CGM.getContext().getCharWidth();
1760 } else {
1761 FieldOffset = 0;
1762 }
1763 } else {
1764 FieldOffset = RL.getFieldOffset(FieldNo);
1765 }
1766
1767 unsigned Flags = 0;
1768 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001769 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00001770 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001771 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001772 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001773 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00001774
Craig Topper8a13c412014-05-21 05:09:00 +00001775 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001776 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001777 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00001778 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001779 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00001780 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001781 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00001782 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001783 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1784 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001785 PropertyNode = DBuilder.createObjCProperty(
1786 PD->getName(), PUnit, PLine,
1787 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
1788 PD->getGetterName()),
1789 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
1790 PD->getSetterName()),
1791 PD->getPropertyAttributes(),
1792 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001793 }
1794 }
1795 }
Eric Christophere7b87e52014-10-26 23:40:33 +00001796 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
1797 FieldSize, FieldAlign, FieldOffset, Flags,
1798 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00001799 EltTys.push_back(FieldTy);
1800 }
1801
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001802 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001803 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00001804
Guy Benyei11169dd2012-12-18 14:30:41 +00001805 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001806 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001807}
1808
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001809llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
1810 llvm::DIFile *Unit) {
1811 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001812 int64_t Count = Ty->getNumElements();
1813 if (Count == 0)
1814 // If number of elements are not known then this is an unbounded array.
1815 // Use Count == -1 to express such arrays.
1816 Count = -1;
1817
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001818 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001819 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00001820
1821 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1822 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1823
1824 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
1825}
1826
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001827llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001828 uint64_t Size;
1829 uint64_t Align;
1830
1831 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
1832 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
1833 Size = 0;
1834 Align =
Eric Christophere7b87e52014-10-26 23:40:33 +00001835 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Guy Benyei11169dd2012-12-18 14:30:41 +00001836 } else if (Ty->isIncompleteArrayType()) {
1837 Size = 0;
1838 if (Ty->getElementType()->isIncompleteType())
1839 Align = 0;
1840 else
1841 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikief03b2e82013-05-09 20:48:12 +00001842 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001843 Size = 0;
1844 Align = 0;
1845 } else {
1846 // Size and align of the whole array, not the element type.
1847 Size = CGM.getContext().getTypeSize(Ty);
1848 Align = CGM.getContext().getTypeAlign(Ty);
1849 }
1850
1851 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1852 // interior arrays, do we care? Why aren't nested arrays represented the
1853 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001854 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001855 QualType EltTy(Ty, 0);
1856 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1857 // If the number of elements is known, then count is that number. Otherwise,
1858 // it's -1. This allows us to represent a subrange with an array of 0
1859 // elements, like this:
1860 //
1861 // struct foo {
1862 // int x[0];
1863 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00001864 int64_t Count = -1; // Count == -1 is an unbounded array.
Guy Benyei11169dd2012-12-18 14:30:41 +00001865 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1866 Count = CAT->getSize().getZExtValue();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001867
Guy Benyei11169dd2012-12-18 14:30:41 +00001868 // FIXME: Verify this is right for VLAs.
1869 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
1870 EltTy = Ty->getElementType();
1871 }
1872
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001873 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001874
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001875 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
1876 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00001877}
1878
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001879llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
1880 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001881 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
1882 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001883}
1884
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001885llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
1886 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001887 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
1888 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001889}
1890
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001891llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
1892 llvm::DIFile *U) {
David Majnemer34568bc2015-05-26 21:54:24 +00001893 uint64_t Size = CGM.getCXXABI().isTypeInfoCalculable(QualType(Ty, 0))
1894 ? CGM.getContext().getTypeSize(Ty)
1895 : 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001896 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00001897 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00001898 return DBuilder.createMemberPointerType(
David Majnemer34568bc2015-05-26 21:54:24 +00001899 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size);
Adrian Prantl0866acd2013-12-19 01:38:47 +00001900
1901 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00001902 Ty->getPointeeType()->getAs<FunctionProtoType>();
1903 return DBuilder.createMemberPointerType(
1904 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
1905 Ty->getClass(), FPT->getTypeQuals())),
1906 FPT, U),
David Majnemer34568bc2015-05-26 21:54:24 +00001907 ClassType, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +00001908}
1909
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001910llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001911 // Ignore the atomic wrapping
1912 // FIXME: What is the correct representation?
1913 return getOrCreateType(Ty->getValueType(), U);
1914}
1915
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001916llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00001917 const EnumDecl *ED = Ty->getDecl();
Guy Benyei11169dd2012-12-18 14:30:41 +00001918 uint64_t Size = 0;
1919 uint64_t Align = 0;
1920 if (!ED->getTypeForDecl()->isIncompleteType()) {
1921 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1922 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1923 }
1924
Manman Rene0064d82013-08-29 23:19:58 +00001925 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1926
Guy Benyei11169dd2012-12-18 14:30:41 +00001927 // If this is just a forward declaration, construct an appropriately
1928 // marked node and just return it.
1929 if (!ED->getDefinition()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001930 llvm::DIScope *EDContext =
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001931 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001932 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001933 unsigned Line = getLineNumber(ED->getLocation());
1934 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001935 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
David Blaikief427b002014-05-06 03:42:01 +00001936 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001937 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001938 ReplaceMap.emplace_back(
1939 std::piecewise_construct, std::make_tuple(Ty),
1940 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00001941 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00001942 }
1943
David Blaikie483a9da2014-05-06 18:35:21 +00001944 return CreateTypeDefinition(Ty);
1945}
1946
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001947llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00001948 const EnumDecl *ED = Ty->getDecl();
1949 uint64_t Size = 0;
1950 uint64_t Align = 0;
1951 if (!ED->getTypeForDecl()->isIncompleteType()) {
1952 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1953 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1954 }
1955
1956 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1957
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001958 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001959 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00001960 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00001961 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001962 Enumerators.push_back(DBuilder.createEnumerator(
1963 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001964 }
1965
1966 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001967 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00001968
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001969 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001970 unsigned Line = getLineNumber(ED->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001971 llvm::DIScope *EnumContext =
Eric Christophere7b87e52014-10-26 23:40:33 +00001972 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001973 llvm::DIType *ClassTy =
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001974 ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
1975 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
1976 Line, Size, Align, EltArray, ClassTy,
1977 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00001978}
1979
David Blaikie05491062013-01-21 04:37:12 +00001980static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
1981 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00001982 do {
Adrian Prantl179af902013-09-26 21:35:50 +00001983 Qualifiers InnerQuals = T.getLocalQualifiers();
1984 // Qualifiers::operator+() doesn't like it if you add a Qualifier
1985 // that is already there.
1986 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
1987 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00001988 QualType LastT = T;
1989 switch (T->getTypeClass()) {
1990 default:
David Blaikie05491062013-01-21 04:37:12 +00001991 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00001992 case Type::TemplateSpecialization: {
1993 const auto *Spec = cast<TemplateSpecializationType>(T);
1994 if (Spec->isTypeAlias())
1995 return C.getQualifiedType(T.getTypePtr(), Quals);
1996 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00001997 break;
1998 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001999 case Type::TypeOfExpr:
2000 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2001 break;
2002 case Type::TypeOf:
2003 T = cast<TypeOfType>(T)->getUnderlyingType();
2004 break;
2005 case Type::Decltype:
2006 T = cast<DecltypeType>(T)->getUnderlyingType();
2007 break;
2008 case Type::UnaryTransform:
2009 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2010 break;
2011 case Type::Attributed:
2012 T = cast<AttributedType>(T)->getEquivalentType();
2013 break;
2014 case Type::Elaborated:
2015 T = cast<ElaboratedType>(T)->getNamedType();
2016 break;
2017 case Type::Paren:
2018 T = cast<ParenType>(T)->getInnerType();
2019 break;
David Blaikie05491062013-01-21 04:37:12 +00002020 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002021 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002022 break;
2023 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00002024 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002025 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002026 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002027 break;
2028 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002029
Guy Benyei11169dd2012-12-18 14:30:41 +00002030 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002031 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002032 } while (true);
2033}
2034
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002035llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002036
2037 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002038 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002039
David Blaikief427b002014-05-06 03:42:01 +00002040 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002041 if (it != TypeCache.end()) {
2042 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002043 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002044 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002045 }
2046
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002047 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002048}
2049
David Blaikie0e716b42014-03-03 23:48:23 +00002050void CGDebugInfo::completeTemplateDefinition(
2051 const ClassTemplateSpecializationDecl &SD) {
David Blaikie0856f662014-03-04 22:01:08 +00002052 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2053 return;
2054
David Blaikie0e716b42014-03-03 23:48:23 +00002055 completeClassData(&SD);
2056 // In case this type has no member function definitions being emitted, ensure
2057 // it is retained
2058 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2059}
2060
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002061llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002062 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002063 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002064
2065 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002066 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002067
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002068 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002069 return T;
2070
2071 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002072 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Eric Christophere7b87e52014-10-26 23:40:33 +00002073 void *TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002074
2075 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002076 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002077
Guy Benyei11169dd2012-12-18 14:30:41 +00002078 return Res;
2079}
2080
Eric Christopher1ecc5632013-06-07 22:54:39 +00002081unsigned CGDebugInfo::Checksum(const ObjCInterfaceDecl *ID) {
Adrian Prantl817bbb32013-06-07 01:10:48 +00002082 // The assumption is that the number of ivars can only increase
2083 // monotonically, so it is safe to just use their current number as
2084 // a checksum.
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002085 unsigned Sum = 0;
2086 for (const ObjCIvarDecl *Ivar = ID->all_declared_ivar_begin();
Craig Topper8a13c412014-05-21 05:09:00 +00002087 Ivar != nullptr; Ivar = Ivar->getNextIvar())
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002088 ++Sum;
2089
2090 return Sum;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002091}
2092
2093ObjCInterfaceDecl *CGDebugInfo::getObjCInterfaceDecl(QualType Ty) {
2094 switch (Ty->getTypeClass()) {
2095 case Type::ObjCObjectPointer:
Eric Christophere7b87e52014-10-26 23:40:33 +00002096 return getObjCInterfaceDecl(
2097 cast<ObjCObjectPointerType>(Ty)->getPointeeType());
Adrian Prantla03a85a2013-03-06 22:03:30 +00002098 case Type::ObjCInterface:
2099 return cast<ObjCInterfaceType>(Ty)->getDecl();
2100 default:
Craig Topper8a13c412014-05-21 05:09:00 +00002101 return nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002102 }
2103}
2104
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002105llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002106 // Handle qualifiers, which recursively handles what they refer to.
2107 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002108 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002109
Guy Benyei11169dd2012-12-18 14:30:41 +00002110 // Work out details of type.
2111 switch (Ty->getTypeClass()) {
2112#define TYPE(Class, Base)
2113#define ABSTRACT_TYPE(Class, Base)
2114#define NON_CANONICAL_TYPE(Class, Base)
2115#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2116#include "clang/AST/TypeNodes.def"
2117 llvm_unreachable("Dependent types cannot show up in debug information");
2118
2119 case Type::ExtVector:
2120 case Type::Vector:
2121 return CreateType(cast<VectorType>(Ty), Unit);
2122 case Type::ObjCObjectPointer:
2123 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2124 case Type::ObjCObject:
2125 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2126 case Type::ObjCInterface:
2127 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2128 case Type::Builtin:
2129 return CreateType(cast<BuiltinType>(Ty));
2130 case Type::Complex:
2131 return CreateType(cast<ComplexType>(Ty));
2132 case Type::Pointer:
2133 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002134 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002135 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002136 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002137 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002138 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002139 case Type::BlockPointer:
2140 return CreateType(cast<BlockPointerType>(Ty), Unit);
2141 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002142 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002143 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002144 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002145 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002146 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002147 case Type::FunctionProto:
2148 case Type::FunctionNoProto:
2149 return CreateType(cast<FunctionType>(Ty), Unit);
2150 case Type::ConstantArray:
2151 case Type::VariableArray:
2152 case Type::IncompleteArray:
2153 return CreateType(cast<ArrayType>(Ty), Unit);
2154
2155 case Type::LValueReference:
2156 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2157 case Type::RValueReference:
2158 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2159
2160 case Type::MemberPointer:
2161 return CreateType(cast<MemberPointerType>(Ty), Unit);
2162
2163 case Type::Atomic:
2164 return CreateType(cast<AtomicType>(Ty), Unit);
2165
Guy Benyei11169dd2012-12-18 14:30:41 +00002166 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002167 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2168
David Blaikie42edade2014-11-11 20:44:45 +00002169 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002170 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002171 case Type::Elaborated:
2172 case Type::Paren:
2173 case Type::SubstTemplateTypeParm:
2174 case Type::TypeOfExpr:
2175 case Type::TypeOf:
2176 case Type::Decltype:
2177 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002178 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002179 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002180 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002181
David Blaikie42edade2014-11-11 20:44:45 +00002182 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002183}
2184
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002185llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2186 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002187 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002188
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002189 auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002190
2191 // We may have cached a forward decl when we could have created
2192 // a non-forward decl. Go ahead and create a non-forward decl
2193 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002194 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002195 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002196
2197 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002198 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002199
2200 // Propagate members from the declaration to the definition
2201 // CreateType(const RecordType*) will overwrite this with the members in the
2202 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002203 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002204
Guy Benyei11169dd2012-12-18 14:30:41 +00002205 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002206 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002207 return Res;
2208}
2209
2210// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002211llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002212 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002213
Guy Benyei11169dd2012-12-18 14:30:41 +00002214 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002215 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002216 unsigned Line = getLineNumber(RD->getLocation());
2217 StringRef RDName = getClassName(RD);
2218
David Blaikiebe822ed2015-07-01 18:07:22 +00002219 llvm::DIScope *RDContext =
2220 getContextDescriptor(cast<Decl>(RD->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002221
David Blaikied2785892013-08-18 17:36:19 +00002222 // If we ended up creating the type during the context chain construction,
2223 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002224 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002225 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002226 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002227 return T;
David Blaikied2785892013-08-18 17:36:19 +00002228
Adrian Prantl381e7552014-02-04 21:29:50 +00002229 // If this is just a forward or incomplete declaration, construct an
2230 // appropriately marked node and just return it.
2231 const RecordDecl *D = RD->getDefinition();
2232 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002233 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002234
2235 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2236 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002237
Manman Rene0064d82013-08-29 23:19:58 +00002238 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2239
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002240 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002241 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align, 0,
2242 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002243
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002244 RegionMap[Ty->getDecl()].reset(RealDecl);
2245 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002246
David Blaikieadfbf992013-08-18 16:55:33 +00002247 if (const ClassTemplateSpecializationDecl *TSpecial =
2248 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002249 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002250 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002251 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002252}
2253
David Blaikieadfbf992013-08-18 16:55:33 +00002254void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002255 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002256 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002257 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002258 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2259 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002260 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002261 while (1) {
2262 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2263 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2264 if (PBT && !BRL.isPrimaryBaseVirtual())
2265 PBase = PBT;
2266 else
2267 break;
2268 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002269 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002270 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2271 getOrCreateFile(RD->getLocation())));
2272 } else if (RD->isDynamicClass())
2273 ContainingType = RealDecl;
2274
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002275 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002276}
2277
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002278llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002279 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002280 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002281 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2282 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002283 llvm::DIType *Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002284 FieldAlign, *Offset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002285 *Offset += FieldSize;
2286 return Ty;
2287}
2288
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002289void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002290 StringRef &Name,
2291 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002292 llvm::DIScope *&FDContext,
2293 llvm::DINodeArray &TParamsArray,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002294 unsigned &Flags) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002295 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2296 Name = getFunctionName(FD);
2297 // Use mangled name as linkage name for C/C++ functions.
2298 if (FD->hasPrototype()) {
2299 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002300 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002301 }
2302 // No need to replicate the linkage name if it isn't different from the
2303 // subprogram name, no need to have it at all unless coverage is enabled or
2304 // debug is set to more than just line tables.
2305 if (LinkageName == Name ||
2306 (!CGM.getCodeGenOpts().EmitGcovArcs &&
2307 !CGM.getCodeGenOpts().EmitGcovNotes &&
2308 DebugKind <= CodeGenOptions::DebugLineTablesOnly))
2309 LinkageName = StringRef();
2310
2311 if (DebugKind >= CodeGenOptions::LimitedDebugInfo) {
2312 if (const NamespaceDecl *NSDecl =
2313 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2314 FDContext = getOrCreateNameSpace(NSDecl);
2315 else if (const RecordDecl *RDecl =
2316 dyn_cast_or_null<RecordDecl>(FD->getDeclContext()))
2317 FDContext = getContextDescriptor(cast<Decl>(RDecl));
2318 // Collect template parameters.
2319 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2320 }
2321}
2322
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002323void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002324 unsigned &LineNo, QualType &T,
2325 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002326 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002327 Unit = getOrCreateFile(VD->getLocation());
2328 LineNo = getLineNumber(VD->getLocation());
2329
2330 setLocation(VD->getLocation());
2331
2332 T = VD->getType();
2333 if (T->isIncompleteArrayType()) {
2334 // CodeGen turns int[] into int[1] so we'll do the same here.
2335 llvm::APInt ConstVal(32, 1);
2336 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2337
2338 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2339 ArrayType::Normal, 0);
2340 }
2341
2342 Name = VD->getName();
2343 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2344 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2345 LinkageName = CGM.getMangledName(VD);
2346 if (LinkageName == Name)
2347 LinkageName = StringRef();
2348
2349 // Since we emit declarations (DW_AT_members) for static members, place the
2350 // definition of those static members in the namespace they were declared in
2351 // in the source code (the lexical decl context).
2352 // FIXME: Generalize this for even non-member global variables where the
2353 // declaration and definition may have different lexical decl contexts, once
2354 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002355 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2356 : VD->getDeclContext();
2357 // When a record type contains an in-line initialization of a static data
2358 // member, and the record type is marked as __declspec(dllexport), an implicit
2359 // definition of the member will be created in the record context. DWARF
2360 // doesn't seem to have a nice way to describe this in a form that consumers
2361 // are likely to understand, so fake the "normal" situation of a definition
2362 // outside the class by putting it in the global scope.
2363 if (DC->isRecord())
2364 DC = CGM.getContext().getTranslationUnitDecl();
David Blaikiebe822ed2015-07-01 18:07:22 +00002365 VDContext = getContextDescriptor(dyn_cast<Decl>(DC));
Frederic Riss9db79f12014-11-18 03:40:46 +00002366}
2367
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002368llvm::DISubprogram *
Frederic Rissd253ed62014-11-18 03:40:51 +00002369CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002370 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00002371 StringRef Name, LinkageName;
2372 unsigned Flags = 0;
2373 SourceLocation Loc = FD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002374 llvm::DIFile *Unit = getOrCreateFile(Loc);
2375 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002376 unsigned Line = getLineNumber(Loc);
2377
2378 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2379 TParamsArray, Flags);
2380 // Build function type.
2381 SmallVector<QualType, 16> ArgTypes;
2382 for (const ParmVarDecl *Parm: FD->parameters())
2383 ArgTypes.push_back(Parm->getType());
2384 QualType FnType =
2385 CGM.getContext().getFunctionType(FD->getReturnType(), ArgTypes,
2386 FunctionProtoType::ExtProtoInfo());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002387 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002388 DContext, Name, LinkageName, Unit, Line,
2389 getOrCreateFunctionType(FD, FnType, Unit), !FD->isExternallyVisible(),
Duncan P. N. Exon Smith31d38f72015-08-26 22:21:09 +00002390 /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize, nullptr,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002391 TParamsArray.get(), getFunctionDeclaration(FD));
Frederic Rissd253ed62014-11-18 03:40:51 +00002392 const FunctionDecl *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002393 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2394 std::make_tuple(CanonDecl),
2395 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00002396 return SP;
2397}
2398
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002399llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00002400CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2401 QualType T;
2402 StringRef Name, LinkageName;
2403 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002404 llvm::DIFile *Unit = getOrCreateFile(Loc);
2405 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002406 unsigned Line = getLineNumber(Loc);
2407
2408 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002409 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
2410 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
2411 !VD->isExternallyVisible(), nullptr, nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002412 FwdDeclReplaceMap.emplace_back(
2413 std::piecewise_construct,
2414 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2415 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002416 return GV;
2417}
2418
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002419llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002420 // We only need a declaration (not a definition) of the type - so use whatever
2421 // we would otherwise do to get a type for a pointee. (forward declarations in
2422 // limited debug info, full definitions (if the type definition is available)
2423 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002424 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2425 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002426 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002427 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002428
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002429 if (I != DeclCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002430 return dyn_cast_or_null<llvm::DINode>(I->second);
Frederic Rissd253ed62014-11-18 03:40:51 +00002431
2432 // No definition for now. Emit a forward definition that might be
2433 // merged with a potential upcoming definition.
2434 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
2435 return getFunctionForwardDeclaration(FD);
2436 else if (const auto *VD = dyn_cast<VarDecl>(D))
2437 return getGlobalVariableForwardDeclaration(VD);
2438
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002439 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00002440}
2441
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002442llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Diego Novillo913690c2014-06-24 17:02:17 +00002443 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002444 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00002445
Guy Benyei11169dd2012-12-18 14:30:41 +00002446 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002447 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002448 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002449
2450 // Setup context.
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00002451 auto *S = getContextDescriptor(cast<Decl>(D->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002452
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002453 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002454 if (MI == SPCache.end()) {
Eric Christopherf86c4052013-08-28 23:12:10 +00002455 if (const CXXMethodDecl *MD =
2456 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00002457 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002458 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00002459 }
2460 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002461 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002462 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002463 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002464 return SP;
2465 }
2466
Aaron Ballman86c93902014-03-06 23:45:36 +00002467 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002468 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002469 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002470 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002471 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002472 return SP;
2473 }
2474 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002475 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002476}
2477
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002478// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00002479// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002480llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002481 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002482 llvm::DIFile *F) {
Diego Novillo913690c2014-06-24 17:02:17 +00002483 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002484 // Create fake but valid subroutine type. Otherwise -verify would fail, and
2485 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Manman Ren67f005e2014-07-28 22:24:34 +00002486 return DBuilder.createSubroutineType(F,
2487 DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002488
2489 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2490 return getOrCreateMethodType(Method, F);
2491 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2492 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002493 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002494
2495 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002496 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002497
2498 // Replace the instancetype keyword with the actual type.
2499 if (ResultTy == CGM.getContext().getObjCInstanceType())
2500 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002501 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002502
Adrian Prantl7bec9032013-05-10 21:08:31 +00002503 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002504 // "self" pointer is always first argument.
Adrian Prantlde17db32013-03-29 19:20:29 +00002505 QualType SelfDeclTy = OMethod->getSelfDecl()->getType();
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002506 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002507 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002508 Elts.push_back(DBuilder.createArtificialType(
2509 getOrCreateType(OMethod->getCmdDecl()->getType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002510 // Get rest of the arguments.
Aaron Ballman43b68be2014-03-07 17:50:17 +00002511 for (const auto *PI : OMethod->params())
2512 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002513 // Variadic methods need a special marker at the end of the type list.
2514 if (OMethod->isVariadic())
2515 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002516
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002517 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002518 return DBuilder.createSubroutineType(F, EltTypeArray);
2519 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002520
Adrian Prantl800faef2014-02-25 23:42:18 +00002521 // Handle variadic function types; they need an additional
2522 // unspecified parameter.
Adrian Prantld45ba252014-02-25 19:38:11 +00002523 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2524 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002525 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00002526 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2527 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2528 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2529 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2530 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002531 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Adrian Prantld45ba252014-02-25 19:38:11 +00002532 return DBuilder.createSubroutineType(F, EltTypeArray);
2533 }
2534
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002535 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002536}
2537
Eric Christophere7b87e52014-10-26 23:40:33 +00002538void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2539 SourceLocation ScopeLoc, QualType FnType,
2540 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002541
2542 StringRef Name;
2543 StringRef LinkageName;
2544
2545 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2546
2547 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002548 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00002549
Guy Benyei11169dd2012-12-18 14:30:41 +00002550 unsigned Flags = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002551 llvm::DIFile *Unit = getOrCreateFile(Loc);
2552 llvm::DIScope *FDContext = Unit;
2553 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00002554 if (!HasDecl) {
2555 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00002556 LinkageName = Fn->getName();
Guy Benyei11169dd2012-12-18 14:30:41 +00002557 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002558 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002559 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002560 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002561 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002562 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002563 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002564 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002565 return;
2566 }
2567 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002568 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2569 TParamsArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002570 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2571 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002572 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002573 } else {
2574 // Use llvm function name.
2575 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002576 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002577 }
2578 if (!Name.empty() && Name[0] == '\01')
2579 Name = Name.substr(1);
2580
Adrian Prantl42d71b92014-04-10 23:21:53 +00002581 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002582 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl42d71b92014-04-10 23:21:53 +00002583 // Artificial functions without a location should not silently reuse CurLoc.
2584 if (Loc.isInvalid())
2585 CurLoc = SourceLocation();
2586 }
2587 unsigned LineNo = getLineNumber(Loc);
2588 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002589
Eric Christopher8018e412014-03-27 18:50:35 +00002590 // FIXME: The function declaration we're constructing here is mostly reusing
2591 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2592 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2593 // all subprograms instead of the actual context since subprogram definitions
2594 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002595 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00002596 FDContext, Name, LinkageName, Unit, LineNo,
2597 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
2598 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize, Fn,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002599 TParamsArray.get(), getFunctionDeclaration(D));
Frederic Rissb1ab28c2014-11-05 19:19:04 +00002600 // We might get here with a VarDecl in the case we're generating
2601 // code for the initialization of globals. Do not record these decls
2602 // as they will overwrite the actual VarDecl Decl in the cache.
2603 if (HasDecl && isa<FunctionDecl>(D))
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002604 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(SP));
Guy Benyei11169dd2012-12-18 14:30:41 +00002605
Adrian Prantlbebb8932014-03-21 21:01:58 +00002606 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002607 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00002608
Guy Benyei11169dd2012-12-18 14:30:41 +00002609 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002610 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002611}
2612
David Blaikie835afb22015-01-21 23:08:17 +00002613void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002614 // Update our current location
2615 setLocation(Loc);
2616
Eric Christophere7b87e52014-10-26 23:40:33 +00002617 if (CurLoc.isInvalid() || CurLoc.isMacroID())
2618 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002619
Adrian Prantle83b1302014-01-07 22:05:52 +00002620 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00002621 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
David Blaikie835afb22015-01-21 23:08:17 +00002622 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002623}
2624
Guy Benyei11169dd2012-12-18 14:30:41 +00002625void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002626 llvm::MDNode *Back = nullptr;
2627 if (!LexicalBlockStack.empty())
2628 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002629 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002630 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002631 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002632}
2633
Eric Christopher0fdcb312013-05-16 00:52:20 +00002634void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2635 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002636 // Set our current location.
2637 setLocation(Loc);
2638
Guy Benyei11169dd2012-12-18 14:30:41 +00002639 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00002640 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2641 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00002642
2643 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2644 return;
2645
2646 // Create a new lexical block and push it on the stack.
2647 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002648}
2649
Eric Christopher0fdcb312013-05-16 00:52:20 +00002650void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2651 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002652 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2653
2654 // Provide an entry in the line table for the end of the block.
2655 EmitLocation(Builder, Loc);
2656
David Blaikie60a877b2014-10-22 19:34:33 +00002657 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2658 return;
2659
Guy Benyei11169dd2012-12-18 14:30:41 +00002660 LexicalBlockStack.pop_back();
2661}
2662
Guy Benyei11169dd2012-12-18 14:30:41 +00002663void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2664 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2665 unsigned RCount = FnBeginRegionCount.back();
2666 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2667
2668 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00002669 while (LexicalBlockStack.size() != RCount) {
2670 // Provide an entry in the line table for the end of the block.
2671 EmitLocation(Builder, CurLoc);
2672 LexicalBlockStack.pop_back();
2673 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002674 FnBeginRegionCount.pop_back();
2675}
2676
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002677llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002678 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002679
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002680 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002681 QualType FType;
2682 uint64_t FieldSize, FieldOffset;
2683 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002684
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002685 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002686 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002687
2688 FieldOffset = 0;
2689 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2690 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2691 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2692 FType = CGM.getContext().IntTy;
2693 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2694 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2695
2696 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2697 if (HasCopyAndDispose) {
2698 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002699 EltTys.push_back(
2700 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
2701 EltTys.push_back(
2702 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00002703 }
2704 bool HasByrefExtendedLayout;
2705 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00002706 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
2707 HasByrefExtendedLayout) &&
2708 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00002709 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002710 EltTys.push_back(
2711 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00002712 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002713
Guy Benyei11169dd2012-12-18 14:30:41 +00002714 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2715 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002716 CGM.getTarget().getPointerAlign(0))) {
2717 CharUnits FieldOffsetInBytes =
2718 CGM.getContext().toCharUnitsFromBits(FieldOffset);
2719 CharUnits AlignedOffsetInBytes =
2720 FieldOffsetInBytes.RoundUpToAlignment(Align);
2721 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002722
Guy Benyei11169dd2012-12-18 14:30:41 +00002723 if (NumPaddingBytes.isPositive()) {
2724 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
2725 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2726 pad, ArrayType::Normal, 0);
2727 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
2728 }
2729 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002730
Guy Benyei11169dd2012-12-18 14:30:41 +00002731 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002732 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002733 FieldSize = CGM.getContext().getTypeSize(FType);
2734 FieldAlign = CGM.getContext().toBits(Align);
2735
Eric Christopherb2a008c2013-05-16 00:45:12 +00002736 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00002737 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
2738 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002739 EltTys.push_back(FieldTy);
2740 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002741
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002742 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002743
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002744 unsigned Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002745
Guy Benyei11169dd2012-12-18 14:30:41 +00002746 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002747 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002748}
2749
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002750void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::Value *Storage,
2751 llvm::Optional<unsigned> ArgNo,
Eric Christophere7b87e52014-10-26 23:40:33 +00002752 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002753 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002754 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2755
David Blaikie7fceebf2013-08-19 03:37:48 +00002756 bool Unwritten =
2757 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
2758 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002759 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00002760 if (!Unwritten)
2761 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002762 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00002763 uint64_t XOffset = 0;
2764 if (VD->hasAttr<BlocksAttr>())
2765 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002766 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002767 Ty = getOrCreateType(VD->getType(), Unit);
2768
2769 // If there is no debug info for this type then do not emit debug info
2770 // for this variable.
2771 if (!Ty)
2772 return;
2773
Guy Benyei11169dd2012-12-18 14:30:41 +00002774 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00002775 unsigned Line = 0;
2776 unsigned Column = 0;
2777 if (!Unwritten) {
2778 Line = getLineNumber(VD->getLocation());
2779 Column = getColumnNumber(VD->getLocation());
2780 }
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002781 SmallVector<int64_t, 9> Expr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002782 unsigned Flags = 0;
2783 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002784 Flags |= llvm::DINode::FlagArtificial;
Guy Benyei11169dd2012-12-18 14:30:41 +00002785 // If this is the first argument and it is implicit then
2786 // give it an object pointer flag.
2787 // FIXME: There has to be a better way to do this, but for static
2788 // functions there won't be an implicit param at arg1 and
2789 // otherwise it is 'self' or 'this'.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002790 if (isa<ImplicitParamDecl>(VD) && ArgNo && *ArgNo == 1)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002791 Flags |= llvm::DINode::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00002792 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00002793 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
2794 !VD->getType()->isPointerType())
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002795 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei11169dd2012-12-18 14:30:41 +00002796
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002797 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00002798
2799 StringRef Name = VD->getName();
2800 if (!Name.empty()) {
2801 if (VD->hasAttr<BlocksAttr>()) {
2802 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002803 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002804 // offset of __forwarding field
2805 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002806 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002807 Expr.push_back(offset.getQuantity());
2808 Expr.push_back(llvm::dwarf::DW_OP_deref);
2809 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002810 // offset of x field
2811 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002812 Expr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002813
2814 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002815 auto *D = ArgNo
2816 ? DBuilder.createParameterVariable(Scope, VD->getName(),
2817 *ArgNo, Unit, Line, Ty)
2818 : DBuilder.createAutoVariable(Scope, VD->getName(), Unit,
2819 Line, Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002820
Guy Benyei11169dd2012-12-18 14:30:41 +00002821 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002822 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2823 llvm::DebugLoc::get(Line, Column, Scope),
2824 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002825 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00002826 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002827 Expr.push_back(llvm::dwarf::DW_OP_deref);
Adrian Prantl0d820892015-04-29 15:05:50 +00002828 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2829 // If VD is an anonymous union then Storage represents value for
2830 // all union fields.
2831 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
2832 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00002833 // GDB has trouble finding local variables in anonymous unions, so we emit
2834 // artifical local variables for each of the members.
2835 //
2836 // FIXME: Remove this code as soon as GDB supports this.
2837 // The debug info verifier in LLVM operates based on the assumption that a
2838 // variable has the same size as its storage and we had to disable the check
2839 // for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00002840 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002841 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00002842 StringRef FieldName = Field->getName();
2843
2844 // Ignore unnamed fields. Do not ignore unnamed records.
2845 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2846 continue;
2847
2848 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002849 auto *D = DBuilder.createAutoVariable(
2850 Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
2851 Flags | llvm::DINode::FlagArtificial);
Adrian Prantl0d820892015-04-29 15:05:50 +00002852
2853 // Insert an llvm.dbg.declare into the current block.
2854 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2855 llvm::DebugLoc::get(Line, Column, Scope),
2856 Builder.GetInsertBlock());
2857 }
Adrian Prantl0d820892015-04-29 15:05:50 +00002858 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002859 }
David Blaikiea76a7c92013-01-05 05:58:35 +00002860
2861 // Create the descriptor for the variable.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002862 auto *D =
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002863 ArgNo
2864 ? DBuilder.createParameterVariable(Scope, Name, *ArgNo, Unit, Line,
2865 Ty, CGM.getLangOpts().Optimize,
2866 Flags)
2867 : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
2868 CGM.getLangOpts().Optimize, Flags);
David Blaikiea76a7c92013-01-05 05:58:35 +00002869
2870 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002871 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2872 llvm::DebugLoc::get(Line, Column, Scope),
2873 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002874}
2875
2876void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
2877 llvm::Value *Storage,
2878 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002879 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002880 EmitDeclare(VD, Storage, llvm::None, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00002881}
2882
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002883llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
2884 llvm::DIType *Ty) {
2885 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002886 if (CachedTy)
2887 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00002888 return DBuilder.createObjectPointerType(Ty);
2889}
2890
Eric Christophere7b87e52014-10-26 23:40:33 +00002891void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
2892 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00002893 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Eric Christopher75e17682013-05-16 00:45:23 +00002894 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002895 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00002896
Craig Topper8a13c412014-05-21 05:09:00 +00002897 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00002898 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002899
Guy Benyei11169dd2012-12-18 14:30:41 +00002900 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002901
Guy Benyei11169dd2012-12-18 14:30:41 +00002902 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002903 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
2904 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00002905 if (isByRef)
2906 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002907 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002908 Ty = getOrCreateType(VD->getType(), Unit);
2909
2910 // Self is passed along as an implicit non-arg variable in a
2911 // block. Mark it as the object pointer.
2912 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00002913 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00002914
2915 // Get location information.
2916 unsigned Line = getLineNumber(VD->getLocation());
2917 unsigned Column = getColumnNumber(VD->getLocation());
2918
2919 const llvm::DataLayout &target = CGM.getDataLayout();
2920
2921 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00002922 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00002923 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
2924
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002925 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00002926 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002927 addr.push_back(llvm::dwarf::DW_OP_deref);
2928 addr.push_back(llvm::dwarf::DW_OP_plus);
2929 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002930 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002931 addr.push_back(llvm::dwarf::DW_OP_deref);
2932 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002933 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00002934 offset =
2935 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002936 addr.push_back(offset.getQuantity());
2937 addr.push_back(llvm::dwarf::DW_OP_deref);
2938 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002939 // offset of x field
2940 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002941 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002942 }
2943
2944 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002945 auto *D = DBuilder.createAutoVariable(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002946 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002947 Line, Ty);
Adrian Prantl0f6df002013-03-29 19:20:35 +00002948
Guy Benyei11169dd2012-12-18 14:30:41 +00002949 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002950 auto DL = llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back());
2951 if (InsertPoint)
2952 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
2953 InsertPoint);
2954 else
2955 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
2956 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002957}
2958
Guy Benyei11169dd2012-12-18 14:30:41 +00002959void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
2960 unsigned ArgNo,
2961 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002962 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002963 EmitDeclare(VD, AI, ArgNo, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00002964}
2965
2966namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00002967struct BlockLayoutChunk {
2968 uint64_t OffsetInBits;
2969 const BlockDecl::Capture *Capture;
2970};
2971bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
2972 return l.OffsetInBits < r.OffsetInBits;
2973}
Guy Benyei11169dd2012-12-18 14:30:41 +00002974}
2975
2976void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00002977 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00002978 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00002979 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00002980 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002981 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002982 ASTContext &C = CGM.getContext();
2983 const BlockDecl *blockDecl = block.getBlockDecl();
2984
2985 // Collect some general information about the block's location.
2986 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002987 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002988 unsigned line = getLineNumber(loc);
2989 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002990
Guy Benyei11169dd2012-12-18 14:30:41 +00002991 // Build the debug-info type for the block literal.
2992 getContextDescriptor(cast<Decl>(blockDecl->getDeclContext()));
2993
2994 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00002995 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00002996
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002997 SmallVector<llvm::Metadata *, 16> fields;
Guy Benyei11169dd2012-12-18 14:30:41 +00002998 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
2999 blockLayout->getElementOffsetInBits(0),
3000 tunit, tunit));
3001 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
3002 blockLayout->getElementOffsetInBits(1),
3003 tunit, tunit));
3004 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
3005 blockLayout->getElementOffsetInBits(2),
3006 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003007 auto *FnTy = block.getBlockExpr()->getFunctionType();
3008 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
3009 fields.push_back(createFieldType("__FuncPtr", FnPtrType, 0, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003010 blockLayout->getElementOffsetInBits(3),
3011 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003012 fields.push_back(createFieldType(
3013 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3014 ? C.getBlockDescriptorExtendedType()
3015 : C.getBlockDescriptorType()),
3016 0, loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003017
3018 // We want to sort the captures by offset, not because DWARF
3019 // requires this, but because we're paranoid about debuggers.
3020 SmallVector<BlockLayoutChunk, 8> chunks;
3021
3022 // 'this' capture.
3023 if (blockDecl->capturesCXXThis()) {
3024 BlockLayoutChunk chunk;
3025 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003026 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003027 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003028 chunks.push_back(chunk);
3029 }
3030
3031 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003032 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003033 const VarDecl *variable = capture.getVariable();
3034 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3035
3036 // Ignore constant captures.
3037 if (captureInfo.isConstant())
3038 continue;
3039
3040 BlockLayoutChunk chunk;
3041 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003042 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003043 chunk.Capture = &capture;
3044 chunks.push_back(chunk);
3045 }
3046
3047 // Sort by offset.
3048 llvm::array_pod_sort(chunks.begin(), chunks.end());
3049
Eric Christophere7b87e52014-10-26 23:40:33 +00003050 for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
3051 e = chunks.end();
3052 i != e; ++i) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003053 uint64_t offsetInBits = i->OffsetInBits;
3054 const BlockDecl::Capture *capture = i->Capture;
3055
3056 // If we have a null capture, this must be the C++ 'this' capture.
3057 if (!capture) {
3058 const CXXMethodDecl *method =
Eric Christophere7b87e52014-10-26 23:40:33 +00003059 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00003060 QualType type = method->getThisType(C);
3061
3062 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
3063 offsetInBits, tunit, tunit));
3064 continue;
3065 }
3066
3067 const VarDecl *variable = capture->getVariable();
3068 StringRef name = variable->getName();
3069
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003070 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003071 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003072 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003073
3074 // FIXME: this creates a second copy of this type!
3075 uint64_t xoffset;
3076 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003077 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3078 fieldType =
3079 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3080 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003081 } else {
Eric Christophere7b87e52014-10-26 23:40:33 +00003082 fieldType = createFieldType(name, variable->getType(), 0, loc, AS_public,
3083 offsetInBits, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003084 }
3085 fields.push_back(fieldType);
3086 }
3087
3088 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003089 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3090 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003091
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003092 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003093
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003094 llvm::DIType *type = DBuilder.createStructType(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003095 tunit, typeName.str(), tunit, line,
3096 CGM.getContext().toBits(block.BlockSize),
3097 CGM.getContext().toBits(block.BlockAlign), 0, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003098 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3099
3100 // Get overall information about the block.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003101 unsigned flags = llvm::DINode::FlagArtificial;
3102 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003103
3104 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003105 auto *debugVar = DBuilder.createParameterVariable(
3106 scope, Arg->getName(), ArgNo, tunit, line, type,
3107 CGM.getLangOpts().Optimize, flags);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003108
Adrian Prantl616bef42013-03-14 21:52:59 +00003109 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003110 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003111 DBuilder.insertDbgValueIntrinsic(
Eric Christophere7b87e52014-10-26 23:40:33 +00003112 LocalAddr, 0, debugVar, DBuilder.createExpression(),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003113 llvm::DebugLoc::get(line, column, scope), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003114 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003115
Adrian Prantl616bef42013-03-14 21:52:59 +00003116 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003117 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
3118 llvm::DebugLoc::get(line, column, scope),
3119 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003120}
3121
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003122llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003123CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3124 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003125 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003126
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003127 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003128 if (MI != StaticDataMemberCache.end()) {
3129 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00003130 return MI->second;
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003131 }
David Blaikiece763042013-08-20 21:49:21 +00003132
3133 // If the member wasn't found in the cache, lazily construct and add it to the
3134 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003135 auto DC = D->getDeclContext();
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003136 auto *Ctxt =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003137 cast<llvm::DICompositeType>(getContextDescriptor(cast<Decl>(DC)));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003138 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003139}
3140
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003141llvm::DIGlobalVariable *CGDebugInfo::CollectAnonRecordDecls(
3142 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3143 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
3144 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003145
3146 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003147 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003148 StringRef FieldName = Field->getName();
3149
3150 // Ignore unnamed fields, but recurse into anonymous records.
3151 if (FieldName.empty()) {
3152 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3153 if (RT)
3154 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3155 Var, DContext);
3156 continue;
3157 }
3158 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003159 GV = DBuilder.createGlobalVariable(DContext, FieldName, LinkageName, Unit,
3160 LineNo, FieldTy,
3161 Var->hasInternalLinkage(), Var, nullptr);
Eric Christophercab9fae2014-04-10 05:20:00 +00003162 }
3163 return GV;
3164}
3165
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003166void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003167 const VarDecl *D) {
Eric Christopher75e17682013-05-16 00:45:23 +00003168 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003169 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003170 llvm::DIFile *Unit = nullptr;
3171 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003172 unsigned LineNo;
3173 StringRef DeclName, LinkageName;
3174 QualType T;
3175 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003176
3177 // Attempt to store one global variable for the declaration - even if we
3178 // emit a lot of fields.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003179 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003180
3181 // If this is an anonymous union then we'll want to emit a global
3182 // variable for each member of the anonymous union so that it's possible
3183 // to find the name of any field in the union.
3184 if (T->isUnionType() && DeclName.empty()) {
3185 const RecordDecl *RD = cast<RecordType>(T)->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003186 assert(RD->isAnonymousStructOrUnion() &&
3187 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003188 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3189 } else {
David Blaikie7550b112014-10-20 17:42:23 +00003190 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003191 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3192 Var->hasInternalLinkage(), Var,
3193 getOrCreateStaticDataMemberDeclarationOrNull(D));
3194 }
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003195 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(GV));
Guy Benyei11169dd2012-12-18 14:30:41 +00003196}
3197
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003198void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3199 llvm::Constant *Init) {
Eric Christopher75e17682013-05-16 00:45:23 +00003200 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003201 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003202 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003203 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003204 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003205 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3206 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3207 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3208 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3209 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003210 // Do not use global variables for enums.
3211 //
3212 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003213 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003214 return;
David Blaikiea15565562014-04-04 20:56:17 +00003215 // Do not emit separate definitions for function local const/statics.
3216 if (isa<FunctionDecl>(VD->getDeclContext()))
3217 return;
David Blaikiebb113912014-04-05 07:23:17 +00003218 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003219 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003220 if (VarD->isStaticDataMember()) {
3221 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
3222 getContextDescriptor(RD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003223 // Ensure that the type is retained even though it's otherwise unreferenced.
3224 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003225 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003226 return;
3227 }
3228
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003229 llvm::DIScope *DContext =
David Blaikieaf080852014-11-21 00:20:58 +00003230 getContextDescriptor(dyn_cast<Decl>(VD->getDeclContext()));
3231
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003232 auto &GV = DeclCache[VD];
3233 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003234 return;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003235 GV.reset(DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003236 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003237 true, Init, getOrCreateStaticDataMemberDeclarationOrNull(VarD)));
David Blaikiebd483762013-05-20 04:58:53 +00003238}
3239
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003240llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003241 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003242 return LexicalBlockStack.back();
David Blaikiebd483762013-05-20 04:58:53 +00003243 return getContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00003244}
3245
David Blaikie9f88fe82013-04-22 06:13:21 +00003246void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
David Blaikiebd483762013-05-20 04:58:53 +00003247 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3248 return;
David Blaikie9f88fe82013-04-22 06:13:21 +00003249 DBuilder.createImportedModule(
David Blaikiebd483762013-05-20 04:58:53 +00003250 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3251 getOrCreateNameSpace(UD.getNominatedNamespace()),
David Blaikie9f88fe82013-04-22 06:13:21 +00003252 getLineNumber(UD.getLocation()));
3253}
3254
David Blaikiebd483762013-05-20 04:58:53 +00003255void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
3256 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3257 return;
3258 assert(UD.shadow_size() &&
3259 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3260 // Emitting one decl is sufficient - debuggers can detect that this is an
3261 // overloaded name & provide lookup for all the overloads.
3262 const UsingShadowDecl &USD = **UD.shadow_begin();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003263 if (llvm::DINode *Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003264 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003265 DBuilder.createImportedDeclaration(
3266 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3267 getLineNumber(USD.getLocation()));
3268}
3269
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003270void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
3271 auto *Reader = CGM.getContext().getExternalSource();
3272 auto Info = Reader->getSourceDescriptor(*ID.getImportedModule());
3273 DBuilder.createImportedDeclaration(
3274 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
3275 getOrCreateModuleRef(Info),
3276 getLineNumber(ID.getLocation()));
3277}
3278
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003279llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00003280CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
3281 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003282 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003283 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003284 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003285 return cast<llvm::DIImportedEntity>(VH);
3286 llvm::DIImportedEntity *R;
David Blaikief121b932013-05-20 22:50:41 +00003287 if (const NamespaceAliasDecl *Underlying =
3288 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3289 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003290 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003291 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3292 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3293 NA.getName());
3294 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003295 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003296 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3297 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3298 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003299 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003300 return R;
3301}
3302
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003303llvm::DINamespace *
Guy Benyei11169dd2012-12-18 14:30:41 +00003304CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003305 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003306 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003307 if (I != NameSpaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003308 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003309
Guy Benyei11169dd2012-12-18 14:30:41 +00003310 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003311 llvm::DIFile *FileD = getOrCreateFile(NSDecl->getLocation());
3312 llvm::DIScope *Context =
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003313 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003314 llvm::DINamespace *NS =
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003315 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003316 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003317 return NS;
3318}
3319
3320void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003321 // Creating types might create further types - invalidating the current
3322 // element and the size(), so don't cache/reference them.
3323 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3324 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003325 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003326 ? CreateTypeDefinition(E.Type, E.Unit)
3327 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003328 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00003329 }
3330
David Blaikief427b002014-05-06 03:42:01 +00003331 for (auto p : ReplaceMap) {
3332 assert(p.second);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003333 auto *Ty = cast<llvm::DIType>(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003334 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003335
David Blaikief427b002014-05-06 03:42:01 +00003336 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003337 assert(it != TypeCache.end());
3338 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003339
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003340 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
3341 cast<llvm::DIType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00003342 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003343
Frederic Rissd253ed62014-11-18 03:40:51 +00003344 for (const auto &p : FwdDeclReplaceMap) {
3345 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003346 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003347 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003348
3349 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003350 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003351 // with ourselves, that will destroy the temporary MDNode and
3352 // replace it with a standard one, avoiding leaking memory.
3353 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003354 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003355 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003356 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003357
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003358 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00003359 }
3360
Adrian Prantl73409ce2013-03-11 18:33:46 +00003361 // We keep our own list of retained types, because we need to look
3362 // up the final type in the type cache.
3363 for (std::vector<void *>::const_iterator RI = RetainedTypes.begin(),
3364 RE = RetainedTypes.end(); RI != RE; ++RI)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003365 DBuilder.retainType(cast<llvm::DIType>(TypeCache[*RI]));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003366
Guy Benyei11169dd2012-12-18 14:30:41 +00003367 DBuilder.finalize();
3368}
David Blaikie66088d52014-09-24 17:01:27 +00003369
3370void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
3371 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3372 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003373
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003374 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003375 // Don't ignore in case of explicit cast where it is referenced indirectly.
3376 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00003377}