blob: 13461980a4b4c0d550de533035f196fe7b81f78b [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 Kleckner31abd802016-06-30 17:41:31 +0000188 // Emit the unqualified name in normal operation. LLVM and the debugger can
189 // compute the fully qualified name from the scope chain. If we're only
190 // emitting line table info, there won't be any scope chains, so emit the
191 // fully qualified name here so that stack traces are more accurate.
192 // FIXME: Do this when emitting DWARF as well as when emitting CodeView after
193 // evaluating the size impact.
194 bool UseQualifiedName = DebugKind == codegenoptions::DebugLineTablesOnly &&
195 CGM.getCodeGenOpts().EmitCodeView;
196
197 if (!Info && FII && !UseQualifiedName)
Guy Benyei11169dd2012-12-18 14:30:41 +0000198 return FII->getName();
199
Benjamin Kramer9170e912013-02-22 15:46:01 +0000200 SmallString<128> NS;
201 llvm::raw_svector_ostream OS(NS);
Reid Kleckner60103382015-12-16 02:04:40 +0000202 PrintingPolicy Policy(CGM.getLangOpts());
Reid Kleckner829398e2016-06-17 16:11:20 +0000203 Policy.MSVCFormatting = CGM.getCodeGenOpts().EmitCodeView;
Reid Kleckner31abd802016-06-30 17:41:31 +0000204 if (!UseQualifiedName)
205 FD->printName(OS);
206 else
207 FD->printQualifiedName(OS, Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000208
Reid Kleckner829398e2016-06-17 16:11:20 +0000209 // Add any template specialization args.
210 if (Info) {
211 const TemplateArgumentList *TArgs = Info->TemplateArguments;
212 const TemplateArgument *Args = TArgs->data();
213 unsigned NumArgs = TArgs->size();
214 TemplateSpecializationType::PrintTemplateArgumentList(OS, Args, NumArgs,
215 Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000216 }
217
218 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000219 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000220}
221
222StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
223 SmallString<256> MethodName;
224 llvm::raw_svector_ostream OS(MethodName);
225 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
226 const DeclContext *DC = OMD->getDeclContext();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000227 if (const ObjCImplementationDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000228 dyn_cast<const ObjCImplementationDecl>(DC)) {
229 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000230 } else if (const ObjCInterfaceDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000231 dyn_cast<const ObjCInterfaceDecl>(DC)) {
232 OS << OID->getName();
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000233 } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(DC)) {
234 if (OC->IsClassExtension()) {
235 OS << OC->getClassInterface()->getName();
236 } else {
237 OS << ((const NamedDecl *)OC)->getIdentifier()->getNameStart() << '('
238 << OC->getIdentifier()->getNameStart() << ')';
239 }
Eric Christopherb2a008c2013-05-16 00:45:12 +0000240 } else if (const ObjCCategoryImplDecl *OCD =
Eric Christophere7b87e52014-10-26 23:40:33 +0000241 dyn_cast<const ObjCCategoryImplDecl>(DC)) {
242 OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '('
243 << OCD->getIdentifier()->getNameStart() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000244 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000245 // We can extract the type of the class from the self pointer.
Eric Christophere7b87e52014-10-26 23:40:33 +0000246 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000247 QualType ClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000248 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000249 ClassTy.print(OS, PrintingPolicy(LangOptions()));
250 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000251 }
252 OS << ' ' << OMD->getSelector().getAsString() << ']';
253
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000254 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000255}
256
Guy Benyei11169dd2012-12-18 14:30:41 +0000257StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000258 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000259}
260
Eric Christophere7b87e52014-10-26 23:40:33 +0000261StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
David Blaikie65813a32014-04-02 18:21:09 +0000262 // quick optimization to avoid having to intern strings that are already
263 // stored reliably elsewhere
264 if (!isa<ClassTemplateSpecializationDecl>(RD))
Guy Benyei11169dd2012-12-18 14:30:41 +0000265 return RD->getName();
266
David Blaikie65813a32014-04-02 18:21:09 +0000267 SmallString<128> Name;
Benjamin Kramer9170e912013-02-22 15:46:01 +0000268 {
David Blaikie65813a32014-04-02 18:21:09 +0000269 llvm::raw_svector_ostream OS(Name);
270 RD->getNameForDiagnostic(OS, CGM.getContext().getPrintingPolicy(),
271 /*Qualified*/ false);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000272 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000273
274 // Copy this name on the side and use its reference.
David Blaikie65813a32014-04-02 18:21:09 +0000275 return internString(Name);
Guy Benyei11169dd2012-12-18 14:30:41 +0000276}
277
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000278llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000279 if (!Loc.isValid())
280 // If Location is not valid then use main input file.
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000281 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
282 remapDIPath(TheCU->getDirectory()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000283
284 SourceManager &SM = CGM.getContext().getSourceManager();
285 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
286
287 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
288 // If the location is not valid then use main input file.
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000289 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
290 remapDIPath(TheCU->getDirectory()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000291
292 // Cache the results.
293 const char *fname = PLoc.getFilename();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000294 auto it = DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000295
296 if (it != DIFileCache.end()) {
297 // Verify that the information still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000298 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000299 return cast<llvm::DIFile>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000300 }
301
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000302 llvm::DIFile *F = DBuilder.createFile(remapDIPath(PLoc.getFilename()),
303 remapDIPath(getCurrentDirname()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000304
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000305 DIFileCache[fname].reset(F);
Guy Benyei11169dd2012-12-18 14:30:41 +0000306 return F;
307}
308
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000309llvm::DIFile *CGDebugInfo::getOrCreateMainFile() {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000310 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
311 remapDIPath(TheCU->getDirectory()));
312}
313
314std::string CGDebugInfo::remapDIPath(StringRef Path) const {
315 for (const auto &Entry : DebugPrefixMap)
316 if (Path.startswith(Entry.first))
317 return (Twine(Entry.second) + Path.substr(Entry.first.size())).str();
318 return Path.str();
Guy Benyei11169dd2012-12-18 14:30:41 +0000319}
320
Guy Benyei11169dd2012-12-18 14:30:41 +0000321unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
322 if (Loc.isInvalid() && CurLoc.isInvalid())
323 return 0;
324 SourceManager &SM = CGM.getContext().getSourceManager();
325 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000326 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000327}
328
Adrian Prantlc7822422013-03-12 20:43:25 +0000329unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000330 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000331 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000332 return 0;
333
334 // If the location is invalid then use the current column.
335 if (Loc.isInvalid() && CurLoc.isInvalid())
336 return 0;
337 SourceManager &SM = CGM.getContext().getSourceManager();
338 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000339 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000340}
341
342StringRef CGDebugInfo::getCurrentDirname() {
343 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
344 return CGM.getCodeGenOpts().DebugCompilationDir;
345
346 if (!CWDName.empty())
347 return CWDName;
348 SmallString<256> CWD;
349 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000350 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000351}
352
Guy Benyei11169dd2012-12-18 14:30:41 +0000353void CGDebugInfo::CreateCompileUnit() {
354
David Blaikieaabde052014-05-14 00:29:00 +0000355 // Should we be asking the SourceManager for the main file name, instead of
356 // accepting it as an argument? This just causes the main file name to
357 // mismatch with source locations and create extra lexical scopes or
358 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
359 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
360 // because that's what the SourceManager says)
361
Guy Benyei11169dd2012-12-18 14:30:41 +0000362 // Get absolute path name.
363 SourceManager &SM = CGM.getContext().getSourceManager();
364 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
365 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000366 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000367
368 // The main file name provided via the "-main-file-name" option contains just
369 // the file name itself with no path information. This file name may have had
370 // a relative path, so we look into the actual file entry for the main
371 // file to determine the real absolute path for the file.
372 std::string MainFileDir;
373 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000374 MainFileDir = remapDIPath(MainFile->getDir()->getName());
Yaron Keren9fb7e902013-10-21 20:07:37 +0000375 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000376 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
377 llvm::sys::path::append(MainFileDirSS, MainFileName);
378 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000379 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000380 }
381
Ed Masteda706022014-05-07 12:49:30 +0000382 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000383 const LangOptions &LO = CGM.getLangOpts();
384 if (LO.CPlusPlus) {
385 if (LO.ObjC1)
386 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
387 else
388 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
389 } else if (LO.ObjC1) {
390 LangTag = llvm::dwarf::DW_LANG_ObjC;
Pirama Arumuga Nainara7484c92016-06-21 21:35:11 +0000391 } else if (LO.RenderScript) {
392 LangTag = llvm::dwarf::DW_LANG_GOOGLE_RenderScript;
Guy Benyei11169dd2012-12-18 14:30:41 +0000393 } else if (LO.C99) {
394 LangTag = llvm::dwarf::DW_LANG_C99;
395 } else {
396 LangTag = llvm::dwarf::DW_LANG_C89;
397 }
398
399 std::string Producer = getClangFullVersion();
400
401 // Figure out which version of the ObjC runtime we have.
402 unsigned RuntimeVers = 0;
403 if (LO.ObjC1)
404 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
405
Adrian Prantl826824e2016-04-08 22:43:06 +0000406 llvm::DICompileUnit::DebugEmissionKind EmissionKind;
407 switch (DebugKind) {
408 case codegenoptions::NoDebugInfo:
409 case codegenoptions::LocTrackingOnly:
410 EmissionKind = llvm::DICompileUnit::NoDebug;
411 break;
412 case codegenoptions::DebugLineTablesOnly:
413 EmissionKind = llvm::DICompileUnit::LineTablesOnly;
414 break;
415 case codegenoptions::LimitedDebugInfo:
416 case codegenoptions::FullDebugInfo:
417 EmissionKind = llvm::DICompileUnit::FullDebug;
418 break;
419 }
420
Guy Benyei11169dd2012-12-18 14:30:41 +0000421 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000422 // FIXME - Eliminate TheCU.
Eric Christophere4200a22014-02-27 01:25:08 +0000423 TheCU = DBuilder.createCompileUnit(
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000424 LangTag, remapDIPath(MainFileName), remapDIPath(getCurrentDirname()),
425 Producer, LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers,
Adrian Prantl826824e2016-04-08 22:43:06 +0000426 CGM.getCodeGenOpts().SplitDwarfFile, EmissionKind, 0 /* DWOid */);
Guy Benyei11169dd2012-12-18 14:30:41 +0000427}
428
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000429llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000430 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000431 StringRef BTName;
432 switch (BT->getKind()) {
433#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000434#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000435#include "clang/AST/BuiltinTypes.def"
436 case BuiltinType::Dependent:
437 llvm_unreachable("Unexpected builtin type");
438 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000439 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000440 case BuiltinType::Void:
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000441 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +0000442 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000443 if (!ClassTy)
444 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
445 "objc_class", TheCU,
446 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000447 return ClassTy;
448 case BuiltinType::ObjCId: {
449 // typedef struct objc_class *Class;
450 // typedef struct objc_object {
451 // Class isa;
452 // } *id;
453
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000454 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000455 return ObjTy;
456
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000457 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000458 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
459 "objc_class", TheCU,
460 getOrCreateMainFile(), 0);
461
462 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000463
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000464 auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000465
Eric Christopher5c7ee8b2013-04-02 22:59:11 +0000466 ObjTy =
David Blaikie6d4fe152013-02-25 01:07:08 +0000467 DBuilder.createStructType(TheCU, "objc_object", getOrCreateMainFile(),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000468 0, 0, 0, 0, nullptr, llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000469
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000470 DBuilder.replaceArrays(
471 ObjTy,
472 DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
473 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0, 0, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000474 return ObjTy;
475 }
476 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000477 if (!SelTy)
478 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
479 "objc_selector", TheCU,
480 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000481 return SelTy;
482 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000483
Alexey Bader954ba212016-04-08 13:40:33 +0000484#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
485 case BuiltinType::Id: \
486 return getOrCreateStructPtrType("opencl_" #ImgType "_" #Suffix "_t", \
487 SingletonId);
Alexey Baderb62f1442016-04-13 08:33:41 +0000488#include "clang/Basic/OpenCLImageTypes.def"
Guy Benyei61054192013-02-07 10:55:47 +0000489 case BuiltinType::OCLSampler:
Eric Christophere7b87e52014-10-26 23:40:33 +0000490 return DBuilder.createBasicType(
491 "opencl_sampler_t", CGM.getContext().getTypeSize(BT),
492 CGM.getContext().getTypeAlign(BT), llvm::dwarf::DW_ATE_unsigned);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000493 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000494 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000495 case BuiltinType::OCLClkEvent:
496 return getOrCreateStructPtrType("opencl_clk_event_t", OCLClkEventDITy);
497 case BuiltinType::OCLQueue:
498 return getOrCreateStructPtrType("opencl_queue_t", OCLQueueDITy);
499 case BuiltinType::OCLNDRange:
500 return getOrCreateStructPtrType("opencl_ndrange_t", OCLNDRangeDITy);
501 case BuiltinType::OCLReserveID:
502 return getOrCreateStructPtrType("opencl_reserve_id_t", OCLReserveIDDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000503
Guy Benyei11169dd2012-12-18 14:30:41 +0000504 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000505 case BuiltinType::Char_U:
506 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
507 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000508 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000509 case BuiltinType::SChar:
510 Encoding = llvm::dwarf::DW_ATE_signed_char;
511 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000512 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000513 case BuiltinType::Char32:
514 Encoding = llvm::dwarf::DW_ATE_UTF;
515 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000516 case BuiltinType::UShort:
517 case BuiltinType::UInt:
518 case BuiltinType::UInt128:
519 case BuiltinType::ULong:
520 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000521 case BuiltinType::ULongLong:
522 Encoding = llvm::dwarf::DW_ATE_unsigned;
523 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000524 case BuiltinType::Short:
525 case BuiltinType::Int:
526 case BuiltinType::Int128:
527 case BuiltinType::Long:
528 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000529 case BuiltinType::LongLong:
530 Encoding = llvm::dwarf::DW_ATE_signed;
531 break;
532 case BuiltinType::Bool:
533 Encoding = llvm::dwarf::DW_ATE_boolean;
534 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000535 case BuiltinType::Half:
536 case BuiltinType::Float:
537 case BuiltinType::LongDouble:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000538 case BuiltinType::Float128:
Eric Christophere7b87e52014-10-26 23:40:33 +0000539 case BuiltinType::Double:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000540 // FIXME: For targets where long double and __float128 have the same size,
541 // they are currently indistinguishable in the debugger without some
542 // special treatment. However, there is currently no consensus on encoding
543 // and this should be updated once a DWARF encoding exists for distinct
544 // floating point types of the same size.
Eric Christophere7b87e52014-10-26 23:40:33 +0000545 Encoding = llvm::dwarf::DW_ATE_float;
546 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000547 }
548
549 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000550 case BuiltinType::Long:
551 BTName = "long int";
552 break;
553 case BuiltinType::LongLong:
554 BTName = "long long int";
555 break;
556 case BuiltinType::ULong:
557 BTName = "long unsigned int";
558 break;
559 case BuiltinType::ULongLong:
560 BTName = "long long unsigned int";
561 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000562 default:
563 BTName = BT->getName(CGM.getLangOpts());
564 break;
565 }
566 // Bit size, align and offset of the type.
567 uint64_t Size = CGM.getContext().getTypeSize(BT);
568 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000569 return DBuilder.createBasicType(BTName, Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000570}
571
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000572llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000573 // Bit size, align and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000574 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000575 if (Ty->isComplexIntegerType())
576 Encoding = llvm::dwarf::DW_ATE_lo_user;
577
578 uint64_t Size = CGM.getContext().getTypeSize(Ty);
579 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000580 return DBuilder.createBasicType("complex", Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000581}
582
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000583llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
584 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000585 QualifierCollector Qc;
586 const Type *T = Qc.strip(Ty);
587
588 // Ignore these qualifiers for now.
589 Qc.removeObjCGCAttr();
590 Qc.removeAddressSpace();
591 Qc.removeObjCLifetime();
592
593 // We will create one Derived type for one qualifier and recurse to handle any
594 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000595 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000596 if (Qc.hasConst()) {
597 Tag = llvm::dwarf::DW_TAG_const_type;
598 Qc.removeConst();
599 } else if (Qc.hasVolatile()) {
600 Tag = llvm::dwarf::DW_TAG_volatile_type;
601 Qc.removeVolatile();
602 } else if (Qc.hasRestrict()) {
603 Tag = llvm::dwarf::DW_TAG_restrict_type;
604 Qc.removeRestrict();
605 } else {
606 assert(Qc.empty() && "Unknown type qualifier for debug info");
607 return getOrCreateType(QualType(T, 0), Unit);
608 }
609
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000610 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000611
612 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
613 // CVR derived types.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000614 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000615}
616
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000617llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
618 llvm::DIFile *Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000619
620 // The frontend treats 'id' as a typedef to an ObjCObjectType,
621 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
622 // debug info, we want to emit 'id' in both cases.
623 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000624 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000625
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000626 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
627 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000628}
629
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000630llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
631 llvm::DIFile *Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000632 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000633 Ty->getPointeeType(), Unit);
634}
635
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000636/// \return whether a C++ mangling exists for the type defined by TD.
637static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
638 switch (TheCU->getSourceLanguage()) {
639 case llvm::dwarf::DW_LANG_C_plus_plus:
640 return true;
641 case llvm::dwarf::DW_LANG_ObjC_plus_plus:
642 return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
643 default:
644 return false;
645 }
646}
647
Manman Rene0064d82013-08-29 23:19:58 +0000648/// In C++ mode, types have linkage, so we can rely on the ODR and
649/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000650static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
651 CodeGenModule &CGM,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000652 llvm::DICompileUnit *TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000653 SmallString<256> FullName;
Manman Rene0064d82013-08-29 23:19:58 +0000654 const TagDecl *TD = Ty->getDecl();
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000655
656 if (!hasCXXMangling(TD, TheCU) || !TD->isExternallyVisible())
Manman Rene0064d82013-08-29 23:19:58 +0000657 return FullName;
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000658
Manman Rene0064d82013-08-29 23:19:58 +0000659 // TODO: This is using the RTTI name. Is there a better way to get
660 // a unique string for a type?
661 llvm::raw_svector_ostream Out(FullName);
662 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
Manman Rene0064d82013-08-29 23:19:58 +0000663 return FullName;
664}
665
Adrian Prantl3e8bad42015-07-08 21:18:34 +0000666/// \return the approproate DWARF tag for a composite type.
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000667static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
668 llvm::dwarf::Tag Tag;
669 if (RD->isStruct() || RD->isInterface())
670 Tag = llvm::dwarf::DW_TAG_structure_type;
671 else if (RD->isUnion())
672 Tag = llvm::dwarf::DW_TAG_union_type;
673 else {
674 // FIXME: This could be a struct type giving a default visibility different
675 // than C++ class type, but needs llvm metadata changes first.
676 assert(RD->isClass());
677 Tag = llvm::dwarf::DW_TAG_class_type;
678 }
679 return Tag;
680}
681
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000682llvm::DICompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000683CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000684 llvm::DIScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000685 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000686 if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
687 return cast<llvm::DICompositeType>(T);
688 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +0000689 unsigned Line = getLineNumber(RD->getLocation());
690 StringRef RDName = getClassName(RD);
691
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000692 uint64_t Size = 0;
693 uint64_t Align = 0;
694
695 const RecordDecl *D = RD->getDefinition();
696 if (D && D->isCompleteDefinition()) {
697 Size = CGM.getContext().getTypeSize(Ty);
698 Align = CGM.getContext().getTypeAlign(Ty);
699 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000700
701 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000702 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000703 llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000704 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000705 llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000706 ReplaceMap.emplace_back(
707 std::piecewise_construct, std::make_tuple(Ty),
708 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000709 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000710}
711
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000712llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000713 const Type *Ty,
714 QualType PointeeTy,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000715 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000716 // Bit size, align and offset of the type.
717 // Size is always the size of a pointer. We can't use getTypeSize here
718 // because that does not return the correct value for references.
719 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000720 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000721 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
722
Keno Fischer87842f32015-11-16 09:04:13 +0000723 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
724 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
725 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit),
726 Size, Align);
727 else
728 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
729 Align);
Guy Benyei11169dd2012-12-18 14:30:41 +0000730}
731
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000732llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
733 llvm::DIType *&Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000734 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000735 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000736 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
737 TheCU, getOrCreateMainFile(), 0);
738 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
739 Cache = DBuilder.createPointerType(Cache, Size);
740 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000741}
742
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000743llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
744 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000745 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000746 QualType FType;
747 uint64_t FieldSize, FieldOffset;
748 unsigned FieldAlign;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000749 llvm::DINodeArray Elements;
Guy Benyei11169dd2012-12-18 14:30:41 +0000750
751 FieldOffset = 0;
752 FType = CGM.getContext().UnsignedLongTy;
753 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
754 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
755
756 Elements = DBuilder.getOrCreateArray(EltTys);
757 EltTys.clear();
758
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000759 unsigned Flags = llvm::DINode::FlagAppleBlock;
Adrian Prantl498fff62015-07-06 21:31:35 +0000760 unsigned LineNo = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000761
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000762 auto *EltTy =
Adrian Prantl498fff62015-07-06 21:31:35 +0000763 DBuilder.createStructType(Unit, "__block_descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000764 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000765
766 // Bit size, align and offset of the type.
767 uint64_t Size = CGM.getContext().getTypeSize(Ty);
768
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000769 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000770
771 FieldOffset = 0;
772 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
773 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
774 FType = CGM.getContext().IntTy;
775 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
776 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000777 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000778 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
779
780 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000781 FieldSize = CGM.getContext().getTypeSize(Ty);
782 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Adrian Prantl498fff62015-07-06 21:31:35 +0000783 EltTys.push_back(DBuilder.createMemberType(Unit, "__descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000784 FieldSize, FieldAlign, FieldOffset,
785 0, DescTy));
Guy Benyei11169dd2012-12-18 14:30:41 +0000786
787 FieldOffset += FieldSize;
788 Elements = DBuilder.getOrCreateArray(EltTys);
789
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000790 // The __block_literal_generic structs are marked with a special
791 // DW_AT_APPLE_BLOCK attribute and are an implementation detail only
792 // the debugger needs to know about. To allow type uniquing, emit
793 // them without a name or a location.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000794 EltTy =
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000795 DBuilder.createStructType(Unit, "", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000796 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000797
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000798 return DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000799}
800
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000801llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
802 llvm::DIFile *Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000803 assert(Ty->isTypeAlias());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000804 llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000805
806 SmallString<128> NS;
807 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000808 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
809 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000810
811 TemplateSpecializationType::PrintTemplateArgumentList(
812 OS, Ty->getArgs(), Ty->getNumArgs(),
813 CGM.getContext().getPrintingPolicy());
814
Eric Christophere7b87e52014-10-26 23:40:33 +0000815 TypeAliasDecl *AliasDecl = cast<TypeAliasTemplateDecl>(
816 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000817
818 SourceLocation Loc = AliasDecl->getLocation();
Adrian Prantlb67dbce2015-09-11 18:45:02 +0000819 return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc),
820 getLineNumber(Loc),
821 getDeclContextDescriptor(AliasDecl));
David Blaikief1b382e2014-04-06 17:14:06 +0000822}
823
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000824llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
825 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000826 // We don't set size information, but do specify where the typedef was
827 // declared.
Amjad Abouddc4531e2016-04-30 01:44:38 +0000828 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000829
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000830 // Typedefs are derived from some other type.
831 return DBuilder.createTypedef(
832 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
833 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
Amjad Abouddc4531e2016-04-30 01:44:38 +0000834 getDeclContextDescriptor(Ty->getDecl()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000835}
836
Reid Klecknerf00f8032016-06-08 20:41:54 +0000837static unsigned getDwarfCC(CallingConv CC) {
838 switch (CC) {
839 case CC_C:
840 // Avoid emitting DW_AT_calling_convention if the C convention was used.
841 return 0;
842
843 case CC_X86StdCall:
844 return llvm::dwarf::DW_CC_BORLAND_stdcall;
845 case CC_X86FastCall:
846 return llvm::dwarf::DW_CC_BORLAND_msfastcall;
847 case CC_X86ThisCall:
848 return llvm::dwarf::DW_CC_BORLAND_thiscall;
849 case CC_X86VectorCall:
850 return llvm::dwarf::DW_CC_LLVM_vectorcall;
851 case CC_X86Pascal:
852 return llvm::dwarf::DW_CC_BORLAND_pascal;
853
854 // FIXME: Create new DW_CC_ codes for these calling conventions.
855 case CC_X86_64Win64:
856 case CC_X86_64SysV:
857 case CC_AAPCS:
858 case CC_AAPCS_VFP:
859 case CC_IntelOclBicc:
860 case CC_SpirFunction:
Nikolay Haustov8c6538b2016-06-30 09:06:33 +0000861 case CC_OpenCLKernel:
Reid Klecknerf00f8032016-06-08 20:41:54 +0000862 case CC_Swift:
863 case CC_PreserveMost:
864 case CC_PreserveAll:
865 return 0;
866 }
867 return 0;
868}
869
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000870llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
871 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000872 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000873
874 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000875 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000876
877 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000878 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000879 if (isa<FunctionNoProtoType>(Ty))
880 EltTys.push_back(DBuilder.createUnspecifiedParameter());
881 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
Alp Toker9cacbab2014-01-20 20:26:09 +0000882 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
883 EltTys.push_back(getOrCreateType(FPT->getParamType(i), Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000884 if (FPT->isVariadic())
885 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000886 }
887
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000888 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Reid Klecknerf00f8032016-06-08 20:41:54 +0000889 return DBuilder.createSubroutineType(EltTypeArray, 0,
890 getDwarfCC(Ty->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000891}
892
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000893/// Convert an AccessSpecifier into the corresponding DINode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +0000894/// As an optimization, return 0 if the access specifier equals the
895/// default for the containing type.
896static unsigned getAccessFlag(AccessSpecifier Access, const RecordDecl *RD) {
897 AccessSpecifier Default = clang::AS_none;
898 if (RD && RD->isClass())
899 Default = clang::AS_private;
900 else if (RD && (RD->isStruct() || RD->isUnion()))
901 Default = clang::AS_public;
902
903 if (Access == Default)
904 return 0;
905
Eric Christophere7b87e52014-10-26 23:40:33 +0000906 switch (Access) {
907 case clang::AS_private:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000908 return llvm::DINode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +0000909 case clang::AS_protected:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000910 return llvm::DINode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +0000911 case clang::AS_public:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000912 return llvm::DINode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +0000913 case clang::AS_none:
914 return 0;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000915 }
916 llvm_unreachable("unexpected access enumerator");
917}
Guy Benyei11169dd2012-12-18 14:30:41 +0000918
David Majnemerb4b671e2016-06-30 03:01:59 +0000919llvm::DIType *CGDebugInfo::createBitFieldType(const FieldDecl *BitFieldDecl,
920 llvm::DIScope *RecordTy,
921 const RecordDecl *RD) {
922 StringRef Name = BitFieldDecl->getName();
923 QualType Ty = BitFieldDecl->getType();
924 SourceLocation Loc = BitFieldDecl->getLocation();
925 llvm::DIFile *VUnit = getOrCreateFile(Loc);
926 llvm::DIType *DebugType = getOrCreateType(Ty, VUnit);
927
928 // Get the location for the field.
929 llvm::DIFile *File = getOrCreateFile(Loc);
930 unsigned Line = getLineNumber(Loc);
931
932 const CGBitFieldInfo &BitFieldInfo =
933 CGM.getTypes().getCGRecordLayout(RD).getBitFieldInfo(BitFieldDecl);
934 uint64_t SizeInBits = BitFieldInfo.Size;
935 assert(SizeInBits > 0 && "found named 0-width bitfield");
936 unsigned AlignInBits = CGM.getContext().getTypeAlign(Ty);
937 uint64_t StorageOffsetInBits =
938 CGM.getContext().toBits(BitFieldInfo.StorageOffset);
939 uint64_t OffsetInBits = StorageOffsetInBits + BitFieldInfo.Offset;
940 unsigned Flags = getAccessFlag(BitFieldDecl->getAccess(), RD);
941 return DBuilder.createBitFieldMemberType(
942 RecordTy, Name, File, Line, SizeInBits, AlignInBits, OffsetInBits,
943 StorageOffsetInBits, Flags, DebugType);
944}
945
946llvm::DIType *
947CGDebugInfo::createFieldType(StringRef name, QualType type, SourceLocation loc,
948 AccessSpecifier AS, uint64_t offsetInBits,
949 llvm::DIFile *tunit, llvm::DIScope *scope,
950 const RecordDecl *RD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000951 llvm::DIType *debugType = getOrCreateType(type, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000952
953 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000954 llvm::DIFile *file = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000955 unsigned line = getLineNumber(loc);
956
David Majnemer34b57492014-07-30 01:30:47 +0000957 uint64_t SizeInBits = 0;
958 unsigned AlignInBits = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000959 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +0000960 TypeInfo TI = CGM.getContext().getTypeInfo(type);
961 SizeInBits = TI.Width;
962 AlignInBits = TI.Align;
Guy Benyei11169dd2012-12-18 14:30:41 +0000963 }
964
Adrian Prantl21361fb2014-08-29 22:44:27 +0000965 unsigned flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +0000966 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
967 AlignInBits, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +0000968}
969
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000970void CGDebugInfo::CollectRecordLambdaFields(
971 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000972 llvm::DIType *RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +0000973 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
974 // has the name and the location of the variable so we should iterate over
975 // both concurrently.
976 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
977 RecordDecl::field_iterator Field = CXXDecl->field_begin();
978 unsigned fieldno = 0;
979 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +0000980 E = CXXDecl->captures_end();
981 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000982 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +0000983 if (C.capturesVariable()) {
David Majnemerb4b671e2016-06-30 03:01:59 +0000984 SourceLocation Loc = C.getLocation();
985 assert(!Field->isBitField() && "lambdas don't have bitfield members!");
Eric Christopher91a31902013-01-16 01:22:32 +0000986 VarDecl *V = C.getCapturedVar();
Eric Christopher91a31902013-01-16 01:22:32 +0000987 StringRef VName = V->getName();
David Majnemerb4b671e2016-06-30 03:01:59 +0000988 llvm::DIFile *VUnit = getOrCreateFile(Loc);
989 llvm::DIType *FieldType = createFieldType(
990 VName, Field->getType(), Loc, Field->getAccess(),
991 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
992 elements.push_back(FieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +0000993 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +0000994 // TODO: Need to handle 'this' in some way by probably renaming the
995 // this of the lambda class and having a field member of 'this' or
996 // by using AT_object_pointer for the function and having that be
997 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +0000998 FieldDecl *f = *Field;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000999 llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +00001000 QualType type = f->getType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001001 llvm::DIType *fieldType = createFieldType(
David Majnemerb4b671e2016-06-30 03:01:59 +00001002 "this", type, f->getLocation(), f->getAccess(),
Eric Christophere7b87e52014-10-26 23:40:33 +00001003 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +00001004
1005 elements.push_back(fieldType);
1006 }
1007 }
1008}
1009
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001010llvm::DIDerivedType *
1011CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00001012 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +00001013 // Create the descriptor for the static variable, with or without
1014 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +00001015 Var = Var->getCanonicalDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001016 llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
1017 llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher91a31902013-01-16 01:22:32 +00001018
Eric Christopher91a31902013-01-16 01:22:32 +00001019 unsigned LineNumber = getLineNumber(Var->getLocation());
1020 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +00001021 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +00001022 if (Var->getInit()) {
1023 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +00001024 if (Value) {
1025 if (Value->isInt())
1026 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
1027 if (Value->isFloat())
1028 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
1029 }
Eric Christopher91a31902013-01-16 01:22:32 +00001030 }
1031
Adrian Prantl21361fb2014-08-29 22:44:27 +00001032 unsigned Flags = getAccessFlag(Var->getAccess(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001033 llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
David Blaikieae019462013-08-15 22:50:29 +00001034 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001035 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +00001036 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +00001037}
1038
Eric Christophere7b87e52014-10-26 23:40:33 +00001039void CGDebugInfo::CollectRecordNormalField(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001040 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
1041 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +00001042 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +00001043 StringRef name = field->getName();
1044 QualType type = field->getType();
1045
1046 // Ignore unnamed fields unless they're anonymous structs/unions.
1047 if (name.empty() && !type->isRecordType())
1048 return;
1049
David Majnemerb4b671e2016-06-30 03:01:59 +00001050 llvm::DIType *FieldType;
Eric Christopher91a31902013-01-16 01:22:32 +00001051 if (field->isBitField()) {
David Majnemerb4b671e2016-06-30 03:01:59 +00001052 FieldType = createBitFieldType(field, RecordTy, RD);
1053 } else {
1054 FieldType =
1055 createFieldType(name, type, field->getLocation(), field->getAccess(),
1056 OffsetInBits, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +00001057 }
1058
David Majnemerb4b671e2016-06-30 03:01:59 +00001059 elements.push_back(FieldType);
Eric Christopher91a31902013-01-16 01:22:32 +00001060}
1061
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001062void CGDebugInfo::CollectRecordFields(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001063 const RecordDecl *record, llvm::DIFile *tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001064 SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001065 llvm::DICompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001066 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
1067
Eric Christopher91a31902013-01-16 01:22:32 +00001068 if (CXXDecl && CXXDecl->isLambda())
1069 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
1070 else {
1071 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001072
Eric Christopher91a31902013-01-16 01:22:32 +00001073 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +00001074 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +00001075
Eric Christopher91a31902013-01-16 01:22:32 +00001076 // Static and non-static members should appear in the same order as
1077 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +00001078 for (const auto *I : record->decls())
1079 if (const auto *V = dyn_cast<VarDecl>(I)) {
Paul Robinsonb17327d2016-04-27 17:37:12 +00001080 if (V->hasAttr<NoDebugAttr>())
1081 continue;
David Blaikiece763042013-08-20 21:49:21 +00001082 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001083 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +00001084 if (MI != StaticDataMemberCache.end()) {
1085 assert(MI->second &&
1086 "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00001087 elements.push_back(MI->second);
Adrian Prantl21361fb2014-08-29 22:44:27 +00001088 } else {
1089 auto Field = CreateRecordStaticField(V, RecordTy, record);
1090 elements.push_back(Field);
1091 }
Aaron Ballman629afae2014-03-07 19:56:05 +00001092 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001093 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
1094 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +00001095
1096 // Bump field number for next field.
1097 ++fieldNo;
Guy Benyei11169dd2012-12-18 14:30:41 +00001098 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001099 }
1100}
1101
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001102llvm::DISubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +00001103CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001104 llvm::DIFile *Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001105 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001106 if (Method->isStatic())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001107 return cast_or_null<llvm::DISubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001108 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001109 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1110 Func, Unit);
1111}
David Blaikie2aaf0652013-01-07 22:24:59 +00001112
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001113llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1114 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001115 // Add "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001116 llvm::DITypeRefArray Args(
1117 cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001118 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001119 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001120
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001121 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001122
1123 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001124 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001125
David Blaikie2aaf0652013-01-07 22:24:59 +00001126 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001127 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001128 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1129 // Create pointer type directly in this case.
1130 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1131 QualType PointeeTy = ThisPtrTy->getPointeeType();
1132 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001133 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie2aaf0652013-01-07 22:24:59 +00001134 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001135 llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1136 llvm::DIType *ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001137 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001138 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001139 // TODO: This and the artificial type below are misleading, the
1140 // types aren't artificial the argument is, but the current
1141 // metadata doesn't represent that.
1142 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1143 Elts.push_back(ThisPtrType);
1144 } else {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001145 llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001146 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001147 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1148 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001149 }
1150
1151 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001152 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1153 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001154
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001155 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001156
Adrian Prantl0630eb72013-12-18 21:48:18 +00001157 unsigned Flags = 0;
1158 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001159 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001160 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001161 Flags |= llvm::DINode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001162
Reid Klecknerf00f8032016-06-08 20:41:54 +00001163 return DBuilder.createSubroutineType(EltTypeArray, Flags,
1164 getDwarfCC(Func->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001165}
1166
Eric Christopherb2a008c2013-05-16 00:45:12 +00001167/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001168/// inside a function.
1169static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1170 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1171 return isFunctionLocalClass(NRD);
1172 if (isa<FunctionDecl>(RD->getDeclContext()))
1173 return true;
1174 return false;
1175}
1176
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001177llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1178 const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001179 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001180 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001181
Guy Benyei11169dd2012-12-18 14:30:41 +00001182 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001183 llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001184
1185 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1186 // make sense to give a single ctor/dtor a linkage name.
1187 StringRef MethodLinkageName;
David Blaikie71647672016-04-12 21:22:48 +00001188 // FIXME: 'isFunctionLocalClass' seems like an arbitrary/unintentional
1189 // property to use here. It may've been intended to model "is non-external
1190 // type" but misses cases of non-function-local but non-external classes such
1191 // as those in anonymous namespaces as well as the reverse - external types
1192 // that are function local, such as those in (non-local) inline functions.
Guy Benyei11169dd2012-12-18 14:30:41 +00001193 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1194 MethodLinkageName = CGM.getMangledName(Method);
1195
1196 // Get the location for the method.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001197 llvm::DIFile *MethodDefUnit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00001198 unsigned MethodLine = 0;
1199 if (!Method->isImplicit()) {
1200 MethodDefUnit = getOrCreateFile(Method->getLocation());
1201 MethodLine = getLineNumber(Method->getLocation());
1202 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001203
1204 // Collect virtual method info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001205 llvm::DIType *ContainingType = nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001206 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001207 unsigned VIndex = 0;
Reid Klecknerfb727182016-06-17 22:27:59 +00001208 unsigned Flags = 0;
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001209 int ThisAdjustment = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001210
Guy Benyei11169dd2012-12-18 14:30:41 +00001211 if (Method->isVirtual()) {
1212 if (Method->isPure())
1213 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1214 else
1215 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001216
Reid Kleckner216d0a12016-06-16 20:08:51 +00001217 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1218 // It doesn't make sense to give a virtual destructor a vtable index,
1219 // since a single destructor has two entries in the vtable.
1220 if (!isa<CXXDestructorDecl>(Method))
1221 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
1222 } else {
1223 // Emit MS ABI vftable information. There is only one entry for the
1224 // deleting dtor.
1225 const auto *DD = dyn_cast<CXXDestructorDecl>(Method);
1226 GlobalDecl GD = DD ? GlobalDecl(DD, Dtor_Deleting) : GlobalDecl(Method);
1227 MicrosoftVTableContext::MethodVFTableLocation ML =
1228 CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD);
1229 VIndex = ML.Index;
Reid Klecknerc4871ed2016-06-22 18:34:45 +00001230
1231 // CodeView only records the vftable offset in the class that introduces
1232 // the virtual method. This is possible because, unlike Itanium, the MS
1233 // C++ ABI does not include all virtual methods from non-primary bases in
1234 // the vtable for the most derived class. For example, if C inherits from
1235 // A and B, C's primary vftable will not include B's virtual methods.
1236 if (Method->begin_overridden_methods() == Method->end_overridden_methods())
1237 Flags |= llvm::DINode::FlagIntroducedVirtual;
1238
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001239 // The 'this' adjustment accounts for both the virtual and non-virtual
1240 // portions of the adjustment. Presumably the debugger only uses it when
1241 // it knows the dynamic type of an object.
1242 ThisAdjustment = CGM.getCXXABI()
1243 .getVirtualFunctionPrologueThisAdjustment(GD)
1244 .getQuantity();
Reid Kleckner216d0a12016-06-16 20:08:51 +00001245 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001246 ContainingType = RecordTy;
1247 }
1248
Guy Benyei11169dd2012-12-18 14:30:41 +00001249 if (Method->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001250 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001251 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
Guy Benyei11169dd2012-12-18 14:30:41 +00001252 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1253 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001254 Flags |= llvm::DINode::FlagExplicit;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001255 } else if (const CXXConversionDecl *CXXC =
Eric Christophere7b87e52014-10-26 23:40:33 +00001256 dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001257 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001258 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001259 }
1260 if (Method->hasPrototype())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001261 Flags |= llvm::DINode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001262 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001263 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001264 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001265 Flags |= llvm::DINode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001266
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001267 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1268 llvm::DISubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001269 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001270 MethodTy, /*isLocalToUnit=*/false, /*isDefinition=*/false, Virtuality,
1271 VIndex, ThisAdjustment, ContainingType, Flags, CGM.getLangOpts().Optimize,
1272 TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001273
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001274 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001275
1276 return SP;
1277}
1278
Eric Christophere7b87e52014-10-26 23:40:33 +00001279void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001280 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1281 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001282
1283 // Since we want more than just the individual member decls if we
1284 // have templated functions iterate over every declaration to gather
1285 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001286 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001287 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1288 // If the member is implicit, don't add it to the member list. This avoids
1289 // the member being added to type units by LLVM, while still allowing it
1290 // to be emitted into the type declaration/reference inside the compile
1291 // unit.
Paul Robinson6a7511b2015-06-25 17:50:43 +00001292 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
David Blaikie6dddfe32014-10-06 05:52:27 +00001293 // FIXME: Handle Using(Shadow?)Decls here to create
1294 // DW_TAG_imported_declarations inside the class for base decls brought into
1295 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1296 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1297 // referenced)
Paul Robinson6a7511b2015-06-25 17:50:43 +00001298 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
David Blaikiefd580722014-10-06 05:18:55 +00001299 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001300
1301 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1302 continue;
1303
David Blaikiefd580722014-10-06 05:18:55 +00001304 // Reuse the existing member function declaration if it exists.
1305 // It may be associated with the declaration of the type & should be
1306 // reused as we're building the definition.
1307 //
1308 // This situation can arise in the vtable-based debug info reduction where
1309 // implicit members are emitted in a non-vtable TU.
1310 auto MI = SPCache.find(Method->getCanonicalDecl());
1311 EltTys.push_back(MI == SPCache.end()
1312 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001313 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001314 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001315}
Guy Benyei11169dd2012-12-18 14:30:41 +00001316
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001317void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001318 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001319 llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001320 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Aaron Ballman574705e2014-03-13 15:41:46 +00001321 for (const auto &BI : RD->bases()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001322 unsigned BFlags = 0;
1323 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001324
Guy Benyei11169dd2012-12-18 14:30:41 +00001325 const CXXRecordDecl *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001326 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001327
Aaron Ballman574705e2014-03-13 15:41:46 +00001328 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001329 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1330 // virtual base offset offset is -ve. The code generator emits dwarf
1331 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001332 BaseOffset = 0 - CGM.getItaniumVTableContext()
1333 .getVirtualBaseOffsetOffset(RD, Base)
1334 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001335 } else {
1336 // In the MS ABI, store the vbtable offset, which is analogous to the
1337 // vbase offset offset in Itanium.
1338 BaseOffset =
1339 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1340 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001341 BFlags = llvm::DINode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001342 } else
1343 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1344 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1345 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001346
Adrian Prantl21361fb2014-08-29 22:44:27 +00001347 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001348 llvm::DIType *DTy = DBuilder.createInheritance(
Eric Christophere7b87e52014-10-26 23:40:33 +00001349 RecordTy, getOrCreateType(BI.getType(), Unit), BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001350 EltTys.push_back(DTy);
1351 }
1352}
1353
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001354llvm::DINodeArray
Eric Christophere7b87e52014-10-26 23:40:33 +00001355CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1356 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001357 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001358 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001359 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1360 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001361 StringRef Name;
1362 if (TPList)
1363 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001364 switch (TA.getKind()) {
1365 case TemplateArgument::Type: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001366 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001367 TemplateParams.push_back(
1368 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001369 } break;
1370 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001371 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001372 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1373 TheCU, Name, TTy,
1374 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001375 } break;
1376 case TemplateArgument::Declaration: {
1377 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001378 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001379 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001380 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001381 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001382 // Variable pointer template parameters have a value that is the address
1383 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001384 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001385 V = CGM.GetAddrOfGlobalVar(VD);
1386 // Member function pointers have special support for building them, though
1387 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001388 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Majnemere2be95b2015-06-23 07:31:01 +00001389 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001390 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001391 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001392 // Member data pointers have special handling too to compute the fixed
1393 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001394 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001395 // These five lines (& possibly the above member function pointer
1396 // handling) might be able to be refactored to use similar code in
1397 // CodeGenModule::getMemberPointerConstant
1398 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1399 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001400 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001401 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001402 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001403 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1404 TheCU, Name, TTy,
1405 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001406 } break;
1407 case TemplateArgument::NullPtr: {
1408 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001409 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001410 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001411 // Special case member data pointer null values since they're actually -1
1412 // instead of zero.
1413 if (const MemberPointerType *MPT =
1414 dyn_cast<MemberPointerType>(T.getTypePtr()))
1415 // But treat member function pointers as simple zero integers because
1416 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1417 // CodeGen grows handling for values of non-null member function
1418 // pointers then perhaps we could remove this special case and rely on
1419 // EmitNullMemberPointer for member function pointers.
1420 if (MPT->isMemberDataPointer())
1421 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1422 if (!V)
1423 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001424 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1425 TheCU, Name, TTy, cast<llvm::Constant>(V)));
David Blaikie38079fd2013-05-10 21:53:14 +00001426 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001427 case TemplateArgument::Template:
1428 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001429 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001430 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1431 break;
1432 case TemplateArgument::Pack:
1433 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1434 TheCU, Name, nullptr,
1435 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1436 break;
David Majnemer5559d472013-08-24 08:21:10 +00001437 case TemplateArgument::Expression: {
1438 const Expr *E = TA.getAsExpr();
1439 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001440 if (E->isGLValue())
1441 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001442 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001443 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001444 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001445 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1446 TheCU, Name, TTy, cast<llvm::Constant>(V->stripPointerCasts())));
David Majnemer5559d472013-08-24 08:21:10 +00001447 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001448 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001449 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001450 case TemplateArgument::Null:
1451 llvm_unreachable(
1452 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001453 }
1454 }
1455 return DBuilder.getOrCreateArray(TemplateParams);
1456}
1457
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001458llvm::DINodeArray
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001459CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001460 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001461 if (FD->getTemplatedKind() ==
1462 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001463 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1464 ->getTemplate()
1465 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001466 return CollectTemplateParams(
1467 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001468 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001469 return llvm::DINodeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001470}
1471
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001472llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1473 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001474 // Always get the full list of parameters, not just the ones from
1475 // the specialization.
1476 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001477 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001478 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001479 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001480}
1481
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001482llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001483 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001484 return VTablePtrType;
1485
1486 ASTContext &Context = CGM.getContext();
1487
1488 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001489 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001490 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
Eric Christopher28a6db52015-10-15 06:56:08 +00001491 llvm::DIType *SubTy = DBuilder.createSubroutineType(SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001492 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001493 llvm::DIType *vtbl_ptr_type =
Eric Christophere7b87e52014-10-26 23:40:33 +00001494 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001495 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1496 return VTablePtrType;
1497}
1498
Guy Benyei11169dd2012-12-18 14:30:41 +00001499StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001500 // Copy the gdb compatible name on the side and use its reference.
1501 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001502}
1503
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001504void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001505 SmallVectorImpl<llvm::Metadata *> &EltTys) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001506 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1507
1508 // If there is a primary base then it will hold vtable info.
1509 if (RL.getPrimaryBase())
1510 return;
1511
1512 // If this class is not dynamic then there is not any vtable info to collect.
1513 if (!RD->isDynamicClass())
1514 return;
1515
1516 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001517 llvm::DIType *VPTR = DBuilder.createMemberType(
Eric Christophere7b87e52014-10-26 23:40:33 +00001518 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001519 llvm::DINode::FlagArtificial, getOrCreateVTablePtrType(Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001520 EltTys.push_back(VPTR);
1521}
1522
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001523llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001524 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001525 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001526 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001527 return T;
1528}
1529
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001530llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001531 SourceLocation Loc) {
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001532 return getOrCreateStandaloneType(D, Loc);
1533}
1534
1535llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
1536 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001537 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001538 assert(!D.isNull() && "null type");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001539 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001540 assert(T && "could not create debug info for type");
Adrian Prantl3a884fa2015-08-27 22:56:46 +00001541
Adrian Prantl73409ce2013-03-11 18:33:46 +00001542 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001543 return T;
1544}
1545
David Blaikie483a9da2014-05-06 18:35:21 +00001546void CGDebugInfo::completeType(const EnumDecl *ED) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001547 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie483a9da2014-05-06 18:35:21 +00001548 return;
1549 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001550 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001551 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001552 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001553 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001554 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001555 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001556 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001557}
1558
David Blaikieb2e86eb2013-08-15 20:49:17 +00001559void CGDebugInfo::completeType(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001560 if (DebugKind > codegenoptions::LimitedDebugInfo ||
David Blaikieb2e86eb2013-08-15 20:49:17 +00001561 !CGM.getLangOpts().CPlusPlus)
1562 completeRequiredType(RD);
1563}
1564
1565void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001566 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie0856f662014-03-04 22:01:08 +00001567 return;
1568
David Blaikie6943dea2013-08-20 01:28:15 +00001569 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1570 if (CXXDecl->isDynamicClass())
1571 return;
1572
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001573 if (DebugTypeExtRefs && RD->isFromASTFile())
1574 return;
1575
David Blaikieb2e86eb2013-08-15 20:49:17 +00001576 QualType Ty = CGM.getContext().getRecordType(RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001577 llvm::DIType *T = getTypeOrNull(Ty);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001578 if (T && T->isForwardDecl())
David Blaikie6943dea2013-08-20 01:28:15 +00001579 completeClassData(RD);
1580}
1581
1582void CGDebugInfo::completeClassData(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001583 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001584 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001585 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001586 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001587 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001588 if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001589 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001590 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001591 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001592 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001593}
1594
David Blaikie0e716b42014-03-03 23:48:23 +00001595static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1596 CXXRecordDecl::method_iterator End) {
1597 for (; I != End; ++I)
1598 if (FunctionDecl *Tmpl = I->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001599 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
1600 !I->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001601 return true;
1602 return false;
1603}
1604
Adrian Prantl05fefa42016-04-25 20:52:40 +00001605/// Does a type definition exist in an imported clang module?
1606static bool isDefinedInClangModule(const RecordDecl *RD) {
Adrian Prantl88d79172016-04-26 21:58:18 +00001607 if (!RD || !RD->isFromASTFile())
Adrian Prantl05fefa42016-04-25 20:52:40 +00001608 return false;
1609 if (!RD->isExternallyVisible() && RD->getName().empty())
1610 return false;
Adrian Prantl94913712016-04-26 23:42:43 +00001611 if (auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) {
1612 assert(CXXDecl->isCompleteDefinition() && "incomplete record definition");
Adrian Prantl0dabc2a2016-04-26 23:37:38 +00001613 if (CXXDecl->getTemplateSpecializationKind() != TSK_Undeclared)
1614 // Make sure the instantiation is actually in a module.
1615 if (CXXDecl->field_begin() != CXXDecl->field_end())
1616 return CXXDecl->field_begin()->isFromASTFile();
Adrian Prantl94913712016-04-26 23:42:43 +00001617 }
Adrian Prantl0dabc2a2016-04-26 23:37:38 +00001618
Adrian Prantl05fefa42016-04-25 20:52:40 +00001619 return true;
1620}
1621
Benjamin Kramer8c305922016-02-02 11:06:51 +00001622static bool shouldOmitDefinition(codegenoptions::DebugInfoKind DebugKind,
1623 bool DebugTypeExtRefs, const RecordDecl *RD,
David Blaikie0e716b42014-03-03 23:48:23 +00001624 const LangOptions &LangOpts) {
Adrian Prantl88d79172016-04-26 21:58:18 +00001625 if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
Adrian Prantl43e00812016-01-19 23:42:53 +00001626 return true;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001627
Benjamin Kramer8c305922016-02-02 11:06:51 +00001628 if (DebugKind > codegenoptions::LimitedDebugInfo)
David Blaikie0e716b42014-03-03 23:48:23 +00001629 return false;
1630
1631 if (!LangOpts.CPlusPlus)
1632 return false;
1633
1634 if (!RD->isCompleteDefinitionRequired())
1635 return true;
1636
1637 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1638
1639 if (!CXXDecl)
1640 return false;
1641
1642 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass())
1643 return true;
1644
1645 TemplateSpecializationKind Spec = TSK_Undeclared;
1646 if (const ClassTemplateSpecializationDecl *SD =
1647 dyn_cast<ClassTemplateSpecializationDecl>(RD))
1648 Spec = SD->getSpecializationKind();
1649
1650 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1651 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1652 CXXDecl->method_end()))
1653 return true;
1654
1655 return false;
1656}
1657
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001658llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001659 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001660 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001661 if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD,
1662 CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001663 if (!T)
Adrian Prantl6ec370a2015-09-10 18:39:45 +00001664 T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001665 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001666 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001667
David Blaikieb2e86eb2013-08-15 20:49:17 +00001668 return CreateTypeDefinition(Ty);
1669}
1670
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001671llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00001672 RecordDecl *RD = Ty->getDecl();
1673
Guy Benyei11169dd2012-12-18 14:30:41 +00001674 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001675 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001676
1677 // Records and classes and unions can all be recursive. To handle them, we
1678 // first generate a debug descriptor for the struct as a forward declaration.
1679 // Then (if it is a definition) we go through and get debug info for all of
1680 // its members. Finally, we create a descriptor for the complete type (which
1681 // may refer to the forward decl if the struct is recursive) and replace all
1682 // uses of the forward declaration with the final definition.
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00001683 llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001684
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001685 const RecordDecl *D = RD->getDefinition();
1686 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001687 return FwdDecl;
1688
David Blaikieadfbf992013-08-18 16:55:33 +00001689 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1690 CollectContainingType(CXXDecl, FwdDecl);
1691
Guy Benyei11169dd2012-12-18 14:30:41 +00001692 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001693 LexicalBlockStack.emplace_back(&*FwdDecl);
1694 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001695
Guy Benyei11169dd2012-12-18 14:30:41 +00001696 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001697 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001698 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001699
1700 // Note: The split of CXXDecl information here is intentional, the
1701 // gdb tests will depend on a certain ordering at printout. The debug
1702 // information offsets are still correct if we merge them all together
1703 // though.
1704 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1705 if (CXXDecl) {
1706 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1707 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1708 }
1709
Eric Christopher91a31902013-01-16 01:22:32 +00001710 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001711 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001712 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001713 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001714
1715 LexicalBlockStack.pop_back();
1716 RegionMap.erase(Ty->getDecl());
1717
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001718 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001719 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001720
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001721 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001722 FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001723 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001724
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001725 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001726 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001727}
1728
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001729llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1730 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001731 // Ignore protocols.
1732 return getOrCreateType(Ty->getBaseType(), Unit);
1733}
1734
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001735/// \return true if Getter has the default name for the property PD.
1736static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1737 const ObjCMethodDecl *Getter) {
1738 assert(PD);
1739 if (!Getter)
1740 return true;
1741
1742 assert(Getter->getDeclName().isObjCZeroArgSelector());
1743 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001744 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001745}
1746
1747/// \return true if Setter has the default name for the property PD.
1748static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1749 const ObjCMethodDecl *Setter) {
1750 assert(PD);
1751 if (!Setter)
1752 return true;
1753
1754 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001755 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001756 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001757}
1758
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001759llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1760 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001761 ObjCInterfaceDecl *ID = Ty->getDecl();
1762 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001763 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001764
Adrian Prantl50fd1a82016-04-20 23:59:32 +00001765 // Return a forward declaration if this type was imported from a clang module,
1766 // and this is not the compile unit with the implementation of the type (which
1767 // may contain hidden ivars).
1768 if (DebugTypeExtRefs && ID->isFromASTFile() && ID->getDefinition() &&
1769 !ID->getImplementation())
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001770 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
1771 ID->getName(),
1772 getDeclContextDescriptor(ID), Unit, 0);
1773
Guy Benyei11169dd2012-12-18 14:30:41 +00001774 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001775 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001776 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001777 auto RuntimeLang =
1778 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00001779
1780 // If this is just a forward declaration return a special forward-declaration
1781 // debug type since we won't be able to lay out the entire type.
1782 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001783 if (!Def || !Def->getImplementation()) {
Adrian Prantl42ce2d32015-10-01 16:57:02 +00001784 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001785 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
Adrian Prantl42ce2d32015-10-01 16:57:02 +00001786 llvm::dwarf::DW_TAG_structure_type, ID->getName(), Mod ? Mod : TheCU,
1787 DefUnit, Line, RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001788 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001789 return FwdDecl;
1790 }
1791
David Blaikieef8a9512014-05-05 23:23:53 +00001792 return CreateTypeDefinition(Ty, Unit);
1793}
1794
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001795llvm::DIModule *
Adrian Prantl66689202015-09-18 23:01:45 +00001796CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod,
1797 bool CreateSkeletonCU) {
Adrian Prantleb66a262015-09-24 16:10:04 +00001798 // Use the Module pointer as the key into the cache. This is a
1799 // nullptr if the "Module" is a PCH, which is safe because we don't
1800 // support chained PCH debug info, so there can only be a single PCH.
1801 const Module *M = Mod.getModuleOrNull();
Adrian Prantl9e8ea352015-09-29 20:44:46 +00001802 auto ModRef = ModuleCache.find(M);
1803 if (ModRef != ModuleCache.end())
1804 return cast<llvm::DIModule>(ModRef->second);
Adrian Prantl2388ead2015-06-30 18:01:05 +00001805
1806 // Macro definitions that were defined with "-D" on the command line.
1807 SmallString<128> ConfigMacros;
1808 {
1809 llvm::raw_svector_ostream OS(ConfigMacros);
1810 const auto &PPOpts = CGM.getPreprocessorOpts();
1811 unsigned I = 0;
1812 // Translate the macro definitions back into a commmand line.
1813 for (auto &M : PPOpts.Macros) {
1814 if (++I > 1)
1815 OS << " ";
1816 const std::string &Macro = M.first;
1817 bool Undef = M.second;
1818 OS << "\"-" << (Undef ? 'U' : 'D');
1819 for (char c : Macro)
1820 switch (c) {
1821 case '\\' : OS << "\\\\"; break;
1822 case '"' : OS << "\\\""; break;
1823 default: OS << c;
1824 }
1825 OS << '\"';
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001826 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001827 }
Adrian Prantl66689202015-09-18 23:01:45 +00001828
Adrian Prantl835e6632015-09-24 16:10:10 +00001829 bool IsRootModule = M ? !M->Parent : true;
1830 if (CreateSkeletonCU && IsRootModule) {
Adrian Prantlc96da8f2016-01-22 17:43:43 +00001831 // PCH files don't have a signature field in the control block,
1832 // but LLVM detects skeleton CUs by looking for a non-zero DWO id.
Adrian Prantl98bfc822016-01-22 19:29:41 +00001833 uint64_t Signature = Mod.getSignature() ? Mod.getSignature() : ~1ULL;
Adrian Prantl66689202015-09-18 23:01:45 +00001834 llvm::DIBuilder DIB(CGM.getModule());
Adrian Prantl835e6632015-09-24 16:10:10 +00001835 DIB.createCompileUnit(TheCU->getSourceLanguage(), Mod.getModuleName(),
Adrian Prantl6083b082015-09-24 16:10:00 +00001836 Mod.getPath(), TheCU->getProducer(), true,
1837 StringRef(), 0, Mod.getASTFile(),
Adrian Prantl3563c552016-03-31 23:57:45 +00001838 llvm::DICompileUnit::FullDebug, Signature);
Adrian Prantl66689202015-09-18 23:01:45 +00001839 DIB.finalize();
Adrian Prantl2f957ac2015-09-19 00:59:22 +00001840 }
Adrian Prantl835e6632015-09-24 16:10:10 +00001841 llvm::DIModule *Parent =
1842 IsRootModule ? nullptr
1843 : getOrCreateModuleRef(
1844 ExternalASTSource::ASTSourceDescriptor(*M->Parent),
1845 CreateSkeletonCU);
Adrian Prantleb66a262015-09-24 16:10:04 +00001846 llvm::DIModule *DIMod =
Adrian Prantl835e6632015-09-24 16:10:10 +00001847 DBuilder.createModule(Parent, Mod.getModuleName(), ConfigMacros,
1848 Mod.getPath(), CGM.getHeaderSearchOpts().Sysroot);
Adrian Prantl9e8ea352015-09-29 20:44:46 +00001849 ModuleCache[M].reset(DIMod);
Adrian Prantleb66a262015-09-24 16:10:04 +00001850 return DIMod;
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001851}
1852
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001853llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
1854 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00001855 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001856 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00001857 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001858 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001859
1860 // Bit size, align and offset of the type.
1861 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1862 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1863
1864 unsigned Flags = 0;
1865 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001866 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00001867
Adrian Prantlfd696112015-10-01 00:48:51 +00001868 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001869 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Adrian Prantlfd696112015-10-01 00:48:51 +00001870 Mod ? Mod : Unit, ID->getName(), DefUnit, Line, Size, Align, Flags,
1871 nullptr, llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001872
David Blaikieef8a9512014-05-05 23:23:53 +00001873 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001874 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001875
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001876 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001877 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001878 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001879
1880 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001881 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001882
1883 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1884 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001885 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001886 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001887 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001888 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001889
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001890 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001891 EltTys.push_back(InhTag);
1892 }
1893
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001894 // Create entries for all of the properties.
Nico Weber7123bca2015-12-04 19:14:14 +00001895 auto AddProperty = [&](const ObjCPropertyDecl *PD) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001896 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001897 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001898 unsigned PLine = getLineNumber(Loc);
1899 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1900 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001901 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
1902 PD->getName(), PUnit, PLine,
1903 hasDefaultGetterName(PD, Getter) ? ""
1904 : getSelectorName(PD->getGetterName()),
1905 hasDefaultSetterName(PD, Setter) ? ""
1906 : getSelectorName(PD->getSetterName()),
1907 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001908 EltTys.push_back(PropertyNode);
Nico Weber7123bca2015-12-04 19:14:14 +00001909 };
1910 {
1911 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Nico Weberde059e12015-12-04 19:35:45 +00001912 for (const ObjCCategoryDecl *ClassExt : ID->known_extensions())
Manman Renefe1bac2016-01-27 20:00:32 +00001913 for (auto *PD : ClassExt->properties()) {
Nico Weberde059e12015-12-04 19:35:45 +00001914 PropertySet.insert(PD->getIdentifier());
1915 AddProperty(PD);
1916 }
Manman Renefe1bac2016-01-27 20:00:32 +00001917 for (const auto *PD : ID->properties()) {
Nico Weber7123bca2015-12-04 19:14:14 +00001918 // Don't emit duplicate metadata for properties that were already in a
1919 // class extension.
1920 if (!PropertySet.insert(PD->getIdentifier()).second)
1921 continue;
1922 AddProperty(PD);
1923 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001924 }
1925
1926 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1927 unsigned FieldNo = 0;
1928 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1929 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001930 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001931 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001932 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001933
Guy Benyei11169dd2012-12-18 14:30:41 +00001934 StringRef FieldName = Field->getName();
1935
1936 // Ignore unnamed fields.
1937 if (FieldName.empty())
1938 continue;
1939
1940 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001941 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001942 unsigned FieldLine = getLineNumber(Field->getLocation());
1943 QualType FType = Field->getType();
1944 uint64_t FieldSize = 0;
1945 unsigned FieldAlign = 0;
1946
1947 if (!FType->isIncompleteArrayType()) {
1948
1949 // Bit size, align and offset of the type.
1950 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001951 ? Field->getBitWidthValue(CGM.getContext())
1952 : CGM.getContext().getTypeSize(FType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001953 FieldAlign = CGM.getContext().getTypeAlign(FType);
1954 }
1955
1956 uint64_t FieldOffset;
1957 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1958 // We don't know the runtime offset of an ivar if we're using the
1959 // non-fragile ABI. For bitfields, use the bit offset into the first
1960 // byte of storage of the bitfield. For other fields, use zero.
1961 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001962 FieldOffset =
1963 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00001964 FieldOffset %= CGM.getContext().getCharWidth();
1965 } else {
1966 FieldOffset = 0;
1967 }
1968 } else {
1969 FieldOffset = RL.getFieldOffset(FieldNo);
1970 }
1971
1972 unsigned Flags = 0;
1973 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001974 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00001975 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001976 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001977 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001978 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00001979
Craig Topper8a13c412014-05-21 05:09:00 +00001980 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001981 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001982 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00001983 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001984 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00001985 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001986 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00001987 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001988 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1989 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001990 PropertyNode = DBuilder.createObjCProperty(
1991 PD->getName(), PUnit, PLine,
1992 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
1993 PD->getGetterName()),
1994 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
1995 PD->getSetterName()),
1996 PD->getPropertyAttributes(),
1997 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001998 }
1999 }
2000 }
Eric Christophere7b87e52014-10-26 23:40:33 +00002001 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
2002 FieldSize, FieldAlign, FieldOffset, Flags,
2003 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00002004 EltTys.push_back(FieldTy);
2005 }
2006
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002007 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002008 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00002009
Guy Benyei11169dd2012-12-18 14:30:41 +00002010 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00002011 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002012}
2013
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002014llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
2015 llvm::DIFile *Unit) {
2016 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002017 int64_t Count = Ty->getNumElements();
2018 if (Count == 0)
2019 // If number of elements are not known then this is an unbounded array.
2020 // Use Count == -1 to express such arrays.
2021 Count = -1;
2022
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002023 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002024 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00002025
2026 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2027 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
2028
2029 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
2030}
2031
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002032llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002033 uint64_t Size;
2034 uint64_t Align;
2035
2036 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
2037 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
2038 Size = 0;
2039 Align =
Eric Christophere7b87e52014-10-26 23:40:33 +00002040 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Guy Benyei11169dd2012-12-18 14:30:41 +00002041 } else if (Ty->isIncompleteArrayType()) {
2042 Size = 0;
2043 if (Ty->getElementType()->isIncompleteType())
2044 Align = 0;
2045 else
2046 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikief03b2e82013-05-09 20:48:12 +00002047 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002048 Size = 0;
2049 Align = 0;
2050 } else {
2051 // Size and align of the whole array, not the element type.
2052 Size = CGM.getContext().getTypeSize(Ty);
2053 Align = CGM.getContext().getTypeAlign(Ty);
2054 }
2055
2056 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
2057 // interior arrays, do we care? Why aren't nested arrays represented the
2058 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002059 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002060 QualType EltTy(Ty, 0);
2061 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
2062 // If the number of elements is known, then count is that number. Otherwise,
2063 // it's -1. This allows us to represent a subrange with an array of 0
2064 // elements, like this:
2065 //
2066 // struct foo {
2067 // int x[0];
2068 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00002069 int64_t Count = -1; // Count == -1 is an unbounded array.
Guy Benyei11169dd2012-12-18 14:30:41 +00002070 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
2071 Count = CAT->getSize().getZExtValue();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002072
Guy Benyei11169dd2012-12-18 14:30:41 +00002073 // FIXME: Verify this is right for VLAs.
2074 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
2075 EltTy = Ty->getElementType();
2076 }
2077
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002078 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002079
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002080 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
2081 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00002082}
2083
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002084llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
2085 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002086 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
2087 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002088}
2089
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002090llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
2091 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002092 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
2093 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002094}
2095
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002096llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
2097 llvm::DIFile *U) {
Reid Klecknerfb727182016-06-17 22:27:59 +00002098 unsigned Flags = 0;
2099 uint64_t Size = 0;
2100
2101 if (!Ty->isIncompleteType()) {
2102 Size = CGM.getContext().getTypeSize(Ty);
2103
2104 // Set the MS inheritance model. There is no flag for the unspecified model.
2105 if (CGM.getTarget().getCXXABI().isMicrosoft()) {
2106 switch (Ty->getMostRecentCXXRecordDecl()->getMSInheritanceModel()) {
2107 case MSInheritanceAttr::Keyword_single_inheritance:
2108 Flags |= llvm::DINode::FlagSingleInheritance;
2109 break;
2110 case MSInheritanceAttr::Keyword_multiple_inheritance:
2111 Flags |= llvm::DINode::FlagMultipleInheritance;
2112 break;
2113 case MSInheritanceAttr::Keyword_virtual_inheritance:
2114 Flags |= llvm::DINode::FlagVirtualInheritance;
2115 break;
2116 case MSInheritanceAttr::Keyword_unspecified_inheritance:
2117 break;
2118 }
2119 }
2120 }
2121
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002122 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00002123 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00002124 return DBuilder.createMemberPointerType(
Reid Klecknerfb727182016-06-17 22:27:59 +00002125 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size, /*Align=*/0,
2126 Flags);
Adrian Prantl0866acd2013-12-19 01:38:47 +00002127
2128 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00002129 Ty->getPointeeType()->getAs<FunctionProtoType>();
2130 return DBuilder.createMemberPointerType(
2131 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
2132 Ty->getClass(), FPT->getTypeQuals())),
2133 FPT, U),
Reid Klecknerfb727182016-06-17 22:27:59 +00002134 ClassType, Size, /*Align=*/0, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002135}
2136
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002137llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002138 // Ignore the atomic wrapping
2139 // FIXME: What is the correct representation?
2140 return getOrCreateType(Ty->getValueType(), U);
2141}
2142
Xiuli Pan9c14e282016-01-09 12:53:17 +00002143llvm::DIType* CGDebugInfo::CreateType(const PipeType *Ty,
2144 llvm::DIFile *U) {
2145 return getOrCreateType(Ty->getElementType(), U);
2146}
2147
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002148llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00002149 const EnumDecl *ED = Ty->getDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002150
Guy Benyei11169dd2012-12-18 14:30:41 +00002151 uint64_t Size = 0;
2152 uint64_t Align = 0;
2153 if (!ED->getTypeForDecl()->isIncompleteType()) {
2154 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
2155 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
2156 }
2157
Manman Rene0064d82013-08-29 23:19:58 +00002158 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2159
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002160 bool isImportedFromModule =
2161 DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition();
2162
Guy Benyei11169dd2012-12-18 14:30:41 +00002163 // If this is just a forward declaration, construct an appropriately
2164 // marked node and just return it.
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002165 if (isImportedFromModule || !ED->getDefinition()) {
Adrian Prantl45946062016-02-23 19:30:08 +00002166 // Note that it is possible for enums to be created as part of
2167 // their own declcontext. In this case a FwdDecl will be created
2168 // twice. This doesn't cause a problem because both FwdDecls are
2169 // entered into the ReplaceMap: finalize() will replace the first
2170 // FwdDecl with the second and then replace the second with
2171 // complete type.
Amjad Abouddc4531e2016-04-30 01:44:38 +00002172 llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002173 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Adrian Prantlff9d83c2016-02-08 17:03:28 +00002174 llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType(
2175 llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0));
Adrian Prantla40030f2016-02-06 01:59:09 +00002176
Guy Benyei11169dd2012-12-18 14:30:41 +00002177 unsigned Line = getLineNumber(ED->getLocation());
2178 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002179 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
Adrian Prantl45946062016-02-23 19:30:08 +00002180 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
2181 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
Adrian Prantla40030f2016-02-06 01:59:09 +00002182
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002183 ReplaceMap.emplace_back(
2184 std::piecewise_construct, std::make_tuple(Ty),
2185 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00002186 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00002187 }
2188
David Blaikie483a9da2014-05-06 18:35:21 +00002189 return CreateTypeDefinition(Ty);
2190}
2191
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002192llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00002193 const EnumDecl *ED = Ty->getDecl();
2194 uint64_t Size = 0;
2195 uint64_t Align = 0;
2196 if (!ED->getTypeForDecl()->isIncompleteType()) {
2197 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
2198 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
2199 }
2200
2201 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2202
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002203 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002204 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00002205 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00002206 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002207 Enumerators.push_back(DBuilder.createEnumerator(
2208 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002209 }
2210
2211 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002212 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00002213
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002214 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002215 unsigned Line = getLineNumber(ED->getLocation());
Amjad Abouddc4531e2016-04-30 01:44:38 +00002216 llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002217 llvm::DIType *ClassTy =
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002218 ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
2219 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
2220 Line, Size, Align, EltArray, ClassTy,
2221 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002222}
2223
David Blaikie05491062013-01-21 04:37:12 +00002224static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
2225 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002226 do {
Adrian Prantl179af902013-09-26 21:35:50 +00002227 Qualifiers InnerQuals = T.getLocalQualifiers();
2228 // Qualifiers::operator+() doesn't like it if you add a Qualifier
2229 // that is already there.
2230 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2231 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002232 QualType LastT = T;
2233 switch (T->getTypeClass()) {
2234 default:
David Blaikie05491062013-01-21 04:37:12 +00002235 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002236 case Type::TemplateSpecialization: {
2237 const auto *Spec = cast<TemplateSpecializationType>(T);
2238 if (Spec->isTypeAlias())
2239 return C.getQualifiedType(T.getTypePtr(), Quals);
2240 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002241 break;
2242 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002243 case Type::TypeOfExpr:
2244 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2245 break;
2246 case Type::TypeOf:
2247 T = cast<TypeOfType>(T)->getUnderlyingType();
2248 break;
2249 case Type::Decltype:
2250 T = cast<DecltypeType>(T)->getUnderlyingType();
2251 break;
2252 case Type::UnaryTransform:
2253 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2254 break;
2255 case Type::Attributed:
2256 T = cast<AttributedType>(T)->getEquivalentType();
2257 break;
2258 case Type::Elaborated:
2259 T = cast<ElaboratedType>(T)->getNamedType();
2260 break;
2261 case Type::Paren:
2262 T = cast<ParenType>(T)->getInnerType();
2263 break;
David Blaikie05491062013-01-21 04:37:12 +00002264 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002265 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002266 break;
2267 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00002268 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002269 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002270 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002271 break;
2272 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002273
Guy Benyei11169dd2012-12-18 14:30:41 +00002274 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002275 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002276 } while (true);
2277}
2278
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002279llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002280
2281 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002282 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002283
David Blaikief427b002014-05-06 03:42:01 +00002284 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002285 if (it != TypeCache.end()) {
2286 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002287 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002288 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002289 }
2290
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002291 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002292}
2293
David Blaikie0e716b42014-03-03 23:48:23 +00002294void CGDebugInfo::completeTemplateDefinition(
2295 const ClassTemplateSpecializationDecl &SD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002296 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie0856f662014-03-04 22:01:08 +00002297 return;
2298
David Blaikie0e716b42014-03-03 23:48:23 +00002299 completeClassData(&SD);
2300 // In case this type has no member function definitions being emitted, ensure
2301 // it is retained
2302 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2303}
2304
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002305llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002306 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002307 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002308
2309 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002310 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002311
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002312 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002313 return T;
2314
Adrian Prantlca844182015-09-11 17:23:03 +00002315 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002316 void* TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002317
2318 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002319 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002320
Guy Benyei11169dd2012-12-18 14:30:41 +00002321 return Res;
2322}
2323
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002324llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
Adrian Prantl335f5c72015-10-02 17:36:14 +00002325 // A forward declaration inside a module header does not belong to the module.
2326 if (isa<RecordDecl>(D) && !cast<RecordDecl>(D)->getDefinition())
2327 return nullptr;
Adrian Prantl85d938a2015-09-21 17:48:37 +00002328 if (DebugTypeExtRefs && D->isFromASTFile()) {
2329 // Record a reference to an imported clang module or precompiled header.
2330 auto *Reader = CGM.getContext().getExternalSource();
2331 auto Idx = D->getOwningModuleID();
2332 auto Info = Reader->getSourceDescriptor(Idx);
2333 if (Info)
2334 return getOrCreateModuleRef(*Info, /*SkeletonCU=*/true);
2335 } else if (ClangModuleMap) {
Adrian Prantl9402cef2015-09-20 16:51:35 +00002336 // We are building a clang module or a precompiled header.
2337 //
2338 // TODO: When D is a CXXRecordDecl or a C++ Enum, the ODR applies
2339 // and it wouldn't be necessary to specify the parent scope
2340 // because the type is already unique by definition (it would look
2341 // like the output of -fno-standalone-debug). On the other hand,
2342 // the parent scope helps a consumer to quickly locate the object
2343 // file where the type's definition is located, so it might be
2344 // best to make this behavior a command line or debugger tuning
2345 // option.
2346 FullSourceLoc Loc(D->getLocation(), CGM.getContext().getSourceManager());
2347 if (Module *M = ClangModuleMap->inferModuleFromLocation(Loc)) {
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002348 // This is a (sub-)module.
Adrian Prantl9402cef2015-09-20 16:51:35 +00002349 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
2350 return getOrCreateModuleRef(Info, /*SkeletonCU=*/false);
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002351 } else {
2352 // This the precompiled header being built.
2353 return getOrCreateModuleRef(PCHDescriptor, /*SkeletonCU=*/false);
Adrian Prantl9402cef2015-09-20 16:51:35 +00002354 }
2355 }
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002356
Adrian Prantl9402cef2015-09-20 16:51:35 +00002357 return nullptr;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002358}
2359
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002360llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002361 // Handle qualifiers, which recursively handles what they refer to.
2362 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002363 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002364
Guy Benyei11169dd2012-12-18 14:30:41 +00002365 // Work out details of type.
2366 switch (Ty->getTypeClass()) {
2367#define TYPE(Class, Base)
2368#define ABSTRACT_TYPE(Class, Base)
2369#define NON_CANONICAL_TYPE(Class, Base)
2370#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2371#include "clang/AST/TypeNodes.def"
2372 llvm_unreachable("Dependent types cannot show up in debug information");
2373
2374 case Type::ExtVector:
2375 case Type::Vector:
2376 return CreateType(cast<VectorType>(Ty), Unit);
2377 case Type::ObjCObjectPointer:
2378 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2379 case Type::ObjCObject:
2380 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2381 case Type::ObjCInterface:
2382 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2383 case Type::Builtin:
2384 return CreateType(cast<BuiltinType>(Ty));
2385 case Type::Complex:
2386 return CreateType(cast<ComplexType>(Ty));
2387 case Type::Pointer:
2388 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002389 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002390 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002391 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002392 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002393 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002394 case Type::BlockPointer:
2395 return CreateType(cast<BlockPointerType>(Ty), Unit);
2396 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002397 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002398 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002399 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002400 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002401 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002402 case Type::FunctionProto:
2403 case Type::FunctionNoProto:
2404 return CreateType(cast<FunctionType>(Ty), Unit);
2405 case Type::ConstantArray:
2406 case Type::VariableArray:
2407 case Type::IncompleteArray:
2408 return CreateType(cast<ArrayType>(Ty), Unit);
2409
2410 case Type::LValueReference:
2411 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2412 case Type::RValueReference:
2413 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2414
2415 case Type::MemberPointer:
2416 return CreateType(cast<MemberPointerType>(Ty), Unit);
2417
2418 case Type::Atomic:
2419 return CreateType(cast<AtomicType>(Ty), Unit);
2420
Xiuli Pan9c14e282016-01-09 12:53:17 +00002421 case Type::Pipe:
2422 return CreateType(cast<PipeType>(Ty), Unit);
2423
Guy Benyei11169dd2012-12-18 14:30:41 +00002424 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002425 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2426
David Blaikie42edade2014-11-11 20:44:45 +00002427 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002428 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002429 case Type::Elaborated:
2430 case Type::Paren:
2431 case Type::SubstTemplateTypeParm:
2432 case Type::TypeOfExpr:
2433 case Type::TypeOf:
2434 case Type::Decltype:
2435 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002436 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002437 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002438 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002439
David Blaikie42edade2014-11-11 20:44:45 +00002440 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002441}
2442
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002443llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2444 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002445 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002446
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002447 auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002448
2449 // We may have cached a forward decl when we could have created
2450 // a non-forward decl. Go ahead and create a non-forward decl
2451 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002452 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002453 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002454
2455 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002456 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002457
2458 // Propagate members from the declaration to the definition
2459 // CreateType(const RecordType*) will overwrite this with the members in the
2460 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002461 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002462
Guy Benyei11169dd2012-12-18 14:30:41 +00002463 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002464 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002465 return Res;
2466}
2467
2468// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002469llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002470 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002471
Guy Benyei11169dd2012-12-18 14:30:41 +00002472 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002473 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002474 unsigned Line = getLineNumber(RD->getLocation());
2475 StringRef RDName = getClassName(RD);
2476
Amjad Abouddc4531e2016-04-30 01:44:38 +00002477 llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002478
David Blaikied2785892013-08-18 17:36:19 +00002479 // If we ended up creating the type during the context chain construction,
2480 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002481 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002482 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002483 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002484 return T;
David Blaikied2785892013-08-18 17:36:19 +00002485
Adrian Prantl381e7552014-02-04 21:29:50 +00002486 // If this is just a forward or incomplete declaration, construct an
2487 // appropriately marked node and just return it.
2488 const RecordDecl *D = RD->getDefinition();
2489 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002490 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002491
2492 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2493 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002494
Manman Rene0064d82013-08-29 23:19:58 +00002495 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2496
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002497 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002498 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align, 0,
2499 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002500
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00002501 // Elements of composite types usually have back to the type, creating
2502 // uniquing cycles. Distinct nodes are more efficient.
2503 switch (RealDecl->getTag()) {
2504 default:
2505 llvm_unreachable("invalid composite type tag");
2506
2507 case llvm::dwarf::DW_TAG_array_type:
2508 case llvm::dwarf::DW_TAG_enumeration_type:
2509 // Array elements and most enumeration elements don't have back references,
2510 // so they don't tend to be involved in uniquing cycles and there is some
2511 // chance of merging them when linking together two modules. Only make
2512 // them distinct if they are ODR-uniqued.
2513 if (FullName.empty())
2514 break;
2515
2516 case llvm::dwarf::DW_TAG_structure_type:
2517 case llvm::dwarf::DW_TAG_union_type:
2518 case llvm::dwarf::DW_TAG_class_type:
2519 // Immediatley resolve to a distinct node.
2520 RealDecl =
2521 llvm::MDNode::replaceWithDistinct(llvm::TempDICompositeType(RealDecl));
2522 break;
2523 }
2524
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002525 RegionMap[Ty->getDecl()].reset(RealDecl);
2526 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002527
David Blaikieadfbf992013-08-18 16:55:33 +00002528 if (const ClassTemplateSpecializationDecl *TSpecial =
2529 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002530 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002531 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002532 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002533}
2534
David Blaikieadfbf992013-08-18 16:55:33 +00002535void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002536 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002537 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002538 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002539 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2540 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002541 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002542 while (1) {
2543 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2544 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2545 if (PBT && !BRL.isPrimaryBaseVirtual())
2546 PBase = PBT;
2547 else
2548 break;
2549 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002550 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002551 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2552 getOrCreateFile(RD->getLocation())));
2553 } else if (RD->isDynamicClass())
2554 ContainingType = RealDecl;
2555
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002556 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002557}
2558
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002559llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002560 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002561 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002562 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2563 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002564 llvm::DIType *Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002565 FieldAlign, *Offset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002566 *Offset += FieldSize;
2567 return Ty;
2568}
2569
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002570void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002571 StringRef &Name,
2572 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002573 llvm::DIScope *&FDContext,
2574 llvm::DINodeArray &TParamsArray,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002575 unsigned &Flags) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002576 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2577 Name = getFunctionName(FD);
2578 // Use mangled name as linkage name for C/C++ functions.
2579 if (FD->hasPrototype()) {
2580 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002581 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002582 }
2583 // No need to replicate the linkage name if it isn't different from the
2584 // subprogram name, no need to have it at all unless coverage is enabled or
2585 // debug is set to more than just line tables.
Benjamin Kramer8c305922016-02-02 11:06:51 +00002586 if (LinkageName == Name || (!CGM.getCodeGenOpts().EmitGcovArcs &&
2587 !CGM.getCodeGenOpts().EmitGcovNotes &&
2588 DebugKind <= codegenoptions::DebugLineTablesOnly))
Frederic Riss9db79f12014-11-18 03:40:46 +00002589 LinkageName = StringRef();
2590
Benjamin Kramer8c305922016-02-02 11:06:51 +00002591 if (DebugKind >= codegenoptions::LimitedDebugInfo) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002592 if (const NamespaceDecl *NSDecl =
2593 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2594 FDContext = getOrCreateNameSpace(NSDecl);
2595 else if (const RecordDecl *RDecl =
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002596 dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) {
2597 llvm::DIScope *Mod = getParentModuleOrNull(RDecl);
2598 FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU);
2599 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002600 // Collect template parameters.
2601 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2602 }
2603}
2604
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002605void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002606 unsigned &LineNo, QualType &T,
2607 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002608 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002609 Unit = getOrCreateFile(VD->getLocation());
2610 LineNo = getLineNumber(VD->getLocation());
2611
2612 setLocation(VD->getLocation());
2613
2614 T = VD->getType();
2615 if (T->isIncompleteArrayType()) {
2616 // CodeGen turns int[] into int[1] so we'll do the same here.
2617 llvm::APInt ConstVal(32, 1);
2618 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2619
2620 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2621 ArrayType::Normal, 0);
2622 }
2623
2624 Name = VD->getName();
2625 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2626 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2627 LinkageName = CGM.getMangledName(VD);
2628 if (LinkageName == Name)
2629 LinkageName = StringRef();
2630
2631 // Since we emit declarations (DW_AT_members) for static members, place the
2632 // definition of those static members in the namespace they were declared in
2633 // in the source code (the lexical decl context).
2634 // FIXME: Generalize this for even non-member global variables where the
2635 // declaration and definition may have different lexical decl contexts, once
2636 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002637 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2638 : VD->getDeclContext();
2639 // When a record type contains an in-line initialization of a static data
2640 // member, and the record type is marked as __declspec(dllexport), an implicit
2641 // definition of the member will be created in the record context. DWARF
2642 // doesn't seem to have a nice way to describe this in a form that consumers
2643 // are likely to understand, so fake the "normal" situation of a definition
2644 // outside the class by putting it in the global scope.
2645 if (DC->isRecord())
2646 DC = CGM.getContext().getTranslationUnitDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002647
Amjad Abouddc4531e2016-04-30 01:44:38 +00002648 llvm::DIScope *Mod = getParentModuleOrNull(VD);
2649 VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
Frederic Riss9db79f12014-11-18 03:40:46 +00002650}
2651
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002652llvm::DISubprogram *
Frederic Rissd253ed62014-11-18 03:40:51 +00002653CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002654 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00002655 StringRef Name, LinkageName;
2656 unsigned Flags = 0;
2657 SourceLocation Loc = FD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002658 llvm::DIFile *Unit = getOrCreateFile(Loc);
2659 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002660 unsigned Line = getLineNumber(Loc);
2661
2662 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2663 TParamsArray, Flags);
2664 // Build function type.
2665 SmallVector<QualType, 16> ArgTypes;
2666 for (const ParmVarDecl *Parm: FD->parameters())
2667 ArgTypes.push_back(Parm->getType());
Reid Klecknerf00f8032016-06-08 20:41:54 +00002668 CallingConv CC = FD->getType()->castAs<FunctionType>()->getCallConv();
2669 QualType FnType = CGM.getContext().getFunctionType(
2670 FD->getReturnType(), ArgTypes, FunctionProtoType::ExtProtoInfo(CC));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002671 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002672 DContext, Name, LinkageName, Unit, Line,
2673 getOrCreateFunctionType(FD, FnType, Unit), !FD->isExternallyVisible(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002674 /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002675 TParamsArray.get(), getFunctionDeclaration(FD));
Frederic Rissd253ed62014-11-18 03:40:51 +00002676 const FunctionDecl *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002677 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2678 std::make_tuple(CanonDecl),
2679 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00002680 return SP;
2681}
2682
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002683llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00002684CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2685 QualType T;
2686 StringRef Name, LinkageName;
2687 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002688 llvm::DIFile *Unit = getOrCreateFile(Loc);
2689 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002690 unsigned Line = getLineNumber(Loc);
2691
2692 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002693 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
2694 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
2695 !VD->isExternallyVisible(), nullptr, nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002696 FwdDeclReplaceMap.emplace_back(
2697 std::piecewise_construct,
2698 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2699 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002700 return GV;
2701}
2702
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002703llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002704 // We only need a declaration (not a definition) of the type - so use whatever
2705 // we would otherwise do to get a type for a pointee. (forward declarations in
2706 // limited debug info, full definitions (if the type definition is available)
2707 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002708 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2709 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002710 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002711 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002712
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002713 if (I != DeclCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002714 return dyn_cast_or_null<llvm::DINode>(I->second);
Frederic Rissd253ed62014-11-18 03:40:51 +00002715
2716 // No definition for now. Emit a forward definition that might be
2717 // merged with a potential upcoming definition.
2718 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
2719 return getFunctionForwardDeclaration(FD);
2720 else if (const auto *VD = dyn_cast<VarDecl>(D))
2721 return getGlobalVariableForwardDeclaration(VD);
2722
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002723 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00002724}
2725
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002726llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002727 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002728 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00002729
Guy Benyei11169dd2012-12-18 14:30:41 +00002730 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002731 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002732 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002733
2734 // Setup context.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002735 auto *S = getDeclContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00002736
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002737 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002738 if (MI == SPCache.end()) {
Eric Christopherf86c4052013-08-28 23:12:10 +00002739 if (const CXXMethodDecl *MD =
2740 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00002741 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002742 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00002743 }
2744 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002745 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002746 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002747 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002748 return SP;
2749 }
2750
Aaron Ballman86c93902014-03-06 23:45:36 +00002751 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002752 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002753 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002754 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002755 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002756 return SP;
2757 }
2758 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002759 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002760}
2761
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002762// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00002763// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002764llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002765 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002766 llvm::DIFile *F) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002767 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002768 // Create fake but valid subroutine type. Otherwise -verify would fail, and
2769 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Eric Christopher28a6db52015-10-15 06:56:08 +00002770 return DBuilder.createSubroutineType(DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002771
2772 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2773 return getOrCreateMethodType(Method, F);
Reid Klecknerf00f8032016-06-08 20:41:54 +00002774
2775 const auto *FTy = FnType->getAs<FunctionType>();
2776 CallingConv CC = FTy ? FTy->getCallConv() : CallingConv::CC_C;
2777
Guy Benyei11169dd2012-12-18 14:30:41 +00002778 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2779 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002780 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002781
2782 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002783 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002784
2785 // Replace the instancetype keyword with the actual type.
2786 if (ResultTy == CGM.getContext().getObjCInstanceType())
2787 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002788 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002789
Adrian Prantl7bec9032013-05-10 21:08:31 +00002790 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002791 // "self" pointer is always first argument.
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002792 QualType SelfDeclTy;
2793 if (auto *SelfDecl = OMethod->getSelfDecl())
2794 SelfDeclTy = SelfDecl->getType();
2795 else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType))
2796 if (FPT->getNumParams() > 1)
2797 SelfDeclTy = FPT->getParamType(0);
2798 if (!SelfDeclTy.isNull())
2799 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002800 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002801 Elts.push_back(DBuilder.createArtificialType(
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002802 getOrCreateType(CGM.getContext().getObjCSelType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002803 // Get rest of the arguments.
David Majnemer59f77922016-06-24 04:05:48 +00002804 for (const auto *PI : OMethod->parameters())
Aaron Ballman43b68be2014-03-07 17:50:17 +00002805 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002806 // Variadic methods need a special marker at the end of the type list.
2807 if (OMethod->isVariadic())
2808 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002809
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002810 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Reid Klecknerf00f8032016-06-08 20:41:54 +00002811 return DBuilder.createSubroutineType(EltTypeArray, 0, getDwarfCC(CC));
Guy Benyei11169dd2012-12-18 14:30:41 +00002812 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002813
Adrian Prantl800faef2014-02-25 23:42:18 +00002814 // Handle variadic function types; they need an additional
2815 // unspecified parameter.
Adrian Prantld45ba252014-02-25 19:38:11 +00002816 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2817 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002818 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00002819 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2820 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2821 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2822 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2823 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002824 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Reid Klecknerf00f8032016-06-08 20:41:54 +00002825 return DBuilder.createSubroutineType(EltTypeArray, 0, getDwarfCC(CC));
Adrian Prantld45ba252014-02-25 19:38:11 +00002826 }
2827
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002828 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002829}
2830
Eric Christophere7b87e52014-10-26 23:40:33 +00002831void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2832 SourceLocation ScopeLoc, QualType FnType,
2833 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002834
2835 StringRef Name;
2836 StringRef LinkageName;
2837
2838 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2839
2840 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002841 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00002842
Guy Benyei11169dd2012-12-18 14:30:41 +00002843 unsigned Flags = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002844 llvm::DIFile *Unit = getOrCreateFile(Loc);
2845 llvm::DIScope *FDContext = Unit;
2846 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00002847 if (!HasDecl) {
2848 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00002849 LinkageName = Fn->getName();
Guy Benyei11169dd2012-12-18 14:30:41 +00002850 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002851 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002852 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002853 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002854 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002855 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002856 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002857 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002858 return;
2859 }
2860 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002861 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2862 TParamsArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002863 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2864 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002865 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002866 } else {
2867 // Use llvm function name.
2868 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002869 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002870 }
2871 if (!Name.empty() && Name[0] == '\01')
2872 Name = Name.substr(1);
2873
Adrian Prantl42d71b92014-04-10 23:21:53 +00002874 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002875 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl42d71b92014-04-10 23:21:53 +00002876 // Artificial functions without a location should not silently reuse CurLoc.
2877 if (Loc.isInvalid())
2878 CurLoc = SourceLocation();
2879 }
2880 unsigned LineNo = getLineNumber(Loc);
2881 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002882
Eric Christopher8018e412014-03-27 18:50:35 +00002883 // FIXME: The function declaration we're constructing here is mostly reusing
2884 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2885 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2886 // all subprograms instead of the actual context since subprogram definitions
2887 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002888 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00002889 FDContext, Name, LinkageName, Unit, LineNo,
2890 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002891 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002892 TParamsArray.get(), getFunctionDeclaration(D));
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002893 Fn->setSubprogram(SP);
Frederic Rissb1ab28c2014-11-05 19:19:04 +00002894 // We might get here with a VarDecl in the case we're generating
2895 // code for the initialization of globals. Do not record these decls
2896 // as they will overwrite the actual VarDecl Decl in the cache.
2897 if (HasDecl && isa<FunctionDecl>(D))
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002898 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(SP));
Guy Benyei11169dd2012-12-18 14:30:41 +00002899
Adrian Prantlbebb8932014-03-21 21:01:58 +00002900 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002901 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00002902
Guy Benyei11169dd2012-12-18 14:30:41 +00002903 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002904 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002905}
2906
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002907void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
2908 QualType FnType) {
2909 StringRef Name;
2910 StringRef LinkageName;
2911
2912 const Decl *D = GD.getDecl();
2913 if (!D)
2914 return;
2915
2916 unsigned Flags = 0;
2917 llvm::DIFile *Unit = getOrCreateFile(Loc);
Adrian Prantlf0cd6772015-10-04 23:23:04 +00002918 llvm::DIScope *FDContext = getDeclContextDescriptor(D);
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002919 llvm::DINodeArray TParamsArray;
2920 if (isa<FunctionDecl>(D)) {
2921 // If there is a DISubprogram for this function available then use it.
2922 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2923 TParamsArray, Flags);
2924 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2925 Name = getObjCMethodName(OMD);
2926 Flags |= llvm::DINode::FlagPrototyped;
2927 } else {
2928 llvm_unreachable("not a function or ObjC method");
2929 }
2930 if (!Name.empty() && Name[0] == '\01')
2931 Name = Name.substr(1);
2932
2933 if (D->isImplicit()) {
2934 Flags |= llvm::DINode::FlagArtificial;
2935 // Artificial functions without a location should not silently reuse CurLoc.
2936 if (Loc.isInvalid())
2937 CurLoc = SourceLocation();
2938 }
2939 unsigned LineNo = getLineNumber(Loc);
2940 unsigned ScopeLine = 0;
2941
Adrian Prantle76bda52016-04-15 15:55:45 +00002942 DBuilder.retainType(DBuilder.createFunction(
2943 FDContext, Name, LinkageName, Unit, LineNo,
2944 getOrCreateFunctionType(D, FnType, Unit), false /*internalLinkage*/,
2945 false /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
2946 TParamsArray.get(), getFunctionDeclaration(D)));
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002947}
2948
David Blaikie835afb22015-01-21 23:08:17 +00002949void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002950 // Update our current location
2951 setLocation(Loc);
2952
Eric Christophere7b87e52014-10-26 23:40:33 +00002953 if (CurLoc.isInvalid() || CurLoc.isMacroID())
2954 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002955
Adrian Prantle83b1302014-01-07 22:05:52 +00002956 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00002957 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
David Blaikie835afb22015-01-21 23:08:17 +00002958 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002959}
2960
Guy Benyei11169dd2012-12-18 14:30:41 +00002961void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002962 llvm::MDNode *Back = nullptr;
2963 if (!LexicalBlockStack.empty())
2964 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002965 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002966 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002967 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002968}
2969
Eric Christopher0fdcb312013-05-16 00:52:20 +00002970void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2971 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002972 // Set our current location.
2973 setLocation(Loc);
2974
Guy Benyei11169dd2012-12-18 14:30:41 +00002975 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00002976 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2977 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00002978
Benjamin Kramer8c305922016-02-02 11:06:51 +00002979 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00002980 return;
2981
2982 // Create a new lexical block and push it on the stack.
2983 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002984}
2985
Eric Christopher0fdcb312013-05-16 00:52:20 +00002986void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2987 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002988 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2989
2990 // Provide an entry in the line table for the end of the block.
2991 EmitLocation(Builder, Loc);
2992
Benjamin Kramer8c305922016-02-02 11:06:51 +00002993 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00002994 return;
2995
Guy Benyei11169dd2012-12-18 14:30:41 +00002996 LexicalBlockStack.pop_back();
2997}
2998
Guy Benyei11169dd2012-12-18 14:30:41 +00002999void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
3000 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3001 unsigned RCount = FnBeginRegionCount.back();
3002 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
3003
3004 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00003005 while (LexicalBlockStack.size() != RCount) {
3006 // Provide an entry in the line table for the end of the block.
3007 EmitLocation(Builder, CurLoc);
3008 LexicalBlockStack.pop_back();
3009 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003010 FnBeginRegionCount.pop_back();
3011}
3012
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003013llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003014 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003015
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003016 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00003017 QualType FType;
3018 uint64_t FieldSize, FieldOffset;
3019 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003020
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003021 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003022 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00003023
3024 FieldOffset = 0;
3025 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
3026 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
3027 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
3028 FType = CGM.getContext().IntTy;
3029 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
3030 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
3031
3032 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
3033 if (HasCopyAndDispose) {
3034 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003035 EltTys.push_back(
3036 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
3037 EltTys.push_back(
3038 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00003039 }
3040 bool HasByrefExtendedLayout;
3041 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00003042 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
3043 HasByrefExtendedLayout) &&
3044 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00003045 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003046 EltTys.push_back(
3047 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00003048 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003049
Guy Benyei11169dd2012-12-18 14:30:41 +00003050 CharUnits Align = CGM.getContext().getDeclAlign(VD);
3051 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003052 CGM.getTarget().getPointerAlign(0))) {
3053 CharUnits FieldOffsetInBytes =
3054 CGM.getContext().toCharUnitsFromBits(FieldOffset);
Rui Ueyama83aa9792016-01-14 21:00:27 +00003055 CharUnits AlignedOffsetInBytes = FieldOffsetInBytes.alignTo(Align);
Eric Christophere7b87e52014-10-26 23:40:33 +00003056 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003057
Guy Benyei11169dd2012-12-18 14:30:41 +00003058 if (NumPaddingBytes.isPositive()) {
3059 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
3060 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
3061 pad, ArrayType::Normal, 0);
3062 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
3063 }
3064 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003065
Guy Benyei11169dd2012-12-18 14:30:41 +00003066 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003067 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003068 FieldSize = CGM.getContext().getTypeSize(FType);
3069 FieldAlign = CGM.getContext().toBits(Align);
3070
Eric Christopherb2a008c2013-05-16 00:45:12 +00003071 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00003072 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
3073 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003074 EltTys.push_back(FieldTy);
3075 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003076
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003077 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003078
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003079 unsigned Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003080
Guy Benyei11169dd2012-12-18 14:30:41 +00003081 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003082 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00003083}
3084
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003085void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::Value *Storage,
3086 llvm::Optional<unsigned> ArgNo,
Eric Christophere7b87e52014-10-26 23:40:33 +00003087 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003088 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003089 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003090 if (VD->hasAttr<NoDebugAttr>())
3091 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003092
David Blaikie7fceebf2013-08-19 03:37:48 +00003093 bool Unwritten =
3094 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
3095 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003096 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00003097 if (!Unwritten)
3098 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003099 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003100 uint64_t XOffset = 0;
3101 if (VD->hasAttr<BlocksAttr>())
3102 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003103 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003104 Ty = getOrCreateType(VD->getType(), Unit);
3105
3106 // If there is no debug info for this type then do not emit debug info
3107 // for this variable.
3108 if (!Ty)
3109 return;
3110
Guy Benyei11169dd2012-12-18 14:30:41 +00003111 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00003112 unsigned Line = 0;
3113 unsigned Column = 0;
3114 if (!Unwritten) {
3115 Line = getLineNumber(VD->getLocation());
3116 Column = getColumnNumber(VD->getLocation());
3117 }
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003118 SmallVector<int64_t, 9> Expr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003119 unsigned Flags = 0;
3120 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003121 Flags |= llvm::DINode::FlagArtificial;
Guy Benyei11169dd2012-12-18 14:30:41 +00003122 // If this is the first argument and it is implicit then
3123 // give it an object pointer flag.
3124 // FIXME: There has to be a better way to do this, but for static
3125 // functions there won't be an implicit param at arg1 and
3126 // otherwise it is 'self' or 'this'.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003127 if (isa<ImplicitParamDecl>(VD) && ArgNo && *ArgNo == 1)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003128 Flags |= llvm::DINode::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00003129 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00003130 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
3131 !VD->getType()->isPointerType())
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003132 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei11169dd2012-12-18 14:30:41 +00003133
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003134 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003135
3136 StringRef Name = VD->getName();
3137 if (!Name.empty()) {
3138 if (VD->hasAttr<BlocksAttr>()) {
3139 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003140 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003141 // offset of __forwarding field
3142 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003143 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003144 Expr.push_back(offset.getQuantity());
3145 Expr.push_back(llvm::dwarf::DW_OP_deref);
3146 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003147 // offset of x field
3148 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003149 Expr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003150
3151 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003152 auto *D = ArgNo
3153 ? DBuilder.createParameterVariable(Scope, VD->getName(),
3154 *ArgNo, Unit, Line, Ty)
3155 : DBuilder.createAutoVariable(Scope, VD->getName(), Unit,
3156 Line, Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003157
Guy Benyei11169dd2012-12-18 14:30:41 +00003158 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003159 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3160 llvm::DebugLoc::get(Line, Column, Scope),
3161 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003162 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00003163 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003164 Expr.push_back(llvm::dwarf::DW_OP_deref);
Adrian Prantl0d820892015-04-29 15:05:50 +00003165 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
3166 // If VD is an anonymous union then Storage represents value for
3167 // all union fields.
3168 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
3169 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00003170 // GDB has trouble finding local variables in anonymous unions, so we emit
3171 // artifical local variables for each of the members.
3172 //
3173 // FIXME: Remove this code as soon as GDB supports this.
3174 // The debug info verifier in LLVM operates based on the assumption that a
3175 // variable has the same size as its storage and we had to disable the check
3176 // for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00003177 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003178 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00003179 StringRef FieldName = Field->getName();
3180
3181 // Ignore unnamed fields. Do not ignore unnamed records.
3182 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
3183 continue;
3184
3185 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003186 auto *D = DBuilder.createAutoVariable(
3187 Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
3188 Flags | llvm::DINode::FlagArtificial);
Adrian Prantl0d820892015-04-29 15:05:50 +00003189
3190 // Insert an llvm.dbg.declare into the current block.
3191 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3192 llvm::DebugLoc::get(Line, Column, Scope),
3193 Builder.GetInsertBlock());
3194 }
Adrian Prantl0d820892015-04-29 15:05:50 +00003195 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003196 }
David Blaikiea76a7c92013-01-05 05:58:35 +00003197
3198 // Create the descriptor for the variable.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003199 auto *D =
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003200 ArgNo
3201 ? DBuilder.createParameterVariable(Scope, Name, *ArgNo, Unit, Line,
3202 Ty, CGM.getLangOpts().Optimize,
3203 Flags)
3204 : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
3205 CGM.getLangOpts().Optimize, Flags);
David Blaikiea76a7c92013-01-05 05:58:35 +00003206
3207 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003208 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3209 llvm::DebugLoc::get(Line, Column, Scope),
3210 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003211}
3212
3213void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
3214 llvm::Value *Storage,
3215 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003216 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003217 EmitDeclare(VD, Storage, llvm::None, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003218}
3219
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003220llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
3221 llvm::DIType *Ty) {
3222 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003223 if (CachedTy)
3224 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00003225 return DBuilder.createObjectPointerType(Ty);
3226}
3227
Eric Christophere7b87e52014-10-26 23:40:33 +00003228void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
3229 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00003230 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003231 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003232 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00003233
Craig Topper8a13c412014-05-21 05:09:00 +00003234 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00003235 return;
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003236 if (VD->hasAttr<NoDebugAttr>())
3237 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003238
Guy Benyei11169dd2012-12-18 14:30:41 +00003239 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00003240
Guy Benyei11169dd2012-12-18 14:30:41 +00003241 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003242 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
3243 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003244 if (isByRef)
3245 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003246 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003247 Ty = getOrCreateType(VD->getType(), Unit);
3248
3249 // Self is passed along as an implicit non-arg variable in a
3250 // block. Mark it as the object pointer.
3251 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00003252 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00003253
3254 // Get location information.
3255 unsigned Line = getLineNumber(VD->getLocation());
3256 unsigned Column = getColumnNumber(VD->getLocation());
3257
3258 const llvm::DataLayout &target = CGM.getDataLayout();
3259
3260 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00003261 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00003262 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
3263
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003264 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00003265 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003266 addr.push_back(llvm::dwarf::DW_OP_deref);
3267 addr.push_back(llvm::dwarf::DW_OP_plus);
3268 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003269 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003270 addr.push_back(llvm::dwarf::DW_OP_deref);
3271 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003272 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00003273 offset =
3274 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003275 addr.push_back(offset.getQuantity());
3276 addr.push_back(llvm::dwarf::DW_OP_deref);
3277 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003278 // offset of x field
3279 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003280 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003281 }
3282
3283 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003284 auto *D = DBuilder.createAutoVariable(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003285 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003286 Line, Ty);
Adrian Prantl0f6df002013-03-29 19:20:35 +00003287
Guy Benyei11169dd2012-12-18 14:30:41 +00003288 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003289 auto DL = llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back());
3290 if (InsertPoint)
3291 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3292 InsertPoint);
3293 else
3294 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3295 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003296}
3297
Guy Benyei11169dd2012-12-18 14:30:41 +00003298void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3299 unsigned ArgNo,
3300 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003301 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003302 EmitDeclare(VD, AI, ArgNo, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003303}
3304
3305namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003306struct BlockLayoutChunk {
3307 uint64_t OffsetInBits;
3308 const BlockDecl::Capture *Capture;
3309};
3310bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3311 return l.OffsetInBits < r.OffsetInBits;
3312}
Guy Benyei11169dd2012-12-18 14:30:41 +00003313}
3314
3315void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003316 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003317 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003318 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003319 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003320 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003321 ASTContext &C = CGM.getContext();
3322 const BlockDecl *blockDecl = block.getBlockDecl();
3323
3324 // Collect some general information about the block's location.
3325 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003326 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003327 unsigned line = getLineNumber(loc);
3328 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003329
Guy Benyei11169dd2012-12-18 14:30:41 +00003330 // Build the debug-info type for the block literal.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003331 getDeclContextDescriptor(blockDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003332
3333 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003334 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003335
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003336 SmallVector<llvm::Metadata *, 16> fields;
David Majnemerb4b671e2016-06-30 03:01:59 +00003337 fields.push_back(createFieldType("__isa", C.VoidPtrTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003338 blockLayout->getElementOffsetInBits(0),
3339 tunit, tunit));
David Majnemerb4b671e2016-06-30 03:01:59 +00003340 fields.push_back(createFieldType("__flags", C.IntTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003341 blockLayout->getElementOffsetInBits(1),
3342 tunit, tunit));
David Majnemerb4b671e2016-06-30 03:01:59 +00003343 fields.push_back(createFieldType("__reserved", C.IntTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003344 blockLayout->getElementOffsetInBits(2),
3345 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003346 auto *FnTy = block.getBlockExpr()->getFunctionType();
3347 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
David Majnemerb4b671e2016-06-30 03:01:59 +00003348 fields.push_back(createFieldType("__FuncPtr", FnPtrType, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003349 blockLayout->getElementOffsetInBits(3),
3350 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003351 fields.push_back(createFieldType(
3352 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3353 ? C.getBlockDescriptorExtendedType()
3354 : C.getBlockDescriptorType()),
David Majnemerb4b671e2016-06-30 03:01:59 +00003355 loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003356
3357 // We want to sort the captures by offset, not because DWARF
3358 // requires this, but because we're paranoid about debuggers.
3359 SmallVector<BlockLayoutChunk, 8> chunks;
3360
3361 // 'this' capture.
3362 if (blockDecl->capturesCXXThis()) {
3363 BlockLayoutChunk chunk;
3364 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003365 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003366 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003367 chunks.push_back(chunk);
3368 }
3369
3370 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003371 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003372 const VarDecl *variable = capture.getVariable();
3373 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3374
3375 // Ignore constant captures.
3376 if (captureInfo.isConstant())
3377 continue;
3378
3379 BlockLayoutChunk chunk;
3380 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003381 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003382 chunk.Capture = &capture;
3383 chunks.push_back(chunk);
3384 }
3385
3386 // Sort by offset.
3387 llvm::array_pod_sort(chunks.begin(), chunks.end());
3388
Eric Christophere7b87e52014-10-26 23:40:33 +00003389 for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
3390 e = chunks.end();
3391 i != e; ++i) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003392 uint64_t offsetInBits = i->OffsetInBits;
3393 const BlockDecl::Capture *capture = i->Capture;
3394
3395 // If we have a null capture, this must be the C++ 'this' capture.
3396 if (!capture) {
Adrian Prantl2526fca2016-04-18 23:48:16 +00003397 QualType type;
3398 if (auto *Method =
3399 cast_or_null<CXXMethodDecl>(blockDecl->getNonClosureContext()))
3400 type = Method->getThisType(C);
3401 else if (auto *RDecl = dyn_cast<CXXRecordDecl>(blockDecl->getParent()))
3402 type = QualType(RDecl->getTypeForDecl(), 0);
3403 else
3404 llvm_unreachable("unexpected block declcontext");
Guy Benyei11169dd2012-12-18 14:30:41 +00003405
David Majnemerb4b671e2016-06-30 03:01:59 +00003406 fields.push_back(createFieldType("this", type, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003407 offsetInBits, tunit, tunit));
3408 continue;
3409 }
3410
3411 const VarDecl *variable = capture->getVariable();
3412 StringRef name = variable->getName();
3413
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003414 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003415 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003416 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003417
3418 // FIXME: this creates a second copy of this type!
3419 uint64_t xoffset;
3420 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003421 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3422 fieldType =
3423 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3424 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003425 } else {
David Majnemerb4b671e2016-06-30 03:01:59 +00003426 fieldType = createFieldType(name, variable->getType(), loc, AS_public,
Eric Christophere7b87e52014-10-26 23:40:33 +00003427 offsetInBits, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003428 }
3429 fields.push_back(fieldType);
3430 }
3431
3432 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003433 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3434 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003435
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003436 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003437
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003438 llvm::DIType *type = DBuilder.createStructType(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003439 tunit, typeName.str(), tunit, line,
3440 CGM.getContext().toBits(block.BlockSize),
3441 CGM.getContext().toBits(block.BlockAlign), 0, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003442 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3443
3444 // Get overall information about the block.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003445 unsigned flags = llvm::DINode::FlagArtificial;
3446 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003447
3448 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003449 auto *debugVar = DBuilder.createParameterVariable(
3450 scope, Arg->getName(), ArgNo, tunit, line, type,
3451 CGM.getLangOpts().Optimize, flags);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003452
Adrian Prantl616bef42013-03-14 21:52:59 +00003453 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003454 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003455 DBuilder.insertDbgValueIntrinsic(
Eric Christophere7b87e52014-10-26 23:40:33 +00003456 LocalAddr, 0, debugVar, DBuilder.createExpression(),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003457 llvm::DebugLoc::get(line, column, scope), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003458 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003459
Adrian Prantl616bef42013-03-14 21:52:59 +00003460 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003461 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
3462 llvm::DebugLoc::get(line, column, scope),
3463 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003464}
3465
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003466llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003467CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3468 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003469 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003470
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003471 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003472 if (MI != StaticDataMemberCache.end()) {
3473 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00003474 return MI->second;
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003475 }
David Blaikiece763042013-08-20 21:49:21 +00003476
3477 // If the member wasn't found in the cache, lazily construct and add it to the
3478 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003479 auto DC = D->getDeclContext();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003480 auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003481 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003482}
3483
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003484llvm::DIGlobalVariable *CGDebugInfo::CollectAnonRecordDecls(
3485 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3486 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
3487 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003488
3489 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003490 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003491 StringRef FieldName = Field->getName();
3492
3493 // Ignore unnamed fields, but recurse into anonymous records.
3494 if (FieldName.empty()) {
3495 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3496 if (RT)
3497 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3498 Var, DContext);
3499 continue;
3500 }
3501 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003502 GV = DBuilder.createGlobalVariable(DContext, FieldName, LinkageName, Unit,
3503 LineNo, FieldTy,
3504 Var->hasInternalLinkage(), Var, nullptr);
Eric Christophercab9fae2014-04-10 05:20:00 +00003505 }
3506 return GV;
3507}
3508
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003509void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003510 const VarDecl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003511 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003512 if (D->hasAttr<NoDebugAttr>())
3513 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003514 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003515 llvm::DIFile *Unit = nullptr;
3516 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003517 unsigned LineNo;
3518 StringRef DeclName, LinkageName;
3519 QualType T;
3520 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003521
3522 // Attempt to store one global variable for the declaration - even if we
3523 // emit a lot of fields.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003524 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003525
3526 // If this is an anonymous union then we'll want to emit a global
3527 // variable for each member of the anonymous union so that it's possible
3528 // to find the name of any field in the union.
3529 if (T->isUnionType() && DeclName.empty()) {
Reid Kleckner43ecd7c2015-11-20 17:41:12 +00003530 const RecordDecl *RD = T->castAs<RecordType>()->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003531 assert(RD->isAnonymousStructOrUnion() &&
3532 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003533 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3534 } else {
David Blaikie7550b112014-10-20 17:42:23 +00003535 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003536 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3537 Var->hasInternalLinkage(), Var,
3538 getOrCreateStaticDataMemberDeclarationOrNull(D));
3539 }
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003540 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(GV));
Guy Benyei11169dd2012-12-18 14:30:41 +00003541}
3542
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003543void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3544 llvm::Constant *Init) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003545 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003546 if (VD->hasAttr<NoDebugAttr>())
3547 return;
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003548 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003549 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003550 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003551 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003552 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3553 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3554 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3555 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3556 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003557 // Do not use global variables for enums.
3558 //
3559 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003560 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003561 return;
David Blaikiea15565562014-04-04 20:56:17 +00003562 // Do not emit separate definitions for function local const/statics.
3563 if (isa<FunctionDecl>(VD->getDeclContext()))
3564 return;
David Blaikiebb113912014-04-05 07:23:17 +00003565 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003566 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003567 if (VarD->isStaticDataMember()) {
3568 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003569 getDeclContextDescriptor(VarD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003570 // Ensure that the type is retained even though it's otherwise unreferenced.
Duncan P. N. Exon Smith383f8412016-04-23 21:08:27 +00003571 //
3572 // FIXME: This is probably unnecessary, since Ty should reference RD
3573 // through its scope.
David Blaikie423eb5a2014-11-19 19:42:40 +00003574 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003575 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003576 return;
3577 }
3578
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003579 llvm::DIScope *DContext = getDeclContextDescriptor(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003580
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003581 auto &GV = DeclCache[VD];
3582 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003583 return;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003584 GV.reset(DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003585 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003586 true, Init, getOrCreateStaticDataMemberDeclarationOrNull(VarD)));
David Blaikiebd483762013-05-20 04:58:53 +00003587}
3588
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003589llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003590 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003591 return LexicalBlockStack.back();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00003592 llvm::DIScope *Mod = getParentModuleOrNull(D);
3593 return getContextDescriptor(D, Mod ? Mod : TheCU);
Guy Benyei11169dd2012-12-18 14:30:41 +00003594}
3595
David Blaikie9f88fe82013-04-22 06:13:21 +00003596void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003597 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00003598 return;
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00003599 const NamespaceDecl *NSDecl = UD.getNominatedNamespace();
3600 if (!NSDecl->isAnonymousNamespace() ||
3601 CGM.getCodeGenOpts().DebugExplicitImport) {
3602 DBuilder.createImportedModule(
3603 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3604 getOrCreateNameSpace(NSDecl),
3605 getLineNumber(UD.getLocation()));
3606 }
David Blaikie9f88fe82013-04-22 06:13:21 +00003607}
3608
David Blaikiebd483762013-05-20 04:58:53 +00003609void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003610 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00003611 return;
3612 assert(UD.shadow_size() &&
3613 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3614 // Emitting one decl is sufficient - debuggers can detect that this is an
3615 // overloaded name & provide lookup for all the overloads.
3616 const UsingShadowDecl &USD = **UD.shadow_begin();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003617 if (llvm::DINode *Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003618 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003619 DBuilder.createImportedDeclaration(
3620 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3621 getLineNumber(USD.getLocation()));
3622}
3623
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003624void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
David Blaikieaf09f4a2016-05-03 23:06:40 +00003625 if (CGM.getCodeGenOpts().getDebuggerTuning() != llvm::DebuggerKind::LLDB)
3626 return;
Adrian Prantl8a634c12015-12-18 19:44:31 +00003627 if (Module *M = ID.getImportedModule()) {
Chad Rosiere5dafd12015-12-18 20:08:40 +00003628 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
Adrian Prantl8a634c12015-12-18 19:44:31 +00003629 DBuilder.createImportedDeclaration(
3630 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
3631 getOrCreateModuleRef(Info, DebugTypeExtRefs),
3632 getLineNumber(ID.getLocation()));
3633 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003634}
3635
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003636llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00003637CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003638 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003639 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003640 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003641 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003642 return cast<llvm::DIImportedEntity>(VH);
3643 llvm::DIImportedEntity *R;
David Blaikief121b932013-05-20 22:50:41 +00003644 if (const NamespaceAliasDecl *Underlying =
3645 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3646 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003647 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003648 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3649 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3650 NA.getName());
3651 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003652 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003653 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3654 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3655 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003656 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003657 return R;
3658}
3659
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003660llvm::DINamespace *
Guy Benyei11169dd2012-12-18 14:30:41 +00003661CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003662 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003663 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003664 if (I != NameSpaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003665 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003666
Guy Benyei11169dd2012-12-18 14:30:41 +00003667 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003668 llvm::DIFile *FileD = getOrCreateFile(NSDecl->getLocation());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003669 llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003670 llvm::DINamespace *NS =
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003671 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003672 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003673 return NS;
3674}
3675
Adrian Prantla5206ce2015-09-22 23:26:43 +00003676void CGDebugInfo::setDwoId(uint64_t Signature) {
3677 assert(TheCU && "no main compile unit");
3678 TheCU->setDWOId(Signature);
3679}
3680
3681
Guy Benyei11169dd2012-12-18 14:30:41 +00003682void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003683 // Creating types might create further types - invalidating the current
3684 // element and the size(), so don't cache/reference them.
3685 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3686 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003687 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003688 ? CreateTypeDefinition(E.Type, E.Unit)
3689 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003690 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00003691 }
3692
David Blaikief427b002014-05-06 03:42:01 +00003693 for (auto p : ReplaceMap) {
3694 assert(p.second);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003695 auto *Ty = cast<llvm::DIType>(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003696 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003697
David Blaikief427b002014-05-06 03:42:01 +00003698 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003699 assert(it != TypeCache.end());
3700 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003701
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003702 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
3703 cast<llvm::DIType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00003704 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003705
Frederic Rissd253ed62014-11-18 03:40:51 +00003706 for (const auto &p : FwdDeclReplaceMap) {
3707 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003708 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003709 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003710
3711 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003712 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003713 // with ourselves, that will destroy the temporary MDNode and
3714 // replace it with a standard one, avoiding leaking memory.
3715 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003716 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003717 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003718 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003719
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003720 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00003721 }
3722
Adrian Prantl73409ce2013-03-11 18:33:46 +00003723 // We keep our own list of retained types, because we need to look
3724 // up the final type in the type cache.
Adrian Prantl3a884fa2015-08-27 22:56:46 +00003725 for (auto &RT : RetainedTypes)
Adrian Prantlad9a195e2015-08-27 21:21:19 +00003726 if (auto MD = TypeCache[RT])
3727 DBuilder.retainType(cast<llvm::DIType>(MD));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003728
Guy Benyei11169dd2012-12-18 14:30:41 +00003729 DBuilder.finalize();
3730}
David Blaikie66088d52014-09-24 17:01:27 +00003731
3732void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003733 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikie66088d52014-09-24 17:01:27 +00003734 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003735
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003736 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003737 // Don't ignore in case of explicit cast where it is referenced indirectly.
3738 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00003739}