blob: 638b2d46fcfea5fd31d2801935503fffa8f7da14 [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 Majnemerb4b671e2016-06-30 03:01:59 +000016#include "CGRecordLayout.h"
David Blaikie38079fd2013-05-10 21:53:14 +000017#include "CGCXXABI.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000018#include "CGObjCRuntime.h"
19#include "CodeGenFunction.h"
20#include "CodeGenModule.h"
21#include "clang/AST/ASTContext.h"
22#include "clang/AST/DeclFriend.h"
23#include "clang/AST/DeclObjC.h"
24#include "clang/AST/DeclTemplate.h"
25#include "clang/AST/Expr.h"
26#include "clang/AST/RecordLayout.h"
27#include "clang/Basic/FileManager.h"
28#include "clang/Basic/SourceManager.h"
29#include "clang/Basic/Version.h"
Saleem Abdulrasool10a49722016-04-08 16:52:00 +000030#include "clang/Frontend/CodeGenOptions.h"
Adrian Prantlc4bb47e2015-06-30 17:39:51 +000031#include "clang/Lex/HeaderSearchOptions.h"
Adrian Prantl9402cef2015-09-20 16:51:35 +000032#include "clang/Lex/ModuleMap.h"
Adrian Prantlc4bb47e2015-06-30 17:39:51 +000033#include "clang/Lex/PreprocessorOptions.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000034#include "llvm/ADT/SmallVector.h"
35#include "llvm/ADT/StringExtras.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000036#include "llvm/IR/Constants.h"
37#include "llvm/IR/DataLayout.h"
38#include "llvm/IR/DerivedTypes.h"
39#include "llvm/IR/Instructions.h"
40#include "llvm/IR/Intrinsics.h"
41#include "llvm/IR/Module.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000042#include "llvm/Support/FileSystem.h"
Adrian Prantl0630eb72013-12-18 21:48:18 +000043#include "llvm/Support/Path.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000044using namespace clang;
45using namespace clang::CodeGen;
46
47CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Eric Christopher324bbbd2013-07-14 21:12:44 +000048 : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
Adrian Prantl6b21ab22015-08-27 19:46:20 +000049 DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs),
Eric Christopher324bbbd2013-07-14 21:12:44 +000050 DBuilder(CGM.getModule()) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +000051 for (const auto &KV : CGM.getCodeGenOpts().DebugPrefixMap)
52 DebugPrefixMap[KV.first] = KV.second;
Guy Benyei11169dd2012-12-18 14:30:41 +000053 CreateCompileUnit();
54}
55
56CGDebugInfo::~CGDebugInfo() {
57 assert(LexicalBlockStack.empty() &&
58 "Region stack mismatch, stack not empty!");
59}
60
David Blaikie66e41972015-01-14 07:38:27 +000061ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
David Blaikie835afb22015-01-21 23:08:17 +000062 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000063 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +000064 init(TemporaryLocation);
65}
66
Adrian Prantl39428e72015-02-03 18:40:42 +000067ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
Adrian Prantl95b24e92015-02-03 20:00:54 +000068 bool DefaultToEmpty,
Adrian Prantl39428e72015-02-03 18:40:42 +000069 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000070 : CGF(&CGF) {
Adrian Prantl95b24e92015-02-03 20:00:54 +000071 init(TemporaryLocation, DefaultToEmpty);
Adrian Prantl39428e72015-02-03 18:40:42 +000072}
73
74void ApplyDebugLocation::init(SourceLocation TemporaryLocation,
Adrian Prantl95b24e92015-02-03 20:00:54 +000075 bool DefaultToEmpty) {
David Blaikied7057d92015-08-12 23:49:57 +000076 auto *DI = CGF->getDebugInfo();
77 if (!DI) {
78 CGF = nullptr;
79 return;
David Blaikie66e41972015-01-14 07:38:27 +000080 }
David Blaikied7057d92015-08-12 23:49:57 +000081
82 OriginalLocation = CGF->Builder.getCurrentDebugLocation();
83 if (TemporaryLocation.isValid()) {
84 DI->EmitLocation(CGF->Builder, TemporaryLocation);
85 return;
86 }
87
88 if (DefaultToEmpty) {
89 CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc());
90 return;
91 }
92
93 // Construct a location that has a valid scope, but no line info.
94 assert(!DI->LexicalBlockStack.empty());
95 CGF->Builder.SetCurrentDebugLocation(
96 llvm::DebugLoc::get(0, 0, DI->LexicalBlockStack.back()));
Adrian Prantl2e0637f2013-07-18 00:28:02 +000097}
98
David Blaikie9b479662015-01-25 01:19:10 +000099ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E)
David Blaikied7057d92015-08-12 23:49:57 +0000100 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +0000101 init(E->getExprLoc());
102}
103
David Blaikie66e41972015-01-14 07:38:27 +0000104ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc)
David Blaikied7057d92015-08-12 23:49:57 +0000105 : CGF(&CGF) {
106 if (!CGF.getDebugInfo()) {
107 this->CGF = nullptr;
108 return;
David Blaikie66e41972015-01-14 07:38:27 +0000109 }
David Blaikied7057d92015-08-12 23:49:57 +0000110 OriginalLocation = CGF.Builder.getCurrentDebugLocation();
111 if (Loc)
112 CGF.Builder.SetCurrentDebugLocation(std::move(Loc));
David Blaikie66e41972015-01-14 07:38:27 +0000113}
114
115ApplyDebugLocation::~ApplyDebugLocation() {
116 // Query CGF so the location isn't overwritten when location updates are
117 // temporarily disabled (for C++ default function arguments)
David Blaikied7057d92015-08-12 23:49:57 +0000118 if (CGF)
119 CGF->Builder.SetCurrentDebugLocation(std::move(OriginalLocation));
David Blaikie66e41972015-01-14 07:38:27 +0000120}
121
Guy Benyei11169dd2012-12-18 14:30:41 +0000122void CGDebugInfo::setLocation(SourceLocation Loc) {
123 // If the new location isn't valid return.
Eric Christophere7b87e52014-10-26 23:40:33 +0000124 if (Loc.isInvalid())
125 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000126
127 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
128
129 // If we've changed files in the middle of a lexical scope go ahead
130 // and create a new lexical scope with file node if it's different
131 // from the one in the scope.
Eric Christophere7b87e52014-10-26 23:40:33 +0000132 if (LexicalBlockStack.empty())
133 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000134
135 SourceManager &SM = CGM.getContext().getSourceManager();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000136 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +0000137 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000138
Duncan P. N. Exon Smith373ee852015-04-16 01:36:36 +0000139 if (PCLoc.isInvalid() || Scope->getFilename() == PCLoc.getFilename())
Guy Benyei11169dd2012-12-18 14:30:41 +0000140 return;
141
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000142 if (auto *LBF = dyn_cast<llvm::DILexicalBlockFile>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000143 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000144 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlockFile(
145 LBF->getScope(), getOrCreateFile(CurLoc)));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000146 } else if (isa<llvm::DILexicalBlock>(Scope) ||
147 isa<llvm::DISubprogram>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000148 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000149 LexicalBlockStack.emplace_back(
150 DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000151 }
152}
153
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000154llvm::DIScope *CGDebugInfo::getDeclContextDescriptor(const Decl *D) {
Adrian Prantl5c8bd882015-09-11 17:23:08 +0000155 llvm::DIScope *Mod = getParentModuleOrNull(D);
156 return getContextDescriptor(cast<Decl>(D->getDeclContext()),
157 Mod ? Mod : TheCU);
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000158}
159
160llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context,
161 llvm::DIScope *Default) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000162 if (!Context)
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000163 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000164
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000165 auto I = RegionMap.find(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +0000166 if (I != RegionMap.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000167 llvm::Metadata *V = I->second;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000168 return dyn_cast_or_null<llvm::DIScope>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000169 }
170
171 // Check namespace.
172 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000173 return getOrCreateNameSpace(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +0000174
David Blaikiebfa52742013-04-19 06:56:38 +0000175 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context))
176 if (!RDecl->isDependentType())
177 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Eric Christophere7b87e52014-10-26 23:40:33 +0000178 getOrCreateMainFile());
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000179 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000180}
181
Guy Benyei11169dd2012-12-18 14:30:41 +0000182StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000183 assert(FD && "Invalid FunctionDecl!");
Guy Benyei11169dd2012-12-18 14:30:41 +0000184 IdentifierInfo *FII = FD->getIdentifier();
Eric Christophere7b87e52014-10-26 23:40:33 +0000185 FunctionTemplateSpecializationInfo *Info =
186 FD->getTemplateSpecializationInfo();
Reid Kleckner60103382015-12-16 02:04:40 +0000187
Reid Kleckner829398e2016-06-17 16:11:20 +0000188 if (!Info && FII)
Guy Benyei11169dd2012-12-18 14:30:41 +0000189 return FII->getName();
190
191 // Otherwise construct human readable name for debug info.
Benjamin Kramer9170e912013-02-22 15:46:01 +0000192 SmallString<128> NS;
193 llvm::raw_svector_ostream OS(NS);
Reid Kleckner60103382015-12-16 02:04:40 +0000194 PrintingPolicy Policy(CGM.getLangOpts());
Reid Kleckner829398e2016-06-17 16:11:20 +0000195 Policy.MSVCFormatting = CGM.getCodeGenOpts().EmitCodeView;
Guy Benyei11169dd2012-12-18 14:30:41 +0000196
Reid Kleckner829398e2016-06-17 16:11:20 +0000197 // Print the unqualified name with some template arguments.
198 FD->printName(OS);
199 // Add any template specialization args.
200 if (Info) {
201 const TemplateArgumentList *TArgs = Info->TemplateArguments;
202 const TemplateArgument *Args = TArgs->data();
203 unsigned NumArgs = TArgs->size();
204 TemplateSpecializationType::PrintTemplateArgumentList(OS, Args, NumArgs,
205 Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000206 }
207
208 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000209 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000210}
211
212StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
213 SmallString<256> MethodName;
214 llvm::raw_svector_ostream OS(MethodName);
215 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
216 const DeclContext *DC = OMD->getDeclContext();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000217 if (const ObjCImplementationDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000218 dyn_cast<const ObjCImplementationDecl>(DC)) {
219 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000220 } else if (const ObjCInterfaceDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000221 dyn_cast<const ObjCInterfaceDecl>(DC)) {
222 OS << OID->getName();
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000223 } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(DC)) {
224 if (OC->IsClassExtension()) {
225 OS << OC->getClassInterface()->getName();
226 } else {
227 OS << ((const NamedDecl *)OC)->getIdentifier()->getNameStart() << '('
228 << OC->getIdentifier()->getNameStart() << ')';
229 }
Eric Christopherb2a008c2013-05-16 00:45:12 +0000230 } else if (const ObjCCategoryImplDecl *OCD =
Eric Christophere7b87e52014-10-26 23:40:33 +0000231 dyn_cast<const ObjCCategoryImplDecl>(DC)) {
232 OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '('
233 << OCD->getIdentifier()->getNameStart() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000234 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000235 // We can extract the type of the class from the self pointer.
Eric Christophere7b87e52014-10-26 23:40:33 +0000236 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000237 QualType ClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000238 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000239 ClassTy.print(OS, PrintingPolicy(LangOptions()));
240 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000241 }
242 OS << ' ' << OMD->getSelector().getAsString() << ']';
243
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000244 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000245}
246
Guy Benyei11169dd2012-12-18 14:30:41 +0000247StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000248 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000249}
250
Eric Christophere7b87e52014-10-26 23:40:33 +0000251StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
David Blaikie65813a32014-04-02 18:21:09 +0000252 // quick optimization to avoid having to intern strings that are already
253 // stored reliably elsewhere
254 if (!isa<ClassTemplateSpecializationDecl>(RD))
Guy Benyei11169dd2012-12-18 14:30:41 +0000255 return RD->getName();
256
David Blaikie65813a32014-04-02 18:21:09 +0000257 SmallString<128> Name;
Benjamin Kramer9170e912013-02-22 15:46:01 +0000258 {
David Blaikie65813a32014-04-02 18:21:09 +0000259 llvm::raw_svector_ostream OS(Name);
260 RD->getNameForDiagnostic(OS, CGM.getContext().getPrintingPolicy(),
261 /*Qualified*/ false);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000262 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000263
264 // Copy this name on the side and use its reference.
David Blaikie65813a32014-04-02 18:21:09 +0000265 return internString(Name);
Guy Benyei11169dd2012-12-18 14:30:41 +0000266}
267
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000268llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000269 if (!Loc.isValid())
270 // If Location is not valid then use main input file.
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000271 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
272 remapDIPath(TheCU->getDirectory()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000273
274 SourceManager &SM = CGM.getContext().getSourceManager();
275 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
276
277 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
278 // If the location is not valid then use main input file.
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000279 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
280 remapDIPath(TheCU->getDirectory()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000281
282 // Cache the results.
283 const char *fname = PLoc.getFilename();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000284 auto it = DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000285
286 if (it != DIFileCache.end()) {
287 // Verify that the information still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000288 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000289 return cast<llvm::DIFile>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000290 }
291
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000292 llvm::DIFile *F = DBuilder.createFile(remapDIPath(PLoc.getFilename()),
293 remapDIPath(getCurrentDirname()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000294
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000295 DIFileCache[fname].reset(F);
Guy Benyei11169dd2012-12-18 14:30:41 +0000296 return F;
297}
298
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000299llvm::DIFile *CGDebugInfo::getOrCreateMainFile() {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000300 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
301 remapDIPath(TheCU->getDirectory()));
302}
303
304std::string CGDebugInfo::remapDIPath(StringRef Path) const {
305 for (const auto &Entry : DebugPrefixMap)
306 if (Path.startswith(Entry.first))
307 return (Twine(Entry.second) + Path.substr(Entry.first.size())).str();
308 return Path.str();
Guy Benyei11169dd2012-12-18 14:30:41 +0000309}
310
Guy Benyei11169dd2012-12-18 14:30:41 +0000311unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
312 if (Loc.isInvalid() && CurLoc.isInvalid())
313 return 0;
314 SourceManager &SM = CGM.getContext().getSourceManager();
315 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000316 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000317}
318
Adrian Prantlc7822422013-03-12 20:43:25 +0000319unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000320 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000321 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000322 return 0;
323
324 // If the location is invalid then use the current column.
325 if (Loc.isInvalid() && CurLoc.isInvalid())
326 return 0;
327 SourceManager &SM = CGM.getContext().getSourceManager();
328 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000329 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000330}
331
332StringRef CGDebugInfo::getCurrentDirname() {
333 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
334 return CGM.getCodeGenOpts().DebugCompilationDir;
335
336 if (!CWDName.empty())
337 return CWDName;
338 SmallString<256> CWD;
339 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000340 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000341}
342
Guy Benyei11169dd2012-12-18 14:30:41 +0000343void CGDebugInfo::CreateCompileUnit() {
344
David Blaikieaabde052014-05-14 00:29:00 +0000345 // Should we be asking the SourceManager for the main file name, instead of
346 // accepting it as an argument? This just causes the main file name to
347 // mismatch with source locations and create extra lexical scopes or
348 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
349 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
350 // because that's what the SourceManager says)
351
Guy Benyei11169dd2012-12-18 14:30:41 +0000352 // Get absolute path name.
353 SourceManager &SM = CGM.getContext().getSourceManager();
354 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
355 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000356 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000357
358 // The main file name provided via the "-main-file-name" option contains just
359 // the file name itself with no path information. This file name may have had
360 // a relative path, so we look into the actual file entry for the main
361 // file to determine the real absolute path for the file.
362 std::string MainFileDir;
363 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000364 MainFileDir = remapDIPath(MainFile->getDir()->getName());
Yaron Keren9fb7e902013-10-21 20:07:37 +0000365 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000366 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
367 llvm::sys::path::append(MainFileDirSS, MainFileName);
368 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000369 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000370 }
371
Ed Masteda706022014-05-07 12:49:30 +0000372 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000373 const LangOptions &LO = CGM.getLangOpts();
374 if (LO.CPlusPlus) {
375 if (LO.ObjC1)
376 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
377 else
378 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
379 } else if (LO.ObjC1) {
380 LangTag = llvm::dwarf::DW_LANG_ObjC;
Pirama Arumuga Nainara7484c92016-06-21 21:35:11 +0000381 } else if (LO.RenderScript) {
382 LangTag = llvm::dwarf::DW_LANG_GOOGLE_RenderScript;
Guy Benyei11169dd2012-12-18 14:30:41 +0000383 } else if (LO.C99) {
384 LangTag = llvm::dwarf::DW_LANG_C99;
385 } else {
386 LangTag = llvm::dwarf::DW_LANG_C89;
387 }
388
389 std::string Producer = getClangFullVersion();
390
391 // Figure out which version of the ObjC runtime we have.
392 unsigned RuntimeVers = 0;
393 if (LO.ObjC1)
394 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
395
Adrian Prantl826824e2016-04-08 22:43:06 +0000396 llvm::DICompileUnit::DebugEmissionKind EmissionKind;
397 switch (DebugKind) {
398 case codegenoptions::NoDebugInfo:
399 case codegenoptions::LocTrackingOnly:
400 EmissionKind = llvm::DICompileUnit::NoDebug;
401 break;
402 case codegenoptions::DebugLineTablesOnly:
403 EmissionKind = llvm::DICompileUnit::LineTablesOnly;
404 break;
405 case codegenoptions::LimitedDebugInfo:
406 case codegenoptions::FullDebugInfo:
407 EmissionKind = llvm::DICompileUnit::FullDebug;
408 break;
409 }
410
Guy Benyei11169dd2012-12-18 14:30:41 +0000411 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000412 // FIXME - Eliminate TheCU.
Eric Christophere4200a22014-02-27 01:25:08 +0000413 TheCU = DBuilder.createCompileUnit(
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000414 LangTag, remapDIPath(MainFileName), remapDIPath(getCurrentDirname()),
415 Producer, LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers,
Adrian Prantl826824e2016-04-08 22:43:06 +0000416 CGM.getCodeGenOpts().SplitDwarfFile, EmissionKind, 0 /* DWOid */);
Guy Benyei11169dd2012-12-18 14:30:41 +0000417}
418
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000419llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000420 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000421 StringRef BTName;
422 switch (BT->getKind()) {
423#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000424#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000425#include "clang/AST/BuiltinTypes.def"
426 case BuiltinType::Dependent:
427 llvm_unreachable("Unexpected builtin type");
428 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000429 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000430 case BuiltinType::Void:
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000431 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +0000432 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000433 if (!ClassTy)
434 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
435 "objc_class", TheCU,
436 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000437 return ClassTy;
438 case BuiltinType::ObjCId: {
439 // typedef struct objc_class *Class;
440 // typedef struct objc_object {
441 // Class isa;
442 // } *id;
443
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000444 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000445 return ObjTy;
446
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000447 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000448 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
449 "objc_class", TheCU,
450 getOrCreateMainFile(), 0);
451
452 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000453
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000454 auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000455
Eric Christopher5c7ee8b2013-04-02 22:59:11 +0000456 ObjTy =
David Blaikie6d4fe152013-02-25 01:07:08 +0000457 DBuilder.createStructType(TheCU, "objc_object", getOrCreateMainFile(),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000458 0, 0, 0, 0, nullptr, llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000459
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000460 DBuilder.replaceArrays(
461 ObjTy,
462 DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
463 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0, 0, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000464 return ObjTy;
465 }
466 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000467 if (!SelTy)
468 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
469 "objc_selector", TheCU,
470 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000471 return SelTy;
472 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000473
Alexey Bader954ba212016-04-08 13:40:33 +0000474#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
475 case BuiltinType::Id: \
476 return getOrCreateStructPtrType("opencl_" #ImgType "_" #Suffix "_t", \
477 SingletonId);
Alexey Baderb62f1442016-04-13 08:33:41 +0000478#include "clang/Basic/OpenCLImageTypes.def"
Guy Benyei61054192013-02-07 10:55:47 +0000479 case BuiltinType::OCLSampler:
Eric Christophere7b87e52014-10-26 23:40:33 +0000480 return DBuilder.createBasicType(
481 "opencl_sampler_t", CGM.getContext().getTypeSize(BT),
482 CGM.getContext().getTypeAlign(BT), llvm::dwarf::DW_ATE_unsigned);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000483 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000484 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000485 case BuiltinType::OCLClkEvent:
486 return getOrCreateStructPtrType("opencl_clk_event_t", OCLClkEventDITy);
487 case BuiltinType::OCLQueue:
488 return getOrCreateStructPtrType("opencl_queue_t", OCLQueueDITy);
489 case BuiltinType::OCLNDRange:
490 return getOrCreateStructPtrType("opencl_ndrange_t", OCLNDRangeDITy);
491 case BuiltinType::OCLReserveID:
492 return getOrCreateStructPtrType("opencl_reserve_id_t", OCLReserveIDDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000493
Guy Benyei11169dd2012-12-18 14:30:41 +0000494 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000495 case BuiltinType::Char_U:
496 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
497 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000498 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000499 case BuiltinType::SChar:
500 Encoding = llvm::dwarf::DW_ATE_signed_char;
501 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000502 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000503 case BuiltinType::Char32:
504 Encoding = llvm::dwarf::DW_ATE_UTF;
505 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000506 case BuiltinType::UShort:
507 case BuiltinType::UInt:
508 case BuiltinType::UInt128:
509 case BuiltinType::ULong:
510 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000511 case BuiltinType::ULongLong:
512 Encoding = llvm::dwarf::DW_ATE_unsigned;
513 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000514 case BuiltinType::Short:
515 case BuiltinType::Int:
516 case BuiltinType::Int128:
517 case BuiltinType::Long:
518 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000519 case BuiltinType::LongLong:
520 Encoding = llvm::dwarf::DW_ATE_signed;
521 break;
522 case BuiltinType::Bool:
523 Encoding = llvm::dwarf::DW_ATE_boolean;
524 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000525 case BuiltinType::Half:
526 case BuiltinType::Float:
527 case BuiltinType::LongDouble:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000528 case BuiltinType::Float128:
Eric Christophere7b87e52014-10-26 23:40:33 +0000529 case BuiltinType::Double:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000530 // FIXME: For targets where long double and __float128 have the same size,
531 // they are currently indistinguishable in the debugger without some
532 // special treatment. However, there is currently no consensus on encoding
533 // and this should be updated once a DWARF encoding exists for distinct
534 // floating point types of the same size.
Eric Christophere7b87e52014-10-26 23:40:33 +0000535 Encoding = llvm::dwarf::DW_ATE_float;
536 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000537 }
538
539 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000540 case BuiltinType::Long:
541 BTName = "long int";
542 break;
543 case BuiltinType::LongLong:
544 BTName = "long long int";
545 break;
546 case BuiltinType::ULong:
547 BTName = "long unsigned int";
548 break;
549 case BuiltinType::ULongLong:
550 BTName = "long long unsigned int";
551 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000552 default:
553 BTName = BT->getName(CGM.getLangOpts());
554 break;
555 }
556 // Bit size, align and offset of the type.
557 uint64_t Size = CGM.getContext().getTypeSize(BT);
558 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000559 return DBuilder.createBasicType(BTName, Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000560}
561
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000562llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000563 // Bit size, align and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000564 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000565 if (Ty->isComplexIntegerType())
566 Encoding = llvm::dwarf::DW_ATE_lo_user;
567
568 uint64_t Size = CGM.getContext().getTypeSize(Ty);
569 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000570 return DBuilder.createBasicType("complex", Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000571}
572
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000573llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
574 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000575 QualifierCollector Qc;
576 const Type *T = Qc.strip(Ty);
577
578 // Ignore these qualifiers for now.
579 Qc.removeObjCGCAttr();
580 Qc.removeAddressSpace();
581 Qc.removeObjCLifetime();
582
583 // We will create one Derived type for one qualifier and recurse to handle any
584 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000585 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000586 if (Qc.hasConst()) {
587 Tag = llvm::dwarf::DW_TAG_const_type;
588 Qc.removeConst();
589 } else if (Qc.hasVolatile()) {
590 Tag = llvm::dwarf::DW_TAG_volatile_type;
591 Qc.removeVolatile();
592 } else if (Qc.hasRestrict()) {
593 Tag = llvm::dwarf::DW_TAG_restrict_type;
594 Qc.removeRestrict();
595 } else {
596 assert(Qc.empty() && "Unknown type qualifier for debug info");
597 return getOrCreateType(QualType(T, 0), Unit);
598 }
599
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000600 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000601
602 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
603 // CVR derived types.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000604 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000605}
606
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000607llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
608 llvm::DIFile *Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000609
610 // The frontend treats 'id' as a typedef to an ObjCObjectType,
611 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
612 // debug info, we want to emit 'id' in both cases.
613 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000614 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000615
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000616 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
617 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000618}
619
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000620llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
621 llvm::DIFile *Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000622 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000623 Ty->getPointeeType(), Unit);
624}
625
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000626/// \return whether a C++ mangling exists for the type defined by TD.
627static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
628 switch (TheCU->getSourceLanguage()) {
629 case llvm::dwarf::DW_LANG_C_plus_plus:
630 return true;
631 case llvm::dwarf::DW_LANG_ObjC_plus_plus:
632 return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
633 default:
634 return false;
635 }
636}
637
Manman Rene0064d82013-08-29 23:19:58 +0000638/// In C++ mode, types have linkage, so we can rely on the ODR and
639/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000640static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
641 CodeGenModule &CGM,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000642 llvm::DICompileUnit *TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000643 SmallString<256> FullName;
Manman Rene0064d82013-08-29 23:19:58 +0000644 const TagDecl *TD = Ty->getDecl();
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000645
646 if (!hasCXXMangling(TD, TheCU) || !TD->isExternallyVisible())
Manman Rene0064d82013-08-29 23:19:58 +0000647 return FullName;
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000648
Manman Rene0064d82013-08-29 23:19:58 +0000649 // TODO: This is using the RTTI name. Is there a better way to get
650 // a unique string for a type?
651 llvm::raw_svector_ostream Out(FullName);
652 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
Manman Rene0064d82013-08-29 23:19:58 +0000653 return FullName;
654}
655
Adrian Prantl3e8bad42015-07-08 21:18:34 +0000656/// \return the approproate DWARF tag for a composite type.
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000657static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
658 llvm::dwarf::Tag Tag;
659 if (RD->isStruct() || RD->isInterface())
660 Tag = llvm::dwarf::DW_TAG_structure_type;
661 else if (RD->isUnion())
662 Tag = llvm::dwarf::DW_TAG_union_type;
663 else {
664 // FIXME: This could be a struct type giving a default visibility different
665 // than C++ class type, but needs llvm metadata changes first.
666 assert(RD->isClass());
667 Tag = llvm::dwarf::DW_TAG_class_type;
668 }
669 return Tag;
670}
671
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000672llvm::DICompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000673CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000674 llvm::DIScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000675 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000676 if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
677 return cast<llvm::DICompositeType>(T);
678 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +0000679 unsigned Line = getLineNumber(RD->getLocation());
680 StringRef RDName = getClassName(RD);
681
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000682 uint64_t Size = 0;
683 uint64_t Align = 0;
684
685 const RecordDecl *D = RD->getDefinition();
686 if (D && D->isCompleteDefinition()) {
687 Size = CGM.getContext().getTypeSize(Ty);
688 Align = CGM.getContext().getTypeAlign(Ty);
689 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000690
691 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000692 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000693 llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000694 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000695 llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000696 ReplaceMap.emplace_back(
697 std::piecewise_construct, std::make_tuple(Ty),
698 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000699 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000700}
701
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000702llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000703 const Type *Ty,
704 QualType PointeeTy,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000705 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000706 // Bit size, align and offset of the type.
707 // Size is always the size of a pointer. We can't use getTypeSize here
708 // because that does not return the correct value for references.
709 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000710 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000711 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
712
Keno Fischer87842f32015-11-16 09:04:13 +0000713 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
714 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
715 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit),
716 Size, Align);
717 else
718 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
719 Align);
Guy Benyei11169dd2012-12-18 14:30:41 +0000720}
721
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000722llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
723 llvm::DIType *&Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000724 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000725 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000726 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
727 TheCU, getOrCreateMainFile(), 0);
728 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
729 Cache = DBuilder.createPointerType(Cache, Size);
730 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000731}
732
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000733llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
734 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000735 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000736 QualType FType;
737 uint64_t FieldSize, FieldOffset;
738 unsigned FieldAlign;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000739 llvm::DINodeArray Elements;
Guy Benyei11169dd2012-12-18 14:30:41 +0000740
741 FieldOffset = 0;
742 FType = CGM.getContext().UnsignedLongTy;
743 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
744 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
745
746 Elements = DBuilder.getOrCreateArray(EltTys);
747 EltTys.clear();
748
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000749 unsigned Flags = llvm::DINode::FlagAppleBlock;
Adrian Prantl498fff62015-07-06 21:31:35 +0000750 unsigned LineNo = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000751
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000752 auto *EltTy =
Adrian Prantl498fff62015-07-06 21:31:35 +0000753 DBuilder.createStructType(Unit, "__block_descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000754 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000755
756 // Bit size, align and offset of the type.
757 uint64_t Size = CGM.getContext().getTypeSize(Ty);
758
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000759 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000760
761 FieldOffset = 0;
762 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
763 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
764 FType = CGM.getContext().IntTy;
765 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
766 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000767 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000768 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
769
770 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000771 FieldSize = CGM.getContext().getTypeSize(Ty);
772 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Adrian Prantl498fff62015-07-06 21:31:35 +0000773 EltTys.push_back(DBuilder.createMemberType(Unit, "__descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000774 FieldSize, FieldAlign, FieldOffset,
775 0, DescTy));
Guy Benyei11169dd2012-12-18 14:30:41 +0000776
777 FieldOffset += FieldSize;
778 Elements = DBuilder.getOrCreateArray(EltTys);
779
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000780 // The __block_literal_generic structs are marked with a special
781 // DW_AT_APPLE_BLOCK attribute and are an implementation detail only
782 // the debugger needs to know about. To allow type uniquing, emit
783 // them without a name or a location.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000784 EltTy =
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000785 DBuilder.createStructType(Unit, "", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000786 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000787
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000788 return DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000789}
790
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000791llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
792 llvm::DIFile *Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000793 assert(Ty->isTypeAlias());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000794 llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000795
796 SmallString<128> NS;
797 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000798 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
799 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000800
801 TemplateSpecializationType::PrintTemplateArgumentList(
802 OS, Ty->getArgs(), Ty->getNumArgs(),
803 CGM.getContext().getPrintingPolicy());
804
Eric Christophere7b87e52014-10-26 23:40:33 +0000805 TypeAliasDecl *AliasDecl = cast<TypeAliasTemplateDecl>(
806 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000807
808 SourceLocation Loc = AliasDecl->getLocation();
Adrian Prantlb67dbce2015-09-11 18:45:02 +0000809 return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc),
810 getLineNumber(Loc),
811 getDeclContextDescriptor(AliasDecl));
David Blaikief1b382e2014-04-06 17:14:06 +0000812}
813
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000814llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
815 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000816 // We don't set size information, but do specify where the typedef was
817 // declared.
Amjad Abouddc4531e2016-04-30 01:44:38 +0000818 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000819
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000820 // Typedefs are derived from some other type.
821 return DBuilder.createTypedef(
822 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
823 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
Amjad Abouddc4531e2016-04-30 01:44:38 +0000824 getDeclContextDescriptor(Ty->getDecl()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000825}
826
Reid Klecknerf00f8032016-06-08 20:41:54 +0000827static unsigned getDwarfCC(CallingConv CC) {
828 switch (CC) {
829 case CC_C:
830 // Avoid emitting DW_AT_calling_convention if the C convention was used.
831 return 0;
832
833 case CC_X86StdCall:
834 return llvm::dwarf::DW_CC_BORLAND_stdcall;
835 case CC_X86FastCall:
836 return llvm::dwarf::DW_CC_BORLAND_msfastcall;
837 case CC_X86ThisCall:
838 return llvm::dwarf::DW_CC_BORLAND_thiscall;
839 case CC_X86VectorCall:
840 return llvm::dwarf::DW_CC_LLVM_vectorcall;
841 case CC_X86Pascal:
842 return llvm::dwarf::DW_CC_BORLAND_pascal;
843
844 // FIXME: Create new DW_CC_ codes for these calling conventions.
845 case CC_X86_64Win64:
846 case CC_X86_64SysV:
847 case CC_AAPCS:
848 case CC_AAPCS_VFP:
849 case CC_IntelOclBicc:
850 case CC_SpirFunction:
851 case CC_SpirKernel:
852 case CC_Swift:
853 case CC_PreserveMost:
854 case CC_PreserveAll:
855 return 0;
856 }
857 return 0;
858}
859
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000860llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
861 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000862 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000863
864 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000865 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000866
867 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000868 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000869 if (isa<FunctionNoProtoType>(Ty))
870 EltTys.push_back(DBuilder.createUnspecifiedParameter());
871 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
Alp Toker9cacbab2014-01-20 20:26:09 +0000872 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
873 EltTys.push_back(getOrCreateType(FPT->getParamType(i), Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000874 if (FPT->isVariadic())
875 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000876 }
877
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000878 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Reid Klecknerf00f8032016-06-08 20:41:54 +0000879 return DBuilder.createSubroutineType(EltTypeArray, 0,
880 getDwarfCC(Ty->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000881}
882
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000883/// Convert an AccessSpecifier into the corresponding DINode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +0000884/// As an optimization, return 0 if the access specifier equals the
885/// default for the containing type.
886static unsigned getAccessFlag(AccessSpecifier Access, const RecordDecl *RD) {
887 AccessSpecifier Default = clang::AS_none;
888 if (RD && RD->isClass())
889 Default = clang::AS_private;
890 else if (RD && (RD->isStruct() || RD->isUnion()))
891 Default = clang::AS_public;
892
893 if (Access == Default)
894 return 0;
895
Eric Christophere7b87e52014-10-26 23:40:33 +0000896 switch (Access) {
897 case clang::AS_private:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000898 return llvm::DINode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +0000899 case clang::AS_protected:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000900 return llvm::DINode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +0000901 case clang::AS_public:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000902 return llvm::DINode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +0000903 case clang::AS_none:
904 return 0;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000905 }
906 llvm_unreachable("unexpected access enumerator");
907}
Guy Benyei11169dd2012-12-18 14:30:41 +0000908
David Majnemerb4b671e2016-06-30 03:01:59 +0000909llvm::DIType *CGDebugInfo::createBitFieldType(const FieldDecl *BitFieldDecl,
910 llvm::DIScope *RecordTy,
911 const RecordDecl *RD) {
912 StringRef Name = BitFieldDecl->getName();
913 QualType Ty = BitFieldDecl->getType();
914 SourceLocation Loc = BitFieldDecl->getLocation();
915 llvm::DIFile *VUnit = getOrCreateFile(Loc);
916 llvm::DIType *DebugType = getOrCreateType(Ty, VUnit);
917
918 // Get the location for the field.
919 llvm::DIFile *File = getOrCreateFile(Loc);
920 unsigned Line = getLineNumber(Loc);
921
922 const CGBitFieldInfo &BitFieldInfo =
923 CGM.getTypes().getCGRecordLayout(RD).getBitFieldInfo(BitFieldDecl);
924 uint64_t SizeInBits = BitFieldInfo.Size;
925 assert(SizeInBits > 0 && "found named 0-width bitfield");
926 unsigned AlignInBits = CGM.getContext().getTypeAlign(Ty);
927 uint64_t StorageOffsetInBits =
928 CGM.getContext().toBits(BitFieldInfo.StorageOffset);
929 uint64_t OffsetInBits = StorageOffsetInBits + BitFieldInfo.Offset;
930 unsigned Flags = getAccessFlag(BitFieldDecl->getAccess(), RD);
931 return DBuilder.createBitFieldMemberType(
932 RecordTy, Name, File, Line, SizeInBits, AlignInBits, OffsetInBits,
933 StorageOffsetInBits, Flags, DebugType);
934}
935
936llvm::DIType *
937CGDebugInfo::createFieldType(StringRef name, QualType type, SourceLocation loc,
938 AccessSpecifier AS, uint64_t offsetInBits,
939 llvm::DIFile *tunit, llvm::DIScope *scope,
940 const RecordDecl *RD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000941 llvm::DIType *debugType = getOrCreateType(type, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000942
943 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000944 llvm::DIFile *file = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000945 unsigned line = getLineNumber(loc);
946
David Majnemer34b57492014-07-30 01:30:47 +0000947 uint64_t SizeInBits = 0;
948 unsigned AlignInBits = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000949 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +0000950 TypeInfo TI = CGM.getContext().getTypeInfo(type);
951 SizeInBits = TI.Width;
952 AlignInBits = TI.Align;
Guy Benyei11169dd2012-12-18 14:30:41 +0000953 }
954
Adrian Prantl21361fb2014-08-29 22:44:27 +0000955 unsigned flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +0000956 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
957 AlignInBits, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +0000958}
959
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000960void CGDebugInfo::CollectRecordLambdaFields(
961 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000962 llvm::DIType *RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +0000963 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
964 // has the name and the location of the variable so we should iterate over
965 // both concurrently.
966 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
967 RecordDecl::field_iterator Field = CXXDecl->field_begin();
968 unsigned fieldno = 0;
969 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +0000970 E = CXXDecl->captures_end();
971 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000972 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +0000973 if (C.capturesVariable()) {
David Majnemerb4b671e2016-06-30 03:01:59 +0000974 SourceLocation Loc = C.getLocation();
975 assert(!Field->isBitField() && "lambdas don't have bitfield members!");
Eric Christopher91a31902013-01-16 01:22:32 +0000976 VarDecl *V = C.getCapturedVar();
Eric Christopher91a31902013-01-16 01:22:32 +0000977 StringRef VName = V->getName();
David Majnemerb4b671e2016-06-30 03:01:59 +0000978 llvm::DIFile *VUnit = getOrCreateFile(Loc);
979 llvm::DIType *FieldType = createFieldType(
980 VName, Field->getType(), Loc, Field->getAccess(),
981 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
982 elements.push_back(FieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +0000983 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +0000984 // TODO: Need to handle 'this' in some way by probably renaming the
985 // this of the lambda class and having a field member of 'this' or
986 // by using AT_object_pointer for the function and having that be
987 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +0000988 FieldDecl *f = *Field;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000989 llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000990 QualType type = f->getType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000991 llvm::DIType *fieldType = createFieldType(
David Majnemerb4b671e2016-06-30 03:01:59 +0000992 "this", type, f->getLocation(), f->getAccess(),
Eric Christophere7b87e52014-10-26 23:40:33 +0000993 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000994
995 elements.push_back(fieldType);
996 }
997 }
998}
999
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001000llvm::DIDerivedType *
1001CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00001002 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +00001003 // Create the descriptor for the static variable, with or without
1004 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +00001005 Var = Var->getCanonicalDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001006 llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
1007 llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher91a31902013-01-16 01:22:32 +00001008
Eric Christopher91a31902013-01-16 01:22:32 +00001009 unsigned LineNumber = getLineNumber(Var->getLocation());
1010 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +00001011 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +00001012 if (Var->getInit()) {
1013 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +00001014 if (Value) {
1015 if (Value->isInt())
1016 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
1017 if (Value->isFloat())
1018 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
1019 }
Eric Christopher91a31902013-01-16 01:22:32 +00001020 }
1021
Adrian Prantl21361fb2014-08-29 22:44:27 +00001022 unsigned Flags = getAccessFlag(Var->getAccess(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001023 llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
David Blaikieae019462013-08-15 22:50:29 +00001024 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001025 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +00001026 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +00001027}
1028
Eric Christophere7b87e52014-10-26 23:40:33 +00001029void CGDebugInfo::CollectRecordNormalField(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001030 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
1031 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +00001032 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +00001033 StringRef name = field->getName();
1034 QualType type = field->getType();
1035
1036 // Ignore unnamed fields unless they're anonymous structs/unions.
1037 if (name.empty() && !type->isRecordType())
1038 return;
1039
David Majnemerb4b671e2016-06-30 03:01:59 +00001040 llvm::DIType *FieldType;
Eric Christopher91a31902013-01-16 01:22:32 +00001041 if (field->isBitField()) {
David Majnemerb4b671e2016-06-30 03:01:59 +00001042 FieldType = createBitFieldType(field, RecordTy, RD);
1043 } else {
1044 FieldType =
1045 createFieldType(name, type, field->getLocation(), field->getAccess(),
1046 OffsetInBits, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +00001047 }
1048
David Majnemerb4b671e2016-06-30 03:01:59 +00001049 elements.push_back(FieldType);
Eric Christopher91a31902013-01-16 01:22:32 +00001050}
1051
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001052void CGDebugInfo::CollectRecordFields(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001053 const RecordDecl *record, llvm::DIFile *tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001054 SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001055 llvm::DICompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001056 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
1057
Eric Christopher91a31902013-01-16 01:22:32 +00001058 if (CXXDecl && CXXDecl->isLambda())
1059 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
1060 else {
1061 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001062
Eric Christopher91a31902013-01-16 01:22:32 +00001063 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +00001064 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +00001065
Eric Christopher91a31902013-01-16 01:22:32 +00001066 // Static and non-static members should appear in the same order as
1067 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +00001068 for (const auto *I : record->decls())
1069 if (const auto *V = dyn_cast<VarDecl>(I)) {
Paul Robinsonb17327d2016-04-27 17:37:12 +00001070 if (V->hasAttr<NoDebugAttr>())
1071 continue;
David Blaikiece763042013-08-20 21:49:21 +00001072 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001073 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +00001074 if (MI != StaticDataMemberCache.end()) {
1075 assert(MI->second &&
1076 "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00001077 elements.push_back(MI->second);
Adrian Prantl21361fb2014-08-29 22:44:27 +00001078 } else {
1079 auto Field = CreateRecordStaticField(V, RecordTy, record);
1080 elements.push_back(Field);
1081 }
Aaron Ballman629afae2014-03-07 19:56:05 +00001082 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001083 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
1084 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +00001085
1086 // Bump field number for next field.
1087 ++fieldNo;
Guy Benyei11169dd2012-12-18 14:30:41 +00001088 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001089 }
1090}
1091
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001092llvm::DISubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +00001093CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001094 llvm::DIFile *Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001095 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001096 if (Method->isStatic())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001097 return cast_or_null<llvm::DISubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001098 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001099 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1100 Func, Unit);
1101}
David Blaikie2aaf0652013-01-07 22:24:59 +00001102
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001103llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1104 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001105 // Add "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001106 llvm::DITypeRefArray Args(
1107 cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001108 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001109 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001110
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001111 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001112
1113 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001114 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001115
David Blaikie2aaf0652013-01-07 22:24:59 +00001116 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001117 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001118 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1119 // Create pointer type directly in this case.
1120 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1121 QualType PointeeTy = ThisPtrTy->getPointeeType();
1122 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001123 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie2aaf0652013-01-07 22:24:59 +00001124 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001125 llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1126 llvm::DIType *ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001127 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001128 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001129 // TODO: This and the artificial type below are misleading, the
1130 // types aren't artificial the argument is, but the current
1131 // metadata doesn't represent that.
1132 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1133 Elts.push_back(ThisPtrType);
1134 } else {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001135 llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001136 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001137 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1138 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001139 }
1140
1141 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001142 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1143 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001144
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001145 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001146
Adrian Prantl0630eb72013-12-18 21:48:18 +00001147 unsigned Flags = 0;
1148 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001149 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001150 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001151 Flags |= llvm::DINode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001152
Reid Klecknerf00f8032016-06-08 20:41:54 +00001153 return DBuilder.createSubroutineType(EltTypeArray, Flags,
1154 getDwarfCC(Func->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001155}
1156
Eric Christopherb2a008c2013-05-16 00:45:12 +00001157/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001158/// inside a function.
1159static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1160 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1161 return isFunctionLocalClass(NRD);
1162 if (isa<FunctionDecl>(RD->getDeclContext()))
1163 return true;
1164 return false;
1165}
1166
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001167llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1168 const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001169 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001170 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001171
Guy Benyei11169dd2012-12-18 14:30:41 +00001172 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001173 llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001174
1175 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1176 // make sense to give a single ctor/dtor a linkage name.
1177 StringRef MethodLinkageName;
David Blaikie71647672016-04-12 21:22:48 +00001178 // FIXME: 'isFunctionLocalClass' seems like an arbitrary/unintentional
1179 // property to use here. It may've been intended to model "is non-external
1180 // type" but misses cases of non-function-local but non-external classes such
1181 // as those in anonymous namespaces as well as the reverse - external types
1182 // that are function local, such as those in (non-local) inline functions.
Guy Benyei11169dd2012-12-18 14:30:41 +00001183 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1184 MethodLinkageName = CGM.getMangledName(Method);
1185
1186 // Get the location for the method.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001187 llvm::DIFile *MethodDefUnit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00001188 unsigned MethodLine = 0;
1189 if (!Method->isImplicit()) {
1190 MethodDefUnit = getOrCreateFile(Method->getLocation());
1191 MethodLine = getLineNumber(Method->getLocation());
1192 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001193
1194 // Collect virtual method info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001195 llvm::DIType *ContainingType = nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001196 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001197 unsigned VIndex = 0;
Reid Klecknerfb727182016-06-17 22:27:59 +00001198 unsigned Flags = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001199
Guy Benyei11169dd2012-12-18 14:30:41 +00001200 if (Method->isVirtual()) {
1201 if (Method->isPure())
1202 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1203 else
1204 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001205
Reid Kleckner216d0a12016-06-16 20:08:51 +00001206 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1207 // It doesn't make sense to give a virtual destructor a vtable index,
1208 // since a single destructor has two entries in the vtable.
1209 if (!isa<CXXDestructorDecl>(Method))
1210 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
1211 } else {
1212 // Emit MS ABI vftable information. There is only one entry for the
1213 // deleting dtor.
1214 const auto *DD = dyn_cast<CXXDestructorDecl>(Method);
1215 GlobalDecl GD = DD ? GlobalDecl(DD, Dtor_Deleting) : GlobalDecl(Method);
1216 MicrosoftVTableContext::MethodVFTableLocation ML =
1217 CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD);
1218 VIndex = ML.Index;
Reid Klecknerc4871ed2016-06-22 18:34:45 +00001219
1220 // CodeView only records the vftable offset in the class that introduces
1221 // the virtual method. This is possible because, unlike Itanium, the MS
1222 // C++ ABI does not include all virtual methods from non-primary bases in
1223 // the vtable for the most derived class. For example, if C inherits from
1224 // A and B, C's primary vftable will not include B's virtual methods.
1225 if (Method->begin_overridden_methods() == Method->end_overridden_methods())
1226 Flags |= llvm::DINode::FlagIntroducedVirtual;
1227
Reid Kleckner216d0a12016-06-16 20:08:51 +00001228 // FIXME: Pass down ML.VFPtrOffset and ML.VBTableIndex. The debugger needs
1229 // these to synthesize a call to a virtual method in a complex inheritance
1230 // hierarchy.
1231 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001232 ContainingType = RecordTy;
1233 }
1234
Guy Benyei11169dd2012-12-18 14:30:41 +00001235 if (Method->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001236 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001237 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
Guy Benyei11169dd2012-12-18 14:30:41 +00001238 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1239 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001240 Flags |= llvm::DINode::FlagExplicit;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001241 } else if (const CXXConversionDecl *CXXC =
Eric Christophere7b87e52014-10-26 23:40:33 +00001242 dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001243 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001244 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001245 }
1246 if (Method->hasPrototype())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001247 Flags |= llvm::DINode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001248 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001249 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001250 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001251 Flags |= llvm::DINode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001252
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001253 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1254 llvm::DISubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001255 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
1256 MethodTy, /*isLocalToUnit=*/false,
1257 /* isDefinition=*/false, Virtuality, VIndex, ContainingType, Flags,
Peter Collingbourne0900fe02015-11-05 22:04:14 +00001258 CGM.getLangOpts().Optimize, TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001259
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001260 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001261
1262 return SP;
1263}
1264
Eric Christophere7b87e52014-10-26 23:40:33 +00001265void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001266 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1267 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001268
1269 // Since we want more than just the individual member decls if we
1270 // have templated functions iterate over every declaration to gather
1271 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001272 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001273 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1274 // If the member is implicit, don't add it to the member list. This avoids
1275 // the member being added to type units by LLVM, while still allowing it
1276 // to be emitted into the type declaration/reference inside the compile
1277 // unit.
Paul Robinson6a7511b2015-06-25 17:50:43 +00001278 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
David Blaikie6dddfe32014-10-06 05:52:27 +00001279 // FIXME: Handle Using(Shadow?)Decls here to create
1280 // DW_TAG_imported_declarations inside the class for base decls brought into
1281 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1282 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1283 // referenced)
Paul Robinson6a7511b2015-06-25 17:50:43 +00001284 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
David Blaikiefd580722014-10-06 05:18:55 +00001285 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001286
1287 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1288 continue;
1289
David Blaikiefd580722014-10-06 05:18:55 +00001290 // Reuse the existing member function declaration if it exists.
1291 // It may be associated with the declaration of the type & should be
1292 // reused as we're building the definition.
1293 //
1294 // This situation can arise in the vtable-based debug info reduction where
1295 // implicit members are emitted in a non-vtable TU.
1296 auto MI = SPCache.find(Method->getCanonicalDecl());
1297 EltTys.push_back(MI == SPCache.end()
1298 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001299 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001300 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001301}
Guy Benyei11169dd2012-12-18 14:30:41 +00001302
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001303void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001304 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001305 llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001306 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Aaron Ballman574705e2014-03-13 15:41:46 +00001307 for (const auto &BI : RD->bases()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001308 unsigned BFlags = 0;
1309 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001310
Guy Benyei11169dd2012-12-18 14:30:41 +00001311 const CXXRecordDecl *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001312 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001313
Aaron Ballman574705e2014-03-13 15:41:46 +00001314 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001315 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1316 // virtual base offset offset is -ve. The code generator emits dwarf
1317 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001318 BaseOffset = 0 - CGM.getItaniumVTableContext()
1319 .getVirtualBaseOffsetOffset(RD, Base)
1320 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001321 } else {
1322 // In the MS ABI, store the vbtable offset, which is analogous to the
1323 // vbase offset offset in Itanium.
1324 BaseOffset =
1325 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1326 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001327 BFlags = llvm::DINode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001328 } else
1329 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1330 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1331 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001332
Adrian Prantl21361fb2014-08-29 22:44:27 +00001333 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001334 llvm::DIType *DTy = DBuilder.createInheritance(
Eric Christophere7b87e52014-10-26 23:40:33 +00001335 RecordTy, getOrCreateType(BI.getType(), Unit), BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001336 EltTys.push_back(DTy);
1337 }
1338}
1339
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001340llvm::DINodeArray
Eric Christophere7b87e52014-10-26 23:40:33 +00001341CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1342 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001343 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001344 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001345 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1346 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001347 StringRef Name;
1348 if (TPList)
1349 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001350 switch (TA.getKind()) {
1351 case TemplateArgument::Type: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001352 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001353 TemplateParams.push_back(
1354 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001355 } break;
1356 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001357 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001358 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1359 TheCU, Name, TTy,
1360 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001361 } break;
1362 case TemplateArgument::Declaration: {
1363 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001364 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001365 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001366 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001367 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001368 // Variable pointer template parameters have a value that is the address
1369 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001370 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001371 V = CGM.GetAddrOfGlobalVar(VD);
1372 // Member function pointers have special support for building them, though
1373 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001374 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Majnemere2be95b2015-06-23 07:31:01 +00001375 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001376 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001377 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001378 // Member data pointers have special handling too to compute the fixed
1379 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001380 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001381 // These five lines (& possibly the above member function pointer
1382 // handling) might be able to be refactored to use similar code in
1383 // CodeGenModule::getMemberPointerConstant
1384 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1385 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001386 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001387 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001388 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001389 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1390 TheCU, Name, TTy,
1391 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001392 } break;
1393 case TemplateArgument::NullPtr: {
1394 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001395 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001396 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001397 // Special case member data pointer null values since they're actually -1
1398 // instead of zero.
1399 if (const MemberPointerType *MPT =
1400 dyn_cast<MemberPointerType>(T.getTypePtr()))
1401 // But treat member function pointers as simple zero integers because
1402 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1403 // CodeGen grows handling for values of non-null member function
1404 // pointers then perhaps we could remove this special case and rely on
1405 // EmitNullMemberPointer for member function pointers.
1406 if (MPT->isMemberDataPointer())
1407 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1408 if (!V)
1409 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001410 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1411 TheCU, Name, TTy, cast<llvm::Constant>(V)));
David Blaikie38079fd2013-05-10 21:53:14 +00001412 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001413 case TemplateArgument::Template:
1414 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001415 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001416 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1417 break;
1418 case TemplateArgument::Pack:
1419 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1420 TheCU, Name, nullptr,
1421 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1422 break;
David Majnemer5559d472013-08-24 08:21:10 +00001423 case TemplateArgument::Expression: {
1424 const Expr *E = TA.getAsExpr();
1425 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001426 if (E->isGLValue())
1427 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001428 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001429 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001430 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001431 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1432 TheCU, Name, TTy, cast<llvm::Constant>(V->stripPointerCasts())));
David Majnemer5559d472013-08-24 08:21:10 +00001433 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001434 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001435 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001436 case TemplateArgument::Null:
1437 llvm_unreachable(
1438 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001439 }
1440 }
1441 return DBuilder.getOrCreateArray(TemplateParams);
1442}
1443
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001444llvm::DINodeArray
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001445CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001446 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001447 if (FD->getTemplatedKind() ==
1448 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001449 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1450 ->getTemplate()
1451 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001452 return CollectTemplateParams(
1453 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001454 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001455 return llvm::DINodeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001456}
1457
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001458llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1459 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001460 // Always get the full list of parameters, not just the ones from
1461 // the specialization.
1462 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001463 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001464 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001465 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001466}
1467
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001468llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001469 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001470 return VTablePtrType;
1471
1472 ASTContext &Context = CGM.getContext();
1473
1474 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001475 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001476 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
Eric Christopher28a6db52015-10-15 06:56:08 +00001477 llvm::DIType *SubTy = DBuilder.createSubroutineType(SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001478 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001479 llvm::DIType *vtbl_ptr_type =
Eric Christophere7b87e52014-10-26 23:40:33 +00001480 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001481 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1482 return VTablePtrType;
1483}
1484
Guy Benyei11169dd2012-12-18 14:30:41 +00001485StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001486 // Copy the gdb compatible name on the side and use its reference.
1487 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001488}
1489
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001490void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001491 SmallVectorImpl<llvm::Metadata *> &EltTys) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001492 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1493
1494 // If there is a primary base then it will hold vtable info.
1495 if (RL.getPrimaryBase())
1496 return;
1497
1498 // If this class is not dynamic then there is not any vtable info to collect.
1499 if (!RD->isDynamicClass())
1500 return;
1501
1502 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001503 llvm::DIType *VPTR = DBuilder.createMemberType(
Eric Christophere7b87e52014-10-26 23:40:33 +00001504 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001505 llvm::DINode::FlagArtificial, getOrCreateVTablePtrType(Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001506 EltTys.push_back(VPTR);
1507}
1508
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001509llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001510 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001511 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001512 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001513 return T;
1514}
1515
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001516llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001517 SourceLocation Loc) {
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001518 return getOrCreateStandaloneType(D, Loc);
1519}
1520
1521llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
1522 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001523 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001524 assert(!D.isNull() && "null type");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001525 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001526 assert(T && "could not create debug info for type");
Adrian Prantl3a884fa2015-08-27 22:56:46 +00001527
Adrian Prantl73409ce2013-03-11 18:33:46 +00001528 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001529 return T;
1530}
1531
David Blaikie483a9da2014-05-06 18:35:21 +00001532void CGDebugInfo::completeType(const EnumDecl *ED) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001533 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie483a9da2014-05-06 18:35:21 +00001534 return;
1535 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001536 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001537 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001538 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001539 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001540 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001541 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001542 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001543}
1544
David Blaikieb2e86eb2013-08-15 20:49:17 +00001545void CGDebugInfo::completeType(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001546 if (DebugKind > codegenoptions::LimitedDebugInfo ||
David Blaikieb2e86eb2013-08-15 20:49:17 +00001547 !CGM.getLangOpts().CPlusPlus)
1548 completeRequiredType(RD);
1549}
1550
1551void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001552 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie0856f662014-03-04 22:01:08 +00001553 return;
1554
David Blaikie6943dea2013-08-20 01:28:15 +00001555 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1556 if (CXXDecl->isDynamicClass())
1557 return;
1558
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001559 if (DebugTypeExtRefs && RD->isFromASTFile())
1560 return;
1561
David Blaikieb2e86eb2013-08-15 20:49:17 +00001562 QualType Ty = CGM.getContext().getRecordType(RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001563 llvm::DIType *T = getTypeOrNull(Ty);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001564 if (T && T->isForwardDecl())
David Blaikie6943dea2013-08-20 01:28:15 +00001565 completeClassData(RD);
1566}
1567
1568void CGDebugInfo::completeClassData(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001569 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001570 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001571 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001572 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001573 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001574 if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001575 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001576 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001577 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001578 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001579}
1580
David Blaikie0e716b42014-03-03 23:48:23 +00001581static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1582 CXXRecordDecl::method_iterator End) {
1583 for (; I != End; ++I)
1584 if (FunctionDecl *Tmpl = I->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001585 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
1586 !I->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001587 return true;
1588 return false;
1589}
1590
Adrian Prantl05fefa42016-04-25 20:52:40 +00001591/// Does a type definition exist in an imported clang module?
1592static bool isDefinedInClangModule(const RecordDecl *RD) {
Adrian Prantl88d79172016-04-26 21:58:18 +00001593 if (!RD || !RD->isFromASTFile())
Adrian Prantl05fefa42016-04-25 20:52:40 +00001594 return false;
1595 if (!RD->isExternallyVisible() && RD->getName().empty())
1596 return false;
Adrian Prantl94913712016-04-26 23:42:43 +00001597 if (auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) {
1598 assert(CXXDecl->isCompleteDefinition() && "incomplete record definition");
Adrian Prantl0dabc2a2016-04-26 23:37:38 +00001599 if (CXXDecl->getTemplateSpecializationKind() != TSK_Undeclared)
1600 // Make sure the instantiation is actually in a module.
1601 if (CXXDecl->field_begin() != CXXDecl->field_end())
1602 return CXXDecl->field_begin()->isFromASTFile();
Adrian Prantl94913712016-04-26 23:42:43 +00001603 }
Adrian Prantl0dabc2a2016-04-26 23:37:38 +00001604
Adrian Prantl05fefa42016-04-25 20:52:40 +00001605 return true;
1606}
1607
Benjamin Kramer8c305922016-02-02 11:06:51 +00001608static bool shouldOmitDefinition(codegenoptions::DebugInfoKind DebugKind,
1609 bool DebugTypeExtRefs, const RecordDecl *RD,
David Blaikie0e716b42014-03-03 23:48:23 +00001610 const LangOptions &LangOpts) {
Adrian Prantl88d79172016-04-26 21:58:18 +00001611 if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
Adrian Prantl43e00812016-01-19 23:42:53 +00001612 return true;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001613
Benjamin Kramer8c305922016-02-02 11:06:51 +00001614 if (DebugKind > codegenoptions::LimitedDebugInfo)
David Blaikie0e716b42014-03-03 23:48:23 +00001615 return false;
1616
1617 if (!LangOpts.CPlusPlus)
1618 return false;
1619
1620 if (!RD->isCompleteDefinitionRequired())
1621 return true;
1622
1623 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1624
1625 if (!CXXDecl)
1626 return false;
1627
1628 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass())
1629 return true;
1630
1631 TemplateSpecializationKind Spec = TSK_Undeclared;
1632 if (const ClassTemplateSpecializationDecl *SD =
1633 dyn_cast<ClassTemplateSpecializationDecl>(RD))
1634 Spec = SD->getSpecializationKind();
1635
1636 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1637 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1638 CXXDecl->method_end()))
1639 return true;
1640
1641 return false;
1642}
1643
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001644llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001645 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001646 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001647 if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD,
1648 CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001649 if (!T)
Adrian Prantl6ec370a2015-09-10 18:39:45 +00001650 T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001651 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001652 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001653
David Blaikieb2e86eb2013-08-15 20:49:17 +00001654 return CreateTypeDefinition(Ty);
1655}
1656
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001657llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00001658 RecordDecl *RD = Ty->getDecl();
1659
Guy Benyei11169dd2012-12-18 14:30:41 +00001660 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001661 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001662
1663 // Records and classes and unions can all be recursive. To handle them, we
1664 // first generate a debug descriptor for the struct as a forward declaration.
1665 // Then (if it is a definition) we go through and get debug info for all of
1666 // its members. Finally, we create a descriptor for the complete type (which
1667 // may refer to the forward decl if the struct is recursive) and replace all
1668 // uses of the forward declaration with the final definition.
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00001669 llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001670
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001671 const RecordDecl *D = RD->getDefinition();
1672 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001673 return FwdDecl;
1674
David Blaikieadfbf992013-08-18 16:55:33 +00001675 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1676 CollectContainingType(CXXDecl, FwdDecl);
1677
Guy Benyei11169dd2012-12-18 14:30:41 +00001678 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001679 LexicalBlockStack.emplace_back(&*FwdDecl);
1680 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001681
Guy Benyei11169dd2012-12-18 14:30:41 +00001682 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001683 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001684 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001685
1686 // Note: The split of CXXDecl information here is intentional, the
1687 // gdb tests will depend on a certain ordering at printout. The debug
1688 // information offsets are still correct if we merge them all together
1689 // though.
1690 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1691 if (CXXDecl) {
1692 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1693 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1694 }
1695
Eric Christopher91a31902013-01-16 01:22:32 +00001696 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001697 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001698 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001699 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001700
1701 LexicalBlockStack.pop_back();
1702 RegionMap.erase(Ty->getDecl());
1703
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001704 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001705 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001706
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001707 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001708 FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001709 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001710
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001711 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001712 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001713}
1714
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001715llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1716 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001717 // Ignore protocols.
1718 return getOrCreateType(Ty->getBaseType(), Unit);
1719}
1720
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001721/// \return true if Getter has the default name for the property PD.
1722static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1723 const ObjCMethodDecl *Getter) {
1724 assert(PD);
1725 if (!Getter)
1726 return true;
1727
1728 assert(Getter->getDeclName().isObjCZeroArgSelector());
1729 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001730 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001731}
1732
1733/// \return true if Setter has the default name for the property PD.
1734static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1735 const ObjCMethodDecl *Setter) {
1736 assert(PD);
1737 if (!Setter)
1738 return true;
1739
1740 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001741 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001742 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001743}
1744
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001745llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1746 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001747 ObjCInterfaceDecl *ID = Ty->getDecl();
1748 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001749 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001750
Adrian Prantl50fd1a82016-04-20 23:59:32 +00001751 // Return a forward declaration if this type was imported from a clang module,
1752 // and this is not the compile unit with the implementation of the type (which
1753 // may contain hidden ivars).
1754 if (DebugTypeExtRefs && ID->isFromASTFile() && ID->getDefinition() &&
1755 !ID->getImplementation())
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001756 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
1757 ID->getName(),
1758 getDeclContextDescriptor(ID), Unit, 0);
1759
Guy Benyei11169dd2012-12-18 14:30:41 +00001760 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001761 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001762 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001763 auto RuntimeLang =
1764 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00001765
1766 // If this is just a forward declaration return a special forward-declaration
1767 // debug type since we won't be able to lay out the entire type.
1768 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001769 if (!Def || !Def->getImplementation()) {
Adrian Prantl42ce2d32015-10-01 16:57:02 +00001770 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001771 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
Adrian Prantl42ce2d32015-10-01 16:57:02 +00001772 llvm::dwarf::DW_TAG_structure_type, ID->getName(), Mod ? Mod : TheCU,
1773 DefUnit, Line, RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001774 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001775 return FwdDecl;
1776 }
1777
David Blaikieef8a9512014-05-05 23:23:53 +00001778 return CreateTypeDefinition(Ty, Unit);
1779}
1780
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001781llvm::DIModule *
Adrian Prantl66689202015-09-18 23:01:45 +00001782CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod,
1783 bool CreateSkeletonCU) {
Adrian Prantleb66a262015-09-24 16:10:04 +00001784 // Use the Module pointer as the key into the cache. This is a
1785 // nullptr if the "Module" is a PCH, which is safe because we don't
1786 // support chained PCH debug info, so there can only be a single PCH.
1787 const Module *M = Mod.getModuleOrNull();
Adrian Prantl9e8ea352015-09-29 20:44:46 +00001788 auto ModRef = ModuleCache.find(M);
1789 if (ModRef != ModuleCache.end())
1790 return cast<llvm::DIModule>(ModRef->second);
Adrian Prantl2388ead2015-06-30 18:01:05 +00001791
1792 // Macro definitions that were defined with "-D" on the command line.
1793 SmallString<128> ConfigMacros;
1794 {
1795 llvm::raw_svector_ostream OS(ConfigMacros);
1796 const auto &PPOpts = CGM.getPreprocessorOpts();
1797 unsigned I = 0;
1798 // Translate the macro definitions back into a commmand line.
1799 for (auto &M : PPOpts.Macros) {
1800 if (++I > 1)
1801 OS << " ";
1802 const std::string &Macro = M.first;
1803 bool Undef = M.second;
1804 OS << "\"-" << (Undef ? 'U' : 'D');
1805 for (char c : Macro)
1806 switch (c) {
1807 case '\\' : OS << "\\\\"; break;
1808 case '"' : OS << "\\\""; break;
1809 default: OS << c;
1810 }
1811 OS << '\"';
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001812 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001813 }
Adrian Prantl66689202015-09-18 23:01:45 +00001814
Adrian Prantl835e6632015-09-24 16:10:10 +00001815 bool IsRootModule = M ? !M->Parent : true;
1816 if (CreateSkeletonCU && IsRootModule) {
Adrian Prantlc96da8f2016-01-22 17:43:43 +00001817 // PCH files don't have a signature field in the control block,
1818 // but LLVM detects skeleton CUs by looking for a non-zero DWO id.
Adrian Prantl98bfc822016-01-22 19:29:41 +00001819 uint64_t Signature = Mod.getSignature() ? Mod.getSignature() : ~1ULL;
Adrian Prantl66689202015-09-18 23:01:45 +00001820 llvm::DIBuilder DIB(CGM.getModule());
Adrian Prantl835e6632015-09-24 16:10:10 +00001821 DIB.createCompileUnit(TheCU->getSourceLanguage(), Mod.getModuleName(),
Adrian Prantl6083b082015-09-24 16:10:00 +00001822 Mod.getPath(), TheCU->getProducer(), true,
1823 StringRef(), 0, Mod.getASTFile(),
Adrian Prantl3563c552016-03-31 23:57:45 +00001824 llvm::DICompileUnit::FullDebug, Signature);
Adrian Prantl66689202015-09-18 23:01:45 +00001825 DIB.finalize();
Adrian Prantl2f957ac2015-09-19 00:59:22 +00001826 }
Adrian Prantl835e6632015-09-24 16:10:10 +00001827 llvm::DIModule *Parent =
1828 IsRootModule ? nullptr
1829 : getOrCreateModuleRef(
1830 ExternalASTSource::ASTSourceDescriptor(*M->Parent),
1831 CreateSkeletonCU);
Adrian Prantleb66a262015-09-24 16:10:04 +00001832 llvm::DIModule *DIMod =
Adrian Prantl835e6632015-09-24 16:10:10 +00001833 DBuilder.createModule(Parent, Mod.getModuleName(), ConfigMacros,
1834 Mod.getPath(), CGM.getHeaderSearchOpts().Sysroot);
Adrian Prantl9e8ea352015-09-29 20:44:46 +00001835 ModuleCache[M].reset(DIMod);
Adrian Prantleb66a262015-09-24 16:10:04 +00001836 return DIMod;
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001837}
1838
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001839llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
1840 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00001841 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001842 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00001843 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001844 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001845
1846 // Bit size, align and offset of the type.
1847 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1848 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1849
1850 unsigned Flags = 0;
1851 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001852 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00001853
Adrian Prantlfd696112015-10-01 00:48:51 +00001854 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001855 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Adrian Prantlfd696112015-10-01 00:48:51 +00001856 Mod ? Mod : Unit, ID->getName(), DefUnit, Line, Size, Align, Flags,
1857 nullptr, llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001858
David Blaikieef8a9512014-05-05 23:23:53 +00001859 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001860 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001861
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001862 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001863 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001864 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001865
1866 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001867 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001868
1869 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1870 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001871 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001872 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001873 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001874 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001875
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001876 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001877 EltTys.push_back(InhTag);
1878 }
1879
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001880 // Create entries for all of the properties.
Nico Weber7123bca2015-12-04 19:14:14 +00001881 auto AddProperty = [&](const ObjCPropertyDecl *PD) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001882 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001883 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001884 unsigned PLine = getLineNumber(Loc);
1885 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1886 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001887 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
1888 PD->getName(), PUnit, PLine,
1889 hasDefaultGetterName(PD, Getter) ? ""
1890 : getSelectorName(PD->getGetterName()),
1891 hasDefaultSetterName(PD, Setter) ? ""
1892 : getSelectorName(PD->getSetterName()),
1893 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001894 EltTys.push_back(PropertyNode);
Nico Weber7123bca2015-12-04 19:14:14 +00001895 };
1896 {
1897 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Nico Weberde059e12015-12-04 19:35:45 +00001898 for (const ObjCCategoryDecl *ClassExt : ID->known_extensions())
Manman Renefe1bac2016-01-27 20:00:32 +00001899 for (auto *PD : ClassExt->properties()) {
Nico Weberde059e12015-12-04 19:35:45 +00001900 PropertySet.insert(PD->getIdentifier());
1901 AddProperty(PD);
1902 }
Manman Renefe1bac2016-01-27 20:00:32 +00001903 for (const auto *PD : ID->properties()) {
Nico Weber7123bca2015-12-04 19:14:14 +00001904 // Don't emit duplicate metadata for properties that were already in a
1905 // class extension.
1906 if (!PropertySet.insert(PD->getIdentifier()).second)
1907 continue;
1908 AddProperty(PD);
1909 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001910 }
1911
1912 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1913 unsigned FieldNo = 0;
1914 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1915 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001916 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001917 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001918 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001919
Guy Benyei11169dd2012-12-18 14:30:41 +00001920 StringRef FieldName = Field->getName();
1921
1922 // Ignore unnamed fields.
1923 if (FieldName.empty())
1924 continue;
1925
1926 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001927 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001928 unsigned FieldLine = getLineNumber(Field->getLocation());
1929 QualType FType = Field->getType();
1930 uint64_t FieldSize = 0;
1931 unsigned FieldAlign = 0;
1932
1933 if (!FType->isIncompleteArrayType()) {
1934
1935 // Bit size, align and offset of the type.
1936 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001937 ? Field->getBitWidthValue(CGM.getContext())
1938 : CGM.getContext().getTypeSize(FType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001939 FieldAlign = CGM.getContext().getTypeAlign(FType);
1940 }
1941
1942 uint64_t FieldOffset;
1943 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1944 // We don't know the runtime offset of an ivar if we're using the
1945 // non-fragile ABI. For bitfields, use the bit offset into the first
1946 // byte of storage of the bitfield. For other fields, use zero.
1947 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001948 FieldOffset =
1949 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00001950 FieldOffset %= CGM.getContext().getCharWidth();
1951 } else {
1952 FieldOffset = 0;
1953 }
1954 } else {
1955 FieldOffset = RL.getFieldOffset(FieldNo);
1956 }
1957
1958 unsigned Flags = 0;
1959 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001960 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00001961 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001962 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001963 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001964 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00001965
Craig Topper8a13c412014-05-21 05:09:00 +00001966 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001967 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001968 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00001969 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001970 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00001971 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001972 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00001973 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001974 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1975 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001976 PropertyNode = DBuilder.createObjCProperty(
1977 PD->getName(), PUnit, PLine,
1978 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
1979 PD->getGetterName()),
1980 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
1981 PD->getSetterName()),
1982 PD->getPropertyAttributes(),
1983 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001984 }
1985 }
1986 }
Eric Christophere7b87e52014-10-26 23:40:33 +00001987 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
1988 FieldSize, FieldAlign, FieldOffset, Flags,
1989 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00001990 EltTys.push_back(FieldTy);
1991 }
1992
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001993 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001994 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00001995
Guy Benyei11169dd2012-12-18 14:30:41 +00001996 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001997 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001998}
1999
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002000llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
2001 llvm::DIFile *Unit) {
2002 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002003 int64_t Count = Ty->getNumElements();
2004 if (Count == 0)
2005 // If number of elements are not known then this is an unbounded array.
2006 // Use Count == -1 to express such arrays.
2007 Count = -1;
2008
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002009 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002010 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00002011
2012 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2013 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
2014
2015 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
2016}
2017
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002018llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002019 uint64_t Size;
2020 uint64_t Align;
2021
2022 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
2023 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
2024 Size = 0;
2025 Align =
Eric Christophere7b87e52014-10-26 23:40:33 +00002026 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Guy Benyei11169dd2012-12-18 14:30:41 +00002027 } else if (Ty->isIncompleteArrayType()) {
2028 Size = 0;
2029 if (Ty->getElementType()->isIncompleteType())
2030 Align = 0;
2031 else
2032 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikief03b2e82013-05-09 20:48:12 +00002033 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002034 Size = 0;
2035 Align = 0;
2036 } else {
2037 // Size and align of the whole array, not the element type.
2038 Size = CGM.getContext().getTypeSize(Ty);
2039 Align = CGM.getContext().getTypeAlign(Ty);
2040 }
2041
2042 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
2043 // interior arrays, do we care? Why aren't nested arrays represented the
2044 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002045 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002046 QualType EltTy(Ty, 0);
2047 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
2048 // If the number of elements is known, then count is that number. Otherwise,
2049 // it's -1. This allows us to represent a subrange with an array of 0
2050 // elements, like this:
2051 //
2052 // struct foo {
2053 // int x[0];
2054 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00002055 int64_t Count = -1; // Count == -1 is an unbounded array.
Guy Benyei11169dd2012-12-18 14:30:41 +00002056 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
2057 Count = CAT->getSize().getZExtValue();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002058
Guy Benyei11169dd2012-12-18 14:30:41 +00002059 // FIXME: Verify this is right for VLAs.
2060 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
2061 EltTy = Ty->getElementType();
2062 }
2063
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002064 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002065
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002066 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
2067 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00002068}
2069
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002070llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
2071 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002072 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
2073 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002074}
2075
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002076llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
2077 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002078 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
2079 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002080}
2081
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002082llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
2083 llvm::DIFile *U) {
Reid Klecknerfb727182016-06-17 22:27:59 +00002084 unsigned Flags = 0;
2085 uint64_t Size = 0;
2086
2087 if (!Ty->isIncompleteType()) {
2088 Size = CGM.getContext().getTypeSize(Ty);
2089
2090 // Set the MS inheritance model. There is no flag for the unspecified model.
2091 if (CGM.getTarget().getCXXABI().isMicrosoft()) {
2092 switch (Ty->getMostRecentCXXRecordDecl()->getMSInheritanceModel()) {
2093 case MSInheritanceAttr::Keyword_single_inheritance:
2094 Flags |= llvm::DINode::FlagSingleInheritance;
2095 break;
2096 case MSInheritanceAttr::Keyword_multiple_inheritance:
2097 Flags |= llvm::DINode::FlagMultipleInheritance;
2098 break;
2099 case MSInheritanceAttr::Keyword_virtual_inheritance:
2100 Flags |= llvm::DINode::FlagVirtualInheritance;
2101 break;
2102 case MSInheritanceAttr::Keyword_unspecified_inheritance:
2103 break;
2104 }
2105 }
2106 }
2107
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002108 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00002109 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00002110 return DBuilder.createMemberPointerType(
Reid Klecknerfb727182016-06-17 22:27:59 +00002111 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size, /*Align=*/0,
2112 Flags);
Adrian Prantl0866acd2013-12-19 01:38:47 +00002113
2114 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00002115 Ty->getPointeeType()->getAs<FunctionProtoType>();
2116 return DBuilder.createMemberPointerType(
2117 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
2118 Ty->getClass(), FPT->getTypeQuals())),
2119 FPT, U),
Reid Klecknerfb727182016-06-17 22:27:59 +00002120 ClassType, Size, /*Align=*/0, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002121}
2122
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002123llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002124 // Ignore the atomic wrapping
2125 // FIXME: What is the correct representation?
2126 return getOrCreateType(Ty->getValueType(), U);
2127}
2128
Xiuli Pan9c14e282016-01-09 12:53:17 +00002129llvm::DIType* CGDebugInfo::CreateType(const PipeType *Ty,
2130 llvm::DIFile *U) {
2131 return getOrCreateType(Ty->getElementType(), U);
2132}
2133
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002134llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00002135 const EnumDecl *ED = Ty->getDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002136
Guy Benyei11169dd2012-12-18 14:30:41 +00002137 uint64_t Size = 0;
2138 uint64_t Align = 0;
2139 if (!ED->getTypeForDecl()->isIncompleteType()) {
2140 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
2141 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
2142 }
2143
Manman Rene0064d82013-08-29 23:19:58 +00002144 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2145
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002146 bool isImportedFromModule =
2147 DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition();
2148
Guy Benyei11169dd2012-12-18 14:30:41 +00002149 // If this is just a forward declaration, construct an appropriately
2150 // marked node and just return it.
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002151 if (isImportedFromModule || !ED->getDefinition()) {
Adrian Prantl45946062016-02-23 19:30:08 +00002152 // Note that it is possible for enums to be created as part of
2153 // their own declcontext. In this case a FwdDecl will be created
2154 // twice. This doesn't cause a problem because both FwdDecls are
2155 // entered into the ReplaceMap: finalize() will replace the first
2156 // FwdDecl with the second and then replace the second with
2157 // complete type.
Amjad Abouddc4531e2016-04-30 01:44:38 +00002158 llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002159 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Adrian Prantlff9d83c2016-02-08 17:03:28 +00002160 llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType(
2161 llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0));
Adrian Prantla40030f2016-02-06 01:59:09 +00002162
Guy Benyei11169dd2012-12-18 14:30:41 +00002163 unsigned Line = getLineNumber(ED->getLocation());
2164 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002165 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
Adrian Prantl45946062016-02-23 19:30:08 +00002166 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
2167 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
Adrian Prantla40030f2016-02-06 01:59:09 +00002168
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002169 ReplaceMap.emplace_back(
2170 std::piecewise_construct, std::make_tuple(Ty),
2171 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00002172 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00002173 }
2174
David Blaikie483a9da2014-05-06 18:35:21 +00002175 return CreateTypeDefinition(Ty);
2176}
2177
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002178llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00002179 const EnumDecl *ED = Ty->getDecl();
2180 uint64_t Size = 0;
2181 uint64_t Align = 0;
2182 if (!ED->getTypeForDecl()->isIncompleteType()) {
2183 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
2184 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
2185 }
2186
2187 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2188
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002189 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002190 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00002191 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00002192 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002193 Enumerators.push_back(DBuilder.createEnumerator(
2194 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002195 }
2196
2197 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002198 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00002199
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002200 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002201 unsigned Line = getLineNumber(ED->getLocation());
Amjad Abouddc4531e2016-04-30 01:44:38 +00002202 llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002203 llvm::DIType *ClassTy =
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002204 ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
2205 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
2206 Line, Size, Align, EltArray, ClassTy,
2207 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002208}
2209
David Blaikie05491062013-01-21 04:37:12 +00002210static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
2211 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002212 do {
Adrian Prantl179af902013-09-26 21:35:50 +00002213 Qualifiers InnerQuals = T.getLocalQualifiers();
2214 // Qualifiers::operator+() doesn't like it if you add a Qualifier
2215 // that is already there.
2216 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2217 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002218 QualType LastT = T;
2219 switch (T->getTypeClass()) {
2220 default:
David Blaikie05491062013-01-21 04:37:12 +00002221 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002222 case Type::TemplateSpecialization: {
2223 const auto *Spec = cast<TemplateSpecializationType>(T);
2224 if (Spec->isTypeAlias())
2225 return C.getQualifiedType(T.getTypePtr(), Quals);
2226 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002227 break;
2228 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002229 case Type::TypeOfExpr:
2230 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2231 break;
2232 case Type::TypeOf:
2233 T = cast<TypeOfType>(T)->getUnderlyingType();
2234 break;
2235 case Type::Decltype:
2236 T = cast<DecltypeType>(T)->getUnderlyingType();
2237 break;
2238 case Type::UnaryTransform:
2239 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2240 break;
2241 case Type::Attributed:
2242 T = cast<AttributedType>(T)->getEquivalentType();
2243 break;
2244 case Type::Elaborated:
2245 T = cast<ElaboratedType>(T)->getNamedType();
2246 break;
2247 case Type::Paren:
2248 T = cast<ParenType>(T)->getInnerType();
2249 break;
David Blaikie05491062013-01-21 04:37:12 +00002250 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002251 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002252 break;
2253 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00002254 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002255 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002256 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002257 break;
2258 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002259
Guy Benyei11169dd2012-12-18 14:30:41 +00002260 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002261 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002262 } while (true);
2263}
2264
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002265llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002266
2267 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002268 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002269
David Blaikief427b002014-05-06 03:42:01 +00002270 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002271 if (it != TypeCache.end()) {
2272 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002273 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002274 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002275 }
2276
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002277 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002278}
2279
David Blaikie0e716b42014-03-03 23:48:23 +00002280void CGDebugInfo::completeTemplateDefinition(
2281 const ClassTemplateSpecializationDecl &SD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002282 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie0856f662014-03-04 22:01:08 +00002283 return;
2284
David Blaikie0e716b42014-03-03 23:48:23 +00002285 completeClassData(&SD);
2286 // In case this type has no member function definitions being emitted, ensure
2287 // it is retained
2288 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2289}
2290
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002291llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002292 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002293 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002294
2295 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002296 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002297
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002298 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002299 return T;
2300
Adrian Prantlca844182015-09-11 17:23:03 +00002301 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002302 void* TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002303
2304 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002305 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002306
Guy Benyei11169dd2012-12-18 14:30:41 +00002307 return Res;
2308}
2309
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002310llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
Adrian Prantl335f5c72015-10-02 17:36:14 +00002311 // A forward declaration inside a module header does not belong to the module.
2312 if (isa<RecordDecl>(D) && !cast<RecordDecl>(D)->getDefinition())
2313 return nullptr;
Adrian Prantl85d938a2015-09-21 17:48:37 +00002314 if (DebugTypeExtRefs && D->isFromASTFile()) {
2315 // Record a reference to an imported clang module or precompiled header.
2316 auto *Reader = CGM.getContext().getExternalSource();
2317 auto Idx = D->getOwningModuleID();
2318 auto Info = Reader->getSourceDescriptor(Idx);
2319 if (Info)
2320 return getOrCreateModuleRef(*Info, /*SkeletonCU=*/true);
2321 } else if (ClangModuleMap) {
Adrian Prantl9402cef2015-09-20 16:51:35 +00002322 // We are building a clang module or a precompiled header.
2323 //
2324 // TODO: When D is a CXXRecordDecl or a C++ Enum, the ODR applies
2325 // and it wouldn't be necessary to specify the parent scope
2326 // because the type is already unique by definition (it would look
2327 // like the output of -fno-standalone-debug). On the other hand,
2328 // the parent scope helps a consumer to quickly locate the object
2329 // file where the type's definition is located, so it might be
2330 // best to make this behavior a command line or debugger tuning
2331 // option.
2332 FullSourceLoc Loc(D->getLocation(), CGM.getContext().getSourceManager());
2333 if (Module *M = ClangModuleMap->inferModuleFromLocation(Loc)) {
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002334 // This is a (sub-)module.
Adrian Prantl9402cef2015-09-20 16:51:35 +00002335 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
2336 return getOrCreateModuleRef(Info, /*SkeletonCU=*/false);
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002337 } else {
2338 // This the precompiled header being built.
2339 return getOrCreateModuleRef(PCHDescriptor, /*SkeletonCU=*/false);
Adrian Prantl9402cef2015-09-20 16:51:35 +00002340 }
2341 }
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002342
Adrian Prantl9402cef2015-09-20 16:51:35 +00002343 return nullptr;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002344}
2345
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002346llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002347 // Handle qualifiers, which recursively handles what they refer to.
2348 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002349 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002350
Guy Benyei11169dd2012-12-18 14:30:41 +00002351 // Work out details of type.
2352 switch (Ty->getTypeClass()) {
2353#define TYPE(Class, Base)
2354#define ABSTRACT_TYPE(Class, Base)
2355#define NON_CANONICAL_TYPE(Class, Base)
2356#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2357#include "clang/AST/TypeNodes.def"
2358 llvm_unreachable("Dependent types cannot show up in debug information");
2359
2360 case Type::ExtVector:
2361 case Type::Vector:
2362 return CreateType(cast<VectorType>(Ty), Unit);
2363 case Type::ObjCObjectPointer:
2364 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2365 case Type::ObjCObject:
2366 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2367 case Type::ObjCInterface:
2368 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2369 case Type::Builtin:
2370 return CreateType(cast<BuiltinType>(Ty));
2371 case Type::Complex:
2372 return CreateType(cast<ComplexType>(Ty));
2373 case Type::Pointer:
2374 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002375 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002376 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002377 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002378 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002379 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002380 case Type::BlockPointer:
2381 return CreateType(cast<BlockPointerType>(Ty), Unit);
2382 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002383 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002384 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002385 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002386 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002387 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002388 case Type::FunctionProto:
2389 case Type::FunctionNoProto:
2390 return CreateType(cast<FunctionType>(Ty), Unit);
2391 case Type::ConstantArray:
2392 case Type::VariableArray:
2393 case Type::IncompleteArray:
2394 return CreateType(cast<ArrayType>(Ty), Unit);
2395
2396 case Type::LValueReference:
2397 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2398 case Type::RValueReference:
2399 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2400
2401 case Type::MemberPointer:
2402 return CreateType(cast<MemberPointerType>(Ty), Unit);
2403
2404 case Type::Atomic:
2405 return CreateType(cast<AtomicType>(Ty), Unit);
2406
Xiuli Pan9c14e282016-01-09 12:53:17 +00002407 case Type::Pipe:
2408 return CreateType(cast<PipeType>(Ty), Unit);
2409
Guy Benyei11169dd2012-12-18 14:30:41 +00002410 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002411 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2412
David Blaikie42edade2014-11-11 20:44:45 +00002413 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002414 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002415 case Type::Elaborated:
2416 case Type::Paren:
2417 case Type::SubstTemplateTypeParm:
2418 case Type::TypeOfExpr:
2419 case Type::TypeOf:
2420 case Type::Decltype:
2421 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002422 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002423 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002424 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002425
David Blaikie42edade2014-11-11 20:44:45 +00002426 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002427}
2428
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002429llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2430 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002431 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002432
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002433 auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002434
2435 // We may have cached a forward decl when we could have created
2436 // a non-forward decl. Go ahead and create a non-forward decl
2437 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002438 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002439 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002440
2441 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002442 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002443
2444 // Propagate members from the declaration to the definition
2445 // CreateType(const RecordType*) will overwrite this with the members in the
2446 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002447 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002448
Guy Benyei11169dd2012-12-18 14:30:41 +00002449 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002450 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002451 return Res;
2452}
2453
2454// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002455llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002456 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002457
Guy Benyei11169dd2012-12-18 14:30:41 +00002458 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002459 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002460 unsigned Line = getLineNumber(RD->getLocation());
2461 StringRef RDName = getClassName(RD);
2462
Amjad Abouddc4531e2016-04-30 01:44:38 +00002463 llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002464
David Blaikied2785892013-08-18 17:36:19 +00002465 // If we ended up creating the type during the context chain construction,
2466 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002467 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002468 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002469 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002470 return T;
David Blaikied2785892013-08-18 17:36:19 +00002471
Adrian Prantl381e7552014-02-04 21:29:50 +00002472 // If this is just a forward or incomplete declaration, construct an
2473 // appropriately marked node and just return it.
2474 const RecordDecl *D = RD->getDefinition();
2475 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002476 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002477
2478 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2479 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002480
Manman Rene0064d82013-08-29 23:19:58 +00002481 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2482
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002483 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002484 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align, 0,
2485 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002486
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00002487 // Elements of composite types usually have back to the type, creating
2488 // uniquing cycles. Distinct nodes are more efficient.
2489 switch (RealDecl->getTag()) {
2490 default:
2491 llvm_unreachable("invalid composite type tag");
2492
2493 case llvm::dwarf::DW_TAG_array_type:
2494 case llvm::dwarf::DW_TAG_enumeration_type:
2495 // Array elements and most enumeration elements don't have back references,
2496 // so they don't tend to be involved in uniquing cycles and there is some
2497 // chance of merging them when linking together two modules. Only make
2498 // them distinct if they are ODR-uniqued.
2499 if (FullName.empty())
2500 break;
2501
2502 case llvm::dwarf::DW_TAG_structure_type:
2503 case llvm::dwarf::DW_TAG_union_type:
2504 case llvm::dwarf::DW_TAG_class_type:
2505 // Immediatley resolve to a distinct node.
2506 RealDecl =
2507 llvm::MDNode::replaceWithDistinct(llvm::TempDICompositeType(RealDecl));
2508 break;
2509 }
2510
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002511 RegionMap[Ty->getDecl()].reset(RealDecl);
2512 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002513
David Blaikieadfbf992013-08-18 16:55:33 +00002514 if (const ClassTemplateSpecializationDecl *TSpecial =
2515 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002516 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002517 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002518 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002519}
2520
David Blaikieadfbf992013-08-18 16:55:33 +00002521void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002522 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002523 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002524 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002525 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2526 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002527 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002528 while (1) {
2529 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2530 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2531 if (PBT && !BRL.isPrimaryBaseVirtual())
2532 PBase = PBT;
2533 else
2534 break;
2535 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002536 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002537 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2538 getOrCreateFile(RD->getLocation())));
2539 } else if (RD->isDynamicClass())
2540 ContainingType = RealDecl;
2541
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002542 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002543}
2544
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002545llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002546 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002547 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002548 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2549 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002550 llvm::DIType *Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002551 FieldAlign, *Offset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002552 *Offset += FieldSize;
2553 return Ty;
2554}
2555
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002556void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002557 StringRef &Name,
2558 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002559 llvm::DIScope *&FDContext,
2560 llvm::DINodeArray &TParamsArray,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002561 unsigned &Flags) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002562 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2563 Name = getFunctionName(FD);
2564 // Use mangled name as linkage name for C/C++ functions.
2565 if (FD->hasPrototype()) {
2566 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002567 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002568 }
2569 // No need to replicate the linkage name if it isn't different from the
2570 // subprogram name, no need to have it at all unless coverage is enabled or
2571 // debug is set to more than just line tables.
Benjamin Kramer8c305922016-02-02 11:06:51 +00002572 if (LinkageName == Name || (!CGM.getCodeGenOpts().EmitGcovArcs &&
2573 !CGM.getCodeGenOpts().EmitGcovNotes &&
2574 DebugKind <= codegenoptions::DebugLineTablesOnly))
Frederic Riss9db79f12014-11-18 03:40:46 +00002575 LinkageName = StringRef();
2576
Benjamin Kramer8c305922016-02-02 11:06:51 +00002577 if (DebugKind >= codegenoptions::LimitedDebugInfo) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002578 if (const NamespaceDecl *NSDecl =
2579 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2580 FDContext = getOrCreateNameSpace(NSDecl);
2581 else if (const RecordDecl *RDecl =
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002582 dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) {
2583 llvm::DIScope *Mod = getParentModuleOrNull(RDecl);
2584 FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU);
2585 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002586 // Collect template parameters.
2587 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2588 }
2589}
2590
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002591void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002592 unsigned &LineNo, QualType &T,
2593 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002594 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002595 Unit = getOrCreateFile(VD->getLocation());
2596 LineNo = getLineNumber(VD->getLocation());
2597
2598 setLocation(VD->getLocation());
2599
2600 T = VD->getType();
2601 if (T->isIncompleteArrayType()) {
2602 // CodeGen turns int[] into int[1] so we'll do the same here.
2603 llvm::APInt ConstVal(32, 1);
2604 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2605
2606 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2607 ArrayType::Normal, 0);
2608 }
2609
2610 Name = VD->getName();
2611 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2612 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2613 LinkageName = CGM.getMangledName(VD);
2614 if (LinkageName == Name)
2615 LinkageName = StringRef();
2616
2617 // Since we emit declarations (DW_AT_members) for static members, place the
2618 // definition of those static members in the namespace they were declared in
2619 // in the source code (the lexical decl context).
2620 // FIXME: Generalize this for even non-member global variables where the
2621 // declaration and definition may have different lexical decl contexts, once
2622 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002623 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2624 : VD->getDeclContext();
2625 // When a record type contains an in-line initialization of a static data
2626 // member, and the record type is marked as __declspec(dllexport), an implicit
2627 // definition of the member will be created in the record context. DWARF
2628 // doesn't seem to have a nice way to describe this in a form that consumers
2629 // are likely to understand, so fake the "normal" situation of a definition
2630 // outside the class by putting it in the global scope.
2631 if (DC->isRecord())
2632 DC = CGM.getContext().getTranslationUnitDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002633
Amjad Abouddc4531e2016-04-30 01:44:38 +00002634 llvm::DIScope *Mod = getParentModuleOrNull(VD);
2635 VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
Frederic Riss9db79f12014-11-18 03:40:46 +00002636}
2637
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002638llvm::DISubprogram *
Frederic Rissd253ed62014-11-18 03:40:51 +00002639CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002640 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00002641 StringRef Name, LinkageName;
2642 unsigned Flags = 0;
2643 SourceLocation Loc = FD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002644 llvm::DIFile *Unit = getOrCreateFile(Loc);
2645 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002646 unsigned Line = getLineNumber(Loc);
2647
2648 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2649 TParamsArray, Flags);
2650 // Build function type.
2651 SmallVector<QualType, 16> ArgTypes;
2652 for (const ParmVarDecl *Parm: FD->parameters())
2653 ArgTypes.push_back(Parm->getType());
Reid Klecknerf00f8032016-06-08 20:41:54 +00002654 CallingConv CC = FD->getType()->castAs<FunctionType>()->getCallConv();
2655 QualType FnType = CGM.getContext().getFunctionType(
2656 FD->getReturnType(), ArgTypes, FunctionProtoType::ExtProtoInfo(CC));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002657 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002658 DContext, Name, LinkageName, Unit, Line,
2659 getOrCreateFunctionType(FD, FnType, Unit), !FD->isExternallyVisible(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002660 /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002661 TParamsArray.get(), getFunctionDeclaration(FD));
Frederic Rissd253ed62014-11-18 03:40:51 +00002662 const FunctionDecl *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002663 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2664 std::make_tuple(CanonDecl),
2665 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00002666 return SP;
2667}
2668
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002669llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00002670CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2671 QualType T;
2672 StringRef Name, LinkageName;
2673 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002674 llvm::DIFile *Unit = getOrCreateFile(Loc);
2675 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002676 unsigned Line = getLineNumber(Loc);
2677
2678 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002679 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
2680 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
2681 !VD->isExternallyVisible(), nullptr, nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002682 FwdDeclReplaceMap.emplace_back(
2683 std::piecewise_construct,
2684 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2685 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002686 return GV;
2687}
2688
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002689llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002690 // We only need a declaration (not a definition) of the type - so use whatever
2691 // we would otherwise do to get a type for a pointee. (forward declarations in
2692 // limited debug info, full definitions (if the type definition is available)
2693 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002694 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2695 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002696 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002697 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002698
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002699 if (I != DeclCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002700 return dyn_cast_or_null<llvm::DINode>(I->second);
Frederic Rissd253ed62014-11-18 03:40:51 +00002701
2702 // No definition for now. Emit a forward definition that might be
2703 // merged with a potential upcoming definition.
2704 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
2705 return getFunctionForwardDeclaration(FD);
2706 else if (const auto *VD = dyn_cast<VarDecl>(D))
2707 return getGlobalVariableForwardDeclaration(VD);
2708
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002709 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00002710}
2711
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002712llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002713 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002714 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00002715
Guy Benyei11169dd2012-12-18 14:30:41 +00002716 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002717 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002718 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002719
2720 // Setup context.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002721 auto *S = getDeclContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00002722
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002723 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002724 if (MI == SPCache.end()) {
Eric Christopherf86c4052013-08-28 23:12:10 +00002725 if (const CXXMethodDecl *MD =
2726 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00002727 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002728 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00002729 }
2730 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002731 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002732 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002733 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002734 return SP;
2735 }
2736
Aaron Ballman86c93902014-03-06 23:45:36 +00002737 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002738 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002739 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002740 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002741 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002742 return SP;
2743 }
2744 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002745 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002746}
2747
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002748// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00002749// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002750llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002751 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002752 llvm::DIFile *F) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002753 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002754 // Create fake but valid subroutine type. Otherwise -verify would fail, and
2755 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Eric Christopher28a6db52015-10-15 06:56:08 +00002756 return DBuilder.createSubroutineType(DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002757
2758 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2759 return getOrCreateMethodType(Method, F);
Reid Klecknerf00f8032016-06-08 20:41:54 +00002760
2761 const auto *FTy = FnType->getAs<FunctionType>();
2762 CallingConv CC = FTy ? FTy->getCallConv() : CallingConv::CC_C;
2763
Guy Benyei11169dd2012-12-18 14:30:41 +00002764 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2765 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002766 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002767
2768 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002769 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002770
2771 // Replace the instancetype keyword with the actual type.
2772 if (ResultTy == CGM.getContext().getObjCInstanceType())
2773 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002774 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002775
Adrian Prantl7bec9032013-05-10 21:08:31 +00002776 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002777 // "self" pointer is always first argument.
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002778 QualType SelfDeclTy;
2779 if (auto *SelfDecl = OMethod->getSelfDecl())
2780 SelfDeclTy = SelfDecl->getType();
2781 else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType))
2782 if (FPT->getNumParams() > 1)
2783 SelfDeclTy = FPT->getParamType(0);
2784 if (!SelfDeclTy.isNull())
2785 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002786 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002787 Elts.push_back(DBuilder.createArtificialType(
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002788 getOrCreateType(CGM.getContext().getObjCSelType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002789 // Get rest of the arguments.
David Majnemer59f77922016-06-24 04:05:48 +00002790 for (const auto *PI : OMethod->parameters())
Aaron Ballman43b68be2014-03-07 17:50:17 +00002791 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002792 // Variadic methods need a special marker at the end of the type list.
2793 if (OMethod->isVariadic())
2794 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002795
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002796 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Reid Klecknerf00f8032016-06-08 20:41:54 +00002797 return DBuilder.createSubroutineType(EltTypeArray, 0, getDwarfCC(CC));
Guy Benyei11169dd2012-12-18 14:30:41 +00002798 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002799
Adrian Prantl800faef2014-02-25 23:42:18 +00002800 // Handle variadic function types; they need an additional
2801 // unspecified parameter.
Adrian Prantld45ba252014-02-25 19:38:11 +00002802 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2803 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002804 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00002805 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2806 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2807 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2808 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2809 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002810 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Reid Klecknerf00f8032016-06-08 20:41:54 +00002811 return DBuilder.createSubroutineType(EltTypeArray, 0, getDwarfCC(CC));
Adrian Prantld45ba252014-02-25 19:38:11 +00002812 }
2813
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002814 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002815}
2816
Eric Christophere7b87e52014-10-26 23:40:33 +00002817void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2818 SourceLocation ScopeLoc, QualType FnType,
2819 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002820
2821 StringRef Name;
2822 StringRef LinkageName;
2823
2824 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2825
2826 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002827 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00002828
Guy Benyei11169dd2012-12-18 14:30:41 +00002829 unsigned Flags = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002830 llvm::DIFile *Unit = getOrCreateFile(Loc);
2831 llvm::DIScope *FDContext = Unit;
2832 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00002833 if (!HasDecl) {
2834 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00002835 LinkageName = Fn->getName();
Guy Benyei11169dd2012-12-18 14:30:41 +00002836 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002837 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002838 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002839 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002840 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002841 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002842 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002843 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002844 return;
2845 }
2846 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002847 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2848 TParamsArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002849 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2850 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002851 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002852 } else {
2853 // Use llvm function name.
2854 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002855 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002856 }
2857 if (!Name.empty() && Name[0] == '\01')
2858 Name = Name.substr(1);
2859
Adrian Prantl42d71b92014-04-10 23:21:53 +00002860 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002861 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl42d71b92014-04-10 23:21:53 +00002862 // Artificial functions without a location should not silently reuse CurLoc.
2863 if (Loc.isInvalid())
2864 CurLoc = SourceLocation();
2865 }
2866 unsigned LineNo = getLineNumber(Loc);
2867 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002868
Eric Christopher8018e412014-03-27 18:50:35 +00002869 // FIXME: The function declaration we're constructing here is mostly reusing
2870 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2871 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2872 // all subprograms instead of the actual context since subprogram definitions
2873 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002874 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00002875 FDContext, Name, LinkageName, Unit, LineNo,
2876 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002877 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002878 TParamsArray.get(), getFunctionDeclaration(D));
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002879 Fn->setSubprogram(SP);
Frederic Rissb1ab28c2014-11-05 19:19:04 +00002880 // We might get here with a VarDecl in the case we're generating
2881 // code for the initialization of globals. Do not record these decls
2882 // as they will overwrite the actual VarDecl Decl in the cache.
2883 if (HasDecl && isa<FunctionDecl>(D))
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002884 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(SP));
Guy Benyei11169dd2012-12-18 14:30:41 +00002885
Adrian Prantlbebb8932014-03-21 21:01:58 +00002886 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002887 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00002888
Guy Benyei11169dd2012-12-18 14:30:41 +00002889 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002890 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002891}
2892
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002893void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
2894 QualType FnType) {
2895 StringRef Name;
2896 StringRef LinkageName;
2897
2898 const Decl *D = GD.getDecl();
2899 if (!D)
2900 return;
2901
2902 unsigned Flags = 0;
2903 llvm::DIFile *Unit = getOrCreateFile(Loc);
Adrian Prantlf0cd6772015-10-04 23:23:04 +00002904 llvm::DIScope *FDContext = getDeclContextDescriptor(D);
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002905 llvm::DINodeArray TParamsArray;
2906 if (isa<FunctionDecl>(D)) {
2907 // If there is a DISubprogram for this function available then use it.
2908 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2909 TParamsArray, Flags);
2910 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2911 Name = getObjCMethodName(OMD);
2912 Flags |= llvm::DINode::FlagPrototyped;
2913 } else {
2914 llvm_unreachable("not a function or ObjC method");
2915 }
2916 if (!Name.empty() && Name[0] == '\01')
2917 Name = Name.substr(1);
2918
2919 if (D->isImplicit()) {
2920 Flags |= llvm::DINode::FlagArtificial;
2921 // Artificial functions without a location should not silently reuse CurLoc.
2922 if (Loc.isInvalid())
2923 CurLoc = SourceLocation();
2924 }
2925 unsigned LineNo = getLineNumber(Loc);
2926 unsigned ScopeLine = 0;
2927
Adrian Prantle76bda52016-04-15 15:55:45 +00002928 DBuilder.retainType(DBuilder.createFunction(
2929 FDContext, Name, LinkageName, Unit, LineNo,
2930 getOrCreateFunctionType(D, FnType, Unit), false /*internalLinkage*/,
2931 false /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
2932 TParamsArray.get(), getFunctionDeclaration(D)));
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002933}
2934
David Blaikie835afb22015-01-21 23:08:17 +00002935void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002936 // Update our current location
2937 setLocation(Loc);
2938
Eric Christophere7b87e52014-10-26 23:40:33 +00002939 if (CurLoc.isInvalid() || CurLoc.isMacroID())
2940 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002941
Adrian Prantle83b1302014-01-07 22:05:52 +00002942 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00002943 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
David Blaikie835afb22015-01-21 23:08:17 +00002944 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002945}
2946
Guy Benyei11169dd2012-12-18 14:30:41 +00002947void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002948 llvm::MDNode *Back = nullptr;
2949 if (!LexicalBlockStack.empty())
2950 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002951 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002952 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002953 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002954}
2955
Eric Christopher0fdcb312013-05-16 00:52:20 +00002956void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2957 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002958 // Set our current location.
2959 setLocation(Loc);
2960
Guy Benyei11169dd2012-12-18 14:30:41 +00002961 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00002962 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2963 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00002964
Benjamin Kramer8c305922016-02-02 11:06:51 +00002965 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00002966 return;
2967
2968 // Create a new lexical block and push it on the stack.
2969 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002970}
2971
Eric Christopher0fdcb312013-05-16 00:52:20 +00002972void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2973 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002974 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2975
2976 // Provide an entry in the line table for the end of the block.
2977 EmitLocation(Builder, Loc);
2978
Benjamin Kramer8c305922016-02-02 11:06:51 +00002979 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00002980 return;
2981
Guy Benyei11169dd2012-12-18 14:30:41 +00002982 LexicalBlockStack.pop_back();
2983}
2984
Guy Benyei11169dd2012-12-18 14:30:41 +00002985void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2986 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2987 unsigned RCount = FnBeginRegionCount.back();
2988 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2989
2990 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00002991 while (LexicalBlockStack.size() != RCount) {
2992 // Provide an entry in the line table for the end of the block.
2993 EmitLocation(Builder, CurLoc);
2994 LexicalBlockStack.pop_back();
2995 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002996 FnBeginRegionCount.pop_back();
2997}
2998
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002999llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003000 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003001
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003002 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00003003 QualType FType;
3004 uint64_t FieldSize, FieldOffset;
3005 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003006
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003007 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003008 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00003009
3010 FieldOffset = 0;
3011 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
3012 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
3013 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
3014 FType = CGM.getContext().IntTy;
3015 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
3016 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
3017
3018 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
3019 if (HasCopyAndDispose) {
3020 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003021 EltTys.push_back(
3022 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
3023 EltTys.push_back(
3024 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00003025 }
3026 bool HasByrefExtendedLayout;
3027 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00003028 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
3029 HasByrefExtendedLayout) &&
3030 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00003031 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003032 EltTys.push_back(
3033 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00003034 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003035
Guy Benyei11169dd2012-12-18 14:30:41 +00003036 CharUnits Align = CGM.getContext().getDeclAlign(VD);
3037 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003038 CGM.getTarget().getPointerAlign(0))) {
3039 CharUnits FieldOffsetInBytes =
3040 CGM.getContext().toCharUnitsFromBits(FieldOffset);
Rui Ueyama83aa9792016-01-14 21:00:27 +00003041 CharUnits AlignedOffsetInBytes = FieldOffsetInBytes.alignTo(Align);
Eric Christophere7b87e52014-10-26 23:40:33 +00003042 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003043
Guy Benyei11169dd2012-12-18 14:30:41 +00003044 if (NumPaddingBytes.isPositive()) {
3045 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
3046 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
3047 pad, ArrayType::Normal, 0);
3048 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
3049 }
3050 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003051
Guy Benyei11169dd2012-12-18 14:30:41 +00003052 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003053 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003054 FieldSize = CGM.getContext().getTypeSize(FType);
3055 FieldAlign = CGM.getContext().toBits(Align);
3056
Eric Christopherb2a008c2013-05-16 00:45:12 +00003057 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00003058 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
3059 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003060 EltTys.push_back(FieldTy);
3061 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003062
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003063 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003064
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003065 unsigned Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003066
Guy Benyei11169dd2012-12-18 14:30:41 +00003067 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003068 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00003069}
3070
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003071void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::Value *Storage,
3072 llvm::Optional<unsigned> ArgNo,
Eric Christophere7b87e52014-10-26 23:40:33 +00003073 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003074 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003075 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003076 if (VD->hasAttr<NoDebugAttr>())
3077 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003078
David Blaikie7fceebf2013-08-19 03:37:48 +00003079 bool Unwritten =
3080 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
3081 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003082 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00003083 if (!Unwritten)
3084 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003085 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003086 uint64_t XOffset = 0;
3087 if (VD->hasAttr<BlocksAttr>())
3088 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003089 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003090 Ty = getOrCreateType(VD->getType(), Unit);
3091
3092 // If there is no debug info for this type then do not emit debug info
3093 // for this variable.
3094 if (!Ty)
3095 return;
3096
Guy Benyei11169dd2012-12-18 14:30:41 +00003097 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00003098 unsigned Line = 0;
3099 unsigned Column = 0;
3100 if (!Unwritten) {
3101 Line = getLineNumber(VD->getLocation());
3102 Column = getColumnNumber(VD->getLocation());
3103 }
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003104 SmallVector<int64_t, 9> Expr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003105 unsigned Flags = 0;
3106 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003107 Flags |= llvm::DINode::FlagArtificial;
Guy Benyei11169dd2012-12-18 14:30:41 +00003108 // If this is the first argument and it is implicit then
3109 // give it an object pointer flag.
3110 // FIXME: There has to be a better way to do this, but for static
3111 // functions there won't be an implicit param at arg1 and
3112 // otherwise it is 'self' or 'this'.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003113 if (isa<ImplicitParamDecl>(VD) && ArgNo && *ArgNo == 1)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003114 Flags |= llvm::DINode::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00003115 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00003116 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
3117 !VD->getType()->isPointerType())
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003118 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei11169dd2012-12-18 14:30:41 +00003119
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003120 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003121
3122 StringRef Name = VD->getName();
3123 if (!Name.empty()) {
3124 if (VD->hasAttr<BlocksAttr>()) {
3125 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003126 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003127 // offset of __forwarding field
3128 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003129 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003130 Expr.push_back(offset.getQuantity());
3131 Expr.push_back(llvm::dwarf::DW_OP_deref);
3132 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003133 // offset of x field
3134 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003135 Expr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003136
3137 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003138 auto *D = ArgNo
3139 ? DBuilder.createParameterVariable(Scope, VD->getName(),
3140 *ArgNo, Unit, Line, Ty)
3141 : DBuilder.createAutoVariable(Scope, VD->getName(), Unit,
3142 Line, Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003143
Guy Benyei11169dd2012-12-18 14:30:41 +00003144 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003145 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3146 llvm::DebugLoc::get(Line, Column, Scope),
3147 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003148 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00003149 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003150 Expr.push_back(llvm::dwarf::DW_OP_deref);
Adrian Prantl0d820892015-04-29 15:05:50 +00003151 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
3152 // If VD is an anonymous union then Storage represents value for
3153 // all union fields.
3154 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
3155 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00003156 // GDB has trouble finding local variables in anonymous unions, so we emit
3157 // artifical local variables for each of the members.
3158 //
3159 // FIXME: Remove this code as soon as GDB supports this.
3160 // The debug info verifier in LLVM operates based on the assumption that a
3161 // variable has the same size as its storage and we had to disable the check
3162 // for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00003163 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003164 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00003165 StringRef FieldName = Field->getName();
3166
3167 // Ignore unnamed fields. Do not ignore unnamed records.
3168 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
3169 continue;
3170
3171 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003172 auto *D = DBuilder.createAutoVariable(
3173 Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
3174 Flags | llvm::DINode::FlagArtificial);
Adrian Prantl0d820892015-04-29 15:05:50 +00003175
3176 // Insert an llvm.dbg.declare into the current block.
3177 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3178 llvm::DebugLoc::get(Line, Column, Scope),
3179 Builder.GetInsertBlock());
3180 }
Adrian Prantl0d820892015-04-29 15:05:50 +00003181 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003182 }
David Blaikiea76a7c92013-01-05 05:58:35 +00003183
3184 // Create the descriptor for the variable.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003185 auto *D =
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003186 ArgNo
3187 ? DBuilder.createParameterVariable(Scope, Name, *ArgNo, Unit, Line,
3188 Ty, CGM.getLangOpts().Optimize,
3189 Flags)
3190 : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
3191 CGM.getLangOpts().Optimize, Flags);
David Blaikiea76a7c92013-01-05 05:58:35 +00003192
3193 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003194 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3195 llvm::DebugLoc::get(Line, Column, Scope),
3196 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003197}
3198
3199void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
3200 llvm::Value *Storage,
3201 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003202 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003203 EmitDeclare(VD, Storage, llvm::None, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003204}
3205
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003206llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
3207 llvm::DIType *Ty) {
3208 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003209 if (CachedTy)
3210 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00003211 return DBuilder.createObjectPointerType(Ty);
3212}
3213
Eric Christophere7b87e52014-10-26 23:40:33 +00003214void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
3215 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00003216 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003217 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003218 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00003219
Craig Topper8a13c412014-05-21 05:09:00 +00003220 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00003221 return;
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003222 if (VD->hasAttr<NoDebugAttr>())
3223 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003224
Guy Benyei11169dd2012-12-18 14:30:41 +00003225 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00003226
Guy Benyei11169dd2012-12-18 14:30:41 +00003227 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003228 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
3229 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003230 if (isByRef)
3231 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003232 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003233 Ty = getOrCreateType(VD->getType(), Unit);
3234
3235 // Self is passed along as an implicit non-arg variable in a
3236 // block. Mark it as the object pointer.
3237 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00003238 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00003239
3240 // Get location information.
3241 unsigned Line = getLineNumber(VD->getLocation());
3242 unsigned Column = getColumnNumber(VD->getLocation());
3243
3244 const llvm::DataLayout &target = CGM.getDataLayout();
3245
3246 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00003247 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00003248 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
3249
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003250 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00003251 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003252 addr.push_back(llvm::dwarf::DW_OP_deref);
3253 addr.push_back(llvm::dwarf::DW_OP_plus);
3254 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003255 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003256 addr.push_back(llvm::dwarf::DW_OP_deref);
3257 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003258 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00003259 offset =
3260 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003261 addr.push_back(offset.getQuantity());
3262 addr.push_back(llvm::dwarf::DW_OP_deref);
3263 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003264 // offset of x field
3265 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003266 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003267 }
3268
3269 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003270 auto *D = DBuilder.createAutoVariable(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003271 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003272 Line, Ty);
Adrian Prantl0f6df002013-03-29 19:20:35 +00003273
Guy Benyei11169dd2012-12-18 14:30:41 +00003274 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003275 auto DL = llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back());
3276 if (InsertPoint)
3277 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3278 InsertPoint);
3279 else
3280 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3281 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003282}
3283
Guy Benyei11169dd2012-12-18 14:30:41 +00003284void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3285 unsigned ArgNo,
3286 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003287 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003288 EmitDeclare(VD, AI, ArgNo, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003289}
3290
3291namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003292struct BlockLayoutChunk {
3293 uint64_t OffsetInBits;
3294 const BlockDecl::Capture *Capture;
3295};
3296bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3297 return l.OffsetInBits < r.OffsetInBits;
3298}
Guy Benyei11169dd2012-12-18 14:30:41 +00003299}
3300
3301void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003302 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003303 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003304 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003305 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003306 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003307 ASTContext &C = CGM.getContext();
3308 const BlockDecl *blockDecl = block.getBlockDecl();
3309
3310 // Collect some general information about the block's location.
3311 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003312 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003313 unsigned line = getLineNumber(loc);
3314 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003315
Guy Benyei11169dd2012-12-18 14:30:41 +00003316 // Build the debug-info type for the block literal.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003317 getDeclContextDescriptor(blockDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003318
3319 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003320 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003321
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003322 SmallVector<llvm::Metadata *, 16> fields;
David Majnemerb4b671e2016-06-30 03:01:59 +00003323 fields.push_back(createFieldType("__isa", C.VoidPtrTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003324 blockLayout->getElementOffsetInBits(0),
3325 tunit, tunit));
David Majnemerb4b671e2016-06-30 03:01:59 +00003326 fields.push_back(createFieldType("__flags", C.IntTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003327 blockLayout->getElementOffsetInBits(1),
3328 tunit, tunit));
David Majnemerb4b671e2016-06-30 03:01:59 +00003329 fields.push_back(createFieldType("__reserved", C.IntTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003330 blockLayout->getElementOffsetInBits(2),
3331 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003332 auto *FnTy = block.getBlockExpr()->getFunctionType();
3333 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
David Majnemerb4b671e2016-06-30 03:01:59 +00003334 fields.push_back(createFieldType("__FuncPtr", FnPtrType, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003335 blockLayout->getElementOffsetInBits(3),
3336 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003337 fields.push_back(createFieldType(
3338 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3339 ? C.getBlockDescriptorExtendedType()
3340 : C.getBlockDescriptorType()),
David Majnemerb4b671e2016-06-30 03:01:59 +00003341 loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003342
3343 // We want to sort the captures by offset, not because DWARF
3344 // requires this, but because we're paranoid about debuggers.
3345 SmallVector<BlockLayoutChunk, 8> chunks;
3346
3347 // 'this' capture.
3348 if (blockDecl->capturesCXXThis()) {
3349 BlockLayoutChunk chunk;
3350 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003351 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003352 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003353 chunks.push_back(chunk);
3354 }
3355
3356 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003357 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003358 const VarDecl *variable = capture.getVariable();
3359 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3360
3361 // Ignore constant captures.
3362 if (captureInfo.isConstant())
3363 continue;
3364
3365 BlockLayoutChunk chunk;
3366 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003367 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003368 chunk.Capture = &capture;
3369 chunks.push_back(chunk);
3370 }
3371
3372 // Sort by offset.
3373 llvm::array_pod_sort(chunks.begin(), chunks.end());
3374
Eric Christophere7b87e52014-10-26 23:40:33 +00003375 for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
3376 e = chunks.end();
3377 i != e; ++i) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003378 uint64_t offsetInBits = i->OffsetInBits;
3379 const BlockDecl::Capture *capture = i->Capture;
3380
3381 // If we have a null capture, this must be the C++ 'this' capture.
3382 if (!capture) {
Adrian Prantl2526fca2016-04-18 23:48:16 +00003383 QualType type;
3384 if (auto *Method =
3385 cast_or_null<CXXMethodDecl>(blockDecl->getNonClosureContext()))
3386 type = Method->getThisType(C);
3387 else if (auto *RDecl = dyn_cast<CXXRecordDecl>(blockDecl->getParent()))
3388 type = QualType(RDecl->getTypeForDecl(), 0);
3389 else
3390 llvm_unreachable("unexpected block declcontext");
Guy Benyei11169dd2012-12-18 14:30:41 +00003391
David Majnemerb4b671e2016-06-30 03:01:59 +00003392 fields.push_back(createFieldType("this", type, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003393 offsetInBits, tunit, tunit));
3394 continue;
3395 }
3396
3397 const VarDecl *variable = capture->getVariable();
3398 StringRef name = variable->getName();
3399
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003400 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003401 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003402 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003403
3404 // FIXME: this creates a second copy of this type!
3405 uint64_t xoffset;
3406 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003407 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3408 fieldType =
3409 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3410 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003411 } else {
David Majnemerb4b671e2016-06-30 03:01:59 +00003412 fieldType = createFieldType(name, variable->getType(), loc, AS_public,
Eric Christophere7b87e52014-10-26 23:40:33 +00003413 offsetInBits, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003414 }
3415 fields.push_back(fieldType);
3416 }
3417
3418 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003419 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3420 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003421
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003422 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003423
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003424 llvm::DIType *type = DBuilder.createStructType(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003425 tunit, typeName.str(), tunit, line,
3426 CGM.getContext().toBits(block.BlockSize),
3427 CGM.getContext().toBits(block.BlockAlign), 0, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003428 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3429
3430 // Get overall information about the block.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003431 unsigned flags = llvm::DINode::FlagArtificial;
3432 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003433
3434 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003435 auto *debugVar = DBuilder.createParameterVariable(
3436 scope, Arg->getName(), ArgNo, tunit, line, type,
3437 CGM.getLangOpts().Optimize, flags);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003438
Adrian Prantl616bef42013-03-14 21:52:59 +00003439 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003440 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003441 DBuilder.insertDbgValueIntrinsic(
Eric Christophere7b87e52014-10-26 23:40:33 +00003442 LocalAddr, 0, debugVar, DBuilder.createExpression(),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003443 llvm::DebugLoc::get(line, column, scope), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003444 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003445
Adrian Prantl616bef42013-03-14 21:52:59 +00003446 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003447 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
3448 llvm::DebugLoc::get(line, column, scope),
3449 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003450}
3451
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003452llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003453CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3454 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003455 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003456
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003457 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003458 if (MI != StaticDataMemberCache.end()) {
3459 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00003460 return MI->second;
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003461 }
David Blaikiece763042013-08-20 21:49:21 +00003462
3463 // If the member wasn't found in the cache, lazily construct and add it to the
3464 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003465 auto DC = D->getDeclContext();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003466 auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003467 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003468}
3469
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003470llvm::DIGlobalVariable *CGDebugInfo::CollectAnonRecordDecls(
3471 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3472 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
3473 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003474
3475 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003476 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003477 StringRef FieldName = Field->getName();
3478
3479 // Ignore unnamed fields, but recurse into anonymous records.
3480 if (FieldName.empty()) {
3481 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3482 if (RT)
3483 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3484 Var, DContext);
3485 continue;
3486 }
3487 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003488 GV = DBuilder.createGlobalVariable(DContext, FieldName, LinkageName, Unit,
3489 LineNo, FieldTy,
3490 Var->hasInternalLinkage(), Var, nullptr);
Eric Christophercab9fae2014-04-10 05:20:00 +00003491 }
3492 return GV;
3493}
3494
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003495void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003496 const VarDecl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003497 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003498 if (D->hasAttr<NoDebugAttr>())
3499 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003500 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003501 llvm::DIFile *Unit = nullptr;
3502 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003503 unsigned LineNo;
3504 StringRef DeclName, LinkageName;
3505 QualType T;
3506 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003507
3508 // Attempt to store one global variable for the declaration - even if we
3509 // emit a lot of fields.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003510 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003511
3512 // If this is an anonymous union then we'll want to emit a global
3513 // variable for each member of the anonymous union so that it's possible
3514 // to find the name of any field in the union.
3515 if (T->isUnionType() && DeclName.empty()) {
Reid Kleckner43ecd7c2015-11-20 17:41:12 +00003516 const RecordDecl *RD = T->castAs<RecordType>()->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003517 assert(RD->isAnonymousStructOrUnion() &&
3518 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003519 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3520 } else {
David Blaikie7550b112014-10-20 17:42:23 +00003521 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003522 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3523 Var->hasInternalLinkage(), Var,
3524 getOrCreateStaticDataMemberDeclarationOrNull(D));
3525 }
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003526 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(GV));
Guy Benyei11169dd2012-12-18 14:30:41 +00003527}
3528
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003529void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3530 llvm::Constant *Init) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003531 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003532 if (VD->hasAttr<NoDebugAttr>())
3533 return;
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003534 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003535 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003536 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003537 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003538 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3539 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3540 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3541 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3542 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003543 // Do not use global variables for enums.
3544 //
3545 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003546 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003547 return;
David Blaikiea15565562014-04-04 20:56:17 +00003548 // Do not emit separate definitions for function local const/statics.
3549 if (isa<FunctionDecl>(VD->getDeclContext()))
3550 return;
David Blaikiebb113912014-04-05 07:23:17 +00003551 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003552 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003553 if (VarD->isStaticDataMember()) {
3554 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003555 getDeclContextDescriptor(VarD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003556 // Ensure that the type is retained even though it's otherwise unreferenced.
Duncan P. N. Exon Smith383f8412016-04-23 21:08:27 +00003557 //
3558 // FIXME: This is probably unnecessary, since Ty should reference RD
3559 // through its scope.
David Blaikie423eb5a2014-11-19 19:42:40 +00003560 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003561 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003562 return;
3563 }
3564
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003565 llvm::DIScope *DContext = getDeclContextDescriptor(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003566
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003567 auto &GV = DeclCache[VD];
3568 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003569 return;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003570 GV.reset(DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003571 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003572 true, Init, getOrCreateStaticDataMemberDeclarationOrNull(VarD)));
David Blaikiebd483762013-05-20 04:58:53 +00003573}
3574
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003575llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003576 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003577 return LexicalBlockStack.back();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00003578 llvm::DIScope *Mod = getParentModuleOrNull(D);
3579 return getContextDescriptor(D, Mod ? Mod : TheCU);
Guy Benyei11169dd2012-12-18 14:30:41 +00003580}
3581
David Blaikie9f88fe82013-04-22 06:13:21 +00003582void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003583 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00003584 return;
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00003585 const NamespaceDecl *NSDecl = UD.getNominatedNamespace();
3586 if (!NSDecl->isAnonymousNamespace() ||
3587 CGM.getCodeGenOpts().DebugExplicitImport) {
3588 DBuilder.createImportedModule(
3589 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3590 getOrCreateNameSpace(NSDecl),
3591 getLineNumber(UD.getLocation()));
3592 }
David Blaikie9f88fe82013-04-22 06:13:21 +00003593}
3594
David Blaikiebd483762013-05-20 04:58:53 +00003595void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003596 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00003597 return;
3598 assert(UD.shadow_size() &&
3599 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3600 // Emitting one decl is sufficient - debuggers can detect that this is an
3601 // overloaded name & provide lookup for all the overloads.
3602 const UsingShadowDecl &USD = **UD.shadow_begin();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003603 if (llvm::DINode *Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003604 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003605 DBuilder.createImportedDeclaration(
3606 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3607 getLineNumber(USD.getLocation()));
3608}
3609
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003610void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
David Blaikieaf09f4a2016-05-03 23:06:40 +00003611 if (CGM.getCodeGenOpts().getDebuggerTuning() != llvm::DebuggerKind::LLDB)
3612 return;
Adrian Prantl8a634c12015-12-18 19:44:31 +00003613 if (Module *M = ID.getImportedModule()) {
Chad Rosiere5dafd12015-12-18 20:08:40 +00003614 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
Adrian Prantl8a634c12015-12-18 19:44:31 +00003615 DBuilder.createImportedDeclaration(
3616 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
3617 getOrCreateModuleRef(Info, DebugTypeExtRefs),
3618 getLineNumber(ID.getLocation()));
3619 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003620}
3621
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003622llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00003623CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003624 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003625 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003626 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003627 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003628 return cast<llvm::DIImportedEntity>(VH);
3629 llvm::DIImportedEntity *R;
David Blaikief121b932013-05-20 22:50:41 +00003630 if (const NamespaceAliasDecl *Underlying =
3631 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3632 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003633 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003634 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3635 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3636 NA.getName());
3637 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003638 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003639 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3640 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3641 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003642 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003643 return R;
3644}
3645
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003646llvm::DINamespace *
Guy Benyei11169dd2012-12-18 14:30:41 +00003647CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003648 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003649 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003650 if (I != NameSpaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003651 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003652
Guy Benyei11169dd2012-12-18 14:30:41 +00003653 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003654 llvm::DIFile *FileD = getOrCreateFile(NSDecl->getLocation());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003655 llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003656 llvm::DINamespace *NS =
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003657 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003658 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003659 return NS;
3660}
3661
Adrian Prantla5206ce2015-09-22 23:26:43 +00003662void CGDebugInfo::setDwoId(uint64_t Signature) {
3663 assert(TheCU && "no main compile unit");
3664 TheCU->setDWOId(Signature);
3665}
3666
3667
Guy Benyei11169dd2012-12-18 14:30:41 +00003668void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003669 // Creating types might create further types - invalidating the current
3670 // element and the size(), so don't cache/reference them.
3671 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3672 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003673 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003674 ? CreateTypeDefinition(E.Type, E.Unit)
3675 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003676 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00003677 }
3678
David Blaikief427b002014-05-06 03:42:01 +00003679 for (auto p : ReplaceMap) {
3680 assert(p.second);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003681 auto *Ty = cast<llvm::DIType>(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003682 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003683
David Blaikief427b002014-05-06 03:42:01 +00003684 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003685 assert(it != TypeCache.end());
3686 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003687
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003688 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
3689 cast<llvm::DIType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00003690 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003691
Frederic Rissd253ed62014-11-18 03:40:51 +00003692 for (const auto &p : FwdDeclReplaceMap) {
3693 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003694 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003695 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003696
3697 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003698 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003699 // with ourselves, that will destroy the temporary MDNode and
3700 // replace it with a standard one, avoiding leaking memory.
3701 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003702 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003703 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003704 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003705
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003706 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00003707 }
3708
Adrian Prantl73409ce2013-03-11 18:33:46 +00003709 // We keep our own list of retained types, because we need to look
3710 // up the final type in the type cache.
Adrian Prantl3a884fa2015-08-27 22:56:46 +00003711 for (auto &RT : RetainedTypes)
Adrian Prantlad9a195e2015-08-27 21:21:19 +00003712 if (auto MD = TypeCache[RT])
3713 DBuilder.retainType(cast<llvm::DIType>(MD));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003714
Guy Benyei11169dd2012-12-18 14:30:41 +00003715 DBuilder.finalize();
3716}
David Blaikie66088d52014-09-24 17:01:27 +00003717
3718void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003719 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikie66088d52014-09-24 17:01:27 +00003720 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003721
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003722 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003723 // Don't ignore in case of explicit cast where it is referenced indirectly.
3724 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00003725}