blob: 792b1b09a0f3fad556155156fdfb91f14243e1d2 [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()) {
Guy Benyei11169dd2012-12-18 14:30:41 +000050 CreateCompileUnit();
51}
52
53CGDebugInfo::~CGDebugInfo() {
54 assert(LexicalBlockStack.empty() &&
55 "Region stack mismatch, stack not empty!");
56}
57
David Blaikie66e41972015-01-14 07:38:27 +000058ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
David Blaikie835afb22015-01-21 23:08:17 +000059 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000060 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +000061 init(TemporaryLocation);
62}
63
Adrian Prantl39428e72015-02-03 18:40:42 +000064ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
Adrian Prantl95b24e92015-02-03 20:00:54 +000065 bool DefaultToEmpty,
Adrian Prantl39428e72015-02-03 18:40:42 +000066 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000067 : CGF(&CGF) {
Adrian Prantl95b24e92015-02-03 20:00:54 +000068 init(TemporaryLocation, DefaultToEmpty);
Adrian Prantl39428e72015-02-03 18:40:42 +000069}
70
71void ApplyDebugLocation::init(SourceLocation TemporaryLocation,
Adrian Prantl95b24e92015-02-03 20:00:54 +000072 bool DefaultToEmpty) {
David Blaikied7057d92015-08-12 23:49:57 +000073 auto *DI = CGF->getDebugInfo();
74 if (!DI) {
75 CGF = nullptr;
76 return;
David Blaikie66e41972015-01-14 07:38:27 +000077 }
David Blaikied7057d92015-08-12 23:49:57 +000078
79 OriginalLocation = CGF->Builder.getCurrentDebugLocation();
80 if (TemporaryLocation.isValid()) {
81 DI->EmitLocation(CGF->Builder, TemporaryLocation);
82 return;
83 }
84
85 if (DefaultToEmpty) {
86 CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc());
87 return;
88 }
89
90 // Construct a location that has a valid scope, but no line info.
91 assert(!DI->LexicalBlockStack.empty());
92 CGF->Builder.SetCurrentDebugLocation(
93 llvm::DebugLoc::get(0, 0, DI->LexicalBlockStack.back()));
Adrian Prantl2e0637f2013-07-18 00:28:02 +000094}
95
David Blaikie9b479662015-01-25 01:19:10 +000096ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E)
David Blaikied7057d92015-08-12 23:49:57 +000097 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +000098 init(E->getExprLoc());
99}
100
David Blaikie66e41972015-01-14 07:38:27 +0000101ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc)
David Blaikied7057d92015-08-12 23:49:57 +0000102 : CGF(&CGF) {
103 if (!CGF.getDebugInfo()) {
104 this->CGF = nullptr;
105 return;
David Blaikie66e41972015-01-14 07:38:27 +0000106 }
David Blaikied7057d92015-08-12 23:49:57 +0000107 OriginalLocation = CGF.Builder.getCurrentDebugLocation();
108 if (Loc)
109 CGF.Builder.SetCurrentDebugLocation(std::move(Loc));
David Blaikie66e41972015-01-14 07:38:27 +0000110}
111
112ApplyDebugLocation::~ApplyDebugLocation() {
113 // Query CGF so the location isn't overwritten when location updates are
114 // temporarily disabled (for C++ default function arguments)
David Blaikied7057d92015-08-12 23:49:57 +0000115 if (CGF)
116 CGF->Builder.SetCurrentDebugLocation(std::move(OriginalLocation));
David Blaikie66e41972015-01-14 07:38:27 +0000117}
118
Guy Benyei11169dd2012-12-18 14:30:41 +0000119void CGDebugInfo::setLocation(SourceLocation Loc) {
120 // If the new location isn't valid return.
Eric Christophere7b87e52014-10-26 23:40:33 +0000121 if (Loc.isInvalid())
122 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000123
124 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
125
126 // If we've changed files in the middle of a lexical scope go ahead
127 // and create a new lexical scope with file node if it's different
128 // from the one in the scope.
Eric Christophere7b87e52014-10-26 23:40:33 +0000129 if (LexicalBlockStack.empty())
130 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000131
132 SourceManager &SM = CGM.getContext().getSourceManager();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000133 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +0000134 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000135
Duncan P. N. Exon Smith373ee852015-04-16 01:36:36 +0000136 if (PCLoc.isInvalid() || Scope->getFilename() == PCLoc.getFilename())
Guy Benyei11169dd2012-12-18 14:30:41 +0000137 return;
138
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000139 if (auto *LBF = dyn_cast<llvm::DILexicalBlockFile>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000140 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000141 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlockFile(
142 LBF->getScope(), getOrCreateFile(CurLoc)));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000143 } else if (isa<llvm::DILexicalBlock>(Scope) ||
144 isa<llvm::DISubprogram>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000145 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000146 LexicalBlockStack.emplace_back(
147 DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000148 }
149}
150
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000151llvm::DIScope *CGDebugInfo::getDeclContextDescriptor(const Decl *D) {
Adrian Prantl5c8bd882015-09-11 17:23:08 +0000152 llvm::DIScope *Mod = getParentModuleOrNull(D);
153 return getContextDescriptor(cast<Decl>(D->getDeclContext()),
154 Mod ? Mod : TheCU);
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000155}
156
157llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context,
158 llvm::DIScope *Default) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000159 if (!Context)
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000160 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000161
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000162 auto I = RegionMap.find(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +0000163 if (I != RegionMap.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000164 llvm::Metadata *V = I->second;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000165 return dyn_cast_or_null<llvm::DIScope>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000166 }
167
168 // Check namespace.
169 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000170 return getOrCreateNameSpace(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +0000171
David Blaikiebfa52742013-04-19 06:56:38 +0000172 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context))
173 if (!RDecl->isDependentType())
174 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Eric Christophere7b87e52014-10-26 23:40:33 +0000175 getOrCreateMainFile());
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000176 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000177}
178
Guy Benyei11169dd2012-12-18 14:30:41 +0000179StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000180 assert(FD && "Invalid FunctionDecl!");
Guy Benyei11169dd2012-12-18 14:30:41 +0000181 IdentifierInfo *FII = FD->getIdentifier();
Eric Christophere7b87e52014-10-26 23:40:33 +0000182 FunctionTemplateSpecializationInfo *Info =
183 FD->getTemplateSpecializationInfo();
Guy Benyei11169dd2012-12-18 14:30:41 +0000184 if (!Info && FII)
185 return FII->getName();
186
187 // Otherwise construct human readable name for debug info.
Benjamin Kramer9170e912013-02-22 15:46:01 +0000188 SmallString<128> NS;
189 llvm::raw_svector_ostream OS(NS);
190 FD->printName(OS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000191
192 // Add any template specialization args.
193 if (Info) {
194 const TemplateArgumentList *TArgs = Info->TemplateArguments;
195 const TemplateArgument *Args = TArgs->data();
196 unsigned NumArgs = TArgs->size();
197 PrintingPolicy Policy(CGM.getLangOpts());
Benjamin Kramer9170e912013-02-22 15:46:01 +0000198 TemplateSpecializationType::PrintTemplateArgumentList(OS, Args, NumArgs,
199 Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000200 }
201
202 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000203 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000204}
205
206StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
207 SmallString<256> MethodName;
208 llvm::raw_svector_ostream OS(MethodName);
209 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
210 const DeclContext *DC = OMD->getDeclContext();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000211 if (const ObjCImplementationDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000212 dyn_cast<const ObjCImplementationDecl>(DC)) {
213 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000214 } else if (const ObjCInterfaceDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000215 dyn_cast<const ObjCInterfaceDecl>(DC)) {
216 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000217 } else if (const ObjCCategoryImplDecl *OCD =
Eric Christophere7b87e52014-10-26 23:40:33 +0000218 dyn_cast<const ObjCCategoryImplDecl>(DC)) {
219 OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '('
220 << OCD->getIdentifier()->getNameStart() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000221 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000222 // We can extract the type of the class from the self pointer.
Eric Christophere7b87e52014-10-26 23:40:33 +0000223 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000224 QualType ClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000225 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000226 ClassTy.print(OS, PrintingPolicy(LangOptions()));
227 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000228 }
229 OS << ' ' << OMD->getSelector().getAsString() << ']';
230
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000231 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000232}
233
Guy Benyei11169dd2012-12-18 14:30:41 +0000234StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000235 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000236}
237
Eric Christophere7b87e52014-10-26 23:40:33 +0000238StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
David Blaikie65813a32014-04-02 18:21:09 +0000239 // quick optimization to avoid having to intern strings that are already
240 // stored reliably elsewhere
241 if (!isa<ClassTemplateSpecializationDecl>(RD))
Guy Benyei11169dd2012-12-18 14:30:41 +0000242 return RD->getName();
243
David Blaikie65813a32014-04-02 18:21:09 +0000244 SmallString<128> Name;
Benjamin Kramer9170e912013-02-22 15:46:01 +0000245 {
David Blaikie65813a32014-04-02 18:21:09 +0000246 llvm::raw_svector_ostream OS(Name);
247 RD->getNameForDiagnostic(OS, CGM.getContext().getPrintingPolicy(),
248 /*Qualified*/ false);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000249 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000250
251 // Copy this name on the side and use its reference.
David Blaikie65813a32014-04-02 18:21:09 +0000252 return internString(Name);
Guy Benyei11169dd2012-12-18 14:30:41 +0000253}
254
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000255llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000256 if (!Loc.isValid())
257 // If Location is not valid then use main input file.
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +0000258 return DBuilder.createFile(TheCU->getFilename(), TheCU->getDirectory());
Guy Benyei11169dd2012-12-18 14:30:41 +0000259
260 SourceManager &SM = CGM.getContext().getSourceManager();
261 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
262
263 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
264 // If the location is not valid then use main input file.
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +0000265 return DBuilder.createFile(TheCU->getFilename(), TheCU->getDirectory());
Guy Benyei11169dd2012-12-18 14:30:41 +0000266
267 // Cache the results.
268 const char *fname = PLoc.getFilename();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000269 auto it = DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000270
271 if (it != DIFileCache.end()) {
272 // Verify that the information still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000273 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000274 return cast<llvm::DIFile>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000275 }
276
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000277 llvm::DIFile *F =
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +0000278 DBuilder.createFile(PLoc.getFilename(), getCurrentDirname());
Guy Benyei11169dd2012-12-18 14:30:41 +0000279
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000280 DIFileCache[fname].reset(F);
Guy Benyei11169dd2012-12-18 14:30:41 +0000281 return F;
282}
283
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000284llvm::DIFile *CGDebugInfo::getOrCreateMainFile() {
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +0000285 return DBuilder.createFile(TheCU->getFilename(), TheCU->getDirectory());
Guy Benyei11169dd2012-12-18 14:30:41 +0000286}
287
Guy Benyei11169dd2012-12-18 14:30:41 +0000288unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
289 if (Loc.isInvalid() && CurLoc.isInvalid())
290 return 0;
291 SourceManager &SM = CGM.getContext().getSourceManager();
292 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000293 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000294}
295
Adrian Prantlc7822422013-03-12 20:43:25 +0000296unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000297 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000298 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000299 return 0;
300
301 // If the location is invalid then use the current column.
302 if (Loc.isInvalid() && CurLoc.isInvalid())
303 return 0;
304 SourceManager &SM = CGM.getContext().getSourceManager();
305 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000306 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000307}
308
309StringRef CGDebugInfo::getCurrentDirname() {
310 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
311 return CGM.getCodeGenOpts().DebugCompilationDir;
312
313 if (!CWDName.empty())
314 return CWDName;
315 SmallString<256> CWD;
316 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000317 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000318}
319
Guy Benyei11169dd2012-12-18 14:30:41 +0000320void CGDebugInfo::CreateCompileUnit() {
321
David Blaikieaabde052014-05-14 00:29:00 +0000322 // Should we be asking the SourceManager for the main file name, instead of
323 // accepting it as an argument? This just causes the main file name to
324 // mismatch with source locations and create extra lexical scopes or
325 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
326 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
327 // because that's what the SourceManager says)
328
Guy Benyei11169dd2012-12-18 14:30:41 +0000329 // Get absolute path name.
330 SourceManager &SM = CGM.getContext().getSourceManager();
331 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
332 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000333 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000334
335 // The main file name provided via the "-main-file-name" option contains just
336 // the file name itself with no path information. This file name may have had
337 // a relative path, so we look into the actual file entry for the main
338 // file to determine the real absolute path for the file.
339 std::string MainFileDir;
340 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
341 MainFileDir = MainFile->getDir()->getName();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000342 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000343 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
344 llvm::sys::path::append(MainFileDirSS, MainFileName);
345 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000346 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000347 }
348
Ed Masteda706022014-05-07 12:49:30 +0000349 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000350 const LangOptions &LO = CGM.getLangOpts();
351 if (LO.CPlusPlus) {
352 if (LO.ObjC1)
353 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
354 else
355 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
356 } else if (LO.ObjC1) {
357 LangTag = llvm::dwarf::DW_LANG_ObjC;
358 } else if (LO.C99) {
359 LangTag = llvm::dwarf::DW_LANG_C99;
360 } else {
361 LangTag = llvm::dwarf::DW_LANG_C89;
362 }
363
364 std::string Producer = getClangFullVersion();
365
366 // Figure out which version of the ObjC runtime we have.
367 unsigned RuntimeVers = 0;
368 if (LO.ObjC1)
369 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
370
371 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000372 // FIXME - Eliminate TheCU.
Eric Christophere4200a22014-02-27 01:25:08 +0000373 TheCU = DBuilder.createCompileUnit(
Adrian Prantlb67dbce2015-09-11 18:45:02 +0000374 LangTag, MainFileName, getCurrentDirname(), Producer, LO.Optimize,
375 CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers,
376 CGM.getCodeGenOpts().SplitDwarfFile,
Diego Novillo913690c2014-06-24 17:02:17 +0000377 DebugKind <= CodeGenOptions::DebugLineTablesOnly
Eric Christophere4200a22014-02-27 01:25:08 +0000378 ? llvm::DIBuilder::LineTablesOnly
Diego Novillo913690c2014-06-24 17:02:17 +0000379 : llvm::DIBuilder::FullDebug,
Adrian Prantlb67dbce2015-09-11 18:45:02 +0000380 0 /* DWOid */, DebugKind != CodeGenOptions::LocTrackingOnly);
Guy Benyei11169dd2012-12-18 14:30:41 +0000381}
382
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000383llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000384 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000385 StringRef BTName;
386 switch (BT->getKind()) {
387#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000388#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000389#include "clang/AST/BuiltinTypes.def"
390 case BuiltinType::Dependent:
391 llvm_unreachable("Unexpected builtin type");
392 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000393 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000394 case BuiltinType::Void:
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000395 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +0000396 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000397 if (!ClassTy)
398 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
399 "objc_class", TheCU,
400 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000401 return ClassTy;
402 case BuiltinType::ObjCId: {
403 // typedef struct objc_class *Class;
404 // typedef struct objc_object {
405 // Class isa;
406 // } *id;
407
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000408 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000409 return ObjTy;
410
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000411 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000412 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
413 "objc_class", TheCU,
414 getOrCreateMainFile(), 0);
415
416 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000417
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000418 auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000419
Eric Christopher5c7ee8b2013-04-02 22:59:11 +0000420 ObjTy =
David Blaikie6d4fe152013-02-25 01:07:08 +0000421 DBuilder.createStructType(TheCU, "objc_object", getOrCreateMainFile(),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000422 0, 0, 0, 0, nullptr, llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000423
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000424 DBuilder.replaceArrays(
425 ObjTy,
426 DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
427 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0, 0, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000428 return ObjTy;
429 }
430 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000431 if (!SelTy)
432 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
433 "objc_selector", TheCU,
434 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000435 return SelTy;
436 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000437
438 case BuiltinType::OCLImage1d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000439 return getOrCreateStructPtrType("opencl_image1d_t", OCLImage1dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000440 case BuiltinType::OCLImage1dArray:
Eric Christopherb2a008c2013-05-16 00:45:12 +0000441 return getOrCreateStructPtrType("opencl_image1d_array_t",
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000442 OCLImage1dArrayDITy);
443 case BuiltinType::OCLImage1dBuffer:
444 return getOrCreateStructPtrType("opencl_image1d_buffer_t",
445 OCLImage1dBufferDITy);
446 case BuiltinType::OCLImage2d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000447 return getOrCreateStructPtrType("opencl_image2d_t", OCLImage2dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000448 case BuiltinType::OCLImage2dArray:
449 return getOrCreateStructPtrType("opencl_image2d_array_t",
450 OCLImage2dArrayDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000451 case BuiltinType::OCLImage2dDepth:
452 return getOrCreateStructPtrType("opencl_image2d_depth_t",
453 OCLImage2dDepthDITy);
454 case BuiltinType::OCLImage2dArrayDepth:
455 return getOrCreateStructPtrType("opencl_image2d_array_depth_t",
456 OCLImage2dArrayDepthDITy);
457 case BuiltinType::OCLImage2dMSAA:
458 return getOrCreateStructPtrType("opencl_image2d_msaa_t",
459 OCLImage2dMSAADITy);
460 case BuiltinType::OCLImage2dArrayMSAA:
461 return getOrCreateStructPtrType("opencl_image2d_array_msaa_t",
462 OCLImage2dArrayMSAADITy);
463 case BuiltinType::OCLImage2dMSAADepth:
464 return getOrCreateStructPtrType("opencl_image2d_msaa_depth_t",
465 OCLImage2dMSAADepthDITy);
466 case BuiltinType::OCLImage2dArrayMSAADepth:
467 return getOrCreateStructPtrType("opencl_image2d_array_msaa_depth_t",
468 OCLImage2dArrayMSAADepthDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000469 case BuiltinType::OCLImage3d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000470 return getOrCreateStructPtrType("opencl_image3d_t", OCLImage3dDITy);
Guy Benyei61054192013-02-07 10:55:47 +0000471 case BuiltinType::OCLSampler:
Eric Christophere7b87e52014-10-26 23:40:33 +0000472 return DBuilder.createBasicType(
473 "opencl_sampler_t", CGM.getContext().getTypeSize(BT),
474 CGM.getContext().getTypeAlign(BT), llvm::dwarf::DW_ATE_unsigned);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000475 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000476 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000477 case BuiltinType::OCLClkEvent:
478 return getOrCreateStructPtrType("opencl_clk_event_t", OCLClkEventDITy);
479 case BuiltinType::OCLQueue:
480 return getOrCreateStructPtrType("opencl_queue_t", OCLQueueDITy);
481 case BuiltinType::OCLNDRange:
482 return getOrCreateStructPtrType("opencl_ndrange_t", OCLNDRangeDITy);
483 case BuiltinType::OCLReserveID:
484 return getOrCreateStructPtrType("opencl_reserve_id_t", OCLReserveIDDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000485
Guy Benyei11169dd2012-12-18 14:30:41 +0000486 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000487 case BuiltinType::Char_U:
488 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
489 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000490 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000491 case BuiltinType::SChar:
492 Encoding = llvm::dwarf::DW_ATE_signed_char;
493 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000494 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000495 case BuiltinType::Char32:
496 Encoding = llvm::dwarf::DW_ATE_UTF;
497 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000498 case BuiltinType::UShort:
499 case BuiltinType::UInt:
500 case BuiltinType::UInt128:
501 case BuiltinType::ULong:
502 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000503 case BuiltinType::ULongLong:
504 Encoding = llvm::dwarf::DW_ATE_unsigned;
505 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000506 case BuiltinType::Short:
507 case BuiltinType::Int:
508 case BuiltinType::Int128:
509 case BuiltinType::Long:
510 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000511 case BuiltinType::LongLong:
512 Encoding = llvm::dwarf::DW_ATE_signed;
513 break;
514 case BuiltinType::Bool:
515 Encoding = llvm::dwarf::DW_ATE_boolean;
516 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000517 case BuiltinType::Half:
518 case BuiltinType::Float:
519 case BuiltinType::LongDouble:
Eric Christophere7b87e52014-10-26 23:40:33 +0000520 case BuiltinType::Double:
521 Encoding = llvm::dwarf::DW_ATE_float;
522 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000523 }
524
525 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000526 case BuiltinType::Long:
527 BTName = "long int";
528 break;
529 case BuiltinType::LongLong:
530 BTName = "long long int";
531 break;
532 case BuiltinType::ULong:
533 BTName = "long unsigned int";
534 break;
535 case BuiltinType::ULongLong:
536 BTName = "long long unsigned int";
537 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000538 default:
539 BTName = BT->getName(CGM.getLangOpts());
540 break;
541 }
542 // Bit size, align and offset of the type.
543 uint64_t Size = CGM.getContext().getTypeSize(BT);
544 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000545 return DBuilder.createBasicType(BTName, Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000546}
547
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000548llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000549 // Bit size, align and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000550 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000551 if (Ty->isComplexIntegerType())
552 Encoding = llvm::dwarf::DW_ATE_lo_user;
553
554 uint64_t Size = CGM.getContext().getTypeSize(Ty);
555 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000556 return DBuilder.createBasicType("complex", Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000557}
558
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000559llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
560 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000561 QualifierCollector Qc;
562 const Type *T = Qc.strip(Ty);
563
564 // Ignore these qualifiers for now.
565 Qc.removeObjCGCAttr();
566 Qc.removeAddressSpace();
567 Qc.removeObjCLifetime();
568
569 // We will create one Derived type for one qualifier and recurse to handle any
570 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000571 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000572 if (Qc.hasConst()) {
573 Tag = llvm::dwarf::DW_TAG_const_type;
574 Qc.removeConst();
575 } else if (Qc.hasVolatile()) {
576 Tag = llvm::dwarf::DW_TAG_volatile_type;
577 Qc.removeVolatile();
578 } else if (Qc.hasRestrict()) {
579 Tag = llvm::dwarf::DW_TAG_restrict_type;
580 Qc.removeRestrict();
581 } else {
582 assert(Qc.empty() && "Unknown type qualifier for debug info");
583 return getOrCreateType(QualType(T, 0), Unit);
584 }
585
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000586 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000587
588 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
589 // CVR derived types.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000590 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000591}
592
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000593llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
594 llvm::DIFile *Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000595
596 // The frontend treats 'id' as a typedef to an ObjCObjectType,
597 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
598 // debug info, we want to emit 'id' in both cases.
599 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000600 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000601
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000602 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
603 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000604}
605
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000606llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
607 llvm::DIFile *Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000608 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000609 Ty->getPointeeType(), Unit);
610}
611
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000612/// \return whether a C++ mangling exists for the type defined by TD.
613static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
614 switch (TheCU->getSourceLanguage()) {
615 case llvm::dwarf::DW_LANG_C_plus_plus:
616 return true;
617 case llvm::dwarf::DW_LANG_ObjC_plus_plus:
618 return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
619 default:
620 return false;
621 }
622}
623
Manman Rene0064d82013-08-29 23:19:58 +0000624/// In C++ mode, types have linkage, so we can rely on the ODR and
625/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000626static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
627 CodeGenModule &CGM,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000628 llvm::DICompileUnit *TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000629 SmallString<256> FullName;
Manman Rene0064d82013-08-29 23:19:58 +0000630 const TagDecl *TD = Ty->getDecl();
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000631
632 if (!hasCXXMangling(TD, TheCU) || !TD->isExternallyVisible())
Manman Rene0064d82013-08-29 23:19:58 +0000633 return FullName;
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000634
Manman Rene0064d82013-08-29 23:19:58 +0000635 // Microsoft Mangler does not have support for mangleCXXRTTIName yet.
636 if (CGM.getTarget().getCXXABI().isMicrosoft())
637 return FullName;
638
639 // TODO: This is using the RTTI name. Is there a better way to get
640 // a unique string for a type?
641 llvm::raw_svector_ostream Out(FullName);
642 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
Manman Rene0064d82013-08-29 23:19:58 +0000643 return FullName;
644}
645
Adrian Prantl3e8bad42015-07-08 21:18:34 +0000646/// \return the approproate DWARF tag for a composite type.
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000647static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
648 llvm::dwarf::Tag Tag;
649 if (RD->isStruct() || RD->isInterface())
650 Tag = llvm::dwarf::DW_TAG_structure_type;
651 else if (RD->isUnion())
652 Tag = llvm::dwarf::DW_TAG_union_type;
653 else {
654 // FIXME: This could be a struct type giving a default visibility different
655 // than C++ class type, but needs llvm metadata changes first.
656 assert(RD->isClass());
657 Tag = llvm::dwarf::DW_TAG_class_type;
658 }
659 return Tag;
660}
661
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000662llvm::DICompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000663CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000664 llvm::DIScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000665 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000666 if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
667 return cast<llvm::DICompositeType>(T);
668 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +0000669 unsigned Line = getLineNumber(RD->getLocation());
670 StringRef RDName = getClassName(RD);
671
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000672 uint64_t Size = 0;
673 uint64_t Align = 0;
674
675 const RecordDecl *D = RD->getDefinition();
676 if (D && D->isCompleteDefinition()) {
677 Size = CGM.getContext().getTypeSize(Ty);
678 Align = CGM.getContext().getTypeAlign(Ty);
679 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000680
681 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000682 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000683 llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000684 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000685 llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000686 ReplaceMap.emplace_back(
687 std::piecewise_construct, std::make_tuple(Ty),
688 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000689 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000690}
691
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000692llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000693 const Type *Ty,
694 QualType PointeeTy,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000695 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000696 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
697 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
David Blaikie99dab3b2013-09-04 22:03:57 +0000698 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit));
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000699
Guy Benyei11169dd2012-12-18 14:30:41 +0000700 // Bit size, align and offset of the type.
701 // Size is always the size of a pointer. We can't use getTypeSize here
702 // because that does not return the correct value for references.
703 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000704 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000705 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
706
David Blaikie99dab3b2013-09-04 22:03:57 +0000707 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
708 Align);
Guy Benyei11169dd2012-12-18 14:30:41 +0000709}
710
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000711llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
712 llvm::DIType *&Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000713 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000714 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000715 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
716 TheCU, getOrCreateMainFile(), 0);
717 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
718 Cache = DBuilder.createPointerType(Cache, Size);
719 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000720}
721
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000722llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
723 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000724 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000725 QualType FType;
726 uint64_t FieldSize, FieldOffset;
727 unsigned FieldAlign;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000728 llvm::DINodeArray Elements;
Guy Benyei11169dd2012-12-18 14:30:41 +0000729
730 FieldOffset = 0;
731 FType = CGM.getContext().UnsignedLongTy;
732 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
733 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
734
735 Elements = DBuilder.getOrCreateArray(EltTys);
736 EltTys.clear();
737
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000738 unsigned Flags = llvm::DINode::FlagAppleBlock;
Adrian Prantl498fff62015-07-06 21:31:35 +0000739 unsigned LineNo = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000740
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000741 auto *EltTy =
Adrian Prantl498fff62015-07-06 21:31:35 +0000742 DBuilder.createStructType(Unit, "__block_descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000743 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000744
745 // Bit size, align and offset of the type.
746 uint64_t Size = CGM.getContext().getTypeSize(Ty);
747
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000748 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000749
750 FieldOffset = 0;
751 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
752 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
753 FType = CGM.getContext().IntTy;
754 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
755 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000756 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000757 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
758
759 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000760 FieldSize = CGM.getContext().getTypeSize(Ty);
761 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Adrian Prantl498fff62015-07-06 21:31:35 +0000762 EltTys.push_back(DBuilder.createMemberType(Unit, "__descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000763 FieldSize, FieldAlign, FieldOffset,
764 0, DescTy));
Guy Benyei11169dd2012-12-18 14:30:41 +0000765
766 FieldOffset += FieldSize;
767 Elements = DBuilder.getOrCreateArray(EltTys);
768
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000769 // The __block_literal_generic structs are marked with a special
770 // DW_AT_APPLE_BLOCK attribute and are an implementation detail only
771 // the debugger needs to know about. To allow type uniquing, emit
772 // them without a name or a location.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000773 EltTy =
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000774 DBuilder.createStructType(Unit, "", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000775 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000776
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000777 return DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000778}
779
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000780llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
781 llvm::DIFile *Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000782 assert(Ty->isTypeAlias());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000783 llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000784
785 SmallString<128> NS;
786 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000787 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
788 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000789
790 TemplateSpecializationType::PrintTemplateArgumentList(
791 OS, Ty->getArgs(), Ty->getNumArgs(),
792 CGM.getContext().getPrintingPolicy());
793
Eric Christophere7b87e52014-10-26 23:40:33 +0000794 TypeAliasDecl *AliasDecl = cast<TypeAliasTemplateDecl>(
795 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000796
797 SourceLocation Loc = AliasDecl->getLocation();
Adrian Prantlb67dbce2015-09-11 18:45:02 +0000798 return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc),
799 getLineNumber(Loc),
800 getDeclContextDescriptor(AliasDecl));
David Blaikief1b382e2014-04-06 17:14:06 +0000801}
802
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000803llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
804 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000805 // We don't set size information, but do specify where the typedef was
806 // declared.
David Blaikiebe822ed2015-07-01 18:07:22 +0000807 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000808
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000809 // Typedefs are derived from some other type.
810 return DBuilder.createTypedef(
811 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
812 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000813 getDeclContextDescriptor(Ty->getDecl()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000814}
815
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000816llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
817 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000818 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000819
820 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000821 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000822
823 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000824 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000825 if (isa<FunctionNoProtoType>(Ty))
826 EltTys.push_back(DBuilder.createUnspecifiedParameter());
827 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
Alp Toker9cacbab2014-01-20 20:26:09 +0000828 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
829 EltTys.push_back(getOrCreateType(FPT->getParamType(i), Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000830 if (FPT->isVariadic())
831 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000832 }
833
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000834 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Guy Benyei11169dd2012-12-18 14:30:41 +0000835 return DBuilder.createSubroutineType(Unit, EltTypeArray);
836}
837
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000838/// Convert an AccessSpecifier into the corresponding DINode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +0000839/// As an optimization, return 0 if the access specifier equals the
840/// default for the containing type.
841static unsigned getAccessFlag(AccessSpecifier Access, const RecordDecl *RD) {
842 AccessSpecifier Default = clang::AS_none;
843 if (RD && RD->isClass())
844 Default = clang::AS_private;
845 else if (RD && (RD->isStruct() || RD->isUnion()))
846 Default = clang::AS_public;
847
848 if (Access == Default)
849 return 0;
850
Eric Christophere7b87e52014-10-26 23:40:33 +0000851 switch (Access) {
852 case clang::AS_private:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000853 return llvm::DINode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +0000854 case clang::AS_protected:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000855 return llvm::DINode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +0000856 case clang::AS_public:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000857 return llvm::DINode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +0000858 case clang::AS_none:
859 return 0;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000860 }
861 llvm_unreachable("unexpected access enumerator");
862}
Guy Benyei11169dd2012-12-18 14:30:41 +0000863
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000864llvm::DIType *CGDebugInfo::createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000865 StringRef name, QualType type, uint64_t sizeInBitsOverride,
866 SourceLocation loc, AccessSpecifier AS, uint64_t offsetInBits,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000867 llvm::DIFile *tunit, llvm::DIScope *scope, const RecordDecl *RD) {
868 llvm::DIType *debugType = getOrCreateType(type, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000869
870 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000871 llvm::DIFile *file = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000872 unsigned line = getLineNumber(loc);
873
David Majnemer34b57492014-07-30 01:30:47 +0000874 uint64_t SizeInBits = 0;
875 unsigned AlignInBits = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000876 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +0000877 TypeInfo TI = CGM.getContext().getTypeInfo(type);
878 SizeInBits = TI.Width;
879 AlignInBits = TI.Align;
Guy Benyei11169dd2012-12-18 14:30:41 +0000880
881 if (sizeInBitsOverride)
David Majnemer34b57492014-07-30 01:30:47 +0000882 SizeInBits = sizeInBitsOverride;
Guy Benyei11169dd2012-12-18 14:30:41 +0000883 }
884
Adrian Prantl21361fb2014-08-29 22:44:27 +0000885 unsigned flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +0000886 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
887 AlignInBits, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +0000888}
889
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000890void CGDebugInfo::CollectRecordLambdaFields(
891 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000892 llvm::DIType *RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +0000893 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
894 // has the name and the location of the variable so we should iterate over
895 // both concurrently.
896 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
897 RecordDecl::field_iterator Field = CXXDecl->field_begin();
898 unsigned fieldno = 0;
899 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +0000900 E = CXXDecl->captures_end();
901 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000902 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +0000903 if (C.capturesVariable()) {
904 VarDecl *V = C.getCapturedVar();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000905 llvm::DIFile *VUnit = getOrCreateFile(C.getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000906 StringRef VName = V->getName();
907 uint64_t SizeInBitsOverride = 0;
908 if (Field->isBitField()) {
909 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
910 assert(SizeInBitsOverride && "found named 0-width bitfield");
911 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000912 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000913 VName, Field->getType(), SizeInBitsOverride, C.getLocation(),
914 Field->getAccess(), layout.getFieldOffset(fieldno), VUnit, RecordTy,
915 CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000916 elements.push_back(fieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +0000917 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +0000918 // TODO: Need to handle 'this' in some way by probably renaming the
919 // this of the lambda class and having a field member of 'this' or
920 // by using AT_object_pointer for the function and having that be
921 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +0000922 FieldDecl *f = *Field;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000923 llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000924 QualType type = f->getType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000925 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000926 "this", type, 0, f->getLocation(), f->getAccess(),
927 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000928
929 elements.push_back(fieldType);
930 }
931 }
932}
933
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000934llvm::DIDerivedType *
935CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +0000936 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000937 // Create the descriptor for the static variable, with or without
938 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +0000939 Var = Var->getCanonicalDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000940 llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
941 llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher91a31902013-01-16 01:22:32 +0000942
Eric Christopher91a31902013-01-16 01:22:32 +0000943 unsigned LineNumber = getLineNumber(Var->getLocation());
944 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +0000945 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +0000946 if (Var->getInit()) {
947 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +0000948 if (Value) {
949 if (Value->isInt())
950 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
951 if (Value->isFloat())
952 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
953 }
Eric Christopher91a31902013-01-16 01:22:32 +0000954 }
955
Adrian Prantl21361fb2014-08-29 22:44:27 +0000956 unsigned Flags = getAccessFlag(Var->getAccess(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000957 llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
David Blaikieae019462013-08-15 22:50:29 +0000958 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000959 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +0000960 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +0000961}
962
Eric Christophere7b87e52014-10-26 23:40:33 +0000963void CGDebugInfo::CollectRecordNormalField(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000964 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
965 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +0000966 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000967 StringRef name = field->getName();
968 QualType type = field->getType();
969
970 // Ignore unnamed fields unless they're anonymous structs/unions.
971 if (name.empty() && !type->isRecordType())
972 return;
973
974 uint64_t SizeInBitsOverride = 0;
975 if (field->isBitField()) {
976 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
977 assert(SizeInBitsOverride && "found named 0-width bitfield");
978 }
979
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000980 llvm::DIType *fieldType =
Eric Christophere7b87e52014-10-26 23:40:33 +0000981 createFieldType(name, type, SizeInBitsOverride, field->getLocation(),
982 field->getAccess(), OffsetInBits, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +0000983
984 elements.push_back(fieldType);
985}
986
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000987void CGDebugInfo::CollectRecordFields(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000988 const RecordDecl *record, llvm::DIFile *tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000989 SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000990 llvm::DICompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000991 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
992
Eric Christopher91a31902013-01-16 01:22:32 +0000993 if (CXXDecl && CXXDecl->isLambda())
994 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
995 else {
996 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +0000997
Eric Christopher91a31902013-01-16 01:22:32 +0000998 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +0000999 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +00001000
Eric Christopher91a31902013-01-16 01:22:32 +00001001 // Static and non-static members should appear in the same order as
1002 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +00001003 for (const auto *I : record->decls())
1004 if (const auto *V = dyn_cast<VarDecl>(I)) {
David Blaikiece763042013-08-20 21:49:21 +00001005 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001006 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +00001007 if (MI != StaticDataMemberCache.end()) {
1008 assert(MI->second &&
1009 "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00001010 elements.push_back(MI->second);
Adrian Prantl21361fb2014-08-29 22:44:27 +00001011 } else {
1012 auto Field = CreateRecordStaticField(V, RecordTy, record);
1013 elements.push_back(Field);
1014 }
Aaron Ballman629afae2014-03-07 19:56:05 +00001015 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001016 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
1017 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +00001018
1019 // Bump field number for next field.
1020 ++fieldNo;
Guy Benyei11169dd2012-12-18 14:30:41 +00001021 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001022 }
1023}
1024
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001025llvm::DISubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +00001026CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001027 llvm::DIFile *Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001028 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001029 if (Method->isStatic())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001030 return cast_or_null<llvm::DISubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001031 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001032 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1033 Func, Unit);
1034}
David Blaikie2aaf0652013-01-07 22:24:59 +00001035
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001036llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1037 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001038 // Add "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001039 llvm::DITypeRefArray Args(
1040 cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001041 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001042 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001043
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001044 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001045
1046 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001047 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001048
David Blaikie2aaf0652013-01-07 22:24:59 +00001049 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001050 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001051 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1052 // Create pointer type directly in this case.
1053 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1054 QualType PointeeTy = ThisPtrTy->getPointeeType();
1055 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001056 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie2aaf0652013-01-07 22:24:59 +00001057 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001058 llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1059 llvm::DIType *ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001060 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001061 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001062 // TODO: This and the artificial type below are misleading, the
1063 // types aren't artificial the argument is, but the current
1064 // metadata doesn't represent that.
1065 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1066 Elts.push_back(ThisPtrType);
1067 } else {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001068 llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001069 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001070 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1071 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001072 }
1073
1074 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001075 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1076 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001077
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001078 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001079
Adrian Prantl0630eb72013-12-18 21:48:18 +00001080 unsigned Flags = 0;
1081 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001082 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001083 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001084 Flags |= llvm::DINode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001085
1086 return DBuilder.createSubroutineType(Unit, EltTypeArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001087}
1088
Eric Christopherb2a008c2013-05-16 00:45:12 +00001089/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001090/// inside a function.
1091static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1092 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1093 return isFunctionLocalClass(NRD);
1094 if (isa<FunctionDecl>(RD->getDeclContext()))
1095 return true;
1096 return false;
1097}
1098
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001099llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1100 const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001101 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001102 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001103
Guy Benyei11169dd2012-12-18 14:30:41 +00001104 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001105 llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001106
1107 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1108 // make sense to give a single ctor/dtor a linkage name.
1109 StringRef MethodLinkageName;
1110 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1111 MethodLinkageName = CGM.getMangledName(Method);
1112
1113 // Get the location for the method.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001114 llvm::DIFile *MethodDefUnit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00001115 unsigned MethodLine = 0;
1116 if (!Method->isImplicit()) {
1117 MethodDefUnit = getOrCreateFile(Method->getLocation());
1118 MethodLine = getLineNumber(Method->getLocation());
1119 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001120
1121 // Collect virtual method info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001122 llvm::DIType *ContainingType = nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001123 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001124 unsigned VIndex = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001125
Guy Benyei11169dd2012-12-18 14:30:41 +00001126 if (Method->isVirtual()) {
1127 if (Method->isPure())
1128 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1129 else
1130 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001131
Guy Benyei11169dd2012-12-18 14:30:41 +00001132 // It doesn't make sense to give a virtual destructor a vtable index,
1133 // since a single destructor has two entries in the vtable.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001134 // FIXME: Add proper support for debug info for virtual calls in
1135 // the Microsoft ABI, where we may use multiple vptrs to make a vftable
1136 // lookup if we have multiple or virtual inheritance.
1137 if (!isa<CXXDestructorDecl>(Method) &&
1138 !CGM.getTarget().getCXXABI().isMicrosoft())
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001139 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
Guy Benyei11169dd2012-12-18 14:30:41 +00001140 ContainingType = RecordTy;
1141 }
1142
1143 unsigned Flags = 0;
1144 if (Method->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001145 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001146 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
Guy Benyei11169dd2012-12-18 14:30:41 +00001147 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1148 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001149 Flags |= llvm::DINode::FlagExplicit;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001150 } else if (const CXXConversionDecl *CXXC =
Eric Christophere7b87e52014-10-26 23:40:33 +00001151 dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001152 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001153 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001154 }
1155 if (Method->hasPrototype())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001156 Flags |= llvm::DINode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001157 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001158 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001159 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001160 Flags |= llvm::DINode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001161
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001162 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1163 llvm::DISubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001164 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
1165 MethodTy, /*isLocalToUnit=*/false,
1166 /* isDefinition=*/false, Virtuality, VIndex, ContainingType, Flags,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00001167 CGM.getLangOpts().Optimize, nullptr, TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001168
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001169 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001170
1171 return SP;
1172}
1173
Eric Christophere7b87e52014-10-26 23:40:33 +00001174void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001175 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1176 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001177
1178 // Since we want more than just the individual member decls if we
1179 // have templated functions iterate over every declaration to gather
1180 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001181 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001182 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1183 // If the member is implicit, don't add it to the member list. This avoids
1184 // the member being added to type units by LLVM, while still allowing it
1185 // to be emitted into the type declaration/reference inside the compile
1186 // unit.
Paul Robinson6a7511b2015-06-25 17:50:43 +00001187 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
David Blaikie6dddfe32014-10-06 05:52:27 +00001188 // FIXME: Handle Using(Shadow?)Decls here to create
1189 // DW_TAG_imported_declarations inside the class for base decls brought into
1190 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1191 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1192 // referenced)
Paul Robinson6a7511b2015-06-25 17:50:43 +00001193 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
David Blaikiefd580722014-10-06 05:18:55 +00001194 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001195
1196 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1197 continue;
1198
David Blaikiefd580722014-10-06 05:18:55 +00001199 // Reuse the existing member function declaration if it exists.
1200 // It may be associated with the declaration of the type & should be
1201 // reused as we're building the definition.
1202 //
1203 // This situation can arise in the vtable-based debug info reduction where
1204 // implicit members are emitted in a non-vtable TU.
1205 auto MI = SPCache.find(Method->getCanonicalDecl());
1206 EltTys.push_back(MI == SPCache.end()
1207 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001208 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001209 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001210}
Guy Benyei11169dd2012-12-18 14:30:41 +00001211
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001212void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001213 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001214 llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001215 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Aaron Ballman574705e2014-03-13 15:41:46 +00001216 for (const auto &BI : RD->bases()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001217 unsigned BFlags = 0;
1218 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001219
Guy Benyei11169dd2012-12-18 14:30:41 +00001220 const CXXRecordDecl *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001221 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001222
Aaron Ballman574705e2014-03-13 15:41:46 +00001223 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001224 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1225 // virtual base offset offset is -ve. The code generator emits dwarf
1226 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001227 BaseOffset = 0 - CGM.getItaniumVTableContext()
1228 .getVirtualBaseOffsetOffset(RD, Base)
1229 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001230 } else {
1231 // In the MS ABI, store the vbtable offset, which is analogous to the
1232 // vbase offset offset in Itanium.
1233 BaseOffset =
1234 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1235 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001236 BFlags = llvm::DINode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001237 } else
1238 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1239 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1240 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001241
Adrian Prantl21361fb2014-08-29 22:44:27 +00001242 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001243 llvm::DIType *DTy = DBuilder.createInheritance(
Eric Christophere7b87e52014-10-26 23:40:33 +00001244 RecordTy, getOrCreateType(BI.getType(), Unit), BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001245 EltTys.push_back(DTy);
1246 }
1247}
1248
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001249llvm::DINodeArray
Eric Christophere7b87e52014-10-26 23:40:33 +00001250CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1251 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001252 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001253 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001254 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1255 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001256 StringRef Name;
1257 if (TPList)
1258 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001259 switch (TA.getKind()) {
1260 case TemplateArgument::Type: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001261 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001262 TemplateParams.push_back(
1263 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001264 } break;
1265 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001266 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001267 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1268 TheCU, Name, TTy,
1269 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001270 } break;
1271 case TemplateArgument::Declaration: {
1272 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001273 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001274 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001275 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001276 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001277 // Variable pointer template parameters have a value that is the address
1278 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001279 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001280 V = CGM.GetAddrOfGlobalVar(VD);
1281 // Member function pointers have special support for building them, though
1282 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001283 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Majnemere2be95b2015-06-23 07:31:01 +00001284 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001285 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001286 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001287 // Member data pointers have special handling too to compute the fixed
1288 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001289 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001290 // These five lines (& possibly the above member function pointer
1291 // handling) might be able to be refactored to use similar code in
1292 // CodeGenModule::getMemberPointerConstant
1293 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1294 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001295 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001296 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001297 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001298 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1299 TheCU, Name, TTy,
1300 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001301 } break;
1302 case TemplateArgument::NullPtr: {
1303 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001304 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001305 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001306 // Special case member data pointer null values since they're actually -1
1307 // instead of zero.
1308 if (const MemberPointerType *MPT =
1309 dyn_cast<MemberPointerType>(T.getTypePtr()))
1310 // But treat member function pointers as simple zero integers because
1311 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1312 // CodeGen grows handling for values of non-null member function
1313 // pointers then perhaps we could remove this special case and rely on
1314 // EmitNullMemberPointer for member function pointers.
1315 if (MPT->isMemberDataPointer())
1316 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1317 if (!V)
1318 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001319 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1320 TheCU, Name, TTy, cast<llvm::Constant>(V)));
David Blaikie38079fd2013-05-10 21:53:14 +00001321 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001322 case TemplateArgument::Template:
1323 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001324 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001325 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1326 break;
1327 case TemplateArgument::Pack:
1328 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1329 TheCU, Name, nullptr,
1330 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1331 break;
David Majnemer5559d472013-08-24 08:21:10 +00001332 case TemplateArgument::Expression: {
1333 const Expr *E = TA.getAsExpr();
1334 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001335 if (E->isGLValue())
1336 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001337 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001338 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001339 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001340 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1341 TheCU, Name, TTy, cast<llvm::Constant>(V->stripPointerCasts())));
David Majnemer5559d472013-08-24 08:21:10 +00001342 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001343 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001344 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001345 case TemplateArgument::Null:
1346 llvm_unreachable(
1347 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001348 }
1349 }
1350 return DBuilder.getOrCreateArray(TemplateParams);
1351}
1352
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001353llvm::DINodeArray
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001354CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001355 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001356 if (FD->getTemplatedKind() ==
1357 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001358 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1359 ->getTemplate()
1360 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001361 return CollectTemplateParams(
1362 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001363 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001364 return llvm::DINodeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001365}
1366
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001367llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1368 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001369 // Always get the full list of parameters, not just the ones from
1370 // the specialization.
1371 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001372 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001373 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001374 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001375}
1376
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001377llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001378 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001379 return VTablePtrType;
1380
1381 ASTContext &Context = CGM.getContext();
1382
1383 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001384 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001385 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
1386 llvm::DIType *SubTy = DBuilder.createSubroutineType(Unit, SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001387 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001388 llvm::DIType *vtbl_ptr_type =
Eric Christophere7b87e52014-10-26 23:40:33 +00001389 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001390 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1391 return VTablePtrType;
1392}
1393
Guy Benyei11169dd2012-12-18 14:30:41 +00001394StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001395 // Copy the gdb compatible name on the side and use its reference.
1396 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001397}
1398
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001399void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001400 SmallVectorImpl<llvm::Metadata *> &EltTys) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001401 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1402
1403 // If there is a primary base then it will hold vtable info.
1404 if (RL.getPrimaryBase())
1405 return;
1406
1407 // If this class is not dynamic then there is not any vtable info to collect.
1408 if (!RD->isDynamicClass())
1409 return;
1410
1411 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001412 llvm::DIType *VPTR = DBuilder.createMemberType(
Eric Christophere7b87e52014-10-26 23:40:33 +00001413 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001414 llvm::DINode::FlagArtificial, getOrCreateVTablePtrType(Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001415 EltTys.push_back(VPTR);
1416}
1417
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001418llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001419 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001420 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001421 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001422 return T;
1423}
1424
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001425llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001426 SourceLocation Loc) {
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001427 return getOrCreateStandaloneType(D, Loc);
1428}
1429
1430llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
1431 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001432 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001433 assert(!D.isNull() && "null type");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001434 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001435 assert(T && "could not create debug info for type");
Adrian Prantl3a884fa2015-08-27 22:56:46 +00001436
1437 // Composite types with UIDs were already retained by DIBuilder
1438 // because they are only referenced by name in the IR.
1439 if (auto *CTy = dyn_cast<llvm::DICompositeType>(T))
1440 if (!CTy->getIdentifier().empty())
1441 return T;
Adrian Prantl73409ce2013-03-11 18:33:46 +00001442 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001443 return T;
1444}
1445
David Blaikie483a9da2014-05-06 18:35:21 +00001446void CGDebugInfo::completeType(const EnumDecl *ED) {
1447 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1448 return;
1449 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001450 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001451 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001452 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001453 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001454 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001455 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001456 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001457}
1458
David Blaikieb2e86eb2013-08-15 20:49:17 +00001459void CGDebugInfo::completeType(const RecordDecl *RD) {
1460 if (DebugKind > CodeGenOptions::LimitedDebugInfo ||
1461 !CGM.getLangOpts().CPlusPlus)
1462 completeRequiredType(RD);
1463}
1464
1465void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
David Blaikie0856f662014-03-04 22:01:08 +00001466 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1467 return;
1468
David Blaikie6943dea2013-08-20 01:28:15 +00001469 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1470 if (CXXDecl->isDynamicClass())
1471 return;
1472
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001473 if (DebugTypeExtRefs && RD->isFromASTFile())
1474 return;
1475
David Blaikieb2e86eb2013-08-15 20:49:17 +00001476 QualType Ty = CGM.getContext().getRecordType(RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001477 llvm::DIType *T = getTypeOrNull(Ty);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001478 if (T && T->isForwardDecl())
David Blaikie6943dea2013-08-20 01:28:15 +00001479 completeClassData(RD);
1480}
1481
1482void CGDebugInfo::completeClassData(const RecordDecl *RD) {
1483 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001484 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001485 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001486 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001487 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001488 if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001489 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001490 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001491 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001492 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001493}
1494
David Blaikie0e716b42014-03-03 23:48:23 +00001495static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1496 CXXRecordDecl::method_iterator End) {
1497 for (; I != End; ++I)
1498 if (FunctionDecl *Tmpl = I->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001499 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
1500 !I->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001501 return true;
1502 return false;
1503}
1504
1505static bool shouldOmitDefinition(CodeGenOptions::DebugInfoKind DebugKind,
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001506 bool DebugTypeExtRefs,
David Blaikie0e716b42014-03-03 23:48:23 +00001507 const RecordDecl *RD,
1508 const LangOptions &LangOpts) {
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001509 // Does the type exist in an imported clang module?
Adrian Prantl5d66c6b2015-09-11 18:54:28 +00001510 if (DebugTypeExtRefs && RD->isFromASTFile() && RD->getDefinition())
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001511 return true;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001512
David Blaikie0e716b42014-03-03 23:48:23 +00001513 if (DebugKind > CodeGenOptions::LimitedDebugInfo)
1514 return false;
1515
1516 if (!LangOpts.CPlusPlus)
1517 return false;
1518
1519 if (!RD->isCompleteDefinitionRequired())
1520 return true;
1521
1522 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1523
1524 if (!CXXDecl)
1525 return false;
1526
1527 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass())
1528 return true;
1529
1530 TemplateSpecializationKind Spec = TSK_Undeclared;
1531 if (const ClassTemplateSpecializationDecl *SD =
1532 dyn_cast<ClassTemplateSpecializationDecl>(RD))
1533 Spec = SD->getSpecializationKind();
1534
1535 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1536 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1537 CXXDecl->method_end()))
1538 return true;
1539
1540 return false;
1541}
1542
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001543llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001544 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001545 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001546 if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD,
1547 CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001548 if (!T)
Adrian Prantl6ec370a2015-09-10 18:39:45 +00001549 T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001550 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001551 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001552
David Blaikieb2e86eb2013-08-15 20:49:17 +00001553 return CreateTypeDefinition(Ty);
1554}
1555
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001556llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00001557 RecordDecl *RD = Ty->getDecl();
1558
Guy Benyei11169dd2012-12-18 14:30:41 +00001559 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001560 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001561
1562 // Records and classes and unions can all be recursive. To handle them, we
1563 // first generate a debug descriptor for the struct as a forward declaration.
1564 // Then (if it is a definition) we go through and get debug info for all of
1565 // its members. Finally, we create a descriptor for the complete type (which
1566 // may refer to the forward decl if the struct is recursive) and replace all
1567 // uses of the forward declaration with the final definition.
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00001568 llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001569
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001570 const RecordDecl *D = RD->getDefinition();
1571 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001572 return FwdDecl;
1573
David Blaikieadfbf992013-08-18 16:55:33 +00001574 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1575 CollectContainingType(CXXDecl, FwdDecl);
1576
Guy Benyei11169dd2012-12-18 14:30:41 +00001577 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001578 LexicalBlockStack.emplace_back(&*FwdDecl);
1579 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001580
Guy Benyei11169dd2012-12-18 14:30:41 +00001581 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001582 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001583 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001584
1585 // Note: The split of CXXDecl information here is intentional, the
1586 // gdb tests will depend on a certain ordering at printout. The debug
1587 // information offsets are still correct if we merge them all together
1588 // though.
1589 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1590 if (CXXDecl) {
1591 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1592 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1593 }
1594
Eric Christopher91a31902013-01-16 01:22:32 +00001595 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001596 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001597 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001598 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001599
1600 LexicalBlockStack.pop_back();
1601 RegionMap.erase(Ty->getDecl());
1602
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001603 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001604 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001605
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001606 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001607 FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001608 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001609
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001610 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001611 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001612}
1613
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001614llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1615 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001616 // Ignore protocols.
1617 return getOrCreateType(Ty->getBaseType(), Unit);
1618}
1619
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001620/// \return true if Getter has the default name for the property PD.
1621static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1622 const ObjCMethodDecl *Getter) {
1623 assert(PD);
1624 if (!Getter)
1625 return true;
1626
1627 assert(Getter->getDeclName().isObjCZeroArgSelector());
1628 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001629 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001630}
1631
1632/// \return true if Setter has the default name for the property PD.
1633static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1634 const ObjCMethodDecl *Setter) {
1635 assert(PD);
1636 if (!Setter)
1637 return true;
1638
1639 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001640 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001641 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001642}
1643
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001644llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1645 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001646 ObjCInterfaceDecl *ID = Ty->getDecl();
1647 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001648 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001649
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001650 // Return a forward declaration if this type was imported from a clang module.
1651 if (DebugTypeExtRefs && ID->isFromASTFile() && ID->getDefinition())
1652 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
1653 ID->getName(),
1654 getDeclContextDescriptor(ID), Unit, 0);
1655
Guy Benyei11169dd2012-12-18 14:30:41 +00001656 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001657 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001658 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001659 auto RuntimeLang =
1660 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00001661
1662 // If this is just a forward declaration return a special forward-declaration
1663 // debug type since we won't be able to lay out the entire type.
1664 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001665 if (!Def || !Def->getImplementation()) {
Adrian Prantl42ce2d32015-10-01 16:57:02 +00001666 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001667 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
Adrian Prantl42ce2d32015-10-01 16:57:02 +00001668 llvm::dwarf::DW_TAG_structure_type, ID->getName(), Mod ? Mod : TheCU,
1669 DefUnit, Line, RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001670 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001671 return FwdDecl;
1672 }
1673
David Blaikieef8a9512014-05-05 23:23:53 +00001674 return CreateTypeDefinition(Ty, Unit);
1675}
1676
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001677llvm::DIModule *
Adrian Prantl66689202015-09-18 23:01:45 +00001678CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod,
1679 bool CreateSkeletonCU) {
Adrian Prantleb66a262015-09-24 16:10:04 +00001680 // Use the Module pointer as the key into the cache. This is a
1681 // nullptr if the "Module" is a PCH, which is safe because we don't
1682 // support chained PCH debug info, so there can only be a single PCH.
1683 const Module *M = Mod.getModuleOrNull();
Adrian Prantl9e8ea352015-09-29 20:44:46 +00001684 auto ModRef = ModuleCache.find(M);
1685 if (ModRef != ModuleCache.end())
1686 return cast<llvm::DIModule>(ModRef->second);
Adrian Prantl2388ead2015-06-30 18:01:05 +00001687
1688 // Macro definitions that were defined with "-D" on the command line.
1689 SmallString<128> ConfigMacros;
1690 {
1691 llvm::raw_svector_ostream OS(ConfigMacros);
1692 const auto &PPOpts = CGM.getPreprocessorOpts();
1693 unsigned I = 0;
1694 // Translate the macro definitions back into a commmand line.
1695 for (auto &M : PPOpts.Macros) {
1696 if (++I > 1)
1697 OS << " ";
1698 const std::string &Macro = M.first;
1699 bool Undef = M.second;
1700 OS << "\"-" << (Undef ? 'U' : 'D');
1701 for (char c : Macro)
1702 switch (c) {
1703 case '\\' : OS << "\\\\"; break;
1704 case '"' : OS << "\\\""; break;
1705 default: OS << c;
1706 }
1707 OS << '\"';
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001708 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001709 }
Adrian Prantl66689202015-09-18 23:01:45 +00001710
Adrian Prantl835e6632015-09-24 16:10:10 +00001711 bool IsRootModule = M ? !M->Parent : true;
1712 if (CreateSkeletonCU && IsRootModule) {
Adrian Prantl66689202015-09-18 23:01:45 +00001713 llvm::DIBuilder DIB(CGM.getModule());
Adrian Prantl835e6632015-09-24 16:10:10 +00001714 DIB.createCompileUnit(TheCU->getSourceLanguage(), Mod.getModuleName(),
Adrian Prantl6083b082015-09-24 16:10:00 +00001715 Mod.getPath(), TheCU->getProducer(), true,
1716 StringRef(), 0, Mod.getASTFile(),
1717 llvm::DIBuilder::FullDebug, Mod.getSignature());
Adrian Prantl66689202015-09-18 23:01:45 +00001718 DIB.finalize();
Adrian Prantl2f957ac2015-09-19 00:59:22 +00001719 }
Adrian Prantl835e6632015-09-24 16:10:10 +00001720 llvm::DIModule *Parent =
1721 IsRootModule ? nullptr
1722 : getOrCreateModuleRef(
1723 ExternalASTSource::ASTSourceDescriptor(*M->Parent),
1724 CreateSkeletonCU);
Adrian Prantleb66a262015-09-24 16:10:04 +00001725 llvm::DIModule *DIMod =
Adrian Prantl835e6632015-09-24 16:10:10 +00001726 DBuilder.createModule(Parent, Mod.getModuleName(), ConfigMacros,
1727 Mod.getPath(), CGM.getHeaderSearchOpts().Sysroot);
Adrian Prantl9e8ea352015-09-29 20:44:46 +00001728 ModuleCache[M].reset(DIMod);
Adrian Prantleb66a262015-09-24 16:10:04 +00001729 return DIMod;
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001730}
1731
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001732llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
1733 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00001734 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001735 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00001736 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001737 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001738
1739 // Bit size, align and offset of the type.
1740 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1741 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1742
1743 unsigned Flags = 0;
1744 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001745 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00001746
Adrian Prantlfd696112015-10-01 00:48:51 +00001747 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001748 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Adrian Prantlfd696112015-10-01 00:48:51 +00001749 Mod ? Mod : Unit, ID->getName(), DefUnit, Line, Size, Align, Flags,
1750 nullptr, llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001751
David Blaikieef8a9512014-05-05 23:23:53 +00001752 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001753 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001754
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001755 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001756 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001757 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001758
1759 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001760 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001761
1762 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1763 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001764 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001765 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001766 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001767 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001768
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001769 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001770 EltTys.push_back(InhTag);
1771 }
1772
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001773 // Create entries for all of the properties.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001774 for (const auto *PD : ID->properties()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001775 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001776 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001777 unsigned PLine = getLineNumber(Loc);
1778 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1779 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001780 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
1781 PD->getName(), PUnit, PLine,
1782 hasDefaultGetterName(PD, Getter) ? ""
1783 : getSelectorName(PD->getGetterName()),
1784 hasDefaultSetterName(PD, Setter) ? ""
1785 : getSelectorName(PD->getSetterName()),
1786 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001787 EltTys.push_back(PropertyNode);
1788 }
1789
1790 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1791 unsigned FieldNo = 0;
1792 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1793 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001794 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001795 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001796 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001797
Guy Benyei11169dd2012-12-18 14:30:41 +00001798 StringRef FieldName = Field->getName();
1799
1800 // Ignore unnamed fields.
1801 if (FieldName.empty())
1802 continue;
1803
1804 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001805 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001806 unsigned FieldLine = getLineNumber(Field->getLocation());
1807 QualType FType = Field->getType();
1808 uint64_t FieldSize = 0;
1809 unsigned FieldAlign = 0;
1810
1811 if (!FType->isIncompleteArrayType()) {
1812
1813 // Bit size, align and offset of the type.
1814 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001815 ? Field->getBitWidthValue(CGM.getContext())
1816 : CGM.getContext().getTypeSize(FType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001817 FieldAlign = CGM.getContext().getTypeAlign(FType);
1818 }
1819
1820 uint64_t FieldOffset;
1821 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1822 // We don't know the runtime offset of an ivar if we're using the
1823 // non-fragile ABI. For bitfields, use the bit offset into the first
1824 // byte of storage of the bitfield. For other fields, use zero.
1825 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001826 FieldOffset =
1827 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00001828 FieldOffset %= CGM.getContext().getCharWidth();
1829 } else {
1830 FieldOffset = 0;
1831 }
1832 } else {
1833 FieldOffset = RL.getFieldOffset(FieldNo);
1834 }
1835
1836 unsigned Flags = 0;
1837 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001838 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00001839 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001840 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001841 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001842 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00001843
Craig Topper8a13c412014-05-21 05:09:00 +00001844 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001845 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001846 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00001847 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001848 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00001849 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001850 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00001851 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001852 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1853 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001854 PropertyNode = DBuilder.createObjCProperty(
1855 PD->getName(), PUnit, PLine,
1856 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
1857 PD->getGetterName()),
1858 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
1859 PD->getSetterName()),
1860 PD->getPropertyAttributes(),
1861 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001862 }
1863 }
1864 }
Eric Christophere7b87e52014-10-26 23:40:33 +00001865 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
1866 FieldSize, FieldAlign, FieldOffset, Flags,
1867 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00001868 EltTys.push_back(FieldTy);
1869 }
1870
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001871 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001872 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00001873
Guy Benyei11169dd2012-12-18 14:30:41 +00001874 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001875 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001876}
1877
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001878llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
1879 llvm::DIFile *Unit) {
1880 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001881 int64_t Count = Ty->getNumElements();
1882 if (Count == 0)
1883 // If number of elements are not known then this is an unbounded array.
1884 // Use Count == -1 to express such arrays.
1885 Count = -1;
1886
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001887 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001888 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00001889
1890 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1891 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1892
1893 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
1894}
1895
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001896llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001897 uint64_t Size;
1898 uint64_t Align;
1899
1900 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
1901 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
1902 Size = 0;
1903 Align =
Eric Christophere7b87e52014-10-26 23:40:33 +00001904 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Guy Benyei11169dd2012-12-18 14:30:41 +00001905 } else if (Ty->isIncompleteArrayType()) {
1906 Size = 0;
1907 if (Ty->getElementType()->isIncompleteType())
1908 Align = 0;
1909 else
1910 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikief03b2e82013-05-09 20:48:12 +00001911 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001912 Size = 0;
1913 Align = 0;
1914 } else {
1915 // Size and align of the whole array, not the element type.
1916 Size = CGM.getContext().getTypeSize(Ty);
1917 Align = CGM.getContext().getTypeAlign(Ty);
1918 }
1919
1920 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1921 // interior arrays, do we care? Why aren't nested arrays represented the
1922 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001923 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001924 QualType EltTy(Ty, 0);
1925 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1926 // If the number of elements is known, then count is that number. Otherwise,
1927 // it's -1. This allows us to represent a subrange with an array of 0
1928 // elements, like this:
1929 //
1930 // struct foo {
1931 // int x[0];
1932 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00001933 int64_t Count = -1; // Count == -1 is an unbounded array.
Guy Benyei11169dd2012-12-18 14:30:41 +00001934 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1935 Count = CAT->getSize().getZExtValue();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001936
Guy Benyei11169dd2012-12-18 14:30:41 +00001937 // FIXME: Verify this is right for VLAs.
1938 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
1939 EltTy = Ty->getElementType();
1940 }
1941
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001942 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001943
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001944 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
1945 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00001946}
1947
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001948llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
1949 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001950 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
1951 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001952}
1953
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001954llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
1955 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001956 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
1957 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001958}
1959
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001960llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
1961 llvm::DIFile *U) {
David Majnemer9df56372015-09-10 21:52:00 +00001962 uint64_t Size =
1963 !Ty->isIncompleteType() ? CGM.getContext().getTypeSize(Ty) : 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001964 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00001965 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00001966 return DBuilder.createMemberPointerType(
David Majnemer34568bc2015-05-26 21:54:24 +00001967 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size);
Adrian Prantl0866acd2013-12-19 01:38:47 +00001968
1969 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00001970 Ty->getPointeeType()->getAs<FunctionProtoType>();
1971 return DBuilder.createMemberPointerType(
1972 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
1973 Ty->getClass(), FPT->getTypeQuals())),
1974 FPT, U),
David Majnemer34568bc2015-05-26 21:54:24 +00001975 ClassType, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +00001976}
1977
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001978llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001979 // Ignore the atomic wrapping
1980 // FIXME: What is the correct representation?
1981 return getOrCreateType(Ty->getValueType(), U);
1982}
1983
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001984llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00001985 const EnumDecl *ED = Ty->getDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001986
Guy Benyei11169dd2012-12-18 14:30:41 +00001987 uint64_t Size = 0;
1988 uint64_t Align = 0;
1989 if (!ED->getTypeForDecl()->isIncompleteType()) {
1990 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1991 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1992 }
1993
Manman Rene0064d82013-08-29 23:19:58 +00001994 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1995
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001996 bool isImportedFromModule =
1997 DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition();
1998
Guy Benyei11169dd2012-12-18 14:30:41 +00001999 // If this is just a forward declaration, construct an appropriately
2000 // marked node and just return it.
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002001 if (isImportedFromModule || !ED->getDefinition()) {
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002002 llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002003 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002004 unsigned Line = getLineNumber(ED->getLocation());
2005 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002006 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
David Blaikief427b002014-05-06 03:42:01 +00002007 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002008 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002009 ReplaceMap.emplace_back(
2010 std::piecewise_construct, std::make_tuple(Ty),
2011 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00002012 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00002013 }
2014
David Blaikie483a9da2014-05-06 18:35:21 +00002015 return CreateTypeDefinition(Ty);
2016}
2017
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002018llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00002019 const EnumDecl *ED = Ty->getDecl();
2020 uint64_t Size = 0;
2021 uint64_t Align = 0;
2022 if (!ED->getTypeForDecl()->isIncompleteType()) {
2023 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
2024 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
2025 }
2026
2027 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2028
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002029 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002030 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00002031 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00002032 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002033 Enumerators.push_back(DBuilder.createEnumerator(
2034 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002035 }
2036
2037 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002038 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00002039
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002040 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002041 unsigned Line = getLineNumber(ED->getLocation());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002042 llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002043 llvm::DIType *ClassTy =
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002044 ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
2045 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
2046 Line, Size, Align, EltArray, ClassTy,
2047 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002048}
2049
David Blaikie05491062013-01-21 04:37:12 +00002050static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
2051 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002052 do {
Adrian Prantl179af902013-09-26 21:35:50 +00002053 Qualifiers InnerQuals = T.getLocalQualifiers();
2054 // Qualifiers::operator+() doesn't like it if you add a Qualifier
2055 // that is already there.
2056 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2057 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002058 QualType LastT = T;
2059 switch (T->getTypeClass()) {
2060 default:
David Blaikie05491062013-01-21 04:37:12 +00002061 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002062 case Type::TemplateSpecialization: {
2063 const auto *Spec = cast<TemplateSpecializationType>(T);
2064 if (Spec->isTypeAlias())
2065 return C.getQualifiedType(T.getTypePtr(), Quals);
2066 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002067 break;
2068 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002069 case Type::TypeOfExpr:
2070 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2071 break;
2072 case Type::TypeOf:
2073 T = cast<TypeOfType>(T)->getUnderlyingType();
2074 break;
2075 case Type::Decltype:
2076 T = cast<DecltypeType>(T)->getUnderlyingType();
2077 break;
2078 case Type::UnaryTransform:
2079 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2080 break;
2081 case Type::Attributed:
2082 T = cast<AttributedType>(T)->getEquivalentType();
2083 break;
2084 case Type::Elaborated:
2085 T = cast<ElaboratedType>(T)->getNamedType();
2086 break;
2087 case Type::Paren:
2088 T = cast<ParenType>(T)->getInnerType();
2089 break;
David Blaikie05491062013-01-21 04:37:12 +00002090 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002091 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002092 break;
2093 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00002094 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002095 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002096 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002097 break;
2098 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002099
Guy Benyei11169dd2012-12-18 14:30:41 +00002100 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002101 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002102 } while (true);
2103}
2104
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002105llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002106
2107 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002108 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002109
David Blaikief427b002014-05-06 03:42:01 +00002110 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002111 if (it != TypeCache.end()) {
2112 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002113 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002114 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002115 }
2116
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002117 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002118}
2119
David Blaikie0e716b42014-03-03 23:48:23 +00002120void CGDebugInfo::completeTemplateDefinition(
2121 const ClassTemplateSpecializationDecl &SD) {
David Blaikie0856f662014-03-04 22:01:08 +00002122 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2123 return;
2124
David Blaikie0e716b42014-03-03 23:48:23 +00002125 completeClassData(&SD);
2126 // In case this type has no member function definitions being emitted, ensure
2127 // it is retained
2128 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2129}
2130
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002131llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002132 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002133 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002134
2135 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002136 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002137
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002138 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002139 return T;
2140
Adrian Prantlca844182015-09-11 17:23:03 +00002141 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002142 void* TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002143
2144 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002145 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002146
Guy Benyei11169dd2012-12-18 14:30:41 +00002147 return Res;
2148}
2149
Eric Christopher1ecc5632013-06-07 22:54:39 +00002150unsigned CGDebugInfo::Checksum(const ObjCInterfaceDecl *ID) {
Adrian Prantl817bbb32013-06-07 01:10:48 +00002151 // The assumption is that the number of ivars can only increase
2152 // monotonically, so it is safe to just use their current number as
2153 // a checksum.
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002154 unsigned Sum = 0;
2155 for (const ObjCIvarDecl *Ivar = ID->all_declared_ivar_begin();
Craig Topper8a13c412014-05-21 05:09:00 +00002156 Ivar != nullptr; Ivar = Ivar->getNextIvar())
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002157 ++Sum;
2158
2159 return Sum;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002160}
2161
2162ObjCInterfaceDecl *CGDebugInfo::getObjCInterfaceDecl(QualType Ty) {
2163 switch (Ty->getTypeClass()) {
2164 case Type::ObjCObjectPointer:
Eric Christophere7b87e52014-10-26 23:40:33 +00002165 return getObjCInterfaceDecl(
2166 cast<ObjCObjectPointerType>(Ty)->getPointeeType());
Adrian Prantla03a85a2013-03-06 22:03:30 +00002167 case Type::ObjCInterface:
2168 return cast<ObjCInterfaceType>(Ty)->getDecl();
2169 default:
Craig Topper8a13c412014-05-21 05:09:00 +00002170 return nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002171 }
2172}
2173
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002174llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
Adrian Prantl335f5c72015-10-02 17:36:14 +00002175 // A forward declaration inside a module header does not belong to the module.
2176 if (isa<RecordDecl>(D) && !cast<RecordDecl>(D)->getDefinition())
2177 return nullptr;
Adrian Prantl85d938a2015-09-21 17:48:37 +00002178 if (DebugTypeExtRefs && D->isFromASTFile()) {
2179 // Record a reference to an imported clang module or precompiled header.
2180 auto *Reader = CGM.getContext().getExternalSource();
2181 auto Idx = D->getOwningModuleID();
2182 auto Info = Reader->getSourceDescriptor(Idx);
2183 if (Info)
2184 return getOrCreateModuleRef(*Info, /*SkeletonCU=*/true);
2185 } else if (ClangModuleMap) {
Adrian Prantl9402cef2015-09-20 16:51:35 +00002186 // We are building a clang module or a precompiled header.
2187 //
2188 // TODO: When D is a CXXRecordDecl or a C++ Enum, the ODR applies
2189 // and it wouldn't be necessary to specify the parent scope
2190 // because the type is already unique by definition (it would look
2191 // like the output of -fno-standalone-debug). On the other hand,
2192 // the parent scope helps a consumer to quickly locate the object
2193 // file where the type's definition is located, so it might be
2194 // best to make this behavior a command line or debugger tuning
2195 // option.
2196 FullSourceLoc Loc(D->getLocation(), CGM.getContext().getSourceManager());
2197 if (Module *M = ClangModuleMap->inferModuleFromLocation(Loc)) {
2198 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
2199 return getOrCreateModuleRef(Info, /*SkeletonCU=*/false);
2200 }
2201 }
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002202
Adrian Prantl9402cef2015-09-20 16:51:35 +00002203 return nullptr;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002204}
2205
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002206llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002207 // Handle qualifiers, which recursively handles what they refer to.
2208 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002209 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002210
Guy Benyei11169dd2012-12-18 14:30:41 +00002211 // Work out details of type.
2212 switch (Ty->getTypeClass()) {
2213#define TYPE(Class, Base)
2214#define ABSTRACT_TYPE(Class, Base)
2215#define NON_CANONICAL_TYPE(Class, Base)
2216#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2217#include "clang/AST/TypeNodes.def"
2218 llvm_unreachable("Dependent types cannot show up in debug information");
2219
2220 case Type::ExtVector:
2221 case Type::Vector:
2222 return CreateType(cast<VectorType>(Ty), Unit);
2223 case Type::ObjCObjectPointer:
2224 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2225 case Type::ObjCObject:
2226 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2227 case Type::ObjCInterface:
2228 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2229 case Type::Builtin:
2230 return CreateType(cast<BuiltinType>(Ty));
2231 case Type::Complex:
2232 return CreateType(cast<ComplexType>(Ty));
2233 case Type::Pointer:
2234 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002235 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002236 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002237 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002238 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002239 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002240 case Type::BlockPointer:
2241 return CreateType(cast<BlockPointerType>(Ty), Unit);
2242 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002243 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002244 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002245 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002246 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002247 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002248 case Type::FunctionProto:
2249 case Type::FunctionNoProto:
2250 return CreateType(cast<FunctionType>(Ty), Unit);
2251 case Type::ConstantArray:
2252 case Type::VariableArray:
2253 case Type::IncompleteArray:
2254 return CreateType(cast<ArrayType>(Ty), Unit);
2255
2256 case Type::LValueReference:
2257 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2258 case Type::RValueReference:
2259 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2260
2261 case Type::MemberPointer:
2262 return CreateType(cast<MemberPointerType>(Ty), Unit);
2263
2264 case Type::Atomic:
2265 return CreateType(cast<AtomicType>(Ty), Unit);
2266
Guy Benyei11169dd2012-12-18 14:30:41 +00002267 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002268 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2269
David Blaikie42edade2014-11-11 20:44:45 +00002270 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002271 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002272 case Type::Elaborated:
2273 case Type::Paren:
2274 case Type::SubstTemplateTypeParm:
2275 case Type::TypeOfExpr:
2276 case Type::TypeOf:
2277 case Type::Decltype:
2278 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002279 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002280 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002281 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002282
David Blaikie42edade2014-11-11 20:44:45 +00002283 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002284}
2285
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002286llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2287 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002288 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002289
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002290 auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002291
2292 // We may have cached a forward decl when we could have created
2293 // a non-forward decl. Go ahead and create a non-forward decl
2294 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002295 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002296 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002297
2298 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002299 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002300
2301 // Propagate members from the declaration to the definition
2302 // CreateType(const RecordType*) will overwrite this with the members in the
2303 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002304 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002305
Guy Benyei11169dd2012-12-18 14:30:41 +00002306 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002307 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002308 return Res;
2309}
2310
2311// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002312llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002313 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002314
Guy Benyei11169dd2012-12-18 14:30:41 +00002315 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002316 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002317 unsigned Line = getLineNumber(RD->getLocation());
2318 StringRef RDName = getClassName(RD);
2319
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002320 llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002321
David Blaikied2785892013-08-18 17:36:19 +00002322 // If we ended up creating the type during the context chain construction,
2323 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002324 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002325 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002326 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002327 return T;
David Blaikied2785892013-08-18 17:36:19 +00002328
Adrian Prantl381e7552014-02-04 21:29:50 +00002329 // If this is just a forward or incomplete declaration, construct an
2330 // appropriately marked node and just return it.
2331 const RecordDecl *D = RD->getDefinition();
2332 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002333 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002334
2335 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2336 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002337
Manman Rene0064d82013-08-29 23:19:58 +00002338 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2339
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002340 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002341 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align, 0,
2342 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002343
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002344 RegionMap[Ty->getDecl()].reset(RealDecl);
2345 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002346
David Blaikieadfbf992013-08-18 16:55:33 +00002347 if (const ClassTemplateSpecializationDecl *TSpecial =
2348 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002349 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002350 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002351 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002352}
2353
David Blaikieadfbf992013-08-18 16:55:33 +00002354void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002355 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002356 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002357 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002358 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2359 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002360 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002361 while (1) {
2362 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2363 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2364 if (PBT && !BRL.isPrimaryBaseVirtual())
2365 PBase = PBT;
2366 else
2367 break;
2368 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002369 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002370 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2371 getOrCreateFile(RD->getLocation())));
2372 } else if (RD->isDynamicClass())
2373 ContainingType = RealDecl;
2374
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002375 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002376}
2377
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002378llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002379 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002380 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002381 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2382 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002383 llvm::DIType *Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002384 FieldAlign, *Offset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002385 *Offset += FieldSize;
2386 return Ty;
2387}
2388
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002389void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002390 StringRef &Name,
2391 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002392 llvm::DIScope *&FDContext,
2393 llvm::DINodeArray &TParamsArray,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002394 unsigned &Flags) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002395 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2396 Name = getFunctionName(FD);
2397 // Use mangled name as linkage name for C/C++ functions.
2398 if (FD->hasPrototype()) {
2399 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002400 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002401 }
2402 // No need to replicate the linkage name if it isn't different from the
2403 // subprogram name, no need to have it at all unless coverage is enabled or
2404 // debug is set to more than just line tables.
2405 if (LinkageName == Name ||
2406 (!CGM.getCodeGenOpts().EmitGcovArcs &&
2407 !CGM.getCodeGenOpts().EmitGcovNotes &&
2408 DebugKind <= CodeGenOptions::DebugLineTablesOnly))
2409 LinkageName = StringRef();
2410
2411 if (DebugKind >= CodeGenOptions::LimitedDebugInfo) {
2412 if (const NamespaceDecl *NSDecl =
2413 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2414 FDContext = getOrCreateNameSpace(NSDecl);
2415 else if (const RecordDecl *RDecl =
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002416 dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) {
2417 llvm::DIScope *Mod = getParentModuleOrNull(RDecl);
2418 FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU);
2419 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002420 // Collect template parameters.
2421 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2422 }
2423}
2424
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002425void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002426 unsigned &LineNo, QualType &T,
2427 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002428 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002429 Unit = getOrCreateFile(VD->getLocation());
2430 LineNo = getLineNumber(VD->getLocation());
2431
2432 setLocation(VD->getLocation());
2433
2434 T = VD->getType();
2435 if (T->isIncompleteArrayType()) {
2436 // CodeGen turns int[] into int[1] so we'll do the same here.
2437 llvm::APInt ConstVal(32, 1);
2438 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2439
2440 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2441 ArrayType::Normal, 0);
2442 }
2443
2444 Name = VD->getName();
2445 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2446 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2447 LinkageName = CGM.getMangledName(VD);
2448 if (LinkageName == Name)
2449 LinkageName = StringRef();
2450
2451 // Since we emit declarations (DW_AT_members) for static members, place the
2452 // definition of those static members in the namespace they were declared in
2453 // in the source code (the lexical decl context).
2454 // FIXME: Generalize this for even non-member global variables where the
2455 // declaration and definition may have different lexical decl contexts, once
2456 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002457 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2458 : VD->getDeclContext();
2459 // When a record type contains an in-line initialization of a static data
2460 // member, and the record type is marked as __declspec(dllexport), an implicit
2461 // definition of the member will be created in the record context. DWARF
2462 // doesn't seem to have a nice way to describe this in a form that consumers
2463 // are likely to understand, so fake the "normal" situation of a definition
2464 // outside the class by putting it in the global scope.
2465 if (DC->isRecord())
2466 DC = CGM.getContext().getTranslationUnitDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002467
2468 llvm::DIScope *Mod = getParentModuleOrNull(VD);
2469 VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
Frederic Riss9db79f12014-11-18 03:40:46 +00002470}
2471
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002472llvm::DISubprogram *
Frederic Rissd253ed62014-11-18 03:40:51 +00002473CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002474 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00002475 StringRef Name, LinkageName;
2476 unsigned Flags = 0;
2477 SourceLocation Loc = FD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002478 llvm::DIFile *Unit = getOrCreateFile(Loc);
2479 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002480 unsigned Line = getLineNumber(Loc);
2481
2482 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2483 TParamsArray, Flags);
2484 // Build function type.
2485 SmallVector<QualType, 16> ArgTypes;
2486 for (const ParmVarDecl *Parm: FD->parameters())
2487 ArgTypes.push_back(Parm->getType());
2488 QualType FnType =
2489 CGM.getContext().getFunctionType(FD->getReturnType(), ArgTypes,
2490 FunctionProtoType::ExtProtoInfo());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002491 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002492 DContext, Name, LinkageName, Unit, Line,
2493 getOrCreateFunctionType(FD, FnType, Unit), !FD->isExternallyVisible(),
Duncan P. N. Exon Smith31d38f72015-08-26 22:21:09 +00002494 /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize, nullptr,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002495 TParamsArray.get(), getFunctionDeclaration(FD));
Frederic Rissd253ed62014-11-18 03:40:51 +00002496 const FunctionDecl *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002497 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2498 std::make_tuple(CanonDecl),
2499 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00002500 return SP;
2501}
2502
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002503llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00002504CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2505 QualType T;
2506 StringRef Name, LinkageName;
2507 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002508 llvm::DIFile *Unit = getOrCreateFile(Loc);
2509 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002510 unsigned Line = getLineNumber(Loc);
2511
2512 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002513 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
2514 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
2515 !VD->isExternallyVisible(), nullptr, nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002516 FwdDeclReplaceMap.emplace_back(
2517 std::piecewise_construct,
2518 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2519 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002520 return GV;
2521}
2522
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002523llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002524 // We only need a declaration (not a definition) of the type - so use whatever
2525 // we would otherwise do to get a type for a pointee. (forward declarations in
2526 // limited debug info, full definitions (if the type definition is available)
2527 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002528 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2529 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002530 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002531 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002532
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002533 if (I != DeclCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002534 return dyn_cast_or_null<llvm::DINode>(I->second);
Frederic Rissd253ed62014-11-18 03:40:51 +00002535
2536 // No definition for now. Emit a forward definition that might be
2537 // merged with a potential upcoming definition.
2538 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
2539 return getFunctionForwardDeclaration(FD);
2540 else if (const auto *VD = dyn_cast<VarDecl>(D))
2541 return getGlobalVariableForwardDeclaration(VD);
2542
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002543 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00002544}
2545
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002546llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Diego Novillo913690c2014-06-24 17:02:17 +00002547 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002548 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00002549
Guy Benyei11169dd2012-12-18 14:30:41 +00002550 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002551 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002552 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002553
2554 // Setup context.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002555 auto *S = getDeclContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00002556
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002557 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002558 if (MI == SPCache.end()) {
Eric Christopherf86c4052013-08-28 23:12:10 +00002559 if (const CXXMethodDecl *MD =
2560 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00002561 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002562 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00002563 }
2564 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002565 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002566 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002567 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002568 return SP;
2569 }
2570
Aaron Ballman86c93902014-03-06 23:45:36 +00002571 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002572 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002573 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002574 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002575 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002576 return SP;
2577 }
2578 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002579 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002580}
2581
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002582// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00002583// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002584llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002585 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002586 llvm::DIFile *F) {
Diego Novillo913690c2014-06-24 17:02:17 +00002587 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002588 // Create fake but valid subroutine type. Otherwise -verify would fail, and
2589 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Manman Ren67f005e2014-07-28 22:24:34 +00002590 return DBuilder.createSubroutineType(F,
2591 DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002592
2593 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2594 return getOrCreateMethodType(Method, F);
2595 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2596 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002597 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002598
2599 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002600 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002601
2602 // Replace the instancetype keyword with the actual type.
2603 if (ResultTy == CGM.getContext().getObjCInstanceType())
2604 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002605 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002606
Adrian Prantl7bec9032013-05-10 21:08:31 +00002607 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002608 // "self" pointer is always first argument.
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002609 QualType SelfDeclTy;
2610 if (auto *SelfDecl = OMethod->getSelfDecl())
2611 SelfDeclTy = SelfDecl->getType();
2612 else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType))
2613 if (FPT->getNumParams() > 1)
2614 SelfDeclTy = FPT->getParamType(0);
2615 if (!SelfDeclTy.isNull())
2616 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002617 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002618 Elts.push_back(DBuilder.createArtificialType(
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002619 getOrCreateType(CGM.getContext().getObjCSelType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002620 // Get rest of the arguments.
Aaron Ballman43b68be2014-03-07 17:50:17 +00002621 for (const auto *PI : OMethod->params())
2622 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002623 // Variadic methods need a special marker at the end of the type list.
2624 if (OMethod->isVariadic())
2625 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002626
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002627 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002628 return DBuilder.createSubroutineType(F, EltTypeArray);
2629 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002630
Adrian Prantl800faef2014-02-25 23:42:18 +00002631 // Handle variadic function types; they need an additional
2632 // unspecified parameter.
Adrian Prantld45ba252014-02-25 19:38:11 +00002633 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2634 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002635 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00002636 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2637 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2638 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2639 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2640 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002641 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Adrian Prantld45ba252014-02-25 19:38:11 +00002642 return DBuilder.createSubroutineType(F, EltTypeArray);
2643 }
2644
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002645 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002646}
2647
Eric Christophere7b87e52014-10-26 23:40:33 +00002648void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2649 SourceLocation ScopeLoc, QualType FnType,
2650 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002651
2652 StringRef Name;
2653 StringRef LinkageName;
2654
2655 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2656
2657 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002658 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00002659
Guy Benyei11169dd2012-12-18 14:30:41 +00002660 unsigned Flags = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002661 llvm::DIFile *Unit = getOrCreateFile(Loc);
2662 llvm::DIScope *FDContext = Unit;
2663 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00002664 if (!HasDecl) {
2665 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00002666 LinkageName = Fn->getName();
Guy Benyei11169dd2012-12-18 14:30:41 +00002667 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002668 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002669 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002670 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002671 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002672 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002673 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002674 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002675 return;
2676 }
2677 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002678 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2679 TParamsArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002680 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2681 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002682 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002683 } else {
2684 // Use llvm function name.
2685 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002686 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002687 }
2688 if (!Name.empty() && Name[0] == '\01')
2689 Name = Name.substr(1);
2690
Adrian Prantl42d71b92014-04-10 23:21:53 +00002691 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002692 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl42d71b92014-04-10 23:21:53 +00002693 // Artificial functions without a location should not silently reuse CurLoc.
2694 if (Loc.isInvalid())
2695 CurLoc = SourceLocation();
2696 }
2697 unsigned LineNo = getLineNumber(Loc);
2698 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002699
Eric Christopher8018e412014-03-27 18:50:35 +00002700 // FIXME: The function declaration we're constructing here is mostly reusing
2701 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2702 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2703 // all subprograms instead of the actual context since subprogram definitions
2704 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002705 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00002706 FDContext, Name, LinkageName, Unit, LineNo,
2707 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
2708 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize, Fn,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002709 TParamsArray.get(), getFunctionDeclaration(D));
Frederic Rissb1ab28c2014-11-05 19:19:04 +00002710 // We might get here with a VarDecl in the case we're generating
2711 // code for the initialization of globals. Do not record these decls
2712 // as they will overwrite the actual VarDecl Decl in the cache.
2713 if (HasDecl && isa<FunctionDecl>(D))
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002714 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(SP));
Guy Benyei11169dd2012-12-18 14:30:41 +00002715
Adrian Prantlbebb8932014-03-21 21:01:58 +00002716 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002717 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00002718
Guy Benyei11169dd2012-12-18 14:30:41 +00002719 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002720 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002721}
2722
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002723void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
2724 QualType FnType) {
2725 StringRef Name;
2726 StringRef LinkageName;
2727
2728 const Decl *D = GD.getDecl();
2729 if (!D)
2730 return;
2731
2732 unsigned Flags = 0;
2733 llvm::DIFile *Unit = getOrCreateFile(Loc);
Adrian Prantlf0cd6772015-10-04 23:23:04 +00002734 llvm::DIScope *FDContext = getDeclContextDescriptor(D);
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002735 llvm::DINodeArray TParamsArray;
2736 if (isa<FunctionDecl>(D)) {
2737 // If there is a DISubprogram for this function available then use it.
2738 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2739 TParamsArray, Flags);
2740 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2741 Name = getObjCMethodName(OMD);
2742 Flags |= llvm::DINode::FlagPrototyped;
2743 } else {
2744 llvm_unreachable("not a function or ObjC method");
2745 }
2746 if (!Name.empty() && Name[0] == '\01')
2747 Name = Name.substr(1);
2748
2749 if (D->isImplicit()) {
2750 Flags |= llvm::DINode::FlagArtificial;
2751 // Artificial functions without a location should not silently reuse CurLoc.
2752 if (Loc.isInvalid())
2753 CurLoc = SourceLocation();
2754 }
2755 unsigned LineNo = getLineNumber(Loc);
2756 unsigned ScopeLine = 0;
2757
2758 DBuilder.createFunction(FDContext, Name, LinkageName, Unit, LineNo,
2759 getOrCreateFunctionType(D, FnType, Unit),
2760 false /*internalLinkage*/, true /*definition*/,
2761 ScopeLine, Flags, CGM.getLangOpts().Optimize, nullptr,
2762 TParamsArray.get(),
2763 getFunctionDeclaration(D));
2764}
2765
David Blaikie835afb22015-01-21 23:08:17 +00002766void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002767 // Update our current location
2768 setLocation(Loc);
2769
Eric Christophere7b87e52014-10-26 23:40:33 +00002770 if (CurLoc.isInvalid() || CurLoc.isMacroID())
2771 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002772
Adrian Prantle83b1302014-01-07 22:05:52 +00002773 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00002774 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
David Blaikie835afb22015-01-21 23:08:17 +00002775 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002776}
2777
Guy Benyei11169dd2012-12-18 14:30:41 +00002778void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002779 llvm::MDNode *Back = nullptr;
2780 if (!LexicalBlockStack.empty())
2781 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002782 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002783 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002784 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002785}
2786
Eric Christopher0fdcb312013-05-16 00:52:20 +00002787void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2788 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002789 // Set our current location.
2790 setLocation(Loc);
2791
Guy Benyei11169dd2012-12-18 14:30:41 +00002792 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00002793 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2794 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00002795
2796 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2797 return;
2798
2799 // Create a new lexical block and push it on the stack.
2800 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002801}
2802
Eric Christopher0fdcb312013-05-16 00:52:20 +00002803void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2804 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002805 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2806
2807 // Provide an entry in the line table for the end of the block.
2808 EmitLocation(Builder, Loc);
2809
David Blaikie60a877b2014-10-22 19:34:33 +00002810 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2811 return;
2812
Guy Benyei11169dd2012-12-18 14:30:41 +00002813 LexicalBlockStack.pop_back();
2814}
2815
Guy Benyei11169dd2012-12-18 14:30:41 +00002816void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2817 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2818 unsigned RCount = FnBeginRegionCount.back();
2819 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2820
2821 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00002822 while (LexicalBlockStack.size() != RCount) {
2823 // Provide an entry in the line table for the end of the block.
2824 EmitLocation(Builder, CurLoc);
2825 LexicalBlockStack.pop_back();
2826 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002827 FnBeginRegionCount.pop_back();
2828}
2829
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002830llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002831 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002832
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002833 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002834 QualType FType;
2835 uint64_t FieldSize, FieldOffset;
2836 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002837
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002838 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002839 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002840
2841 FieldOffset = 0;
2842 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2843 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2844 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2845 FType = CGM.getContext().IntTy;
2846 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2847 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2848
2849 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2850 if (HasCopyAndDispose) {
2851 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002852 EltTys.push_back(
2853 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
2854 EltTys.push_back(
2855 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00002856 }
2857 bool HasByrefExtendedLayout;
2858 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00002859 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
2860 HasByrefExtendedLayout) &&
2861 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00002862 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002863 EltTys.push_back(
2864 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00002865 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002866
Guy Benyei11169dd2012-12-18 14:30:41 +00002867 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2868 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002869 CGM.getTarget().getPointerAlign(0))) {
2870 CharUnits FieldOffsetInBytes =
2871 CGM.getContext().toCharUnitsFromBits(FieldOffset);
2872 CharUnits AlignedOffsetInBytes =
2873 FieldOffsetInBytes.RoundUpToAlignment(Align);
2874 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002875
Guy Benyei11169dd2012-12-18 14:30:41 +00002876 if (NumPaddingBytes.isPositive()) {
2877 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
2878 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2879 pad, ArrayType::Normal, 0);
2880 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
2881 }
2882 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002883
Guy Benyei11169dd2012-12-18 14:30:41 +00002884 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002885 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002886 FieldSize = CGM.getContext().getTypeSize(FType);
2887 FieldAlign = CGM.getContext().toBits(Align);
2888
Eric Christopherb2a008c2013-05-16 00:45:12 +00002889 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00002890 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
2891 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002892 EltTys.push_back(FieldTy);
2893 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002894
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002895 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002896
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002897 unsigned Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002898
Guy Benyei11169dd2012-12-18 14:30:41 +00002899 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002900 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002901}
2902
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002903void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::Value *Storage,
2904 llvm::Optional<unsigned> ArgNo,
Eric Christophere7b87e52014-10-26 23:40:33 +00002905 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002906 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002907 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2908
David Blaikie7fceebf2013-08-19 03:37:48 +00002909 bool Unwritten =
2910 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
2911 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002912 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00002913 if (!Unwritten)
2914 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002915 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00002916 uint64_t XOffset = 0;
2917 if (VD->hasAttr<BlocksAttr>())
2918 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002919 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002920 Ty = getOrCreateType(VD->getType(), Unit);
2921
2922 // If there is no debug info for this type then do not emit debug info
2923 // for this variable.
2924 if (!Ty)
2925 return;
2926
Guy Benyei11169dd2012-12-18 14:30:41 +00002927 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00002928 unsigned Line = 0;
2929 unsigned Column = 0;
2930 if (!Unwritten) {
2931 Line = getLineNumber(VD->getLocation());
2932 Column = getColumnNumber(VD->getLocation());
2933 }
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002934 SmallVector<int64_t, 9> Expr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002935 unsigned Flags = 0;
2936 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002937 Flags |= llvm::DINode::FlagArtificial;
Guy Benyei11169dd2012-12-18 14:30:41 +00002938 // If this is the first argument and it is implicit then
2939 // give it an object pointer flag.
2940 // FIXME: There has to be a better way to do this, but for static
2941 // functions there won't be an implicit param at arg1 and
2942 // otherwise it is 'self' or 'this'.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002943 if (isa<ImplicitParamDecl>(VD) && ArgNo && *ArgNo == 1)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002944 Flags |= llvm::DINode::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00002945 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00002946 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
2947 !VD->getType()->isPointerType())
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002948 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei11169dd2012-12-18 14:30:41 +00002949
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002950 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00002951
2952 StringRef Name = VD->getName();
2953 if (!Name.empty()) {
2954 if (VD->hasAttr<BlocksAttr>()) {
2955 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002956 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002957 // offset of __forwarding field
2958 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002959 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002960 Expr.push_back(offset.getQuantity());
2961 Expr.push_back(llvm::dwarf::DW_OP_deref);
2962 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002963 // offset of x field
2964 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002965 Expr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002966
2967 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002968 auto *D = ArgNo
2969 ? DBuilder.createParameterVariable(Scope, VD->getName(),
2970 *ArgNo, Unit, Line, Ty)
2971 : DBuilder.createAutoVariable(Scope, VD->getName(), Unit,
2972 Line, Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002973
Guy Benyei11169dd2012-12-18 14:30:41 +00002974 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002975 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2976 llvm::DebugLoc::get(Line, Column, Scope),
2977 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002978 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00002979 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002980 Expr.push_back(llvm::dwarf::DW_OP_deref);
Adrian Prantl0d820892015-04-29 15:05:50 +00002981 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2982 // If VD is an anonymous union then Storage represents value for
2983 // all union fields.
2984 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
2985 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00002986 // GDB has trouble finding local variables in anonymous unions, so we emit
2987 // artifical local variables for each of the members.
2988 //
2989 // FIXME: Remove this code as soon as GDB supports this.
2990 // The debug info verifier in LLVM operates based on the assumption that a
2991 // variable has the same size as its storage and we had to disable the check
2992 // for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00002993 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002994 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00002995 StringRef FieldName = Field->getName();
2996
2997 // Ignore unnamed fields. Do not ignore unnamed records.
2998 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2999 continue;
3000
3001 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003002 auto *D = DBuilder.createAutoVariable(
3003 Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
3004 Flags | llvm::DINode::FlagArtificial);
Adrian Prantl0d820892015-04-29 15:05:50 +00003005
3006 // Insert an llvm.dbg.declare into the current block.
3007 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3008 llvm::DebugLoc::get(Line, Column, Scope),
3009 Builder.GetInsertBlock());
3010 }
Adrian Prantl0d820892015-04-29 15:05:50 +00003011 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003012 }
David Blaikiea76a7c92013-01-05 05:58:35 +00003013
3014 // Create the descriptor for the variable.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003015 auto *D =
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003016 ArgNo
3017 ? DBuilder.createParameterVariable(Scope, Name, *ArgNo, Unit, Line,
3018 Ty, CGM.getLangOpts().Optimize,
3019 Flags)
3020 : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
3021 CGM.getLangOpts().Optimize, Flags);
David Blaikiea76a7c92013-01-05 05:58:35 +00003022
3023 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003024 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3025 llvm::DebugLoc::get(Line, Column, Scope),
3026 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003027}
3028
3029void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
3030 llvm::Value *Storage,
3031 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003032 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003033 EmitDeclare(VD, Storage, llvm::None, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003034}
3035
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003036llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
3037 llvm::DIType *Ty) {
3038 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003039 if (CachedTy)
3040 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00003041 return DBuilder.createObjectPointerType(Ty);
3042}
3043
Eric Christophere7b87e52014-10-26 23:40:33 +00003044void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
3045 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00003046 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Eric Christopher75e17682013-05-16 00:45:23 +00003047 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003048 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00003049
Craig Topper8a13c412014-05-21 05:09:00 +00003050 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00003051 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003052
Guy Benyei11169dd2012-12-18 14:30:41 +00003053 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00003054
Guy Benyei11169dd2012-12-18 14:30:41 +00003055 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003056 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
3057 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003058 if (isByRef)
3059 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003060 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003061 Ty = getOrCreateType(VD->getType(), Unit);
3062
3063 // Self is passed along as an implicit non-arg variable in a
3064 // block. Mark it as the object pointer.
3065 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00003066 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00003067
3068 // Get location information.
3069 unsigned Line = getLineNumber(VD->getLocation());
3070 unsigned Column = getColumnNumber(VD->getLocation());
3071
3072 const llvm::DataLayout &target = CGM.getDataLayout();
3073
3074 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00003075 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00003076 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
3077
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003078 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00003079 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003080 addr.push_back(llvm::dwarf::DW_OP_deref);
3081 addr.push_back(llvm::dwarf::DW_OP_plus);
3082 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003083 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003084 addr.push_back(llvm::dwarf::DW_OP_deref);
3085 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003086 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00003087 offset =
3088 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003089 addr.push_back(offset.getQuantity());
3090 addr.push_back(llvm::dwarf::DW_OP_deref);
3091 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003092 // offset of x field
3093 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003094 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003095 }
3096
3097 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003098 auto *D = DBuilder.createAutoVariable(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003099 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003100 Line, Ty);
Adrian Prantl0f6df002013-03-29 19:20:35 +00003101
Guy Benyei11169dd2012-12-18 14:30:41 +00003102 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003103 auto DL = llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back());
3104 if (InsertPoint)
3105 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3106 InsertPoint);
3107 else
3108 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3109 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003110}
3111
Guy Benyei11169dd2012-12-18 14:30:41 +00003112void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3113 unsigned ArgNo,
3114 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003115 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003116 EmitDeclare(VD, AI, ArgNo, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003117}
3118
3119namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003120struct BlockLayoutChunk {
3121 uint64_t OffsetInBits;
3122 const BlockDecl::Capture *Capture;
3123};
3124bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3125 return l.OffsetInBits < r.OffsetInBits;
3126}
Guy Benyei11169dd2012-12-18 14:30:41 +00003127}
3128
3129void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003130 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003131 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003132 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003133 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003134 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003135 ASTContext &C = CGM.getContext();
3136 const BlockDecl *blockDecl = block.getBlockDecl();
3137
3138 // Collect some general information about the block's location.
3139 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003140 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003141 unsigned line = getLineNumber(loc);
3142 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003143
Guy Benyei11169dd2012-12-18 14:30:41 +00003144 // Build the debug-info type for the block literal.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003145 getDeclContextDescriptor(blockDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003146
3147 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003148 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003149
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003150 SmallVector<llvm::Metadata *, 16> fields;
Guy Benyei11169dd2012-12-18 14:30:41 +00003151 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
3152 blockLayout->getElementOffsetInBits(0),
3153 tunit, tunit));
3154 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
3155 blockLayout->getElementOffsetInBits(1),
3156 tunit, tunit));
3157 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
3158 blockLayout->getElementOffsetInBits(2),
3159 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003160 auto *FnTy = block.getBlockExpr()->getFunctionType();
3161 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
3162 fields.push_back(createFieldType("__FuncPtr", FnPtrType, 0, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003163 blockLayout->getElementOffsetInBits(3),
3164 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003165 fields.push_back(createFieldType(
3166 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3167 ? C.getBlockDescriptorExtendedType()
3168 : C.getBlockDescriptorType()),
3169 0, loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003170
3171 // We want to sort the captures by offset, not because DWARF
3172 // requires this, but because we're paranoid about debuggers.
3173 SmallVector<BlockLayoutChunk, 8> chunks;
3174
3175 // 'this' capture.
3176 if (blockDecl->capturesCXXThis()) {
3177 BlockLayoutChunk chunk;
3178 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003179 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003180 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003181 chunks.push_back(chunk);
3182 }
3183
3184 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003185 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003186 const VarDecl *variable = capture.getVariable();
3187 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3188
3189 // Ignore constant captures.
3190 if (captureInfo.isConstant())
3191 continue;
3192
3193 BlockLayoutChunk chunk;
3194 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003195 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003196 chunk.Capture = &capture;
3197 chunks.push_back(chunk);
3198 }
3199
3200 // Sort by offset.
3201 llvm::array_pod_sort(chunks.begin(), chunks.end());
3202
Eric Christophere7b87e52014-10-26 23:40:33 +00003203 for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
3204 e = chunks.end();
3205 i != e; ++i) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003206 uint64_t offsetInBits = i->OffsetInBits;
3207 const BlockDecl::Capture *capture = i->Capture;
3208
3209 // If we have a null capture, this must be the C++ 'this' capture.
3210 if (!capture) {
3211 const CXXMethodDecl *method =
Eric Christophere7b87e52014-10-26 23:40:33 +00003212 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00003213 QualType type = method->getThisType(C);
3214
3215 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
3216 offsetInBits, tunit, tunit));
3217 continue;
3218 }
3219
3220 const VarDecl *variable = capture->getVariable();
3221 StringRef name = variable->getName();
3222
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003223 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003224 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003225 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003226
3227 // FIXME: this creates a second copy of this type!
3228 uint64_t xoffset;
3229 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003230 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3231 fieldType =
3232 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3233 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003234 } else {
Eric Christophere7b87e52014-10-26 23:40:33 +00003235 fieldType = createFieldType(name, variable->getType(), 0, loc, AS_public,
3236 offsetInBits, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003237 }
3238 fields.push_back(fieldType);
3239 }
3240
3241 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003242 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3243 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003244
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003245 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003246
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003247 llvm::DIType *type = DBuilder.createStructType(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003248 tunit, typeName.str(), tunit, line,
3249 CGM.getContext().toBits(block.BlockSize),
3250 CGM.getContext().toBits(block.BlockAlign), 0, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003251 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3252
3253 // Get overall information about the block.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003254 unsigned flags = llvm::DINode::FlagArtificial;
3255 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003256
3257 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003258 auto *debugVar = DBuilder.createParameterVariable(
3259 scope, Arg->getName(), ArgNo, tunit, line, type,
3260 CGM.getLangOpts().Optimize, flags);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003261
Adrian Prantl616bef42013-03-14 21:52:59 +00003262 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003263 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003264 DBuilder.insertDbgValueIntrinsic(
Eric Christophere7b87e52014-10-26 23:40:33 +00003265 LocalAddr, 0, debugVar, DBuilder.createExpression(),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003266 llvm::DebugLoc::get(line, column, scope), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003267 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003268
Adrian Prantl616bef42013-03-14 21:52:59 +00003269 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003270 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
3271 llvm::DebugLoc::get(line, column, scope),
3272 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003273}
3274
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003275llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003276CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3277 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003278 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003279
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003280 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003281 if (MI != StaticDataMemberCache.end()) {
3282 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00003283 return MI->second;
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003284 }
David Blaikiece763042013-08-20 21:49:21 +00003285
3286 // If the member wasn't found in the cache, lazily construct and add it to the
3287 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003288 auto DC = D->getDeclContext();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003289 auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003290 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003291}
3292
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003293llvm::DIGlobalVariable *CGDebugInfo::CollectAnonRecordDecls(
3294 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3295 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
3296 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003297
3298 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003299 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003300 StringRef FieldName = Field->getName();
3301
3302 // Ignore unnamed fields, but recurse into anonymous records.
3303 if (FieldName.empty()) {
3304 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3305 if (RT)
3306 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3307 Var, DContext);
3308 continue;
3309 }
3310 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003311 GV = DBuilder.createGlobalVariable(DContext, FieldName, LinkageName, Unit,
3312 LineNo, FieldTy,
3313 Var->hasInternalLinkage(), Var, nullptr);
Eric Christophercab9fae2014-04-10 05:20:00 +00003314 }
3315 return GV;
3316}
3317
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003318void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003319 const VarDecl *D) {
Eric Christopher75e17682013-05-16 00:45:23 +00003320 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003321 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003322 llvm::DIFile *Unit = nullptr;
3323 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003324 unsigned LineNo;
3325 StringRef DeclName, LinkageName;
3326 QualType T;
3327 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003328
3329 // Attempt to store one global variable for the declaration - even if we
3330 // emit a lot of fields.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003331 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003332
3333 // If this is an anonymous union then we'll want to emit a global
3334 // variable for each member of the anonymous union so that it's possible
3335 // to find the name of any field in the union.
3336 if (T->isUnionType() && DeclName.empty()) {
3337 const RecordDecl *RD = cast<RecordType>(T)->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003338 assert(RD->isAnonymousStructOrUnion() &&
3339 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003340 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3341 } else {
David Blaikie7550b112014-10-20 17:42:23 +00003342 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003343 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3344 Var->hasInternalLinkage(), Var,
3345 getOrCreateStaticDataMemberDeclarationOrNull(D));
3346 }
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003347 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(GV));
Guy Benyei11169dd2012-12-18 14:30:41 +00003348}
3349
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003350void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3351 llvm::Constant *Init) {
Eric Christopher75e17682013-05-16 00:45:23 +00003352 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003353 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003354 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003355 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003356 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003357 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3358 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3359 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3360 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3361 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003362 // Do not use global variables for enums.
3363 //
3364 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003365 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003366 return;
David Blaikiea15565562014-04-04 20:56:17 +00003367 // Do not emit separate definitions for function local const/statics.
3368 if (isa<FunctionDecl>(VD->getDeclContext()))
3369 return;
David Blaikiebb113912014-04-05 07:23:17 +00003370 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003371 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003372 if (VarD->isStaticDataMember()) {
3373 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003374 getDeclContextDescriptor(VarD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003375 // Ensure that the type is retained even though it's otherwise unreferenced.
3376 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003377 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003378 return;
3379 }
3380
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003381 llvm::DIScope *DContext = getDeclContextDescriptor(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003382
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003383 auto &GV = DeclCache[VD];
3384 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003385 return;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003386 GV.reset(DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003387 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003388 true, Init, getOrCreateStaticDataMemberDeclarationOrNull(VarD)));
David Blaikiebd483762013-05-20 04:58:53 +00003389}
3390
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003391llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003392 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003393 return LexicalBlockStack.back();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00003394 llvm::DIScope *Mod = getParentModuleOrNull(D);
3395 return getContextDescriptor(D, Mod ? Mod : TheCU);
Guy Benyei11169dd2012-12-18 14:30:41 +00003396}
3397
David Blaikie9f88fe82013-04-22 06:13:21 +00003398void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
David Blaikiebd483762013-05-20 04:58:53 +00003399 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3400 return;
David Blaikie9f88fe82013-04-22 06:13:21 +00003401 DBuilder.createImportedModule(
David Blaikiebd483762013-05-20 04:58:53 +00003402 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3403 getOrCreateNameSpace(UD.getNominatedNamespace()),
David Blaikie9f88fe82013-04-22 06:13:21 +00003404 getLineNumber(UD.getLocation()));
3405}
3406
David Blaikiebd483762013-05-20 04:58:53 +00003407void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
3408 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3409 return;
3410 assert(UD.shadow_size() &&
3411 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3412 // Emitting one decl is sufficient - debuggers can detect that this is an
3413 // overloaded name & provide lookup for all the overloads.
3414 const UsingShadowDecl &USD = **UD.shadow_begin();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003415 if (llvm::DINode *Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003416 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003417 DBuilder.createImportedDeclaration(
3418 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3419 getLineNumber(USD.getLocation()));
3420}
3421
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003422void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
Adrian Prantlc6458d62015-09-19 00:10:32 +00003423 auto Info = ExternalASTSource::ASTSourceDescriptor(*ID.getImportedModule());
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003424 DBuilder.createImportedDeclaration(
Adrian Prantl66689202015-09-18 23:01:45 +00003425 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
3426 getOrCreateModuleRef(Info, DebugTypeExtRefs),
3427 getLineNumber(ID.getLocation()));
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003428}
3429
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003430llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00003431CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
3432 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003433 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003434 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003435 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003436 return cast<llvm::DIImportedEntity>(VH);
3437 llvm::DIImportedEntity *R;
David Blaikief121b932013-05-20 22:50:41 +00003438 if (const NamespaceAliasDecl *Underlying =
3439 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3440 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003441 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003442 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3443 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3444 NA.getName());
3445 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003446 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003447 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3448 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3449 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003450 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003451 return R;
3452}
3453
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003454llvm::DINamespace *
Guy Benyei11169dd2012-12-18 14:30:41 +00003455CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003456 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003457 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003458 if (I != NameSpaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003459 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003460
Guy Benyei11169dd2012-12-18 14:30:41 +00003461 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003462 llvm::DIFile *FileD = getOrCreateFile(NSDecl->getLocation());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003463 llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003464 llvm::DINamespace *NS =
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003465 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003466 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003467 return NS;
3468}
3469
Adrian Prantla5206ce2015-09-22 23:26:43 +00003470void CGDebugInfo::setDwoId(uint64_t Signature) {
3471 assert(TheCU && "no main compile unit");
3472 TheCU->setDWOId(Signature);
3473}
3474
3475
Guy Benyei11169dd2012-12-18 14:30:41 +00003476void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003477 // Creating types might create further types - invalidating the current
3478 // element and the size(), so don't cache/reference them.
3479 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3480 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003481 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003482 ? CreateTypeDefinition(E.Type, E.Unit)
3483 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003484 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00003485 }
3486
David Blaikief427b002014-05-06 03:42:01 +00003487 for (auto p : ReplaceMap) {
3488 assert(p.second);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003489 auto *Ty = cast<llvm::DIType>(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003490 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003491
David Blaikief427b002014-05-06 03:42:01 +00003492 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003493 assert(it != TypeCache.end());
3494 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003495
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003496 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
3497 cast<llvm::DIType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00003498 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003499
Frederic Rissd253ed62014-11-18 03:40:51 +00003500 for (const auto &p : FwdDeclReplaceMap) {
3501 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003502 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003503 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003504
3505 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003506 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003507 // with ourselves, that will destroy the temporary MDNode and
3508 // replace it with a standard one, avoiding leaking memory.
3509 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003510 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003511 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003512 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003513
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003514 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00003515 }
3516
Adrian Prantl73409ce2013-03-11 18:33:46 +00003517 // We keep our own list of retained types, because we need to look
3518 // up the final type in the type cache.
Adrian Prantl3a884fa2015-08-27 22:56:46 +00003519 for (auto &RT : RetainedTypes)
Adrian Prantlad9a195e2015-08-27 21:21:19 +00003520 if (auto MD = TypeCache[RT])
3521 DBuilder.retainType(cast<llvm::DIType>(MD));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003522
Guy Benyei11169dd2012-12-18 14:30:41 +00003523 DBuilder.finalize();
3524}
David Blaikie66088d52014-09-24 17:01:27 +00003525
3526void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
3527 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3528 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003529
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003530 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003531 // Don't ignore in case of explicit cast where it is referenced indirectly.
3532 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00003533}