blob: 09912493433c860f6aefb326eb9321909dc567ac [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"
Adrian Prantl9402cef2015-09-20 16:51:35 +000031#include "clang/Lex/ModuleMap.h"
Adrian Prantlc4bb47e2015-06-30 17:39:51 +000032#include "clang/Lex/PreprocessorOptions.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000033#include "llvm/ADT/SmallVector.h"
34#include "llvm/ADT/StringExtras.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000035#include "llvm/IR/Constants.h"
36#include "llvm/IR/DataLayout.h"
37#include "llvm/IR/DerivedTypes.h"
38#include "llvm/IR/Instructions.h"
39#include "llvm/IR/Intrinsics.h"
40#include "llvm/IR/Module.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000041#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()) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +000050 for (const auto &KV : CGM.getCodeGenOpts().DebugPrefixMap)
51 DebugPrefixMap[KV.first] = KV.second;
Guy Benyei11169dd2012-12-18 14:30:41 +000052 CreateCompileUnit();
53}
54
55CGDebugInfo::~CGDebugInfo() {
56 assert(LexicalBlockStack.empty() &&
57 "Region stack mismatch, stack not empty!");
58}
59
David Blaikie66e41972015-01-14 07:38:27 +000060ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
David Blaikie835afb22015-01-21 23:08:17 +000061 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000062 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +000063 init(TemporaryLocation);
64}
65
Adrian Prantl39428e72015-02-03 18:40:42 +000066ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
Adrian Prantl95b24e92015-02-03 20:00:54 +000067 bool DefaultToEmpty,
Adrian Prantl39428e72015-02-03 18:40:42 +000068 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000069 : CGF(&CGF) {
Adrian Prantl95b24e92015-02-03 20:00:54 +000070 init(TemporaryLocation, DefaultToEmpty);
Adrian Prantl39428e72015-02-03 18:40:42 +000071}
72
73void ApplyDebugLocation::init(SourceLocation TemporaryLocation,
Adrian Prantl95b24e92015-02-03 20:00:54 +000074 bool DefaultToEmpty) {
David Blaikied7057d92015-08-12 23:49:57 +000075 auto *DI = CGF->getDebugInfo();
76 if (!DI) {
77 CGF = nullptr;
78 return;
David Blaikie66e41972015-01-14 07:38:27 +000079 }
David Blaikied7057d92015-08-12 23:49:57 +000080
81 OriginalLocation = CGF->Builder.getCurrentDebugLocation();
82 if (TemporaryLocation.isValid()) {
83 DI->EmitLocation(CGF->Builder, TemporaryLocation);
84 return;
85 }
86
87 if (DefaultToEmpty) {
88 CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc());
89 return;
90 }
91
92 // Construct a location that has a valid scope, but no line info.
93 assert(!DI->LexicalBlockStack.empty());
94 CGF->Builder.SetCurrentDebugLocation(
95 llvm::DebugLoc::get(0, 0, DI->LexicalBlockStack.back()));
Adrian Prantl2e0637f2013-07-18 00:28:02 +000096}
97
David Blaikie9b479662015-01-25 01:19:10 +000098ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E)
David Blaikied7057d92015-08-12 23:49:57 +000099 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +0000100 init(E->getExprLoc());
101}
102
David Blaikie66e41972015-01-14 07:38:27 +0000103ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc)
David Blaikied7057d92015-08-12 23:49:57 +0000104 : CGF(&CGF) {
105 if (!CGF.getDebugInfo()) {
106 this->CGF = nullptr;
107 return;
David Blaikie66e41972015-01-14 07:38:27 +0000108 }
David Blaikied7057d92015-08-12 23:49:57 +0000109 OriginalLocation = CGF.Builder.getCurrentDebugLocation();
110 if (Loc)
111 CGF.Builder.SetCurrentDebugLocation(std::move(Loc));
David Blaikie66e41972015-01-14 07:38:27 +0000112}
113
114ApplyDebugLocation::~ApplyDebugLocation() {
115 // Query CGF so the location isn't overwritten when location updates are
116 // temporarily disabled (for C++ default function arguments)
David Blaikied7057d92015-08-12 23:49:57 +0000117 if (CGF)
118 CGF->Builder.SetCurrentDebugLocation(std::move(OriginalLocation));
David Blaikie66e41972015-01-14 07:38:27 +0000119}
120
Guy Benyei11169dd2012-12-18 14:30:41 +0000121void CGDebugInfo::setLocation(SourceLocation Loc) {
122 // If the new location isn't valid return.
Eric Christophere7b87e52014-10-26 23:40:33 +0000123 if (Loc.isInvalid())
124 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000125
126 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
127
128 // If we've changed files in the middle of a lexical scope go ahead
129 // and create a new lexical scope with file node if it's different
130 // from the one in the scope.
Eric Christophere7b87e52014-10-26 23:40:33 +0000131 if (LexicalBlockStack.empty())
132 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000133
134 SourceManager &SM = CGM.getContext().getSourceManager();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000135 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +0000136 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000137
Duncan P. N. Exon Smith373ee852015-04-16 01:36:36 +0000138 if (PCLoc.isInvalid() || Scope->getFilename() == PCLoc.getFilename())
Guy Benyei11169dd2012-12-18 14:30:41 +0000139 return;
140
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000141 if (auto *LBF = dyn_cast<llvm::DILexicalBlockFile>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000142 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000143 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlockFile(
144 LBF->getScope(), getOrCreateFile(CurLoc)));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000145 } else if (isa<llvm::DILexicalBlock>(Scope) ||
146 isa<llvm::DISubprogram>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000147 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000148 LexicalBlockStack.emplace_back(
149 DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000150 }
151}
152
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000153llvm::DIScope *CGDebugInfo::getDeclContextDescriptor(const Decl *D) {
Adrian Prantl5c8bd882015-09-11 17:23:08 +0000154 llvm::DIScope *Mod = getParentModuleOrNull(D);
155 return getContextDescriptor(cast<Decl>(D->getDeclContext()),
156 Mod ? Mod : TheCU);
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000157}
158
159llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context,
160 llvm::DIScope *Default) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000161 if (!Context)
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000162 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000163
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000164 auto I = RegionMap.find(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +0000165 if (I != RegionMap.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000166 llvm::Metadata *V = I->second;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000167 return dyn_cast_or_null<llvm::DIScope>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000168 }
169
170 // Check namespace.
171 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000172 return getOrCreateNameSpace(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +0000173
David Blaikiebfa52742013-04-19 06:56:38 +0000174 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context))
175 if (!RDecl->isDependentType())
176 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Eric Christophere7b87e52014-10-26 23:40:33 +0000177 getOrCreateMainFile());
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000178 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000179}
180
Guy Benyei11169dd2012-12-18 14:30:41 +0000181StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000182 assert(FD && "Invalid FunctionDecl!");
Guy Benyei11169dd2012-12-18 14:30:41 +0000183 IdentifierInfo *FII = FD->getIdentifier();
Eric Christophere7b87e52014-10-26 23:40:33 +0000184 FunctionTemplateSpecializationInfo *Info =
185 FD->getTemplateSpecializationInfo();
Guy Benyei11169dd2012-12-18 14:30:41 +0000186 if (!Info && FII)
187 return FII->getName();
188
189 // Otherwise construct human readable name for debug info.
Benjamin Kramer9170e912013-02-22 15:46:01 +0000190 SmallString<128> NS;
191 llvm::raw_svector_ostream OS(NS);
192 FD->printName(OS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000193
194 // Add any template specialization args.
195 if (Info) {
196 const TemplateArgumentList *TArgs = Info->TemplateArguments;
197 const TemplateArgument *Args = TArgs->data();
198 unsigned NumArgs = TArgs->size();
199 PrintingPolicy Policy(CGM.getLangOpts());
Benjamin Kramer9170e912013-02-22 15:46:01 +0000200 TemplateSpecializationType::PrintTemplateArgumentList(OS, Args, NumArgs,
201 Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000202 }
203
204 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000205 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000206}
207
208StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
209 SmallString<256> MethodName;
210 llvm::raw_svector_ostream OS(MethodName);
211 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
212 const DeclContext *DC = OMD->getDeclContext();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000213 if (const ObjCImplementationDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000214 dyn_cast<const ObjCImplementationDecl>(DC)) {
215 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000216 } else if (const ObjCInterfaceDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000217 dyn_cast<const ObjCInterfaceDecl>(DC)) {
218 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000219 } else if (const ObjCCategoryImplDecl *OCD =
Eric Christophere7b87e52014-10-26 23:40:33 +0000220 dyn_cast<const ObjCCategoryImplDecl>(DC)) {
221 OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '('
222 << OCD->getIdentifier()->getNameStart() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000223 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000224 // We can extract the type of the class from the self pointer.
Eric Christophere7b87e52014-10-26 23:40:33 +0000225 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000226 QualType ClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000227 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000228 ClassTy.print(OS, PrintingPolicy(LangOptions()));
229 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000230 }
231 OS << ' ' << OMD->getSelector().getAsString() << ']';
232
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000233 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000234}
235
Guy Benyei11169dd2012-12-18 14:30:41 +0000236StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000237 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000238}
239
Eric Christophere7b87e52014-10-26 23:40:33 +0000240StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
David Blaikie65813a32014-04-02 18:21:09 +0000241 // quick optimization to avoid having to intern strings that are already
242 // stored reliably elsewhere
243 if (!isa<ClassTemplateSpecializationDecl>(RD))
Guy Benyei11169dd2012-12-18 14:30:41 +0000244 return RD->getName();
245
David Blaikie65813a32014-04-02 18:21:09 +0000246 SmallString<128> Name;
Benjamin Kramer9170e912013-02-22 15:46:01 +0000247 {
David Blaikie65813a32014-04-02 18:21:09 +0000248 llvm::raw_svector_ostream OS(Name);
249 RD->getNameForDiagnostic(OS, CGM.getContext().getPrintingPolicy(),
250 /*Qualified*/ false);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000251 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000252
253 // Copy this name on the side and use its reference.
David Blaikie65813a32014-04-02 18:21:09 +0000254 return internString(Name);
Guy Benyei11169dd2012-12-18 14:30:41 +0000255}
256
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000257llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000258 if (!Loc.isValid())
259 // If Location is not valid then use main input file.
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000260 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
261 remapDIPath(TheCU->getDirectory()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000262
263 SourceManager &SM = CGM.getContext().getSourceManager();
264 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
265
266 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
267 // If the location is not valid then use main input file.
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000268 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
269 remapDIPath(TheCU->getDirectory()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000270
271 // Cache the results.
272 const char *fname = PLoc.getFilename();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000273 auto it = DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000274
275 if (it != DIFileCache.end()) {
276 // Verify that the information still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000277 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000278 return cast<llvm::DIFile>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000279 }
280
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000281 llvm::DIFile *F = DBuilder.createFile(remapDIPath(PLoc.getFilename()),
282 remapDIPath(getCurrentDirname()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000283
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000284 DIFileCache[fname].reset(F);
Guy Benyei11169dd2012-12-18 14:30:41 +0000285 return F;
286}
287
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000288llvm::DIFile *CGDebugInfo::getOrCreateMainFile() {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000289 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
290 remapDIPath(TheCU->getDirectory()));
291}
292
293std::string CGDebugInfo::remapDIPath(StringRef Path) const {
294 for (const auto &Entry : DebugPrefixMap)
295 if (Path.startswith(Entry.first))
296 return (Twine(Entry.second) + Path.substr(Entry.first.size())).str();
297 return Path.str();
Guy Benyei11169dd2012-12-18 14:30:41 +0000298}
299
Guy Benyei11169dd2012-12-18 14:30:41 +0000300unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
301 if (Loc.isInvalid() && CurLoc.isInvalid())
302 return 0;
303 SourceManager &SM = CGM.getContext().getSourceManager();
304 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000305 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000306}
307
Adrian Prantlc7822422013-03-12 20:43:25 +0000308unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000309 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000310 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000311 return 0;
312
313 // If the location is invalid then use the current column.
314 if (Loc.isInvalid() && CurLoc.isInvalid())
315 return 0;
316 SourceManager &SM = CGM.getContext().getSourceManager();
317 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000318 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000319}
320
321StringRef CGDebugInfo::getCurrentDirname() {
322 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
323 return CGM.getCodeGenOpts().DebugCompilationDir;
324
325 if (!CWDName.empty())
326 return CWDName;
327 SmallString<256> CWD;
328 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000329 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000330}
331
Guy Benyei11169dd2012-12-18 14:30:41 +0000332void CGDebugInfo::CreateCompileUnit() {
333
David Blaikieaabde052014-05-14 00:29:00 +0000334 // Should we be asking the SourceManager for the main file name, instead of
335 // accepting it as an argument? This just causes the main file name to
336 // mismatch with source locations and create extra lexical scopes or
337 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
338 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
339 // because that's what the SourceManager says)
340
Guy Benyei11169dd2012-12-18 14:30:41 +0000341 // Get absolute path name.
342 SourceManager &SM = CGM.getContext().getSourceManager();
343 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
344 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000345 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000346
347 // The main file name provided via the "-main-file-name" option contains just
348 // the file name itself with no path information. This file name may have had
349 // a relative path, so we look into the actual file entry for the main
350 // file to determine the real absolute path for the file.
351 std::string MainFileDir;
352 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000353 MainFileDir = remapDIPath(MainFile->getDir()->getName());
Yaron Keren9fb7e902013-10-21 20:07:37 +0000354 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000355 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
356 llvm::sys::path::append(MainFileDirSS, MainFileName);
357 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000358 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000359 }
360
Ed Masteda706022014-05-07 12:49:30 +0000361 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000362 const LangOptions &LO = CGM.getLangOpts();
363 if (LO.CPlusPlus) {
364 if (LO.ObjC1)
365 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
366 else
367 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
368 } else if (LO.ObjC1) {
369 LangTag = llvm::dwarf::DW_LANG_ObjC;
370 } else if (LO.C99) {
371 LangTag = llvm::dwarf::DW_LANG_C99;
372 } else {
373 LangTag = llvm::dwarf::DW_LANG_C89;
374 }
375
376 std::string Producer = getClangFullVersion();
377
378 // Figure out which version of the ObjC runtime we have.
379 unsigned RuntimeVers = 0;
380 if (LO.ObjC1)
381 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
382
383 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000384 // FIXME - Eliminate TheCU.
Eric Christophere4200a22014-02-27 01:25:08 +0000385 TheCU = DBuilder.createCompileUnit(
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000386 LangTag, remapDIPath(MainFileName), remapDIPath(getCurrentDirname()),
387 Producer, LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers,
Adrian Prantlb67dbce2015-09-11 18:45:02 +0000388 CGM.getCodeGenOpts().SplitDwarfFile,
Diego Novillo913690c2014-06-24 17:02:17 +0000389 DebugKind <= CodeGenOptions::DebugLineTablesOnly
Eric Christophere4200a22014-02-27 01:25:08 +0000390 ? llvm::DIBuilder::LineTablesOnly
Diego Novillo913690c2014-06-24 17:02:17 +0000391 : llvm::DIBuilder::FullDebug,
Adrian Prantlb67dbce2015-09-11 18:45:02 +0000392 0 /* DWOid */, DebugKind != CodeGenOptions::LocTrackingOnly);
Guy Benyei11169dd2012-12-18 14:30:41 +0000393}
394
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000395llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000396 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000397 StringRef BTName;
398 switch (BT->getKind()) {
399#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000400#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000401#include "clang/AST/BuiltinTypes.def"
402 case BuiltinType::Dependent:
403 llvm_unreachable("Unexpected builtin type");
404 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000405 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000406 case BuiltinType::Void:
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000407 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +0000408 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000409 if (!ClassTy)
410 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
411 "objc_class", TheCU,
412 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000413 return ClassTy;
414 case BuiltinType::ObjCId: {
415 // typedef struct objc_class *Class;
416 // typedef struct objc_object {
417 // Class isa;
418 // } *id;
419
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000420 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000421 return ObjTy;
422
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000423 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000424 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
425 "objc_class", TheCU,
426 getOrCreateMainFile(), 0);
427
428 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000429
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000430 auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000431
Eric Christopher5c7ee8b2013-04-02 22:59:11 +0000432 ObjTy =
David Blaikie6d4fe152013-02-25 01:07:08 +0000433 DBuilder.createStructType(TheCU, "objc_object", getOrCreateMainFile(),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000434 0, 0, 0, 0, nullptr, llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000435
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000436 DBuilder.replaceArrays(
437 ObjTy,
438 DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
439 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0, 0, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000440 return ObjTy;
441 }
442 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000443 if (!SelTy)
444 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
445 "objc_selector", TheCU,
446 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000447 return SelTy;
448 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000449
450 case BuiltinType::OCLImage1d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000451 return getOrCreateStructPtrType("opencl_image1d_t", OCLImage1dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000452 case BuiltinType::OCLImage1dArray:
Eric Christopherb2a008c2013-05-16 00:45:12 +0000453 return getOrCreateStructPtrType("opencl_image1d_array_t",
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000454 OCLImage1dArrayDITy);
455 case BuiltinType::OCLImage1dBuffer:
456 return getOrCreateStructPtrType("opencl_image1d_buffer_t",
457 OCLImage1dBufferDITy);
458 case BuiltinType::OCLImage2d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000459 return getOrCreateStructPtrType("opencl_image2d_t", OCLImage2dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000460 case BuiltinType::OCLImage2dArray:
461 return getOrCreateStructPtrType("opencl_image2d_array_t",
462 OCLImage2dArrayDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000463 case BuiltinType::OCLImage2dDepth:
464 return getOrCreateStructPtrType("opencl_image2d_depth_t",
465 OCLImage2dDepthDITy);
466 case BuiltinType::OCLImage2dArrayDepth:
467 return getOrCreateStructPtrType("opencl_image2d_array_depth_t",
468 OCLImage2dArrayDepthDITy);
469 case BuiltinType::OCLImage2dMSAA:
470 return getOrCreateStructPtrType("opencl_image2d_msaa_t",
471 OCLImage2dMSAADITy);
472 case BuiltinType::OCLImage2dArrayMSAA:
473 return getOrCreateStructPtrType("opencl_image2d_array_msaa_t",
474 OCLImage2dArrayMSAADITy);
475 case BuiltinType::OCLImage2dMSAADepth:
476 return getOrCreateStructPtrType("opencl_image2d_msaa_depth_t",
477 OCLImage2dMSAADepthDITy);
478 case BuiltinType::OCLImage2dArrayMSAADepth:
479 return getOrCreateStructPtrType("opencl_image2d_array_msaa_depth_t",
480 OCLImage2dArrayMSAADepthDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000481 case BuiltinType::OCLImage3d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000482 return getOrCreateStructPtrType("opencl_image3d_t", OCLImage3dDITy);
Guy Benyei61054192013-02-07 10:55:47 +0000483 case BuiltinType::OCLSampler:
Eric Christophere7b87e52014-10-26 23:40:33 +0000484 return DBuilder.createBasicType(
485 "opencl_sampler_t", CGM.getContext().getTypeSize(BT),
486 CGM.getContext().getTypeAlign(BT), llvm::dwarf::DW_ATE_unsigned);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000487 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000488 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000489 case BuiltinType::OCLClkEvent:
490 return getOrCreateStructPtrType("opencl_clk_event_t", OCLClkEventDITy);
491 case BuiltinType::OCLQueue:
492 return getOrCreateStructPtrType("opencl_queue_t", OCLQueueDITy);
493 case BuiltinType::OCLNDRange:
494 return getOrCreateStructPtrType("opencl_ndrange_t", OCLNDRangeDITy);
495 case BuiltinType::OCLReserveID:
496 return getOrCreateStructPtrType("opencl_reserve_id_t", OCLReserveIDDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000497
Guy Benyei11169dd2012-12-18 14:30:41 +0000498 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000499 case BuiltinType::Char_U:
500 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
501 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000502 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000503 case BuiltinType::SChar:
504 Encoding = llvm::dwarf::DW_ATE_signed_char;
505 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000506 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000507 case BuiltinType::Char32:
508 Encoding = llvm::dwarf::DW_ATE_UTF;
509 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000510 case BuiltinType::UShort:
511 case BuiltinType::UInt:
512 case BuiltinType::UInt128:
513 case BuiltinType::ULong:
514 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000515 case BuiltinType::ULongLong:
516 Encoding = llvm::dwarf::DW_ATE_unsigned;
517 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000518 case BuiltinType::Short:
519 case BuiltinType::Int:
520 case BuiltinType::Int128:
521 case BuiltinType::Long:
522 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000523 case BuiltinType::LongLong:
524 Encoding = llvm::dwarf::DW_ATE_signed;
525 break;
526 case BuiltinType::Bool:
527 Encoding = llvm::dwarf::DW_ATE_boolean;
528 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000529 case BuiltinType::Half:
530 case BuiltinType::Float:
531 case BuiltinType::LongDouble:
Eric Christophere7b87e52014-10-26 23:40:33 +0000532 case BuiltinType::Double:
533 Encoding = llvm::dwarf::DW_ATE_float;
534 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000535 }
536
537 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000538 case BuiltinType::Long:
539 BTName = "long int";
540 break;
541 case BuiltinType::LongLong:
542 BTName = "long long int";
543 break;
544 case BuiltinType::ULong:
545 BTName = "long unsigned int";
546 break;
547 case BuiltinType::ULongLong:
548 BTName = "long long unsigned int";
549 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000550 default:
551 BTName = BT->getName(CGM.getLangOpts());
552 break;
553 }
554 // Bit size, align and offset of the type.
555 uint64_t Size = CGM.getContext().getTypeSize(BT);
556 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000557 return DBuilder.createBasicType(BTName, Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000558}
559
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000560llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000561 // Bit size, align and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000562 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000563 if (Ty->isComplexIntegerType())
564 Encoding = llvm::dwarf::DW_ATE_lo_user;
565
566 uint64_t Size = CGM.getContext().getTypeSize(Ty);
567 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000568 return DBuilder.createBasicType("complex", Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000569}
570
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000571llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
572 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000573 QualifierCollector Qc;
574 const Type *T = Qc.strip(Ty);
575
576 // Ignore these qualifiers for now.
577 Qc.removeObjCGCAttr();
578 Qc.removeAddressSpace();
579 Qc.removeObjCLifetime();
580
581 // We will create one Derived type for one qualifier and recurse to handle any
582 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000583 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000584 if (Qc.hasConst()) {
585 Tag = llvm::dwarf::DW_TAG_const_type;
586 Qc.removeConst();
587 } else if (Qc.hasVolatile()) {
588 Tag = llvm::dwarf::DW_TAG_volatile_type;
589 Qc.removeVolatile();
590 } else if (Qc.hasRestrict()) {
591 Tag = llvm::dwarf::DW_TAG_restrict_type;
592 Qc.removeRestrict();
593 } else {
594 assert(Qc.empty() && "Unknown type qualifier for debug info");
595 return getOrCreateType(QualType(T, 0), Unit);
596 }
597
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000598 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000599
600 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
601 // CVR derived types.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000602 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000603}
604
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000605llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
606 llvm::DIFile *Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000607
608 // The frontend treats 'id' as a typedef to an ObjCObjectType,
609 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
610 // debug info, we want to emit 'id' in both cases.
611 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000612 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000613
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000614 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
615 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000616}
617
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000618llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
619 llvm::DIFile *Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000620 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000621 Ty->getPointeeType(), Unit);
622}
623
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000624/// \return whether a C++ mangling exists for the type defined by TD.
625static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
626 switch (TheCU->getSourceLanguage()) {
627 case llvm::dwarf::DW_LANG_C_plus_plus:
628 return true;
629 case llvm::dwarf::DW_LANG_ObjC_plus_plus:
630 return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
631 default:
632 return false;
633 }
634}
635
Manman Rene0064d82013-08-29 23:19:58 +0000636/// In C++ mode, types have linkage, so we can rely on the ODR and
637/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000638static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
639 CodeGenModule &CGM,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000640 llvm::DICompileUnit *TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000641 SmallString<256> FullName;
Manman Rene0064d82013-08-29 23:19:58 +0000642 const TagDecl *TD = Ty->getDecl();
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000643
644 if (!hasCXXMangling(TD, TheCU) || !TD->isExternallyVisible())
Manman Rene0064d82013-08-29 23:19:58 +0000645 return FullName;
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000646
Manman Rene0064d82013-08-29 23:19:58 +0000647 // Microsoft Mangler does not have support for mangleCXXRTTIName yet.
648 if (CGM.getTarget().getCXXABI().isMicrosoft())
649 return FullName;
650
651 // TODO: This is using the RTTI name. Is there a better way to get
652 // a unique string for a type?
653 llvm::raw_svector_ostream Out(FullName);
654 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
Manman Rene0064d82013-08-29 23:19:58 +0000655 return FullName;
656}
657
Adrian Prantl3e8bad42015-07-08 21:18:34 +0000658/// \return the approproate DWARF tag for a composite type.
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000659static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
660 llvm::dwarf::Tag Tag;
661 if (RD->isStruct() || RD->isInterface())
662 Tag = llvm::dwarf::DW_TAG_structure_type;
663 else if (RD->isUnion())
664 Tag = llvm::dwarf::DW_TAG_union_type;
665 else {
666 // FIXME: This could be a struct type giving a default visibility different
667 // than C++ class type, but needs llvm metadata changes first.
668 assert(RD->isClass());
669 Tag = llvm::dwarf::DW_TAG_class_type;
670 }
671 return Tag;
672}
673
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000674llvm::DICompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000675CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000676 llvm::DIScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000677 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000678 if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
679 return cast<llvm::DICompositeType>(T);
680 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +0000681 unsigned Line = getLineNumber(RD->getLocation());
682 StringRef RDName = getClassName(RD);
683
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000684 uint64_t Size = 0;
685 uint64_t Align = 0;
686
687 const RecordDecl *D = RD->getDefinition();
688 if (D && D->isCompleteDefinition()) {
689 Size = CGM.getContext().getTypeSize(Ty);
690 Align = CGM.getContext().getTypeAlign(Ty);
691 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000692
693 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000694 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000695 llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000696 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000697 llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000698 ReplaceMap.emplace_back(
699 std::piecewise_construct, std::make_tuple(Ty),
700 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000701 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000702}
703
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000704llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000705 const Type *Ty,
706 QualType PointeeTy,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000707 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000708 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
709 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
David Blaikie99dab3b2013-09-04 22:03:57 +0000710 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit));
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000711
Guy Benyei11169dd2012-12-18 14:30:41 +0000712 // Bit size, align and offset of the type.
713 // Size is always the size of a pointer. We can't use getTypeSize here
714 // because that does not return the correct value for references.
715 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000716 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000717 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
718
David Blaikie99dab3b2013-09-04 22:03:57 +0000719 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
720 Align);
Guy Benyei11169dd2012-12-18 14:30:41 +0000721}
722
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000723llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
724 llvm::DIType *&Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000725 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000726 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000727 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
728 TheCU, getOrCreateMainFile(), 0);
729 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
730 Cache = DBuilder.createPointerType(Cache, Size);
731 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000732}
733
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000734llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
735 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000736 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000737 QualType FType;
738 uint64_t FieldSize, FieldOffset;
739 unsigned FieldAlign;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000740 llvm::DINodeArray Elements;
Guy Benyei11169dd2012-12-18 14:30:41 +0000741
742 FieldOffset = 0;
743 FType = CGM.getContext().UnsignedLongTy;
744 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
745 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
746
747 Elements = DBuilder.getOrCreateArray(EltTys);
748 EltTys.clear();
749
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000750 unsigned Flags = llvm::DINode::FlagAppleBlock;
Adrian Prantl498fff62015-07-06 21:31:35 +0000751 unsigned LineNo = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000752
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000753 auto *EltTy =
Adrian Prantl498fff62015-07-06 21:31:35 +0000754 DBuilder.createStructType(Unit, "__block_descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000755 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000756
757 // Bit size, align and offset of the type.
758 uint64_t Size = CGM.getContext().getTypeSize(Ty);
759
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000760 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000761
762 FieldOffset = 0;
763 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
764 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
765 FType = CGM.getContext().IntTy;
766 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
767 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000768 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000769 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
770
771 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000772 FieldSize = CGM.getContext().getTypeSize(Ty);
773 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Adrian Prantl498fff62015-07-06 21:31:35 +0000774 EltTys.push_back(DBuilder.createMemberType(Unit, "__descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000775 FieldSize, FieldAlign, FieldOffset,
776 0, DescTy));
Guy Benyei11169dd2012-12-18 14:30:41 +0000777
778 FieldOffset += FieldSize;
779 Elements = DBuilder.getOrCreateArray(EltTys);
780
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000781 // The __block_literal_generic structs are marked with a special
782 // DW_AT_APPLE_BLOCK attribute and are an implementation detail only
783 // the debugger needs to know about. To allow type uniquing, emit
784 // them without a name or a location.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000785 EltTy =
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000786 DBuilder.createStructType(Unit, "", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000787 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000788
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000789 return DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000790}
791
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000792llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
793 llvm::DIFile *Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000794 assert(Ty->isTypeAlias());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000795 llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000796
797 SmallString<128> NS;
798 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000799 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
800 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000801
802 TemplateSpecializationType::PrintTemplateArgumentList(
803 OS, Ty->getArgs(), Ty->getNumArgs(),
804 CGM.getContext().getPrintingPolicy());
805
Eric Christophere7b87e52014-10-26 23:40:33 +0000806 TypeAliasDecl *AliasDecl = cast<TypeAliasTemplateDecl>(
807 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000808
809 SourceLocation Loc = AliasDecl->getLocation();
Adrian Prantlb67dbce2015-09-11 18:45:02 +0000810 return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc),
811 getLineNumber(Loc),
812 getDeclContextDescriptor(AliasDecl));
David Blaikief1b382e2014-04-06 17:14:06 +0000813}
814
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000815llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
816 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000817 // We don't set size information, but do specify where the typedef was
818 // declared.
David Blaikiebe822ed2015-07-01 18:07:22 +0000819 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000820
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000821 // Typedefs are derived from some other type.
822 return DBuilder.createTypedef(
823 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
824 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000825 getDeclContextDescriptor(Ty->getDecl()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000826}
827
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000828llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
829 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000830 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000831
832 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000833 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000834
835 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000836 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000837 if (isa<FunctionNoProtoType>(Ty))
838 EltTys.push_back(DBuilder.createUnspecifiedParameter());
839 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
Alp Toker9cacbab2014-01-20 20:26:09 +0000840 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
841 EltTys.push_back(getOrCreateType(FPT->getParamType(i), Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000842 if (FPT->isVariadic())
843 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000844 }
845
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000846 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Guy Benyei11169dd2012-12-18 14:30:41 +0000847 return DBuilder.createSubroutineType(Unit, EltTypeArray);
848}
849
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000850/// Convert an AccessSpecifier into the corresponding DINode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +0000851/// As an optimization, return 0 if the access specifier equals the
852/// default for the containing type.
853static unsigned getAccessFlag(AccessSpecifier Access, const RecordDecl *RD) {
854 AccessSpecifier Default = clang::AS_none;
855 if (RD && RD->isClass())
856 Default = clang::AS_private;
857 else if (RD && (RD->isStruct() || RD->isUnion()))
858 Default = clang::AS_public;
859
860 if (Access == Default)
861 return 0;
862
Eric Christophere7b87e52014-10-26 23:40:33 +0000863 switch (Access) {
864 case clang::AS_private:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000865 return llvm::DINode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +0000866 case clang::AS_protected:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000867 return llvm::DINode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +0000868 case clang::AS_public:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000869 return llvm::DINode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +0000870 case clang::AS_none:
871 return 0;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000872 }
873 llvm_unreachable("unexpected access enumerator");
874}
Guy Benyei11169dd2012-12-18 14:30:41 +0000875
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000876llvm::DIType *CGDebugInfo::createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000877 StringRef name, QualType type, uint64_t sizeInBitsOverride,
878 SourceLocation loc, AccessSpecifier AS, uint64_t offsetInBits,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000879 llvm::DIFile *tunit, llvm::DIScope *scope, const RecordDecl *RD) {
880 llvm::DIType *debugType = getOrCreateType(type, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000881
882 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000883 llvm::DIFile *file = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000884 unsigned line = getLineNumber(loc);
885
David Majnemer34b57492014-07-30 01:30:47 +0000886 uint64_t SizeInBits = 0;
887 unsigned AlignInBits = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000888 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +0000889 TypeInfo TI = CGM.getContext().getTypeInfo(type);
890 SizeInBits = TI.Width;
891 AlignInBits = TI.Align;
Guy Benyei11169dd2012-12-18 14:30:41 +0000892
893 if (sizeInBitsOverride)
David Majnemer34b57492014-07-30 01:30:47 +0000894 SizeInBits = sizeInBitsOverride;
Guy Benyei11169dd2012-12-18 14:30:41 +0000895 }
896
Adrian Prantl21361fb2014-08-29 22:44:27 +0000897 unsigned flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +0000898 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
899 AlignInBits, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +0000900}
901
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000902void CGDebugInfo::CollectRecordLambdaFields(
903 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000904 llvm::DIType *RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +0000905 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
906 // has the name and the location of the variable so we should iterate over
907 // both concurrently.
908 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
909 RecordDecl::field_iterator Field = CXXDecl->field_begin();
910 unsigned fieldno = 0;
911 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +0000912 E = CXXDecl->captures_end();
913 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000914 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +0000915 if (C.capturesVariable()) {
916 VarDecl *V = C.getCapturedVar();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000917 llvm::DIFile *VUnit = getOrCreateFile(C.getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000918 StringRef VName = V->getName();
919 uint64_t SizeInBitsOverride = 0;
920 if (Field->isBitField()) {
921 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
922 assert(SizeInBitsOverride && "found named 0-width bitfield");
923 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000924 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000925 VName, Field->getType(), SizeInBitsOverride, C.getLocation(),
926 Field->getAccess(), layout.getFieldOffset(fieldno), VUnit, RecordTy,
927 CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000928 elements.push_back(fieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +0000929 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +0000930 // TODO: Need to handle 'this' in some way by probably renaming the
931 // this of the lambda class and having a field member of 'this' or
932 // by using AT_object_pointer for the function and having that be
933 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +0000934 FieldDecl *f = *Field;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000935 llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000936 QualType type = f->getType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000937 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000938 "this", type, 0, f->getLocation(), f->getAccess(),
939 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000940
941 elements.push_back(fieldType);
942 }
943 }
944}
945
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000946llvm::DIDerivedType *
947CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +0000948 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000949 // Create the descriptor for the static variable, with or without
950 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +0000951 Var = Var->getCanonicalDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000952 llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
953 llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher91a31902013-01-16 01:22:32 +0000954
Eric Christopher91a31902013-01-16 01:22:32 +0000955 unsigned LineNumber = getLineNumber(Var->getLocation());
956 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +0000957 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +0000958 if (Var->getInit()) {
959 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +0000960 if (Value) {
961 if (Value->isInt())
962 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
963 if (Value->isFloat())
964 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
965 }
Eric Christopher91a31902013-01-16 01:22:32 +0000966 }
967
Adrian Prantl21361fb2014-08-29 22:44:27 +0000968 unsigned Flags = getAccessFlag(Var->getAccess(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000969 llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
David Blaikieae019462013-08-15 22:50:29 +0000970 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000971 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +0000972 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +0000973}
974
Eric Christophere7b87e52014-10-26 23:40:33 +0000975void CGDebugInfo::CollectRecordNormalField(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000976 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
977 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +0000978 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000979 StringRef name = field->getName();
980 QualType type = field->getType();
981
982 // Ignore unnamed fields unless they're anonymous structs/unions.
983 if (name.empty() && !type->isRecordType())
984 return;
985
986 uint64_t SizeInBitsOverride = 0;
987 if (field->isBitField()) {
988 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
989 assert(SizeInBitsOverride && "found named 0-width bitfield");
990 }
991
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000992 llvm::DIType *fieldType =
Eric Christophere7b87e52014-10-26 23:40:33 +0000993 createFieldType(name, type, SizeInBitsOverride, field->getLocation(),
994 field->getAccess(), OffsetInBits, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +0000995
996 elements.push_back(fieldType);
997}
998
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000999void CGDebugInfo::CollectRecordFields(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001000 const RecordDecl *record, llvm::DIFile *tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001001 SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001002 llvm::DICompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001003 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
1004
Eric Christopher91a31902013-01-16 01:22:32 +00001005 if (CXXDecl && CXXDecl->isLambda())
1006 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
1007 else {
1008 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001009
Eric Christopher91a31902013-01-16 01:22:32 +00001010 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +00001011 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +00001012
Eric Christopher91a31902013-01-16 01:22:32 +00001013 // Static and non-static members should appear in the same order as
1014 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +00001015 for (const auto *I : record->decls())
1016 if (const auto *V = dyn_cast<VarDecl>(I)) {
David Blaikiece763042013-08-20 21:49:21 +00001017 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001018 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +00001019 if (MI != StaticDataMemberCache.end()) {
1020 assert(MI->second &&
1021 "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00001022 elements.push_back(MI->second);
Adrian Prantl21361fb2014-08-29 22:44:27 +00001023 } else {
1024 auto Field = CreateRecordStaticField(V, RecordTy, record);
1025 elements.push_back(Field);
1026 }
Aaron Ballman629afae2014-03-07 19:56:05 +00001027 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001028 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
1029 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +00001030
1031 // Bump field number for next field.
1032 ++fieldNo;
Guy Benyei11169dd2012-12-18 14:30:41 +00001033 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001034 }
1035}
1036
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001037llvm::DISubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +00001038CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001039 llvm::DIFile *Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001040 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001041 if (Method->isStatic())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001042 return cast_or_null<llvm::DISubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001043 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001044 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1045 Func, Unit);
1046}
David Blaikie2aaf0652013-01-07 22:24:59 +00001047
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001048llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1049 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001050 // Add "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001051 llvm::DITypeRefArray Args(
1052 cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001053 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001054 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001055
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001056 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001057
1058 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001059 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001060
David Blaikie2aaf0652013-01-07 22:24:59 +00001061 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001062 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001063 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1064 // Create pointer type directly in this case.
1065 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1066 QualType PointeeTy = ThisPtrTy->getPointeeType();
1067 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001068 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie2aaf0652013-01-07 22:24:59 +00001069 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001070 llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1071 llvm::DIType *ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001072 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001073 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001074 // TODO: This and the artificial type below are misleading, the
1075 // types aren't artificial the argument is, but the current
1076 // metadata doesn't represent that.
1077 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1078 Elts.push_back(ThisPtrType);
1079 } else {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001080 llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001081 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001082 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1083 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001084 }
1085
1086 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001087 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1088 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001089
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001090 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001091
Adrian Prantl0630eb72013-12-18 21:48:18 +00001092 unsigned Flags = 0;
1093 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001094 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001095 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001096 Flags |= llvm::DINode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001097
1098 return DBuilder.createSubroutineType(Unit, EltTypeArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001099}
1100
Eric Christopherb2a008c2013-05-16 00:45:12 +00001101/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001102/// inside a function.
1103static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1104 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1105 return isFunctionLocalClass(NRD);
1106 if (isa<FunctionDecl>(RD->getDeclContext()))
1107 return true;
1108 return false;
1109}
1110
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001111llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1112 const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001113 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001114 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001115
Guy Benyei11169dd2012-12-18 14:30:41 +00001116 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001117 llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001118
1119 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1120 // make sense to give a single ctor/dtor a linkage name.
1121 StringRef MethodLinkageName;
1122 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1123 MethodLinkageName = CGM.getMangledName(Method);
1124
1125 // Get the location for the method.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001126 llvm::DIFile *MethodDefUnit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00001127 unsigned MethodLine = 0;
1128 if (!Method->isImplicit()) {
1129 MethodDefUnit = getOrCreateFile(Method->getLocation());
1130 MethodLine = getLineNumber(Method->getLocation());
1131 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001132
1133 // Collect virtual method info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001134 llvm::DIType *ContainingType = nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001135 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001136 unsigned VIndex = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001137
Guy Benyei11169dd2012-12-18 14:30:41 +00001138 if (Method->isVirtual()) {
1139 if (Method->isPure())
1140 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1141 else
1142 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001143
Guy Benyei11169dd2012-12-18 14:30:41 +00001144 // It doesn't make sense to give a virtual destructor a vtable index,
1145 // since a single destructor has two entries in the vtable.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001146 // FIXME: Add proper support for debug info for virtual calls in
1147 // the Microsoft ABI, where we may use multiple vptrs to make a vftable
1148 // lookup if we have multiple or virtual inheritance.
1149 if (!isa<CXXDestructorDecl>(Method) &&
1150 !CGM.getTarget().getCXXABI().isMicrosoft())
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001151 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
Guy Benyei11169dd2012-12-18 14:30:41 +00001152 ContainingType = RecordTy;
1153 }
1154
1155 unsigned Flags = 0;
1156 if (Method->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001157 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001158 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
Guy Benyei11169dd2012-12-18 14:30:41 +00001159 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1160 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001161 Flags |= llvm::DINode::FlagExplicit;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001162 } else if (const CXXConversionDecl *CXXC =
Eric Christophere7b87e52014-10-26 23:40:33 +00001163 dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001164 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001165 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001166 }
1167 if (Method->hasPrototype())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001168 Flags |= llvm::DINode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001169 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001170 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001171 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001172 Flags |= llvm::DINode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001173
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001174 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1175 llvm::DISubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001176 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
1177 MethodTy, /*isLocalToUnit=*/false,
1178 /* isDefinition=*/false, Virtuality, VIndex, ContainingType, Flags,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00001179 CGM.getLangOpts().Optimize, nullptr, TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001180
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001181 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001182
1183 return SP;
1184}
1185
Eric Christophere7b87e52014-10-26 23:40:33 +00001186void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001187 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1188 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001189
1190 // Since we want more than just the individual member decls if we
1191 // have templated functions iterate over every declaration to gather
1192 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001193 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001194 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1195 // If the member is implicit, don't add it to the member list. This avoids
1196 // the member being added to type units by LLVM, while still allowing it
1197 // to be emitted into the type declaration/reference inside the compile
1198 // unit.
Paul Robinson6a7511b2015-06-25 17:50:43 +00001199 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
David Blaikie6dddfe32014-10-06 05:52:27 +00001200 // FIXME: Handle Using(Shadow?)Decls here to create
1201 // DW_TAG_imported_declarations inside the class for base decls brought into
1202 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1203 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1204 // referenced)
Paul Robinson6a7511b2015-06-25 17:50:43 +00001205 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
David Blaikiefd580722014-10-06 05:18:55 +00001206 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001207
1208 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1209 continue;
1210
David Blaikiefd580722014-10-06 05:18:55 +00001211 // Reuse the existing member function declaration if it exists.
1212 // It may be associated with the declaration of the type & should be
1213 // reused as we're building the definition.
1214 //
1215 // This situation can arise in the vtable-based debug info reduction where
1216 // implicit members are emitted in a non-vtable TU.
1217 auto MI = SPCache.find(Method->getCanonicalDecl());
1218 EltTys.push_back(MI == SPCache.end()
1219 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001220 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001221 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001222}
Guy Benyei11169dd2012-12-18 14:30:41 +00001223
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001224void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001225 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001226 llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001227 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Aaron Ballman574705e2014-03-13 15:41:46 +00001228 for (const auto &BI : RD->bases()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001229 unsigned BFlags = 0;
1230 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001231
Guy Benyei11169dd2012-12-18 14:30:41 +00001232 const CXXRecordDecl *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001233 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001234
Aaron Ballman574705e2014-03-13 15:41:46 +00001235 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001236 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1237 // virtual base offset offset is -ve. The code generator emits dwarf
1238 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001239 BaseOffset = 0 - CGM.getItaniumVTableContext()
1240 .getVirtualBaseOffsetOffset(RD, Base)
1241 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001242 } else {
1243 // In the MS ABI, store the vbtable offset, which is analogous to the
1244 // vbase offset offset in Itanium.
1245 BaseOffset =
1246 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1247 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001248 BFlags = llvm::DINode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001249 } else
1250 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1251 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1252 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001253
Adrian Prantl21361fb2014-08-29 22:44:27 +00001254 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001255 llvm::DIType *DTy = DBuilder.createInheritance(
Eric Christophere7b87e52014-10-26 23:40:33 +00001256 RecordTy, getOrCreateType(BI.getType(), Unit), BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001257 EltTys.push_back(DTy);
1258 }
1259}
1260
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001261llvm::DINodeArray
Eric Christophere7b87e52014-10-26 23:40:33 +00001262CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1263 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001264 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001265 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001266 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1267 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001268 StringRef Name;
1269 if (TPList)
1270 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001271 switch (TA.getKind()) {
1272 case TemplateArgument::Type: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001273 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001274 TemplateParams.push_back(
1275 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001276 } break;
1277 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001278 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001279 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1280 TheCU, Name, TTy,
1281 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001282 } break;
1283 case TemplateArgument::Declaration: {
1284 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001285 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001286 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001287 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001288 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001289 // Variable pointer template parameters have a value that is the address
1290 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001291 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001292 V = CGM.GetAddrOfGlobalVar(VD);
1293 // Member function pointers have special support for building them, though
1294 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001295 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Majnemere2be95b2015-06-23 07:31:01 +00001296 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001297 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001298 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001299 // Member data pointers have special handling too to compute the fixed
1300 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001301 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001302 // These five lines (& possibly the above member function pointer
1303 // handling) might be able to be refactored to use similar code in
1304 // CodeGenModule::getMemberPointerConstant
1305 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1306 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001307 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001308 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001309 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001310 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1311 TheCU, Name, TTy,
1312 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001313 } break;
1314 case TemplateArgument::NullPtr: {
1315 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001316 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001317 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001318 // Special case member data pointer null values since they're actually -1
1319 // instead of zero.
1320 if (const MemberPointerType *MPT =
1321 dyn_cast<MemberPointerType>(T.getTypePtr()))
1322 // But treat member function pointers as simple zero integers because
1323 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1324 // CodeGen grows handling for values of non-null member function
1325 // pointers then perhaps we could remove this special case and rely on
1326 // EmitNullMemberPointer for member function pointers.
1327 if (MPT->isMemberDataPointer())
1328 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1329 if (!V)
1330 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001331 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1332 TheCU, Name, TTy, cast<llvm::Constant>(V)));
David Blaikie38079fd2013-05-10 21:53:14 +00001333 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001334 case TemplateArgument::Template:
1335 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001336 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001337 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1338 break;
1339 case TemplateArgument::Pack:
1340 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1341 TheCU, Name, nullptr,
1342 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1343 break;
David Majnemer5559d472013-08-24 08:21:10 +00001344 case TemplateArgument::Expression: {
1345 const Expr *E = TA.getAsExpr();
1346 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001347 if (E->isGLValue())
1348 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001349 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001350 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001351 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001352 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1353 TheCU, Name, TTy, cast<llvm::Constant>(V->stripPointerCasts())));
David Majnemer5559d472013-08-24 08:21:10 +00001354 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001355 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001356 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001357 case TemplateArgument::Null:
1358 llvm_unreachable(
1359 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001360 }
1361 }
1362 return DBuilder.getOrCreateArray(TemplateParams);
1363}
1364
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001365llvm::DINodeArray
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001366CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001367 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001368 if (FD->getTemplatedKind() ==
1369 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001370 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1371 ->getTemplate()
1372 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001373 return CollectTemplateParams(
1374 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001375 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001376 return llvm::DINodeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001377}
1378
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001379llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1380 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001381 // Always get the full list of parameters, not just the ones from
1382 // the specialization.
1383 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001384 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001385 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001386 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001387}
1388
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001389llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001390 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001391 return VTablePtrType;
1392
1393 ASTContext &Context = CGM.getContext();
1394
1395 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001396 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001397 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
1398 llvm::DIType *SubTy = DBuilder.createSubroutineType(Unit, SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001399 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001400 llvm::DIType *vtbl_ptr_type =
Eric Christophere7b87e52014-10-26 23:40:33 +00001401 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001402 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1403 return VTablePtrType;
1404}
1405
Guy Benyei11169dd2012-12-18 14:30:41 +00001406StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001407 // Copy the gdb compatible name on the side and use its reference.
1408 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001409}
1410
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001411void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001412 SmallVectorImpl<llvm::Metadata *> &EltTys) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001413 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1414
1415 // If there is a primary base then it will hold vtable info.
1416 if (RL.getPrimaryBase())
1417 return;
1418
1419 // If this class is not dynamic then there is not any vtable info to collect.
1420 if (!RD->isDynamicClass())
1421 return;
1422
1423 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001424 llvm::DIType *VPTR = DBuilder.createMemberType(
Eric Christophere7b87e52014-10-26 23:40:33 +00001425 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001426 llvm::DINode::FlagArtificial, getOrCreateVTablePtrType(Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001427 EltTys.push_back(VPTR);
1428}
1429
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001430llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001431 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001432 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001433 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001434 return T;
1435}
1436
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001437llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001438 SourceLocation Loc) {
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001439 return getOrCreateStandaloneType(D, Loc);
1440}
1441
1442llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
1443 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001444 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001445 assert(!D.isNull() && "null type");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001446 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001447 assert(T && "could not create debug info for type");
Adrian Prantl3a884fa2015-08-27 22:56:46 +00001448
1449 // Composite types with UIDs were already retained by DIBuilder
1450 // because they are only referenced by name in the IR.
1451 if (auto *CTy = dyn_cast<llvm::DICompositeType>(T))
1452 if (!CTy->getIdentifier().empty())
1453 return T;
Adrian Prantl73409ce2013-03-11 18:33:46 +00001454 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001455 return T;
1456}
1457
David Blaikie483a9da2014-05-06 18:35:21 +00001458void CGDebugInfo::completeType(const EnumDecl *ED) {
1459 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1460 return;
1461 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001462 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001463 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001464 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001465 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001466 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001467 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001468 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001469}
1470
David Blaikieb2e86eb2013-08-15 20:49:17 +00001471void CGDebugInfo::completeType(const RecordDecl *RD) {
1472 if (DebugKind > CodeGenOptions::LimitedDebugInfo ||
1473 !CGM.getLangOpts().CPlusPlus)
1474 completeRequiredType(RD);
1475}
1476
1477void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
David Blaikie0856f662014-03-04 22:01:08 +00001478 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1479 return;
1480
David Blaikie6943dea2013-08-20 01:28:15 +00001481 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1482 if (CXXDecl->isDynamicClass())
1483 return;
1484
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001485 if (DebugTypeExtRefs && RD->isFromASTFile())
1486 return;
1487
David Blaikieb2e86eb2013-08-15 20:49:17 +00001488 QualType Ty = CGM.getContext().getRecordType(RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001489 llvm::DIType *T = getTypeOrNull(Ty);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001490 if (T && T->isForwardDecl())
David Blaikie6943dea2013-08-20 01:28:15 +00001491 completeClassData(RD);
1492}
1493
1494void CGDebugInfo::completeClassData(const RecordDecl *RD) {
1495 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001496 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001497 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001498 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001499 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001500 if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001501 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001502 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001503 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001504 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001505}
1506
David Blaikie0e716b42014-03-03 23:48:23 +00001507static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1508 CXXRecordDecl::method_iterator End) {
1509 for (; I != End; ++I)
1510 if (FunctionDecl *Tmpl = I->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001511 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
1512 !I->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001513 return true;
1514 return false;
1515}
1516
1517static bool shouldOmitDefinition(CodeGenOptions::DebugInfoKind DebugKind,
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001518 bool DebugTypeExtRefs,
David Blaikie0e716b42014-03-03 23:48:23 +00001519 const RecordDecl *RD,
1520 const LangOptions &LangOpts) {
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001521 // Does the type exist in an imported clang module?
Adrian Prantl5d66c6b2015-09-11 18:54:28 +00001522 if (DebugTypeExtRefs && RD->isFromASTFile() && RD->getDefinition())
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001523 return true;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001524
David Blaikie0e716b42014-03-03 23:48:23 +00001525 if (DebugKind > CodeGenOptions::LimitedDebugInfo)
1526 return false;
1527
1528 if (!LangOpts.CPlusPlus)
1529 return false;
1530
1531 if (!RD->isCompleteDefinitionRequired())
1532 return true;
1533
1534 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1535
1536 if (!CXXDecl)
1537 return false;
1538
1539 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass())
1540 return true;
1541
1542 TemplateSpecializationKind Spec = TSK_Undeclared;
1543 if (const ClassTemplateSpecializationDecl *SD =
1544 dyn_cast<ClassTemplateSpecializationDecl>(RD))
1545 Spec = SD->getSpecializationKind();
1546
1547 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1548 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1549 CXXDecl->method_end()))
1550 return true;
1551
1552 return false;
1553}
1554
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001555llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001556 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001557 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001558 if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD,
1559 CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001560 if (!T)
Adrian Prantl6ec370a2015-09-10 18:39:45 +00001561 T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001562 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001563 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001564
David Blaikieb2e86eb2013-08-15 20:49:17 +00001565 return CreateTypeDefinition(Ty);
1566}
1567
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001568llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00001569 RecordDecl *RD = Ty->getDecl();
1570
Guy Benyei11169dd2012-12-18 14:30:41 +00001571 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001572 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001573
1574 // Records and classes and unions can all be recursive. To handle them, we
1575 // first generate a debug descriptor for the struct as a forward declaration.
1576 // Then (if it is a definition) we go through and get debug info for all of
1577 // its members. Finally, we create a descriptor for the complete type (which
1578 // may refer to the forward decl if the struct is recursive) and replace all
1579 // uses of the forward declaration with the final definition.
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00001580 llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001581
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001582 const RecordDecl *D = RD->getDefinition();
1583 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001584 return FwdDecl;
1585
David Blaikieadfbf992013-08-18 16:55:33 +00001586 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1587 CollectContainingType(CXXDecl, FwdDecl);
1588
Guy Benyei11169dd2012-12-18 14:30:41 +00001589 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001590 LexicalBlockStack.emplace_back(&*FwdDecl);
1591 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001592
Guy Benyei11169dd2012-12-18 14:30:41 +00001593 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001594 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001595 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001596
1597 // Note: The split of CXXDecl information here is intentional, the
1598 // gdb tests will depend on a certain ordering at printout. The debug
1599 // information offsets are still correct if we merge them all together
1600 // though.
1601 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1602 if (CXXDecl) {
1603 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1604 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1605 }
1606
Eric Christopher91a31902013-01-16 01:22:32 +00001607 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001608 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001609 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001610 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001611
1612 LexicalBlockStack.pop_back();
1613 RegionMap.erase(Ty->getDecl());
1614
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001615 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001616 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001617
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001618 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001619 FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001620 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001621
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001622 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001623 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001624}
1625
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001626llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1627 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001628 // Ignore protocols.
1629 return getOrCreateType(Ty->getBaseType(), Unit);
1630}
1631
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001632/// \return true if Getter has the default name for the property PD.
1633static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1634 const ObjCMethodDecl *Getter) {
1635 assert(PD);
1636 if (!Getter)
1637 return true;
1638
1639 assert(Getter->getDeclName().isObjCZeroArgSelector());
1640 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001641 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001642}
1643
1644/// \return true if Setter has the default name for the property PD.
1645static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1646 const ObjCMethodDecl *Setter) {
1647 assert(PD);
1648 if (!Setter)
1649 return true;
1650
1651 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001652 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001653 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001654}
1655
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001656llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1657 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001658 ObjCInterfaceDecl *ID = Ty->getDecl();
1659 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001660 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001661
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001662 // Return a forward declaration if this type was imported from a clang module.
1663 if (DebugTypeExtRefs && ID->isFromASTFile() && ID->getDefinition())
1664 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
1665 ID->getName(),
1666 getDeclContextDescriptor(ID), Unit, 0);
1667
Guy Benyei11169dd2012-12-18 14:30:41 +00001668 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001669 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001670 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001671 auto RuntimeLang =
1672 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00001673
1674 // If this is just a forward declaration return a special forward-declaration
1675 // debug type since we won't be able to lay out the entire type.
1676 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001677 if (!Def || !Def->getImplementation()) {
Adrian Prantl42ce2d32015-10-01 16:57:02 +00001678 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001679 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
Adrian Prantl42ce2d32015-10-01 16:57:02 +00001680 llvm::dwarf::DW_TAG_structure_type, ID->getName(), Mod ? Mod : TheCU,
1681 DefUnit, Line, RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001682 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001683 return FwdDecl;
1684 }
1685
David Blaikieef8a9512014-05-05 23:23:53 +00001686 return CreateTypeDefinition(Ty, Unit);
1687}
1688
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001689llvm::DIModule *
Adrian Prantl66689202015-09-18 23:01:45 +00001690CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod,
1691 bool CreateSkeletonCU) {
Adrian Prantleb66a262015-09-24 16:10:04 +00001692 // Use the Module pointer as the key into the cache. This is a
1693 // nullptr if the "Module" is a PCH, which is safe because we don't
1694 // support chained PCH debug info, so there can only be a single PCH.
1695 const Module *M = Mod.getModuleOrNull();
Adrian Prantl9e8ea352015-09-29 20:44:46 +00001696 auto ModRef = ModuleCache.find(M);
1697 if (ModRef != ModuleCache.end())
1698 return cast<llvm::DIModule>(ModRef->second);
Adrian Prantl2388ead2015-06-30 18:01:05 +00001699
1700 // Macro definitions that were defined with "-D" on the command line.
1701 SmallString<128> ConfigMacros;
1702 {
1703 llvm::raw_svector_ostream OS(ConfigMacros);
1704 const auto &PPOpts = CGM.getPreprocessorOpts();
1705 unsigned I = 0;
1706 // Translate the macro definitions back into a commmand line.
1707 for (auto &M : PPOpts.Macros) {
1708 if (++I > 1)
1709 OS << " ";
1710 const std::string &Macro = M.first;
1711 bool Undef = M.second;
1712 OS << "\"-" << (Undef ? 'U' : 'D');
1713 for (char c : Macro)
1714 switch (c) {
1715 case '\\' : OS << "\\\\"; break;
1716 case '"' : OS << "\\\""; break;
1717 default: OS << c;
1718 }
1719 OS << '\"';
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001720 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001721 }
Adrian Prantl66689202015-09-18 23:01:45 +00001722
Adrian Prantl835e6632015-09-24 16:10:10 +00001723 bool IsRootModule = M ? !M->Parent : true;
1724 if (CreateSkeletonCU && IsRootModule) {
Adrian Prantl66689202015-09-18 23:01:45 +00001725 llvm::DIBuilder DIB(CGM.getModule());
Adrian Prantl835e6632015-09-24 16:10:10 +00001726 DIB.createCompileUnit(TheCU->getSourceLanguage(), Mod.getModuleName(),
Adrian Prantl6083b082015-09-24 16:10:00 +00001727 Mod.getPath(), TheCU->getProducer(), true,
1728 StringRef(), 0, Mod.getASTFile(),
1729 llvm::DIBuilder::FullDebug, Mod.getSignature());
Adrian Prantl66689202015-09-18 23:01:45 +00001730 DIB.finalize();
Adrian Prantl2f957ac2015-09-19 00:59:22 +00001731 }
Adrian Prantl835e6632015-09-24 16:10:10 +00001732 llvm::DIModule *Parent =
1733 IsRootModule ? nullptr
1734 : getOrCreateModuleRef(
1735 ExternalASTSource::ASTSourceDescriptor(*M->Parent),
1736 CreateSkeletonCU);
Adrian Prantleb66a262015-09-24 16:10:04 +00001737 llvm::DIModule *DIMod =
Adrian Prantl835e6632015-09-24 16:10:10 +00001738 DBuilder.createModule(Parent, Mod.getModuleName(), ConfigMacros,
1739 Mod.getPath(), CGM.getHeaderSearchOpts().Sysroot);
Adrian Prantl9e8ea352015-09-29 20:44:46 +00001740 ModuleCache[M].reset(DIMod);
Adrian Prantleb66a262015-09-24 16:10:04 +00001741 return DIMod;
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001742}
1743
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001744llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
1745 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00001746 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001747 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00001748 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001749 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001750
1751 // Bit size, align and offset of the type.
1752 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1753 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1754
1755 unsigned Flags = 0;
1756 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001757 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00001758
Adrian Prantlfd696112015-10-01 00:48:51 +00001759 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001760 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Adrian Prantlfd696112015-10-01 00:48:51 +00001761 Mod ? Mod : Unit, ID->getName(), DefUnit, Line, Size, Align, Flags,
1762 nullptr, llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001763
David Blaikieef8a9512014-05-05 23:23:53 +00001764 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001765 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001766
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001767 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001768 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001769 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001770
1771 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001772 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001773
1774 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1775 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001776 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001777 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001778 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001779 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001780
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001781 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001782 EltTys.push_back(InhTag);
1783 }
1784
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001785 // Create entries for all of the properties.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001786 for (const auto *PD : ID->properties()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001787 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001788 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001789 unsigned PLine = getLineNumber(Loc);
1790 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1791 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001792 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
1793 PD->getName(), PUnit, PLine,
1794 hasDefaultGetterName(PD, Getter) ? ""
1795 : getSelectorName(PD->getGetterName()),
1796 hasDefaultSetterName(PD, Setter) ? ""
1797 : getSelectorName(PD->getSetterName()),
1798 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001799 EltTys.push_back(PropertyNode);
1800 }
1801
1802 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1803 unsigned FieldNo = 0;
1804 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1805 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001806 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001807 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001808 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001809
Guy Benyei11169dd2012-12-18 14:30:41 +00001810 StringRef FieldName = Field->getName();
1811
1812 // Ignore unnamed fields.
1813 if (FieldName.empty())
1814 continue;
1815
1816 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001817 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001818 unsigned FieldLine = getLineNumber(Field->getLocation());
1819 QualType FType = Field->getType();
1820 uint64_t FieldSize = 0;
1821 unsigned FieldAlign = 0;
1822
1823 if (!FType->isIncompleteArrayType()) {
1824
1825 // Bit size, align and offset of the type.
1826 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001827 ? Field->getBitWidthValue(CGM.getContext())
1828 : CGM.getContext().getTypeSize(FType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001829 FieldAlign = CGM.getContext().getTypeAlign(FType);
1830 }
1831
1832 uint64_t FieldOffset;
1833 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1834 // We don't know the runtime offset of an ivar if we're using the
1835 // non-fragile ABI. For bitfields, use the bit offset into the first
1836 // byte of storage of the bitfield. For other fields, use zero.
1837 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001838 FieldOffset =
1839 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00001840 FieldOffset %= CGM.getContext().getCharWidth();
1841 } else {
1842 FieldOffset = 0;
1843 }
1844 } else {
1845 FieldOffset = RL.getFieldOffset(FieldNo);
1846 }
1847
1848 unsigned Flags = 0;
1849 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001850 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00001851 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001852 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001853 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001854 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00001855
Craig Topper8a13c412014-05-21 05:09:00 +00001856 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001857 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001858 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00001859 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001860 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00001861 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001862 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00001863 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001864 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1865 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001866 PropertyNode = DBuilder.createObjCProperty(
1867 PD->getName(), PUnit, PLine,
1868 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
1869 PD->getGetterName()),
1870 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
1871 PD->getSetterName()),
1872 PD->getPropertyAttributes(),
1873 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001874 }
1875 }
1876 }
Eric Christophere7b87e52014-10-26 23:40:33 +00001877 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
1878 FieldSize, FieldAlign, FieldOffset, Flags,
1879 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00001880 EltTys.push_back(FieldTy);
1881 }
1882
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001883 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001884 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00001885
Guy Benyei11169dd2012-12-18 14:30:41 +00001886 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001887 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001888}
1889
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001890llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
1891 llvm::DIFile *Unit) {
1892 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001893 int64_t Count = Ty->getNumElements();
1894 if (Count == 0)
1895 // If number of elements are not known then this is an unbounded array.
1896 // Use Count == -1 to express such arrays.
1897 Count = -1;
1898
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001899 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001900 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00001901
1902 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1903 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1904
1905 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
1906}
1907
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001908llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001909 uint64_t Size;
1910 uint64_t Align;
1911
1912 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
1913 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
1914 Size = 0;
1915 Align =
Eric Christophere7b87e52014-10-26 23:40:33 +00001916 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Guy Benyei11169dd2012-12-18 14:30:41 +00001917 } else if (Ty->isIncompleteArrayType()) {
1918 Size = 0;
1919 if (Ty->getElementType()->isIncompleteType())
1920 Align = 0;
1921 else
1922 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikief03b2e82013-05-09 20:48:12 +00001923 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001924 Size = 0;
1925 Align = 0;
1926 } else {
1927 // Size and align of the whole array, not the element type.
1928 Size = CGM.getContext().getTypeSize(Ty);
1929 Align = CGM.getContext().getTypeAlign(Ty);
1930 }
1931
1932 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1933 // interior arrays, do we care? Why aren't nested arrays represented the
1934 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001935 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001936 QualType EltTy(Ty, 0);
1937 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1938 // If the number of elements is known, then count is that number. Otherwise,
1939 // it's -1. This allows us to represent a subrange with an array of 0
1940 // elements, like this:
1941 //
1942 // struct foo {
1943 // int x[0];
1944 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00001945 int64_t Count = -1; // Count == -1 is an unbounded array.
Guy Benyei11169dd2012-12-18 14:30:41 +00001946 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1947 Count = CAT->getSize().getZExtValue();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001948
Guy Benyei11169dd2012-12-18 14:30:41 +00001949 // FIXME: Verify this is right for VLAs.
1950 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
1951 EltTy = Ty->getElementType();
1952 }
1953
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001954 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001955
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001956 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
1957 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00001958}
1959
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001960llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
1961 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001962 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
1963 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001964}
1965
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001966llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
1967 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001968 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
1969 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001970}
1971
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001972llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
1973 llvm::DIFile *U) {
David Majnemer9df56372015-09-10 21:52:00 +00001974 uint64_t Size =
1975 !Ty->isIncompleteType() ? CGM.getContext().getTypeSize(Ty) : 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001976 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00001977 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00001978 return DBuilder.createMemberPointerType(
David Majnemer34568bc2015-05-26 21:54:24 +00001979 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size);
Adrian Prantl0866acd2013-12-19 01:38:47 +00001980
1981 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00001982 Ty->getPointeeType()->getAs<FunctionProtoType>();
1983 return DBuilder.createMemberPointerType(
1984 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
1985 Ty->getClass(), FPT->getTypeQuals())),
1986 FPT, U),
David Majnemer34568bc2015-05-26 21:54:24 +00001987 ClassType, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +00001988}
1989
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001990llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001991 // Ignore the atomic wrapping
1992 // FIXME: What is the correct representation?
1993 return getOrCreateType(Ty->getValueType(), U);
1994}
1995
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001996llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00001997 const EnumDecl *ED = Ty->getDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001998
Guy Benyei11169dd2012-12-18 14:30:41 +00001999 uint64_t Size = 0;
2000 uint64_t Align = 0;
2001 if (!ED->getTypeForDecl()->isIncompleteType()) {
2002 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
2003 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
2004 }
2005
Manman Rene0064d82013-08-29 23:19:58 +00002006 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2007
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002008 bool isImportedFromModule =
2009 DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition();
2010
Guy Benyei11169dd2012-12-18 14:30:41 +00002011 // If this is just a forward declaration, construct an appropriately
2012 // marked node and just return it.
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002013 if (isImportedFromModule || !ED->getDefinition()) {
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002014 llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002015 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002016 unsigned Line = getLineNumber(ED->getLocation());
2017 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002018 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
David Blaikief427b002014-05-06 03:42:01 +00002019 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002020 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002021 ReplaceMap.emplace_back(
2022 std::piecewise_construct, std::make_tuple(Ty),
2023 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00002024 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00002025 }
2026
David Blaikie483a9da2014-05-06 18:35:21 +00002027 return CreateTypeDefinition(Ty);
2028}
2029
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002030llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00002031 const EnumDecl *ED = Ty->getDecl();
2032 uint64_t Size = 0;
2033 uint64_t Align = 0;
2034 if (!ED->getTypeForDecl()->isIncompleteType()) {
2035 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
2036 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
2037 }
2038
2039 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2040
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002041 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002042 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00002043 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00002044 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002045 Enumerators.push_back(DBuilder.createEnumerator(
2046 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002047 }
2048
2049 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002050 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00002051
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002052 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002053 unsigned Line = getLineNumber(ED->getLocation());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002054 llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002055 llvm::DIType *ClassTy =
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002056 ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
2057 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
2058 Line, Size, Align, EltArray, ClassTy,
2059 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002060}
2061
David Blaikie05491062013-01-21 04:37:12 +00002062static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
2063 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002064 do {
Adrian Prantl179af902013-09-26 21:35:50 +00002065 Qualifiers InnerQuals = T.getLocalQualifiers();
2066 // Qualifiers::operator+() doesn't like it if you add a Qualifier
2067 // that is already there.
2068 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2069 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002070 QualType LastT = T;
2071 switch (T->getTypeClass()) {
2072 default:
David Blaikie05491062013-01-21 04:37:12 +00002073 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002074 case Type::TemplateSpecialization: {
2075 const auto *Spec = cast<TemplateSpecializationType>(T);
2076 if (Spec->isTypeAlias())
2077 return C.getQualifiedType(T.getTypePtr(), Quals);
2078 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002079 break;
2080 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002081 case Type::TypeOfExpr:
2082 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2083 break;
2084 case Type::TypeOf:
2085 T = cast<TypeOfType>(T)->getUnderlyingType();
2086 break;
2087 case Type::Decltype:
2088 T = cast<DecltypeType>(T)->getUnderlyingType();
2089 break;
2090 case Type::UnaryTransform:
2091 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2092 break;
2093 case Type::Attributed:
2094 T = cast<AttributedType>(T)->getEquivalentType();
2095 break;
2096 case Type::Elaborated:
2097 T = cast<ElaboratedType>(T)->getNamedType();
2098 break;
2099 case Type::Paren:
2100 T = cast<ParenType>(T)->getInnerType();
2101 break;
David Blaikie05491062013-01-21 04:37:12 +00002102 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002103 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002104 break;
2105 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00002106 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002107 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002108 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002109 break;
2110 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002111
Guy Benyei11169dd2012-12-18 14:30:41 +00002112 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002113 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002114 } while (true);
2115}
2116
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002117llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002118
2119 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002120 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002121
David Blaikief427b002014-05-06 03:42:01 +00002122 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002123 if (it != TypeCache.end()) {
2124 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002125 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002126 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002127 }
2128
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002129 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002130}
2131
David Blaikie0e716b42014-03-03 23:48:23 +00002132void CGDebugInfo::completeTemplateDefinition(
2133 const ClassTemplateSpecializationDecl &SD) {
David Blaikie0856f662014-03-04 22:01:08 +00002134 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2135 return;
2136
David Blaikie0e716b42014-03-03 23:48:23 +00002137 completeClassData(&SD);
2138 // In case this type has no member function definitions being emitted, ensure
2139 // it is retained
2140 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2141}
2142
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002143llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002144 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002145 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002146
2147 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002148 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002149
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002150 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002151 return T;
2152
Adrian Prantlca844182015-09-11 17:23:03 +00002153 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002154 void* TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002155
2156 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002157 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002158
Guy Benyei11169dd2012-12-18 14:30:41 +00002159 return Res;
2160}
2161
Eric Christopher1ecc5632013-06-07 22:54:39 +00002162unsigned CGDebugInfo::Checksum(const ObjCInterfaceDecl *ID) {
Adrian Prantl817bbb32013-06-07 01:10:48 +00002163 // The assumption is that the number of ivars can only increase
2164 // monotonically, so it is safe to just use their current number as
2165 // a checksum.
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002166 unsigned Sum = 0;
2167 for (const ObjCIvarDecl *Ivar = ID->all_declared_ivar_begin();
Craig Topper8a13c412014-05-21 05:09:00 +00002168 Ivar != nullptr; Ivar = Ivar->getNextIvar())
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002169 ++Sum;
2170
2171 return Sum;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002172}
2173
2174ObjCInterfaceDecl *CGDebugInfo::getObjCInterfaceDecl(QualType Ty) {
2175 switch (Ty->getTypeClass()) {
2176 case Type::ObjCObjectPointer:
Eric Christophere7b87e52014-10-26 23:40:33 +00002177 return getObjCInterfaceDecl(
2178 cast<ObjCObjectPointerType>(Ty)->getPointeeType());
Adrian Prantla03a85a2013-03-06 22:03:30 +00002179 case Type::ObjCInterface:
2180 return cast<ObjCInterfaceType>(Ty)->getDecl();
2181 default:
Craig Topper8a13c412014-05-21 05:09:00 +00002182 return nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002183 }
2184}
2185
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002186llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
Adrian Prantl335f5c72015-10-02 17:36:14 +00002187 // A forward declaration inside a module header does not belong to the module.
2188 if (isa<RecordDecl>(D) && !cast<RecordDecl>(D)->getDefinition())
2189 return nullptr;
Adrian Prantl85d938a2015-09-21 17:48:37 +00002190 if (DebugTypeExtRefs && D->isFromASTFile()) {
2191 // Record a reference to an imported clang module or precompiled header.
2192 auto *Reader = CGM.getContext().getExternalSource();
2193 auto Idx = D->getOwningModuleID();
2194 auto Info = Reader->getSourceDescriptor(Idx);
2195 if (Info)
2196 return getOrCreateModuleRef(*Info, /*SkeletonCU=*/true);
2197 } else if (ClangModuleMap) {
Adrian Prantl9402cef2015-09-20 16:51:35 +00002198 // We are building a clang module or a precompiled header.
2199 //
2200 // TODO: When D is a CXXRecordDecl or a C++ Enum, the ODR applies
2201 // and it wouldn't be necessary to specify the parent scope
2202 // because the type is already unique by definition (it would look
2203 // like the output of -fno-standalone-debug). On the other hand,
2204 // the parent scope helps a consumer to quickly locate the object
2205 // file where the type's definition is located, so it might be
2206 // best to make this behavior a command line or debugger tuning
2207 // option.
2208 FullSourceLoc Loc(D->getLocation(), CGM.getContext().getSourceManager());
2209 if (Module *M = ClangModuleMap->inferModuleFromLocation(Loc)) {
2210 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
2211 return getOrCreateModuleRef(Info, /*SkeletonCU=*/false);
2212 }
2213 }
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002214
Adrian Prantl9402cef2015-09-20 16:51:35 +00002215 return nullptr;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002216}
2217
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002218llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002219 // Handle qualifiers, which recursively handles what they refer to.
2220 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002221 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002222
Guy Benyei11169dd2012-12-18 14:30:41 +00002223 // Work out details of type.
2224 switch (Ty->getTypeClass()) {
2225#define TYPE(Class, Base)
2226#define ABSTRACT_TYPE(Class, Base)
2227#define NON_CANONICAL_TYPE(Class, Base)
2228#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2229#include "clang/AST/TypeNodes.def"
2230 llvm_unreachable("Dependent types cannot show up in debug information");
2231
2232 case Type::ExtVector:
2233 case Type::Vector:
2234 return CreateType(cast<VectorType>(Ty), Unit);
2235 case Type::ObjCObjectPointer:
2236 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2237 case Type::ObjCObject:
2238 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2239 case Type::ObjCInterface:
2240 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2241 case Type::Builtin:
2242 return CreateType(cast<BuiltinType>(Ty));
2243 case Type::Complex:
2244 return CreateType(cast<ComplexType>(Ty));
2245 case Type::Pointer:
2246 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002247 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002248 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002249 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002250 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002251 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002252 case Type::BlockPointer:
2253 return CreateType(cast<BlockPointerType>(Ty), Unit);
2254 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002255 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002256 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002257 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002258 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002259 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002260 case Type::FunctionProto:
2261 case Type::FunctionNoProto:
2262 return CreateType(cast<FunctionType>(Ty), Unit);
2263 case Type::ConstantArray:
2264 case Type::VariableArray:
2265 case Type::IncompleteArray:
2266 return CreateType(cast<ArrayType>(Ty), Unit);
2267
2268 case Type::LValueReference:
2269 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2270 case Type::RValueReference:
2271 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2272
2273 case Type::MemberPointer:
2274 return CreateType(cast<MemberPointerType>(Ty), Unit);
2275
2276 case Type::Atomic:
2277 return CreateType(cast<AtomicType>(Ty), Unit);
2278
Guy Benyei11169dd2012-12-18 14:30:41 +00002279 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002280 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2281
David Blaikie42edade2014-11-11 20:44:45 +00002282 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002283 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002284 case Type::Elaborated:
2285 case Type::Paren:
2286 case Type::SubstTemplateTypeParm:
2287 case Type::TypeOfExpr:
2288 case Type::TypeOf:
2289 case Type::Decltype:
2290 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002291 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002292 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002293 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002294
David Blaikie42edade2014-11-11 20:44:45 +00002295 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002296}
2297
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002298llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2299 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002300 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002301
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002302 auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002303
2304 // We may have cached a forward decl when we could have created
2305 // a non-forward decl. Go ahead and create a non-forward decl
2306 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002307 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002308 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002309
2310 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002311 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002312
2313 // Propagate members from the declaration to the definition
2314 // CreateType(const RecordType*) will overwrite this with the members in the
2315 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002316 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002317
Guy Benyei11169dd2012-12-18 14:30:41 +00002318 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002319 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002320 return Res;
2321}
2322
2323// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002324llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002325 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002326
Guy Benyei11169dd2012-12-18 14:30:41 +00002327 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002328 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002329 unsigned Line = getLineNumber(RD->getLocation());
2330 StringRef RDName = getClassName(RD);
2331
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002332 llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002333
David Blaikied2785892013-08-18 17:36:19 +00002334 // If we ended up creating the type during the context chain construction,
2335 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002336 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002337 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002338 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002339 return T;
David Blaikied2785892013-08-18 17:36:19 +00002340
Adrian Prantl381e7552014-02-04 21:29:50 +00002341 // If this is just a forward or incomplete declaration, construct an
2342 // appropriately marked node and just return it.
2343 const RecordDecl *D = RD->getDefinition();
2344 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002345 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002346
2347 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2348 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002349
Manman Rene0064d82013-08-29 23:19:58 +00002350 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2351
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002352 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002353 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align, 0,
2354 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002355
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002356 RegionMap[Ty->getDecl()].reset(RealDecl);
2357 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002358
David Blaikieadfbf992013-08-18 16:55:33 +00002359 if (const ClassTemplateSpecializationDecl *TSpecial =
2360 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002361 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002362 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002363 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002364}
2365
David Blaikieadfbf992013-08-18 16:55:33 +00002366void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002367 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002368 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002369 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002370 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2371 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002372 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002373 while (1) {
2374 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2375 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2376 if (PBT && !BRL.isPrimaryBaseVirtual())
2377 PBase = PBT;
2378 else
2379 break;
2380 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002381 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002382 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2383 getOrCreateFile(RD->getLocation())));
2384 } else if (RD->isDynamicClass())
2385 ContainingType = RealDecl;
2386
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002387 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002388}
2389
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002390llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002391 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002392 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002393 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2394 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002395 llvm::DIType *Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002396 FieldAlign, *Offset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002397 *Offset += FieldSize;
2398 return Ty;
2399}
2400
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002401void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002402 StringRef &Name,
2403 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002404 llvm::DIScope *&FDContext,
2405 llvm::DINodeArray &TParamsArray,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002406 unsigned &Flags) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002407 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2408 Name = getFunctionName(FD);
2409 // Use mangled name as linkage name for C/C++ functions.
2410 if (FD->hasPrototype()) {
2411 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002412 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002413 }
2414 // No need to replicate the linkage name if it isn't different from the
2415 // subprogram name, no need to have it at all unless coverage is enabled or
2416 // debug is set to more than just line tables.
2417 if (LinkageName == Name ||
2418 (!CGM.getCodeGenOpts().EmitGcovArcs &&
2419 !CGM.getCodeGenOpts().EmitGcovNotes &&
2420 DebugKind <= CodeGenOptions::DebugLineTablesOnly))
2421 LinkageName = StringRef();
2422
2423 if (DebugKind >= CodeGenOptions::LimitedDebugInfo) {
2424 if (const NamespaceDecl *NSDecl =
2425 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2426 FDContext = getOrCreateNameSpace(NSDecl);
2427 else if (const RecordDecl *RDecl =
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002428 dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) {
2429 llvm::DIScope *Mod = getParentModuleOrNull(RDecl);
2430 FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU);
2431 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002432 // Collect template parameters.
2433 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2434 }
2435}
2436
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002437void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002438 unsigned &LineNo, QualType &T,
2439 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002440 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002441 Unit = getOrCreateFile(VD->getLocation());
2442 LineNo = getLineNumber(VD->getLocation());
2443
2444 setLocation(VD->getLocation());
2445
2446 T = VD->getType();
2447 if (T->isIncompleteArrayType()) {
2448 // CodeGen turns int[] into int[1] so we'll do the same here.
2449 llvm::APInt ConstVal(32, 1);
2450 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2451
2452 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2453 ArrayType::Normal, 0);
2454 }
2455
2456 Name = VD->getName();
2457 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2458 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2459 LinkageName = CGM.getMangledName(VD);
2460 if (LinkageName == Name)
2461 LinkageName = StringRef();
2462
2463 // Since we emit declarations (DW_AT_members) for static members, place the
2464 // definition of those static members in the namespace they were declared in
2465 // in the source code (the lexical decl context).
2466 // FIXME: Generalize this for even non-member global variables where the
2467 // declaration and definition may have different lexical decl contexts, once
2468 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002469 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2470 : VD->getDeclContext();
2471 // When a record type contains an in-line initialization of a static data
2472 // member, and the record type is marked as __declspec(dllexport), an implicit
2473 // definition of the member will be created in the record context. DWARF
2474 // doesn't seem to have a nice way to describe this in a form that consumers
2475 // are likely to understand, so fake the "normal" situation of a definition
2476 // outside the class by putting it in the global scope.
2477 if (DC->isRecord())
2478 DC = CGM.getContext().getTranslationUnitDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002479
2480 llvm::DIScope *Mod = getParentModuleOrNull(VD);
2481 VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
Frederic Riss9db79f12014-11-18 03:40:46 +00002482}
2483
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002484llvm::DISubprogram *
Frederic Rissd253ed62014-11-18 03:40:51 +00002485CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002486 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00002487 StringRef Name, LinkageName;
2488 unsigned Flags = 0;
2489 SourceLocation Loc = FD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002490 llvm::DIFile *Unit = getOrCreateFile(Loc);
2491 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002492 unsigned Line = getLineNumber(Loc);
2493
2494 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2495 TParamsArray, Flags);
2496 // Build function type.
2497 SmallVector<QualType, 16> ArgTypes;
2498 for (const ParmVarDecl *Parm: FD->parameters())
2499 ArgTypes.push_back(Parm->getType());
2500 QualType FnType =
2501 CGM.getContext().getFunctionType(FD->getReturnType(), ArgTypes,
2502 FunctionProtoType::ExtProtoInfo());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002503 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002504 DContext, Name, LinkageName, Unit, Line,
2505 getOrCreateFunctionType(FD, FnType, Unit), !FD->isExternallyVisible(),
Duncan P. N. Exon Smith31d38f72015-08-26 22:21:09 +00002506 /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize, nullptr,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002507 TParamsArray.get(), getFunctionDeclaration(FD));
Frederic Rissd253ed62014-11-18 03:40:51 +00002508 const FunctionDecl *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002509 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2510 std::make_tuple(CanonDecl),
2511 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00002512 return SP;
2513}
2514
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002515llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00002516CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2517 QualType T;
2518 StringRef Name, LinkageName;
2519 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002520 llvm::DIFile *Unit = getOrCreateFile(Loc);
2521 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002522 unsigned Line = getLineNumber(Loc);
2523
2524 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002525 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
2526 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
2527 !VD->isExternallyVisible(), nullptr, nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002528 FwdDeclReplaceMap.emplace_back(
2529 std::piecewise_construct,
2530 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2531 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002532 return GV;
2533}
2534
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002535llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002536 // We only need a declaration (not a definition) of the type - so use whatever
2537 // we would otherwise do to get a type for a pointee. (forward declarations in
2538 // limited debug info, full definitions (if the type definition is available)
2539 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002540 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2541 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002542 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002543 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002544
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002545 if (I != DeclCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002546 return dyn_cast_or_null<llvm::DINode>(I->second);
Frederic Rissd253ed62014-11-18 03:40:51 +00002547
2548 // No definition for now. Emit a forward definition that might be
2549 // merged with a potential upcoming definition.
2550 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
2551 return getFunctionForwardDeclaration(FD);
2552 else if (const auto *VD = dyn_cast<VarDecl>(D))
2553 return getGlobalVariableForwardDeclaration(VD);
2554
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002555 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00002556}
2557
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002558llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Diego Novillo913690c2014-06-24 17:02:17 +00002559 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002560 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00002561
Guy Benyei11169dd2012-12-18 14:30:41 +00002562 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002563 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002564 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002565
2566 // Setup context.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002567 auto *S = getDeclContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00002568
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002569 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002570 if (MI == SPCache.end()) {
Eric Christopherf86c4052013-08-28 23:12:10 +00002571 if (const CXXMethodDecl *MD =
2572 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00002573 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002574 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00002575 }
2576 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002577 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002578 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002579 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002580 return SP;
2581 }
2582
Aaron Ballman86c93902014-03-06 23:45:36 +00002583 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002584 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002585 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002586 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002587 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002588 return SP;
2589 }
2590 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002591 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002592}
2593
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002594// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00002595// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002596llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002597 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002598 llvm::DIFile *F) {
Diego Novillo913690c2014-06-24 17:02:17 +00002599 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002600 // Create fake but valid subroutine type. Otherwise -verify would fail, and
2601 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Manman Ren67f005e2014-07-28 22:24:34 +00002602 return DBuilder.createSubroutineType(F,
2603 DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002604
2605 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2606 return getOrCreateMethodType(Method, F);
2607 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2608 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002609 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002610
2611 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002612 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002613
2614 // Replace the instancetype keyword with the actual type.
2615 if (ResultTy == CGM.getContext().getObjCInstanceType())
2616 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002617 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002618
Adrian Prantl7bec9032013-05-10 21:08:31 +00002619 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002620 // "self" pointer is always first argument.
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002621 QualType SelfDeclTy;
2622 if (auto *SelfDecl = OMethod->getSelfDecl())
2623 SelfDeclTy = SelfDecl->getType();
2624 else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType))
2625 if (FPT->getNumParams() > 1)
2626 SelfDeclTy = FPT->getParamType(0);
2627 if (!SelfDeclTy.isNull())
2628 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002629 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002630 Elts.push_back(DBuilder.createArtificialType(
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002631 getOrCreateType(CGM.getContext().getObjCSelType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002632 // Get rest of the arguments.
Aaron Ballman43b68be2014-03-07 17:50:17 +00002633 for (const auto *PI : OMethod->params())
2634 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002635 // Variadic methods need a special marker at the end of the type list.
2636 if (OMethod->isVariadic())
2637 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002638
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002639 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002640 return DBuilder.createSubroutineType(F, EltTypeArray);
2641 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002642
Adrian Prantl800faef2014-02-25 23:42:18 +00002643 // Handle variadic function types; they need an additional
2644 // unspecified parameter.
Adrian Prantld45ba252014-02-25 19:38:11 +00002645 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2646 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002647 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00002648 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2649 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2650 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2651 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2652 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002653 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Adrian Prantld45ba252014-02-25 19:38:11 +00002654 return DBuilder.createSubroutineType(F, EltTypeArray);
2655 }
2656
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002657 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002658}
2659
Eric Christophere7b87e52014-10-26 23:40:33 +00002660void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2661 SourceLocation ScopeLoc, QualType FnType,
2662 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002663
2664 StringRef Name;
2665 StringRef LinkageName;
2666
2667 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2668
2669 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002670 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00002671
Guy Benyei11169dd2012-12-18 14:30:41 +00002672 unsigned Flags = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002673 llvm::DIFile *Unit = getOrCreateFile(Loc);
2674 llvm::DIScope *FDContext = Unit;
2675 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00002676 if (!HasDecl) {
2677 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00002678 LinkageName = Fn->getName();
Guy Benyei11169dd2012-12-18 14:30:41 +00002679 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002680 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002681 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002682 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002683 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002684 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002685 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002686 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002687 return;
2688 }
2689 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002690 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2691 TParamsArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002692 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2693 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002694 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002695 } else {
2696 // Use llvm function name.
2697 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002698 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002699 }
2700 if (!Name.empty() && Name[0] == '\01')
2701 Name = Name.substr(1);
2702
Adrian Prantl42d71b92014-04-10 23:21:53 +00002703 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002704 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl42d71b92014-04-10 23:21:53 +00002705 // Artificial functions without a location should not silently reuse CurLoc.
2706 if (Loc.isInvalid())
2707 CurLoc = SourceLocation();
2708 }
2709 unsigned LineNo = getLineNumber(Loc);
2710 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002711
Eric Christopher8018e412014-03-27 18:50:35 +00002712 // FIXME: The function declaration we're constructing here is mostly reusing
2713 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2714 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2715 // all subprograms instead of the actual context since subprogram definitions
2716 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002717 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00002718 FDContext, Name, LinkageName, Unit, LineNo,
2719 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
2720 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize, Fn,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002721 TParamsArray.get(), getFunctionDeclaration(D));
Frederic Rissb1ab28c2014-11-05 19:19:04 +00002722 // We might get here with a VarDecl in the case we're generating
2723 // code for the initialization of globals. Do not record these decls
2724 // as they will overwrite the actual VarDecl Decl in the cache.
2725 if (HasDecl && isa<FunctionDecl>(D))
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002726 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(SP));
Guy Benyei11169dd2012-12-18 14:30:41 +00002727
Adrian Prantlbebb8932014-03-21 21:01:58 +00002728 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002729 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00002730
Guy Benyei11169dd2012-12-18 14:30:41 +00002731 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002732 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002733}
2734
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002735void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
2736 QualType FnType) {
2737 StringRef Name;
2738 StringRef LinkageName;
2739
2740 const Decl *D = GD.getDecl();
2741 if (!D)
2742 return;
2743
2744 unsigned Flags = 0;
2745 llvm::DIFile *Unit = getOrCreateFile(Loc);
Adrian Prantlf0cd6772015-10-04 23:23:04 +00002746 llvm::DIScope *FDContext = getDeclContextDescriptor(D);
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002747 llvm::DINodeArray TParamsArray;
2748 if (isa<FunctionDecl>(D)) {
2749 // If there is a DISubprogram for this function available then use it.
2750 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2751 TParamsArray, Flags);
2752 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2753 Name = getObjCMethodName(OMD);
2754 Flags |= llvm::DINode::FlagPrototyped;
2755 } else {
2756 llvm_unreachable("not a function or ObjC method");
2757 }
2758 if (!Name.empty() && Name[0] == '\01')
2759 Name = Name.substr(1);
2760
2761 if (D->isImplicit()) {
2762 Flags |= llvm::DINode::FlagArtificial;
2763 // Artificial functions without a location should not silently reuse CurLoc.
2764 if (Loc.isInvalid())
2765 CurLoc = SourceLocation();
2766 }
2767 unsigned LineNo = getLineNumber(Loc);
2768 unsigned ScopeLine = 0;
2769
2770 DBuilder.createFunction(FDContext, Name, LinkageName, Unit, LineNo,
2771 getOrCreateFunctionType(D, FnType, Unit),
2772 false /*internalLinkage*/, true /*definition*/,
2773 ScopeLine, Flags, CGM.getLangOpts().Optimize, nullptr,
2774 TParamsArray.get(),
2775 getFunctionDeclaration(D));
2776}
2777
David Blaikie835afb22015-01-21 23:08:17 +00002778void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002779 // Update our current location
2780 setLocation(Loc);
2781
Eric Christophere7b87e52014-10-26 23:40:33 +00002782 if (CurLoc.isInvalid() || CurLoc.isMacroID())
2783 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002784
Adrian Prantle83b1302014-01-07 22:05:52 +00002785 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00002786 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
David Blaikie835afb22015-01-21 23:08:17 +00002787 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002788}
2789
Guy Benyei11169dd2012-12-18 14:30:41 +00002790void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002791 llvm::MDNode *Back = nullptr;
2792 if (!LexicalBlockStack.empty())
2793 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002794 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002795 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002796 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002797}
2798
Eric Christopher0fdcb312013-05-16 00:52:20 +00002799void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2800 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002801 // Set our current location.
2802 setLocation(Loc);
2803
Guy Benyei11169dd2012-12-18 14:30:41 +00002804 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00002805 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2806 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00002807
2808 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2809 return;
2810
2811 // Create a new lexical block and push it on the stack.
2812 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002813}
2814
Eric Christopher0fdcb312013-05-16 00:52:20 +00002815void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2816 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002817 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2818
2819 // Provide an entry in the line table for the end of the block.
2820 EmitLocation(Builder, Loc);
2821
David Blaikie60a877b2014-10-22 19:34:33 +00002822 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2823 return;
2824
Guy Benyei11169dd2012-12-18 14:30:41 +00002825 LexicalBlockStack.pop_back();
2826}
2827
Guy Benyei11169dd2012-12-18 14:30:41 +00002828void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2829 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2830 unsigned RCount = FnBeginRegionCount.back();
2831 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2832
2833 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00002834 while (LexicalBlockStack.size() != RCount) {
2835 // Provide an entry in the line table for the end of the block.
2836 EmitLocation(Builder, CurLoc);
2837 LexicalBlockStack.pop_back();
2838 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002839 FnBeginRegionCount.pop_back();
2840}
2841
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002842llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002843 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002844
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002845 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002846 QualType FType;
2847 uint64_t FieldSize, FieldOffset;
2848 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002849
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002850 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002851 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002852
2853 FieldOffset = 0;
2854 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2855 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2856 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2857 FType = CGM.getContext().IntTy;
2858 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2859 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2860
2861 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2862 if (HasCopyAndDispose) {
2863 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002864 EltTys.push_back(
2865 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
2866 EltTys.push_back(
2867 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00002868 }
2869 bool HasByrefExtendedLayout;
2870 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00002871 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
2872 HasByrefExtendedLayout) &&
2873 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00002874 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002875 EltTys.push_back(
2876 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00002877 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002878
Guy Benyei11169dd2012-12-18 14:30:41 +00002879 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2880 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002881 CGM.getTarget().getPointerAlign(0))) {
2882 CharUnits FieldOffsetInBytes =
2883 CGM.getContext().toCharUnitsFromBits(FieldOffset);
2884 CharUnits AlignedOffsetInBytes =
2885 FieldOffsetInBytes.RoundUpToAlignment(Align);
2886 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002887
Guy Benyei11169dd2012-12-18 14:30:41 +00002888 if (NumPaddingBytes.isPositive()) {
2889 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
2890 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2891 pad, ArrayType::Normal, 0);
2892 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
2893 }
2894 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002895
Guy Benyei11169dd2012-12-18 14:30:41 +00002896 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002897 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002898 FieldSize = CGM.getContext().getTypeSize(FType);
2899 FieldAlign = CGM.getContext().toBits(Align);
2900
Eric Christopherb2a008c2013-05-16 00:45:12 +00002901 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00002902 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
2903 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002904 EltTys.push_back(FieldTy);
2905 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002906
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002907 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002908
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002909 unsigned Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002910
Guy Benyei11169dd2012-12-18 14:30:41 +00002911 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002912 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002913}
2914
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002915void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::Value *Storage,
2916 llvm::Optional<unsigned> ArgNo,
Eric Christophere7b87e52014-10-26 23:40:33 +00002917 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002918 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002919 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2920
David Blaikie7fceebf2013-08-19 03:37:48 +00002921 bool Unwritten =
2922 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
2923 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002924 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00002925 if (!Unwritten)
2926 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002927 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00002928 uint64_t XOffset = 0;
2929 if (VD->hasAttr<BlocksAttr>())
2930 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002931 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002932 Ty = getOrCreateType(VD->getType(), Unit);
2933
2934 // If there is no debug info for this type then do not emit debug info
2935 // for this variable.
2936 if (!Ty)
2937 return;
2938
Guy Benyei11169dd2012-12-18 14:30:41 +00002939 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00002940 unsigned Line = 0;
2941 unsigned Column = 0;
2942 if (!Unwritten) {
2943 Line = getLineNumber(VD->getLocation());
2944 Column = getColumnNumber(VD->getLocation());
2945 }
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002946 SmallVector<int64_t, 9> Expr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002947 unsigned Flags = 0;
2948 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002949 Flags |= llvm::DINode::FlagArtificial;
Guy Benyei11169dd2012-12-18 14:30:41 +00002950 // If this is the first argument and it is implicit then
2951 // give it an object pointer flag.
2952 // FIXME: There has to be a better way to do this, but for static
2953 // functions there won't be an implicit param at arg1 and
2954 // otherwise it is 'self' or 'this'.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002955 if (isa<ImplicitParamDecl>(VD) && ArgNo && *ArgNo == 1)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002956 Flags |= llvm::DINode::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00002957 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00002958 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
2959 !VD->getType()->isPointerType())
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002960 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei11169dd2012-12-18 14:30:41 +00002961
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002962 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00002963
2964 StringRef Name = VD->getName();
2965 if (!Name.empty()) {
2966 if (VD->hasAttr<BlocksAttr>()) {
2967 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002968 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002969 // offset of __forwarding field
2970 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002971 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002972 Expr.push_back(offset.getQuantity());
2973 Expr.push_back(llvm::dwarf::DW_OP_deref);
2974 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002975 // offset of x field
2976 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002977 Expr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002978
2979 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002980 auto *D = ArgNo
2981 ? DBuilder.createParameterVariable(Scope, VD->getName(),
2982 *ArgNo, Unit, Line, Ty)
2983 : DBuilder.createAutoVariable(Scope, VD->getName(), Unit,
2984 Line, Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002985
Guy Benyei11169dd2012-12-18 14:30:41 +00002986 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002987 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2988 llvm::DebugLoc::get(Line, Column, Scope),
2989 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002990 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00002991 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002992 Expr.push_back(llvm::dwarf::DW_OP_deref);
Adrian Prantl0d820892015-04-29 15:05:50 +00002993 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2994 // If VD is an anonymous union then Storage represents value for
2995 // all union fields.
2996 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
2997 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00002998 // GDB has trouble finding local variables in anonymous unions, so we emit
2999 // artifical local variables for each of the members.
3000 //
3001 // FIXME: Remove this code as soon as GDB supports this.
3002 // The debug info verifier in LLVM operates based on the assumption that a
3003 // variable has the same size as its storage and we had to disable the check
3004 // for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00003005 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003006 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00003007 StringRef FieldName = Field->getName();
3008
3009 // Ignore unnamed fields. Do not ignore unnamed records.
3010 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
3011 continue;
3012
3013 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003014 auto *D = DBuilder.createAutoVariable(
3015 Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
3016 Flags | llvm::DINode::FlagArtificial);
Adrian Prantl0d820892015-04-29 15:05:50 +00003017
3018 // Insert an llvm.dbg.declare into the current block.
3019 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3020 llvm::DebugLoc::get(Line, Column, Scope),
3021 Builder.GetInsertBlock());
3022 }
Adrian Prantl0d820892015-04-29 15:05:50 +00003023 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003024 }
David Blaikiea76a7c92013-01-05 05:58:35 +00003025
3026 // Create the descriptor for the variable.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003027 auto *D =
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003028 ArgNo
3029 ? DBuilder.createParameterVariable(Scope, Name, *ArgNo, Unit, Line,
3030 Ty, CGM.getLangOpts().Optimize,
3031 Flags)
3032 : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
3033 CGM.getLangOpts().Optimize, Flags);
David Blaikiea76a7c92013-01-05 05:58:35 +00003034
3035 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003036 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3037 llvm::DebugLoc::get(Line, Column, Scope),
3038 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003039}
3040
3041void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
3042 llvm::Value *Storage,
3043 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003044 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003045 EmitDeclare(VD, Storage, llvm::None, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003046}
3047
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003048llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
3049 llvm::DIType *Ty) {
3050 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003051 if (CachedTy)
3052 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00003053 return DBuilder.createObjectPointerType(Ty);
3054}
3055
Eric Christophere7b87e52014-10-26 23:40:33 +00003056void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
3057 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00003058 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Eric Christopher75e17682013-05-16 00:45:23 +00003059 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003060 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00003061
Craig Topper8a13c412014-05-21 05:09:00 +00003062 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00003063 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003064
Guy Benyei11169dd2012-12-18 14:30:41 +00003065 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00003066
Guy Benyei11169dd2012-12-18 14:30:41 +00003067 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003068 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
3069 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003070 if (isByRef)
3071 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003072 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003073 Ty = getOrCreateType(VD->getType(), Unit);
3074
3075 // Self is passed along as an implicit non-arg variable in a
3076 // block. Mark it as the object pointer.
3077 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00003078 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00003079
3080 // Get location information.
3081 unsigned Line = getLineNumber(VD->getLocation());
3082 unsigned Column = getColumnNumber(VD->getLocation());
3083
3084 const llvm::DataLayout &target = CGM.getDataLayout();
3085
3086 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00003087 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00003088 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
3089
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003090 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00003091 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003092 addr.push_back(llvm::dwarf::DW_OP_deref);
3093 addr.push_back(llvm::dwarf::DW_OP_plus);
3094 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003095 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003096 addr.push_back(llvm::dwarf::DW_OP_deref);
3097 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003098 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00003099 offset =
3100 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003101 addr.push_back(offset.getQuantity());
3102 addr.push_back(llvm::dwarf::DW_OP_deref);
3103 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003104 // offset of x field
3105 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003106 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003107 }
3108
3109 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003110 auto *D = DBuilder.createAutoVariable(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003111 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003112 Line, Ty);
Adrian Prantl0f6df002013-03-29 19:20:35 +00003113
Guy Benyei11169dd2012-12-18 14:30:41 +00003114 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003115 auto DL = llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back());
3116 if (InsertPoint)
3117 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3118 InsertPoint);
3119 else
3120 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3121 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003122}
3123
Guy Benyei11169dd2012-12-18 14:30:41 +00003124void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3125 unsigned ArgNo,
3126 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003127 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003128 EmitDeclare(VD, AI, ArgNo, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003129}
3130
3131namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003132struct BlockLayoutChunk {
3133 uint64_t OffsetInBits;
3134 const BlockDecl::Capture *Capture;
3135};
3136bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3137 return l.OffsetInBits < r.OffsetInBits;
3138}
Guy Benyei11169dd2012-12-18 14:30:41 +00003139}
3140
3141void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003142 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003143 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003144 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003145 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003146 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003147 ASTContext &C = CGM.getContext();
3148 const BlockDecl *blockDecl = block.getBlockDecl();
3149
3150 // Collect some general information about the block's location.
3151 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003152 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003153 unsigned line = getLineNumber(loc);
3154 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003155
Guy Benyei11169dd2012-12-18 14:30:41 +00003156 // Build the debug-info type for the block literal.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003157 getDeclContextDescriptor(blockDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003158
3159 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003160 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003161
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003162 SmallVector<llvm::Metadata *, 16> fields;
Guy Benyei11169dd2012-12-18 14:30:41 +00003163 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
3164 blockLayout->getElementOffsetInBits(0),
3165 tunit, tunit));
3166 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
3167 blockLayout->getElementOffsetInBits(1),
3168 tunit, tunit));
3169 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
3170 blockLayout->getElementOffsetInBits(2),
3171 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003172 auto *FnTy = block.getBlockExpr()->getFunctionType();
3173 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
3174 fields.push_back(createFieldType("__FuncPtr", FnPtrType, 0, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003175 blockLayout->getElementOffsetInBits(3),
3176 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003177 fields.push_back(createFieldType(
3178 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3179 ? C.getBlockDescriptorExtendedType()
3180 : C.getBlockDescriptorType()),
3181 0, loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003182
3183 // We want to sort the captures by offset, not because DWARF
3184 // requires this, but because we're paranoid about debuggers.
3185 SmallVector<BlockLayoutChunk, 8> chunks;
3186
3187 // 'this' capture.
3188 if (blockDecl->capturesCXXThis()) {
3189 BlockLayoutChunk chunk;
3190 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003191 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003192 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003193 chunks.push_back(chunk);
3194 }
3195
3196 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003197 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003198 const VarDecl *variable = capture.getVariable();
3199 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3200
3201 // Ignore constant captures.
3202 if (captureInfo.isConstant())
3203 continue;
3204
3205 BlockLayoutChunk chunk;
3206 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003207 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003208 chunk.Capture = &capture;
3209 chunks.push_back(chunk);
3210 }
3211
3212 // Sort by offset.
3213 llvm::array_pod_sort(chunks.begin(), chunks.end());
3214
Eric Christophere7b87e52014-10-26 23:40:33 +00003215 for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
3216 e = chunks.end();
3217 i != e; ++i) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003218 uint64_t offsetInBits = i->OffsetInBits;
3219 const BlockDecl::Capture *capture = i->Capture;
3220
3221 // If we have a null capture, this must be the C++ 'this' capture.
3222 if (!capture) {
3223 const CXXMethodDecl *method =
Eric Christophere7b87e52014-10-26 23:40:33 +00003224 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00003225 QualType type = method->getThisType(C);
3226
3227 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
3228 offsetInBits, tunit, tunit));
3229 continue;
3230 }
3231
3232 const VarDecl *variable = capture->getVariable();
3233 StringRef name = variable->getName();
3234
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003235 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003236 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003237 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003238
3239 // FIXME: this creates a second copy of this type!
3240 uint64_t xoffset;
3241 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003242 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3243 fieldType =
3244 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3245 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003246 } else {
Eric Christophere7b87e52014-10-26 23:40:33 +00003247 fieldType = createFieldType(name, variable->getType(), 0, loc, AS_public,
3248 offsetInBits, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003249 }
3250 fields.push_back(fieldType);
3251 }
3252
3253 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003254 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3255 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003256
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003257 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003258
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003259 llvm::DIType *type = DBuilder.createStructType(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003260 tunit, typeName.str(), tunit, line,
3261 CGM.getContext().toBits(block.BlockSize),
3262 CGM.getContext().toBits(block.BlockAlign), 0, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003263 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3264
3265 // Get overall information about the block.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003266 unsigned flags = llvm::DINode::FlagArtificial;
3267 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003268
3269 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003270 auto *debugVar = DBuilder.createParameterVariable(
3271 scope, Arg->getName(), ArgNo, tunit, line, type,
3272 CGM.getLangOpts().Optimize, flags);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003273
Adrian Prantl616bef42013-03-14 21:52:59 +00003274 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003275 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003276 DBuilder.insertDbgValueIntrinsic(
Eric Christophere7b87e52014-10-26 23:40:33 +00003277 LocalAddr, 0, debugVar, DBuilder.createExpression(),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003278 llvm::DebugLoc::get(line, column, scope), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003279 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003280
Adrian Prantl616bef42013-03-14 21:52:59 +00003281 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003282 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
3283 llvm::DebugLoc::get(line, column, scope),
3284 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003285}
3286
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003287llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003288CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3289 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003290 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003291
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003292 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003293 if (MI != StaticDataMemberCache.end()) {
3294 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00003295 return MI->second;
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003296 }
David Blaikiece763042013-08-20 21:49:21 +00003297
3298 // If the member wasn't found in the cache, lazily construct and add it to the
3299 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003300 auto DC = D->getDeclContext();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003301 auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003302 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003303}
3304
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003305llvm::DIGlobalVariable *CGDebugInfo::CollectAnonRecordDecls(
3306 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3307 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
3308 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003309
3310 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003311 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003312 StringRef FieldName = Field->getName();
3313
3314 // Ignore unnamed fields, but recurse into anonymous records.
3315 if (FieldName.empty()) {
3316 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3317 if (RT)
3318 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3319 Var, DContext);
3320 continue;
3321 }
3322 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003323 GV = DBuilder.createGlobalVariable(DContext, FieldName, LinkageName, Unit,
3324 LineNo, FieldTy,
3325 Var->hasInternalLinkage(), Var, nullptr);
Eric Christophercab9fae2014-04-10 05:20:00 +00003326 }
3327 return GV;
3328}
3329
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003330void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003331 const VarDecl *D) {
Eric Christopher75e17682013-05-16 00:45:23 +00003332 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003333 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003334 llvm::DIFile *Unit = nullptr;
3335 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003336 unsigned LineNo;
3337 StringRef DeclName, LinkageName;
3338 QualType T;
3339 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003340
3341 // Attempt to store one global variable for the declaration - even if we
3342 // emit a lot of fields.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003343 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003344
3345 // If this is an anonymous union then we'll want to emit a global
3346 // variable for each member of the anonymous union so that it's possible
3347 // to find the name of any field in the union.
3348 if (T->isUnionType() && DeclName.empty()) {
3349 const RecordDecl *RD = cast<RecordType>(T)->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003350 assert(RD->isAnonymousStructOrUnion() &&
3351 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003352 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3353 } else {
David Blaikie7550b112014-10-20 17:42:23 +00003354 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003355 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3356 Var->hasInternalLinkage(), Var,
3357 getOrCreateStaticDataMemberDeclarationOrNull(D));
3358 }
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003359 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(GV));
Guy Benyei11169dd2012-12-18 14:30:41 +00003360}
3361
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003362void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3363 llvm::Constant *Init) {
Eric Christopher75e17682013-05-16 00:45:23 +00003364 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003365 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003366 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003367 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003368 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003369 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3370 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3371 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3372 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3373 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003374 // Do not use global variables for enums.
3375 //
3376 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003377 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003378 return;
David Blaikiea15565562014-04-04 20:56:17 +00003379 // Do not emit separate definitions for function local const/statics.
3380 if (isa<FunctionDecl>(VD->getDeclContext()))
3381 return;
David Blaikiebb113912014-04-05 07:23:17 +00003382 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003383 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003384 if (VarD->isStaticDataMember()) {
3385 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003386 getDeclContextDescriptor(VarD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003387 // Ensure that the type is retained even though it's otherwise unreferenced.
3388 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003389 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003390 return;
3391 }
3392
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003393 llvm::DIScope *DContext = getDeclContextDescriptor(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003394
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003395 auto &GV = DeclCache[VD];
3396 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003397 return;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003398 GV.reset(DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003399 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003400 true, Init, getOrCreateStaticDataMemberDeclarationOrNull(VarD)));
David Blaikiebd483762013-05-20 04:58:53 +00003401}
3402
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003403llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003404 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003405 return LexicalBlockStack.back();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00003406 llvm::DIScope *Mod = getParentModuleOrNull(D);
3407 return getContextDescriptor(D, Mod ? Mod : TheCU);
Guy Benyei11169dd2012-12-18 14:30:41 +00003408}
3409
David Blaikie9f88fe82013-04-22 06:13:21 +00003410void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
David Blaikiebd483762013-05-20 04:58:53 +00003411 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3412 return;
David Blaikie9f88fe82013-04-22 06:13:21 +00003413 DBuilder.createImportedModule(
David Blaikiebd483762013-05-20 04:58:53 +00003414 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3415 getOrCreateNameSpace(UD.getNominatedNamespace()),
David Blaikie9f88fe82013-04-22 06:13:21 +00003416 getLineNumber(UD.getLocation()));
3417}
3418
David Blaikiebd483762013-05-20 04:58:53 +00003419void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
3420 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3421 return;
3422 assert(UD.shadow_size() &&
3423 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3424 // Emitting one decl is sufficient - debuggers can detect that this is an
3425 // overloaded name & provide lookup for all the overloads.
3426 const UsingShadowDecl &USD = **UD.shadow_begin();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003427 if (llvm::DINode *Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003428 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003429 DBuilder.createImportedDeclaration(
3430 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3431 getLineNumber(USD.getLocation()));
3432}
3433
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003434void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
Adrian Prantlc6458d62015-09-19 00:10:32 +00003435 auto Info = ExternalASTSource::ASTSourceDescriptor(*ID.getImportedModule());
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003436 DBuilder.createImportedDeclaration(
Adrian Prantl66689202015-09-18 23:01:45 +00003437 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
3438 getOrCreateModuleRef(Info, DebugTypeExtRefs),
3439 getLineNumber(ID.getLocation()));
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003440}
3441
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003442llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00003443CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
3444 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003445 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003446 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003447 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003448 return cast<llvm::DIImportedEntity>(VH);
3449 llvm::DIImportedEntity *R;
David Blaikief121b932013-05-20 22:50:41 +00003450 if (const NamespaceAliasDecl *Underlying =
3451 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3452 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003453 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003454 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3455 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3456 NA.getName());
3457 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003458 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003459 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3460 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3461 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003462 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003463 return R;
3464}
3465
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003466llvm::DINamespace *
Guy Benyei11169dd2012-12-18 14:30:41 +00003467CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003468 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003469 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003470 if (I != NameSpaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003471 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003472
Guy Benyei11169dd2012-12-18 14:30:41 +00003473 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003474 llvm::DIFile *FileD = getOrCreateFile(NSDecl->getLocation());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003475 llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003476 llvm::DINamespace *NS =
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003477 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003478 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003479 return NS;
3480}
3481
Adrian Prantla5206ce2015-09-22 23:26:43 +00003482void CGDebugInfo::setDwoId(uint64_t Signature) {
3483 assert(TheCU && "no main compile unit");
3484 TheCU->setDWOId(Signature);
3485}
3486
3487
Guy Benyei11169dd2012-12-18 14:30:41 +00003488void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003489 // Creating types might create further types - invalidating the current
3490 // element and the size(), so don't cache/reference them.
3491 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3492 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003493 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003494 ? CreateTypeDefinition(E.Type, E.Unit)
3495 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003496 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00003497 }
3498
David Blaikief427b002014-05-06 03:42:01 +00003499 for (auto p : ReplaceMap) {
3500 assert(p.second);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003501 auto *Ty = cast<llvm::DIType>(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003502 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003503
David Blaikief427b002014-05-06 03:42:01 +00003504 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003505 assert(it != TypeCache.end());
3506 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003507
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003508 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
3509 cast<llvm::DIType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00003510 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003511
Frederic Rissd253ed62014-11-18 03:40:51 +00003512 for (const auto &p : FwdDeclReplaceMap) {
3513 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003514 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003515 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003516
3517 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003518 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003519 // with ourselves, that will destroy the temporary MDNode and
3520 // replace it with a standard one, avoiding leaking memory.
3521 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003522 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003523 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003524 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003525
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003526 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00003527 }
3528
Adrian Prantl73409ce2013-03-11 18:33:46 +00003529 // We keep our own list of retained types, because we need to look
3530 // up the final type in the type cache.
Adrian Prantl3a884fa2015-08-27 22:56:46 +00003531 for (auto &RT : RetainedTypes)
Adrian Prantlad9a195e2015-08-27 21:21:19 +00003532 if (auto MD = TypeCache[RT])
3533 DBuilder.retainType(cast<llvm::DIType>(MD));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003534
Guy Benyei11169dd2012-12-18 14:30:41 +00003535 DBuilder.finalize();
3536}
David Blaikie66088d52014-09-24 17:01:27 +00003537
3538void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
3539 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3540 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003541
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003542 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003543 // Don't ignore in case of explicit cast where it is referenced indirectly.
3544 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00003545}