1*67e74705SXin Li //===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===//
2*67e74705SXin Li //
3*67e74705SXin Li // The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li //
10*67e74705SXin Li // This coordinates the debug information generation while generating code.
11*67e74705SXin Li //
12*67e74705SXin Li //===----------------------------------------------------------------------===//
13*67e74705SXin Li
14*67e74705SXin Li #include "CGDebugInfo.h"
15*67e74705SXin Li #include "CGBlocks.h"
16*67e74705SXin Li #include "CGRecordLayout.h"
17*67e74705SXin Li #include "CGCXXABI.h"
18*67e74705SXin Li #include "CGObjCRuntime.h"
19*67e74705SXin Li #include "CodeGenFunction.h"
20*67e74705SXin Li #include "CodeGenModule.h"
21*67e74705SXin Li #include "clang/AST/ASTContext.h"
22*67e74705SXin Li #include "clang/AST/DeclFriend.h"
23*67e74705SXin Li #include "clang/AST/DeclObjC.h"
24*67e74705SXin Li #include "clang/AST/DeclTemplate.h"
25*67e74705SXin Li #include "clang/AST/Expr.h"
26*67e74705SXin Li #include "clang/AST/RecordLayout.h"
27*67e74705SXin Li #include "clang/Basic/FileManager.h"
28*67e74705SXin Li #include "clang/Basic/SourceManager.h"
29*67e74705SXin Li #include "clang/Basic/Version.h"
30*67e74705SXin Li #include "clang/Frontend/CodeGenOptions.h"
31*67e74705SXin Li #include "clang/Lex/HeaderSearchOptions.h"
32*67e74705SXin Li #include "clang/Lex/ModuleMap.h"
33*67e74705SXin Li #include "clang/Lex/PreprocessorOptions.h"
34*67e74705SXin Li #include "llvm/ADT/SmallVector.h"
35*67e74705SXin Li #include "llvm/ADT/StringExtras.h"
36*67e74705SXin Li #include "llvm/IR/Constants.h"
37*67e74705SXin Li #include "llvm/IR/DataLayout.h"
38*67e74705SXin Li #include "llvm/IR/DerivedTypes.h"
39*67e74705SXin Li #include "llvm/IR/Instructions.h"
40*67e74705SXin Li #include "llvm/IR/Intrinsics.h"
41*67e74705SXin Li #include "llvm/IR/Module.h"
42*67e74705SXin Li #include "llvm/Support/FileSystem.h"
43*67e74705SXin Li #include "llvm/Support/Path.h"
44*67e74705SXin Li using namespace clang;
45*67e74705SXin Li using namespace clang::CodeGen;
46*67e74705SXin Li
CGDebugInfo(CodeGenModule & CGM)47*67e74705SXin Li CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
48*67e74705SXin Li : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
49*67e74705SXin Li DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs),
50*67e74705SXin Li DBuilder(CGM.getModule()) {
51*67e74705SXin Li for (const auto &KV : CGM.getCodeGenOpts().DebugPrefixMap)
52*67e74705SXin Li DebugPrefixMap[KV.first] = KV.second;
53*67e74705SXin Li CreateCompileUnit();
54*67e74705SXin Li }
55*67e74705SXin Li
~CGDebugInfo()56*67e74705SXin Li CGDebugInfo::~CGDebugInfo() {
57*67e74705SXin Li assert(LexicalBlockStack.empty() &&
58*67e74705SXin Li "Region stack mismatch, stack not empty!");
59*67e74705SXin Li }
60*67e74705SXin Li
ApplyDebugLocation(CodeGenFunction & CGF,SourceLocation TemporaryLocation)61*67e74705SXin Li ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
62*67e74705SXin Li SourceLocation TemporaryLocation)
63*67e74705SXin Li : CGF(&CGF) {
64*67e74705SXin Li init(TemporaryLocation);
65*67e74705SXin Li }
66*67e74705SXin Li
ApplyDebugLocation(CodeGenFunction & CGF,bool DefaultToEmpty,SourceLocation TemporaryLocation)67*67e74705SXin Li ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
68*67e74705SXin Li bool DefaultToEmpty,
69*67e74705SXin Li SourceLocation TemporaryLocation)
70*67e74705SXin Li : CGF(&CGF) {
71*67e74705SXin Li init(TemporaryLocation, DefaultToEmpty);
72*67e74705SXin Li }
73*67e74705SXin Li
init(SourceLocation TemporaryLocation,bool DefaultToEmpty)74*67e74705SXin Li void ApplyDebugLocation::init(SourceLocation TemporaryLocation,
75*67e74705SXin Li bool DefaultToEmpty) {
76*67e74705SXin Li auto *DI = CGF->getDebugInfo();
77*67e74705SXin Li if (!DI) {
78*67e74705SXin Li CGF = nullptr;
79*67e74705SXin Li return;
80*67e74705SXin Li }
81*67e74705SXin Li
82*67e74705SXin Li OriginalLocation = CGF->Builder.getCurrentDebugLocation();
83*67e74705SXin Li if (TemporaryLocation.isValid()) {
84*67e74705SXin Li DI->EmitLocation(CGF->Builder, TemporaryLocation);
85*67e74705SXin Li return;
86*67e74705SXin Li }
87*67e74705SXin Li
88*67e74705SXin Li if (DefaultToEmpty) {
89*67e74705SXin Li CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc());
90*67e74705SXin Li return;
91*67e74705SXin Li }
92*67e74705SXin Li
93*67e74705SXin Li // Construct a location that has a valid scope, but no line info.
94*67e74705SXin Li assert(!DI->LexicalBlockStack.empty());
95*67e74705SXin Li CGF->Builder.SetCurrentDebugLocation(
96*67e74705SXin Li llvm::DebugLoc::get(0, 0, DI->LexicalBlockStack.back()));
97*67e74705SXin Li }
98*67e74705SXin Li
ApplyDebugLocation(CodeGenFunction & CGF,const Expr * E)99*67e74705SXin Li ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E)
100*67e74705SXin Li : CGF(&CGF) {
101*67e74705SXin Li init(E->getExprLoc());
102*67e74705SXin Li }
103*67e74705SXin Li
ApplyDebugLocation(CodeGenFunction & CGF,llvm::DebugLoc Loc)104*67e74705SXin Li ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc)
105*67e74705SXin Li : CGF(&CGF) {
106*67e74705SXin Li if (!CGF.getDebugInfo()) {
107*67e74705SXin Li this->CGF = nullptr;
108*67e74705SXin Li return;
109*67e74705SXin Li }
110*67e74705SXin Li OriginalLocation = CGF.Builder.getCurrentDebugLocation();
111*67e74705SXin Li if (Loc)
112*67e74705SXin Li CGF.Builder.SetCurrentDebugLocation(std::move(Loc));
113*67e74705SXin Li }
114*67e74705SXin Li
~ApplyDebugLocation()115*67e74705SXin Li ApplyDebugLocation::~ApplyDebugLocation() {
116*67e74705SXin Li // Query CGF so the location isn't overwritten when location updates are
117*67e74705SXin Li // temporarily disabled (for C++ default function arguments)
118*67e74705SXin Li if (CGF)
119*67e74705SXin Li CGF->Builder.SetCurrentDebugLocation(std::move(OriginalLocation));
120*67e74705SXin Li }
121*67e74705SXin Li
setLocation(SourceLocation Loc)122*67e74705SXin Li void CGDebugInfo::setLocation(SourceLocation Loc) {
123*67e74705SXin Li // If the new location isn't valid return.
124*67e74705SXin Li if (Loc.isInvalid())
125*67e74705SXin Li return;
126*67e74705SXin Li
127*67e74705SXin Li CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
128*67e74705SXin Li
129*67e74705SXin Li // If we've changed files in the middle of a lexical scope go ahead
130*67e74705SXin Li // and create a new lexical scope with file node if it's different
131*67e74705SXin Li // from the one in the scope.
132*67e74705SXin Li if (LexicalBlockStack.empty())
133*67e74705SXin Li return;
134*67e74705SXin Li
135*67e74705SXin Li SourceManager &SM = CGM.getContext().getSourceManager();
136*67e74705SXin Li auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
137*67e74705SXin Li PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
138*67e74705SXin Li
139*67e74705SXin Li if (PCLoc.isInvalid() || Scope->getFilename() == PCLoc.getFilename())
140*67e74705SXin Li return;
141*67e74705SXin Li
142*67e74705SXin Li if (auto *LBF = dyn_cast<llvm::DILexicalBlockFile>(Scope)) {
143*67e74705SXin Li LexicalBlockStack.pop_back();
144*67e74705SXin Li LexicalBlockStack.emplace_back(DBuilder.createLexicalBlockFile(
145*67e74705SXin Li LBF->getScope(), getOrCreateFile(CurLoc)));
146*67e74705SXin Li } else if (isa<llvm::DILexicalBlock>(Scope) ||
147*67e74705SXin Li isa<llvm::DISubprogram>(Scope)) {
148*67e74705SXin Li LexicalBlockStack.pop_back();
149*67e74705SXin Li LexicalBlockStack.emplace_back(
150*67e74705SXin Li DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc)));
151*67e74705SXin Li }
152*67e74705SXin Li }
153*67e74705SXin Li
getDeclContextDescriptor(const Decl * D)154*67e74705SXin Li llvm::DIScope *CGDebugInfo::getDeclContextDescriptor(const Decl *D) {
155*67e74705SXin Li llvm::DIScope *Mod = getParentModuleOrNull(D);
156*67e74705SXin Li return getContextDescriptor(cast<Decl>(D->getDeclContext()),
157*67e74705SXin Li Mod ? Mod : TheCU);
158*67e74705SXin Li }
159*67e74705SXin Li
getContextDescriptor(const Decl * Context,llvm::DIScope * Default)160*67e74705SXin Li llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context,
161*67e74705SXin Li llvm::DIScope *Default) {
162*67e74705SXin Li if (!Context)
163*67e74705SXin Li return Default;
164*67e74705SXin Li
165*67e74705SXin Li auto I = RegionMap.find(Context);
166*67e74705SXin Li if (I != RegionMap.end()) {
167*67e74705SXin Li llvm::Metadata *V = I->second;
168*67e74705SXin Li return dyn_cast_or_null<llvm::DIScope>(V);
169*67e74705SXin Li }
170*67e74705SXin Li
171*67e74705SXin Li // Check namespace.
172*67e74705SXin Li if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
173*67e74705SXin Li return getOrCreateNameSpace(NSDecl);
174*67e74705SXin Li
175*67e74705SXin Li if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context))
176*67e74705SXin Li if (!RDecl->isDependentType())
177*67e74705SXin Li return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
178*67e74705SXin Li getOrCreateMainFile());
179*67e74705SXin Li return Default;
180*67e74705SXin Li }
181*67e74705SXin Li
getFunctionName(const FunctionDecl * FD)182*67e74705SXin Li StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
183*67e74705SXin Li assert(FD && "Invalid FunctionDecl!");
184*67e74705SXin Li IdentifierInfo *FII = FD->getIdentifier();
185*67e74705SXin Li FunctionTemplateSpecializationInfo *Info =
186*67e74705SXin Li FD->getTemplateSpecializationInfo();
187*67e74705SXin Li
188*67e74705SXin Li // Emit the unqualified name in normal operation. LLVM and the debugger can
189*67e74705SXin Li // compute the fully qualified name from the scope chain. If we're only
190*67e74705SXin Li // emitting line table info, there won't be any scope chains, so emit the
191*67e74705SXin Li // fully qualified name here so that stack traces are more accurate.
192*67e74705SXin Li // FIXME: Do this when emitting DWARF as well as when emitting CodeView after
193*67e74705SXin Li // evaluating the size impact.
194*67e74705SXin Li bool UseQualifiedName = DebugKind == codegenoptions::DebugLineTablesOnly &&
195*67e74705SXin Li CGM.getCodeGenOpts().EmitCodeView;
196*67e74705SXin Li
197*67e74705SXin Li if (!Info && FII && !UseQualifiedName)
198*67e74705SXin Li return FII->getName();
199*67e74705SXin Li
200*67e74705SXin Li SmallString<128> NS;
201*67e74705SXin Li llvm::raw_svector_ostream OS(NS);
202*67e74705SXin Li PrintingPolicy Policy(CGM.getLangOpts());
203*67e74705SXin Li Policy.MSVCFormatting = CGM.getCodeGenOpts().EmitCodeView;
204*67e74705SXin Li if (!UseQualifiedName)
205*67e74705SXin Li FD->printName(OS);
206*67e74705SXin Li else
207*67e74705SXin Li FD->printQualifiedName(OS, Policy);
208*67e74705SXin Li
209*67e74705SXin Li // Add any template specialization args.
210*67e74705SXin Li if (Info) {
211*67e74705SXin Li const TemplateArgumentList *TArgs = Info->TemplateArguments;
212*67e74705SXin Li TemplateSpecializationType::PrintTemplateArgumentList(OS, TArgs->asArray(),
213*67e74705SXin Li Policy);
214*67e74705SXin Li }
215*67e74705SXin Li
216*67e74705SXin Li // Copy this name on the side and use its reference.
217*67e74705SXin Li return internString(OS.str());
218*67e74705SXin Li }
219*67e74705SXin Li
getObjCMethodName(const ObjCMethodDecl * OMD)220*67e74705SXin Li StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
221*67e74705SXin Li SmallString<256> MethodName;
222*67e74705SXin Li llvm::raw_svector_ostream OS(MethodName);
223*67e74705SXin Li OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
224*67e74705SXin Li const DeclContext *DC = OMD->getDeclContext();
225*67e74705SXin Li if (const ObjCImplementationDecl *OID =
226*67e74705SXin Li dyn_cast<const ObjCImplementationDecl>(DC)) {
227*67e74705SXin Li OS << OID->getName();
228*67e74705SXin Li } else if (const ObjCInterfaceDecl *OID =
229*67e74705SXin Li dyn_cast<const ObjCInterfaceDecl>(DC)) {
230*67e74705SXin Li OS << OID->getName();
231*67e74705SXin Li } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(DC)) {
232*67e74705SXin Li if (OC->IsClassExtension()) {
233*67e74705SXin Li OS << OC->getClassInterface()->getName();
234*67e74705SXin Li } else {
235*67e74705SXin Li OS << ((const NamedDecl *)OC)->getIdentifier()->getNameStart() << '('
236*67e74705SXin Li << OC->getIdentifier()->getNameStart() << ')';
237*67e74705SXin Li }
238*67e74705SXin Li } else if (const ObjCCategoryImplDecl *OCD =
239*67e74705SXin Li dyn_cast<const ObjCCategoryImplDecl>(DC)) {
240*67e74705SXin Li OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '('
241*67e74705SXin Li << OCD->getIdentifier()->getNameStart() << ')';
242*67e74705SXin Li } else if (isa<ObjCProtocolDecl>(DC)) {
243*67e74705SXin Li // We can extract the type of the class from the self pointer.
244*67e74705SXin Li if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
245*67e74705SXin Li QualType ClassTy =
246*67e74705SXin Li cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
247*67e74705SXin Li ClassTy.print(OS, PrintingPolicy(LangOptions()));
248*67e74705SXin Li }
249*67e74705SXin Li }
250*67e74705SXin Li OS << ' ' << OMD->getSelector().getAsString() << ']';
251*67e74705SXin Li
252*67e74705SXin Li return internString(OS.str());
253*67e74705SXin Li }
254*67e74705SXin Li
getSelectorName(Selector S)255*67e74705SXin Li StringRef CGDebugInfo::getSelectorName(Selector S) {
256*67e74705SXin Li return internString(S.getAsString());
257*67e74705SXin Li }
258*67e74705SXin Li
getClassName(const RecordDecl * RD)259*67e74705SXin Li StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
260*67e74705SXin Li if (isa<ClassTemplateSpecializationDecl>(RD)) {
261*67e74705SXin Li SmallString<128> Name;
262*67e74705SXin Li llvm::raw_svector_ostream OS(Name);
263*67e74705SXin Li RD->getNameForDiagnostic(OS, CGM.getContext().getPrintingPolicy(),
264*67e74705SXin Li /*Qualified*/ false);
265*67e74705SXin Li
266*67e74705SXin Li // Copy this name on the side and use its reference.
267*67e74705SXin Li return internString(Name);
268*67e74705SXin Li }
269*67e74705SXin Li
270*67e74705SXin Li // quick optimization to avoid having to intern strings that are already
271*67e74705SXin Li // stored reliably elsewhere
272*67e74705SXin Li if (const IdentifierInfo *II = RD->getIdentifier())
273*67e74705SXin Li return II->getName();
274*67e74705SXin Li
275*67e74705SXin Li // The CodeView printer in LLVM wants to see the names of unnamed types: it is
276*67e74705SXin Li // used to reconstruct the fully qualified type names.
277*67e74705SXin Li if (CGM.getCodeGenOpts().EmitCodeView) {
278*67e74705SXin Li if (const TypedefNameDecl *D = RD->getTypedefNameForAnonDecl()) {
279*67e74705SXin Li assert(RD->getDeclContext() == D->getDeclContext() &&
280*67e74705SXin Li "Typedef should not be in another decl context!");
281*67e74705SXin Li assert(D->getDeclName().getAsIdentifierInfo() &&
282*67e74705SXin Li "Typedef was not named!");
283*67e74705SXin Li return D->getDeclName().getAsIdentifierInfo()->getName();
284*67e74705SXin Li }
285*67e74705SXin Li
286*67e74705SXin Li if (CGM.getLangOpts().CPlusPlus) {
287*67e74705SXin Li StringRef Name;
288*67e74705SXin Li
289*67e74705SXin Li ASTContext &Context = CGM.getContext();
290*67e74705SXin Li if (const DeclaratorDecl *DD = Context.getDeclaratorForUnnamedTagDecl(RD))
291*67e74705SXin Li // Anonymous types without a name for linkage purposes have their
292*67e74705SXin Li // declarator mangled in if they have one.
293*67e74705SXin Li Name = DD->getName();
294*67e74705SXin Li else if (const TypedefNameDecl *TND =
295*67e74705SXin Li Context.getTypedefNameForUnnamedTagDecl(RD))
296*67e74705SXin Li // Anonymous types without a name for linkage purposes have their
297*67e74705SXin Li // associate typedef mangled in if they have one.
298*67e74705SXin Li Name = TND->getName();
299*67e74705SXin Li
300*67e74705SXin Li if (!Name.empty()) {
301*67e74705SXin Li SmallString<256> UnnamedType("<unnamed-type-");
302*67e74705SXin Li UnnamedType += Name;
303*67e74705SXin Li UnnamedType += '>';
304*67e74705SXin Li return internString(UnnamedType);
305*67e74705SXin Li }
306*67e74705SXin Li }
307*67e74705SXin Li }
308*67e74705SXin Li
309*67e74705SXin Li return StringRef();
310*67e74705SXin Li }
311*67e74705SXin Li
getOrCreateFile(SourceLocation Loc)312*67e74705SXin Li llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
313*67e74705SXin Li if (!Loc.isValid())
314*67e74705SXin Li // If Location is not valid then use main input file.
315*67e74705SXin Li return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
316*67e74705SXin Li remapDIPath(TheCU->getDirectory()));
317*67e74705SXin Li
318*67e74705SXin Li SourceManager &SM = CGM.getContext().getSourceManager();
319*67e74705SXin Li PresumedLoc PLoc = SM.getPresumedLoc(Loc);
320*67e74705SXin Li
321*67e74705SXin Li if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
322*67e74705SXin Li // If the location is not valid then use main input file.
323*67e74705SXin Li return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
324*67e74705SXin Li remapDIPath(TheCU->getDirectory()));
325*67e74705SXin Li
326*67e74705SXin Li // Cache the results.
327*67e74705SXin Li const char *fname = PLoc.getFilename();
328*67e74705SXin Li auto it = DIFileCache.find(fname);
329*67e74705SXin Li
330*67e74705SXin Li if (it != DIFileCache.end()) {
331*67e74705SXin Li // Verify that the information still exists.
332*67e74705SXin Li if (llvm::Metadata *V = it->second)
333*67e74705SXin Li return cast<llvm::DIFile>(V);
334*67e74705SXin Li }
335*67e74705SXin Li
336*67e74705SXin Li llvm::DIFile *F = DBuilder.createFile(remapDIPath(PLoc.getFilename()),
337*67e74705SXin Li remapDIPath(getCurrentDirname()));
338*67e74705SXin Li
339*67e74705SXin Li DIFileCache[fname].reset(F);
340*67e74705SXin Li return F;
341*67e74705SXin Li }
342*67e74705SXin Li
getOrCreateMainFile()343*67e74705SXin Li llvm::DIFile *CGDebugInfo::getOrCreateMainFile() {
344*67e74705SXin Li return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
345*67e74705SXin Li remapDIPath(TheCU->getDirectory()));
346*67e74705SXin Li }
347*67e74705SXin Li
remapDIPath(StringRef Path) const348*67e74705SXin Li std::string CGDebugInfo::remapDIPath(StringRef Path) const {
349*67e74705SXin Li for (const auto &Entry : DebugPrefixMap)
350*67e74705SXin Li if (Path.startswith(Entry.first))
351*67e74705SXin Li return (Twine(Entry.second) + Path.substr(Entry.first.size())).str();
352*67e74705SXin Li return Path.str();
353*67e74705SXin Li }
354*67e74705SXin Li
getLineNumber(SourceLocation Loc)355*67e74705SXin Li unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
356*67e74705SXin Li if (Loc.isInvalid() && CurLoc.isInvalid())
357*67e74705SXin Li return 0;
358*67e74705SXin Li SourceManager &SM = CGM.getContext().getSourceManager();
359*67e74705SXin Li PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
360*67e74705SXin Li return PLoc.isValid() ? PLoc.getLine() : 0;
361*67e74705SXin Li }
362*67e74705SXin Li
getColumnNumber(SourceLocation Loc,bool Force)363*67e74705SXin Li unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
364*67e74705SXin Li // We may not want column information at all.
365*67e74705SXin Li if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
366*67e74705SXin Li return 0;
367*67e74705SXin Li
368*67e74705SXin Li // If the location is invalid then use the current column.
369*67e74705SXin Li if (Loc.isInvalid() && CurLoc.isInvalid())
370*67e74705SXin Li return 0;
371*67e74705SXin Li SourceManager &SM = CGM.getContext().getSourceManager();
372*67e74705SXin Li PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
373*67e74705SXin Li return PLoc.isValid() ? PLoc.getColumn() : 0;
374*67e74705SXin Li }
375*67e74705SXin Li
getCurrentDirname()376*67e74705SXin Li StringRef CGDebugInfo::getCurrentDirname() {
377*67e74705SXin Li if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
378*67e74705SXin Li return CGM.getCodeGenOpts().DebugCompilationDir;
379*67e74705SXin Li
380*67e74705SXin Li if (!CWDName.empty())
381*67e74705SXin Li return CWDName;
382*67e74705SXin Li SmallString<256> CWD;
383*67e74705SXin Li llvm::sys::fs::current_path(CWD);
384*67e74705SXin Li return CWDName = internString(CWD);
385*67e74705SXin Li }
386*67e74705SXin Li
CreateCompileUnit()387*67e74705SXin Li void CGDebugInfo::CreateCompileUnit() {
388*67e74705SXin Li
389*67e74705SXin Li // Should we be asking the SourceManager for the main file name, instead of
390*67e74705SXin Li // accepting it as an argument? This just causes the main file name to
391*67e74705SXin Li // mismatch with source locations and create extra lexical scopes or
392*67e74705SXin Li // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
393*67e74705SXin Li // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
394*67e74705SXin Li // because that's what the SourceManager says)
395*67e74705SXin Li
396*67e74705SXin Li // Get absolute path name.
397*67e74705SXin Li SourceManager &SM = CGM.getContext().getSourceManager();
398*67e74705SXin Li std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
399*67e74705SXin Li if (MainFileName.empty())
400*67e74705SXin Li MainFileName = "<stdin>";
401*67e74705SXin Li
402*67e74705SXin Li // The main file name provided via the "-main-file-name" option contains just
403*67e74705SXin Li // the file name itself with no path information. This file name may have had
404*67e74705SXin Li // a relative path, so we look into the actual file entry for the main
405*67e74705SXin Li // file to determine the real absolute path for the file.
406*67e74705SXin Li std::string MainFileDir;
407*67e74705SXin Li if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
408*67e74705SXin Li MainFileDir = remapDIPath(MainFile->getDir()->getName());
409*67e74705SXin Li if (MainFileDir != ".") {
410*67e74705SXin Li llvm::SmallString<1024> MainFileDirSS(MainFileDir);
411*67e74705SXin Li llvm::sys::path::append(MainFileDirSS, MainFileName);
412*67e74705SXin Li MainFileName = MainFileDirSS.str();
413*67e74705SXin Li }
414*67e74705SXin Li }
415*67e74705SXin Li
416*67e74705SXin Li llvm::dwarf::SourceLanguage LangTag;
417*67e74705SXin Li const LangOptions &LO = CGM.getLangOpts();
418*67e74705SXin Li if (LO.CPlusPlus) {
419*67e74705SXin Li if (LO.ObjC1)
420*67e74705SXin Li LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
421*67e74705SXin Li else
422*67e74705SXin Li LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
423*67e74705SXin Li } else if (LO.ObjC1) {
424*67e74705SXin Li LangTag = llvm::dwarf::DW_LANG_ObjC;
425*67e74705SXin Li } else if (LO.RenderScript) {
426*67e74705SXin Li LangTag = llvm::dwarf::DW_LANG_GOOGLE_RenderScript;
427*67e74705SXin Li } else if (LO.C99) {
428*67e74705SXin Li LangTag = llvm::dwarf::DW_LANG_C99;
429*67e74705SXin Li } else {
430*67e74705SXin Li LangTag = llvm::dwarf::DW_LANG_C89;
431*67e74705SXin Li }
432*67e74705SXin Li
433*67e74705SXin Li std::string Producer = getClangFullVersion();
434*67e74705SXin Li
435*67e74705SXin Li // Figure out which version of the ObjC runtime we have.
436*67e74705SXin Li unsigned RuntimeVers = 0;
437*67e74705SXin Li if (LO.ObjC1)
438*67e74705SXin Li RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
439*67e74705SXin Li
440*67e74705SXin Li llvm::DICompileUnit::DebugEmissionKind EmissionKind;
441*67e74705SXin Li switch (DebugKind) {
442*67e74705SXin Li case codegenoptions::NoDebugInfo:
443*67e74705SXin Li case codegenoptions::LocTrackingOnly:
444*67e74705SXin Li EmissionKind = llvm::DICompileUnit::NoDebug;
445*67e74705SXin Li break;
446*67e74705SXin Li case codegenoptions::DebugLineTablesOnly:
447*67e74705SXin Li EmissionKind = llvm::DICompileUnit::LineTablesOnly;
448*67e74705SXin Li break;
449*67e74705SXin Li case codegenoptions::LimitedDebugInfo:
450*67e74705SXin Li case codegenoptions::FullDebugInfo:
451*67e74705SXin Li EmissionKind = llvm::DICompileUnit::FullDebug;
452*67e74705SXin Li break;
453*67e74705SXin Li }
454*67e74705SXin Li
455*67e74705SXin Li // Create new compile unit.
456*67e74705SXin Li // FIXME - Eliminate TheCU.
457*67e74705SXin Li TheCU = DBuilder.createCompileUnit(
458*67e74705SXin Li LangTag, remapDIPath(MainFileName), remapDIPath(getCurrentDirname()),
459*67e74705SXin Li Producer, LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers,
460*67e74705SXin Li CGM.getCodeGenOpts().SplitDwarfFile, EmissionKind, 0 /* DWOid */);
461*67e74705SXin Li }
462*67e74705SXin Li
CreateType(const BuiltinType * BT)463*67e74705SXin Li llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
464*67e74705SXin Li llvm::dwarf::TypeKind Encoding;
465*67e74705SXin Li StringRef BTName;
466*67e74705SXin Li switch (BT->getKind()) {
467*67e74705SXin Li #define BUILTIN_TYPE(Id, SingletonId)
468*67e74705SXin Li #define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
469*67e74705SXin Li #include "clang/AST/BuiltinTypes.def"
470*67e74705SXin Li case BuiltinType::Dependent:
471*67e74705SXin Li llvm_unreachable("Unexpected builtin type");
472*67e74705SXin Li case BuiltinType::NullPtr:
473*67e74705SXin Li return DBuilder.createNullPtrType();
474*67e74705SXin Li case BuiltinType::Void:
475*67e74705SXin Li return nullptr;
476*67e74705SXin Li case BuiltinType::ObjCClass:
477*67e74705SXin Li if (!ClassTy)
478*67e74705SXin Li ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
479*67e74705SXin Li "objc_class", TheCU,
480*67e74705SXin Li getOrCreateMainFile(), 0);
481*67e74705SXin Li return ClassTy;
482*67e74705SXin Li case BuiltinType::ObjCId: {
483*67e74705SXin Li // typedef struct objc_class *Class;
484*67e74705SXin Li // typedef struct objc_object {
485*67e74705SXin Li // Class isa;
486*67e74705SXin Li // } *id;
487*67e74705SXin Li
488*67e74705SXin Li if (ObjTy)
489*67e74705SXin Li return ObjTy;
490*67e74705SXin Li
491*67e74705SXin Li if (!ClassTy)
492*67e74705SXin Li ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
493*67e74705SXin Li "objc_class", TheCU,
494*67e74705SXin Li getOrCreateMainFile(), 0);
495*67e74705SXin Li
496*67e74705SXin Li unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
497*67e74705SXin Li
498*67e74705SXin Li auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
499*67e74705SXin Li
500*67e74705SXin Li ObjTy =
501*67e74705SXin Li DBuilder.createStructType(TheCU, "objc_object", getOrCreateMainFile(),
502*67e74705SXin Li 0, 0, 0, 0, nullptr, llvm::DINodeArray());
503*67e74705SXin Li
504*67e74705SXin Li DBuilder.replaceArrays(
505*67e74705SXin Li ObjTy,
506*67e74705SXin Li DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
507*67e74705SXin Li ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0, 0, ISATy)));
508*67e74705SXin Li return ObjTy;
509*67e74705SXin Li }
510*67e74705SXin Li case BuiltinType::ObjCSel: {
511*67e74705SXin Li if (!SelTy)
512*67e74705SXin Li SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
513*67e74705SXin Li "objc_selector", TheCU,
514*67e74705SXin Li getOrCreateMainFile(), 0);
515*67e74705SXin Li return SelTy;
516*67e74705SXin Li }
517*67e74705SXin Li
518*67e74705SXin Li #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
519*67e74705SXin Li case BuiltinType::Id: \
520*67e74705SXin Li return getOrCreateStructPtrType("opencl_" #ImgType "_" #Suffix "_t", \
521*67e74705SXin Li SingletonId);
522*67e74705SXin Li #include "clang/Basic/OpenCLImageTypes.def"
523*67e74705SXin Li case BuiltinType::OCLSampler:
524*67e74705SXin Li return DBuilder.createBasicType(
525*67e74705SXin Li "opencl_sampler_t", CGM.getContext().getTypeSize(BT),
526*67e74705SXin Li CGM.getContext().getTypeAlign(BT), llvm::dwarf::DW_ATE_unsigned);
527*67e74705SXin Li case BuiltinType::OCLEvent:
528*67e74705SXin Li return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
529*67e74705SXin Li case BuiltinType::OCLClkEvent:
530*67e74705SXin Li return getOrCreateStructPtrType("opencl_clk_event_t", OCLClkEventDITy);
531*67e74705SXin Li case BuiltinType::OCLQueue:
532*67e74705SXin Li return getOrCreateStructPtrType("opencl_queue_t", OCLQueueDITy);
533*67e74705SXin Li case BuiltinType::OCLNDRange:
534*67e74705SXin Li return getOrCreateStructPtrType("opencl_ndrange_t", OCLNDRangeDITy);
535*67e74705SXin Li case BuiltinType::OCLReserveID:
536*67e74705SXin Li return getOrCreateStructPtrType("opencl_reserve_id_t", OCLReserveIDDITy);
537*67e74705SXin Li
538*67e74705SXin Li case BuiltinType::UChar:
539*67e74705SXin Li case BuiltinType::Char_U:
540*67e74705SXin Li Encoding = llvm::dwarf::DW_ATE_unsigned_char;
541*67e74705SXin Li break;
542*67e74705SXin Li case BuiltinType::Char_S:
543*67e74705SXin Li case BuiltinType::SChar:
544*67e74705SXin Li Encoding = llvm::dwarf::DW_ATE_signed_char;
545*67e74705SXin Li break;
546*67e74705SXin Li case BuiltinType::Char16:
547*67e74705SXin Li case BuiltinType::Char32:
548*67e74705SXin Li Encoding = llvm::dwarf::DW_ATE_UTF;
549*67e74705SXin Li break;
550*67e74705SXin Li case BuiltinType::UShort:
551*67e74705SXin Li case BuiltinType::UInt:
552*67e74705SXin Li case BuiltinType::UInt128:
553*67e74705SXin Li case BuiltinType::ULong:
554*67e74705SXin Li case BuiltinType::WChar_U:
555*67e74705SXin Li case BuiltinType::ULongLong:
556*67e74705SXin Li Encoding = llvm::dwarf::DW_ATE_unsigned;
557*67e74705SXin Li break;
558*67e74705SXin Li case BuiltinType::Short:
559*67e74705SXin Li case BuiltinType::Int:
560*67e74705SXin Li case BuiltinType::Int128:
561*67e74705SXin Li case BuiltinType::Long:
562*67e74705SXin Li case BuiltinType::WChar_S:
563*67e74705SXin Li case BuiltinType::LongLong:
564*67e74705SXin Li Encoding = llvm::dwarf::DW_ATE_signed;
565*67e74705SXin Li break;
566*67e74705SXin Li case BuiltinType::Bool:
567*67e74705SXin Li Encoding = llvm::dwarf::DW_ATE_boolean;
568*67e74705SXin Li break;
569*67e74705SXin Li case BuiltinType::Half:
570*67e74705SXin Li case BuiltinType::Float:
571*67e74705SXin Li case BuiltinType::LongDouble:
572*67e74705SXin Li case BuiltinType::Float128:
573*67e74705SXin Li case BuiltinType::Double:
574*67e74705SXin Li // FIXME: For targets where long double and __float128 have the same size,
575*67e74705SXin Li // they are currently indistinguishable in the debugger without some
576*67e74705SXin Li // special treatment. However, there is currently no consensus on encoding
577*67e74705SXin Li // and this should be updated once a DWARF encoding exists for distinct
578*67e74705SXin Li // floating point types of the same size.
579*67e74705SXin Li Encoding = llvm::dwarf::DW_ATE_float;
580*67e74705SXin Li break;
581*67e74705SXin Li }
582*67e74705SXin Li
583*67e74705SXin Li switch (BT->getKind()) {
584*67e74705SXin Li case BuiltinType::Long:
585*67e74705SXin Li BTName = "long int";
586*67e74705SXin Li break;
587*67e74705SXin Li case BuiltinType::LongLong:
588*67e74705SXin Li BTName = "long long int";
589*67e74705SXin Li break;
590*67e74705SXin Li case BuiltinType::ULong:
591*67e74705SXin Li BTName = "long unsigned int";
592*67e74705SXin Li break;
593*67e74705SXin Li case BuiltinType::ULongLong:
594*67e74705SXin Li BTName = "long long unsigned int";
595*67e74705SXin Li break;
596*67e74705SXin Li default:
597*67e74705SXin Li BTName = BT->getName(CGM.getLangOpts());
598*67e74705SXin Li break;
599*67e74705SXin Li }
600*67e74705SXin Li // Bit size, align and offset of the type.
601*67e74705SXin Li uint64_t Size = CGM.getContext().getTypeSize(BT);
602*67e74705SXin Li uint64_t Align = CGM.getContext().getTypeAlign(BT);
603*67e74705SXin Li return DBuilder.createBasicType(BTName, Size, Align, Encoding);
604*67e74705SXin Li }
605*67e74705SXin Li
CreateType(const ComplexType * Ty)606*67e74705SXin Li llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
607*67e74705SXin Li // Bit size, align and offset of the type.
608*67e74705SXin Li llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
609*67e74705SXin Li if (Ty->isComplexIntegerType())
610*67e74705SXin Li Encoding = llvm::dwarf::DW_ATE_lo_user;
611*67e74705SXin Li
612*67e74705SXin Li uint64_t Size = CGM.getContext().getTypeSize(Ty);
613*67e74705SXin Li uint64_t Align = CGM.getContext().getTypeAlign(Ty);
614*67e74705SXin Li return DBuilder.createBasicType("complex", Size, Align, Encoding);
615*67e74705SXin Li }
616*67e74705SXin Li
CreateQualifiedType(QualType Ty,llvm::DIFile * Unit)617*67e74705SXin Li llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
618*67e74705SXin Li llvm::DIFile *Unit) {
619*67e74705SXin Li QualifierCollector Qc;
620*67e74705SXin Li const Type *T = Qc.strip(Ty);
621*67e74705SXin Li
622*67e74705SXin Li // Ignore these qualifiers for now.
623*67e74705SXin Li Qc.removeObjCGCAttr();
624*67e74705SXin Li Qc.removeAddressSpace();
625*67e74705SXin Li Qc.removeObjCLifetime();
626*67e74705SXin Li
627*67e74705SXin Li // We will create one Derived type for one qualifier and recurse to handle any
628*67e74705SXin Li // additional ones.
629*67e74705SXin Li llvm::dwarf::Tag Tag;
630*67e74705SXin Li if (Qc.hasConst()) {
631*67e74705SXin Li Tag = llvm::dwarf::DW_TAG_const_type;
632*67e74705SXin Li Qc.removeConst();
633*67e74705SXin Li } else if (Qc.hasVolatile()) {
634*67e74705SXin Li Tag = llvm::dwarf::DW_TAG_volatile_type;
635*67e74705SXin Li Qc.removeVolatile();
636*67e74705SXin Li } else if (Qc.hasRestrict()) {
637*67e74705SXin Li Tag = llvm::dwarf::DW_TAG_restrict_type;
638*67e74705SXin Li Qc.removeRestrict();
639*67e74705SXin Li } else {
640*67e74705SXin Li assert(Qc.empty() && "Unknown type qualifier for debug info");
641*67e74705SXin Li return getOrCreateType(QualType(T, 0), Unit);
642*67e74705SXin Li }
643*67e74705SXin Li
644*67e74705SXin Li auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
645*67e74705SXin Li
646*67e74705SXin Li // No need to fill in the Name, Line, Size, Alignment, Offset in case of
647*67e74705SXin Li // CVR derived types.
648*67e74705SXin Li return DBuilder.createQualifiedType(Tag, FromTy);
649*67e74705SXin Li }
650*67e74705SXin Li
CreateType(const ObjCObjectPointerType * Ty,llvm::DIFile * Unit)651*67e74705SXin Li llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
652*67e74705SXin Li llvm::DIFile *Unit) {
653*67e74705SXin Li
654*67e74705SXin Li // The frontend treats 'id' as a typedef to an ObjCObjectType,
655*67e74705SXin Li // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
656*67e74705SXin Li // debug info, we want to emit 'id' in both cases.
657*67e74705SXin Li if (Ty->isObjCQualifiedIdType())
658*67e74705SXin Li return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
659*67e74705SXin Li
660*67e74705SXin Li return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
661*67e74705SXin Li Ty->getPointeeType(), Unit);
662*67e74705SXin Li }
663*67e74705SXin Li
CreateType(const PointerType * Ty,llvm::DIFile * Unit)664*67e74705SXin Li llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
665*67e74705SXin Li llvm::DIFile *Unit) {
666*67e74705SXin Li return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
667*67e74705SXin Li Ty->getPointeeType(), Unit);
668*67e74705SXin Li }
669*67e74705SXin Li
670*67e74705SXin Li /// \return whether a C++ mangling exists for the type defined by TD.
hasCXXMangling(const TagDecl * TD,llvm::DICompileUnit * TheCU)671*67e74705SXin Li static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
672*67e74705SXin Li switch (TheCU->getSourceLanguage()) {
673*67e74705SXin Li case llvm::dwarf::DW_LANG_C_plus_plus:
674*67e74705SXin Li return true;
675*67e74705SXin Li case llvm::dwarf::DW_LANG_ObjC_plus_plus:
676*67e74705SXin Li return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
677*67e74705SXin Li default:
678*67e74705SXin Li return false;
679*67e74705SXin Li }
680*67e74705SXin Li }
681*67e74705SXin Li
682*67e74705SXin Li /// In C++ mode, types have linkage, so we can rely on the ODR and
683*67e74705SXin Li /// on their mangled names, if they're external.
getUniqueTagTypeName(const TagType * Ty,CodeGenModule & CGM,llvm::DICompileUnit * TheCU)684*67e74705SXin Li static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
685*67e74705SXin Li CodeGenModule &CGM,
686*67e74705SXin Li llvm::DICompileUnit *TheCU) {
687*67e74705SXin Li SmallString<256> FullName;
688*67e74705SXin Li const TagDecl *TD = Ty->getDecl();
689*67e74705SXin Li
690*67e74705SXin Li if (!hasCXXMangling(TD, TheCU) || !TD->isExternallyVisible())
691*67e74705SXin Li return FullName;
692*67e74705SXin Li
693*67e74705SXin Li // TODO: This is using the RTTI name. Is there a better way to get
694*67e74705SXin Li // a unique string for a type?
695*67e74705SXin Li llvm::raw_svector_ostream Out(FullName);
696*67e74705SXin Li CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
697*67e74705SXin Li return FullName;
698*67e74705SXin Li }
699*67e74705SXin Li
700*67e74705SXin Li /// \return the approproate DWARF tag for a composite type.
getTagForRecord(const RecordDecl * RD)701*67e74705SXin Li static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
702*67e74705SXin Li llvm::dwarf::Tag Tag;
703*67e74705SXin Li if (RD->isStruct() || RD->isInterface())
704*67e74705SXin Li Tag = llvm::dwarf::DW_TAG_structure_type;
705*67e74705SXin Li else if (RD->isUnion())
706*67e74705SXin Li Tag = llvm::dwarf::DW_TAG_union_type;
707*67e74705SXin Li else {
708*67e74705SXin Li // FIXME: This could be a struct type giving a default visibility different
709*67e74705SXin Li // than C++ class type, but needs llvm metadata changes first.
710*67e74705SXin Li assert(RD->isClass());
711*67e74705SXin Li Tag = llvm::dwarf::DW_TAG_class_type;
712*67e74705SXin Li }
713*67e74705SXin Li return Tag;
714*67e74705SXin Li }
715*67e74705SXin Li
716*67e74705SXin Li llvm::DICompositeType *
getOrCreateRecordFwdDecl(const RecordType * Ty,llvm::DIScope * Ctx)717*67e74705SXin Li CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
718*67e74705SXin Li llvm::DIScope *Ctx) {
719*67e74705SXin Li const RecordDecl *RD = Ty->getDecl();
720*67e74705SXin Li if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
721*67e74705SXin Li return cast<llvm::DICompositeType>(T);
722*67e74705SXin Li llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
723*67e74705SXin Li unsigned Line = getLineNumber(RD->getLocation());
724*67e74705SXin Li StringRef RDName = getClassName(RD);
725*67e74705SXin Li
726*67e74705SXin Li uint64_t Size = 0;
727*67e74705SXin Li uint64_t Align = 0;
728*67e74705SXin Li
729*67e74705SXin Li const RecordDecl *D = RD->getDefinition();
730*67e74705SXin Li if (D && D->isCompleteDefinition()) {
731*67e74705SXin Li Size = CGM.getContext().getTypeSize(Ty);
732*67e74705SXin Li Align = CGM.getContext().getTypeAlign(Ty);
733*67e74705SXin Li }
734*67e74705SXin Li
735*67e74705SXin Li // Create the type.
736*67e74705SXin Li SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
737*67e74705SXin Li llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
738*67e74705SXin Li getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
739*67e74705SXin Li llvm::DINode::FlagFwdDecl, FullName);
740*67e74705SXin Li ReplaceMap.emplace_back(
741*67e74705SXin Li std::piecewise_construct, std::make_tuple(Ty),
742*67e74705SXin Li std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
743*67e74705SXin Li return RetTy;
744*67e74705SXin Li }
745*67e74705SXin Li
CreatePointerLikeType(llvm::dwarf::Tag Tag,const Type * Ty,QualType PointeeTy,llvm::DIFile * Unit)746*67e74705SXin Li llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
747*67e74705SXin Li const Type *Ty,
748*67e74705SXin Li QualType PointeeTy,
749*67e74705SXin Li llvm::DIFile *Unit) {
750*67e74705SXin Li // Bit size, align and offset of the type.
751*67e74705SXin Li // Size is always the size of a pointer. We can't use getTypeSize here
752*67e74705SXin Li // because that does not return the correct value for references.
753*67e74705SXin Li unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
754*67e74705SXin Li uint64_t Size = CGM.getTarget().getPointerWidth(AS);
755*67e74705SXin Li uint64_t Align = CGM.getContext().getTypeAlign(Ty);
756*67e74705SXin Li
757*67e74705SXin Li if (Tag == llvm::dwarf::DW_TAG_reference_type ||
758*67e74705SXin Li Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
759*67e74705SXin Li return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit),
760*67e74705SXin Li Size, Align);
761*67e74705SXin Li else
762*67e74705SXin Li return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
763*67e74705SXin Li Align);
764*67e74705SXin Li }
765*67e74705SXin Li
getOrCreateStructPtrType(StringRef Name,llvm::DIType * & Cache)766*67e74705SXin Li llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
767*67e74705SXin Li llvm::DIType *&Cache) {
768*67e74705SXin Li if (Cache)
769*67e74705SXin Li return Cache;
770*67e74705SXin Li Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
771*67e74705SXin Li TheCU, getOrCreateMainFile(), 0);
772*67e74705SXin Li unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
773*67e74705SXin Li Cache = DBuilder.createPointerType(Cache, Size);
774*67e74705SXin Li return Cache;
775*67e74705SXin Li }
776*67e74705SXin Li
CreateType(const BlockPointerType * Ty,llvm::DIFile * Unit)777*67e74705SXin Li llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
778*67e74705SXin Li llvm::DIFile *Unit) {
779*67e74705SXin Li SmallVector<llvm::Metadata *, 8> EltTys;
780*67e74705SXin Li QualType FType;
781*67e74705SXin Li uint64_t FieldSize, FieldOffset;
782*67e74705SXin Li unsigned FieldAlign;
783*67e74705SXin Li llvm::DINodeArray Elements;
784*67e74705SXin Li
785*67e74705SXin Li FieldOffset = 0;
786*67e74705SXin Li FType = CGM.getContext().UnsignedLongTy;
787*67e74705SXin Li EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
788*67e74705SXin Li EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
789*67e74705SXin Li
790*67e74705SXin Li Elements = DBuilder.getOrCreateArray(EltTys);
791*67e74705SXin Li EltTys.clear();
792*67e74705SXin Li
793*67e74705SXin Li unsigned Flags = llvm::DINode::FlagAppleBlock;
794*67e74705SXin Li unsigned LineNo = 0;
795*67e74705SXin Li
796*67e74705SXin Li auto *EltTy =
797*67e74705SXin Li DBuilder.createStructType(Unit, "__block_descriptor", nullptr, LineNo,
798*67e74705SXin Li FieldOffset, 0, Flags, nullptr, Elements);
799*67e74705SXin Li
800*67e74705SXin Li // Bit size, align and offset of the type.
801*67e74705SXin Li uint64_t Size = CGM.getContext().getTypeSize(Ty);
802*67e74705SXin Li
803*67e74705SXin Li auto *DescTy = DBuilder.createPointerType(EltTy, Size);
804*67e74705SXin Li
805*67e74705SXin Li FieldOffset = 0;
806*67e74705SXin Li FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
807*67e74705SXin Li EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
808*67e74705SXin Li FType = CGM.getContext().IntTy;
809*67e74705SXin Li EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
810*67e74705SXin Li EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
811*67e74705SXin Li FType = CGM.getContext().getPointerType(Ty->getPointeeType());
812*67e74705SXin Li EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
813*67e74705SXin Li
814*67e74705SXin Li FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
815*67e74705SXin Li FieldSize = CGM.getContext().getTypeSize(Ty);
816*67e74705SXin Li FieldAlign = CGM.getContext().getTypeAlign(Ty);
817*67e74705SXin Li EltTys.push_back(DBuilder.createMemberType(Unit, "__descriptor", nullptr, LineNo,
818*67e74705SXin Li FieldSize, FieldAlign, FieldOffset,
819*67e74705SXin Li 0, DescTy));
820*67e74705SXin Li
821*67e74705SXin Li FieldOffset += FieldSize;
822*67e74705SXin Li Elements = DBuilder.getOrCreateArray(EltTys);
823*67e74705SXin Li
824*67e74705SXin Li // The __block_literal_generic structs are marked with a special
825*67e74705SXin Li // DW_AT_APPLE_BLOCK attribute and are an implementation detail only
826*67e74705SXin Li // the debugger needs to know about. To allow type uniquing, emit
827*67e74705SXin Li // them without a name or a location.
828*67e74705SXin Li EltTy =
829*67e74705SXin Li DBuilder.createStructType(Unit, "", nullptr, LineNo,
830*67e74705SXin Li FieldOffset, 0, Flags, nullptr, Elements);
831*67e74705SXin Li
832*67e74705SXin Li return DBuilder.createPointerType(EltTy, Size);
833*67e74705SXin Li }
834*67e74705SXin Li
CreateType(const TemplateSpecializationType * Ty,llvm::DIFile * Unit)835*67e74705SXin Li llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
836*67e74705SXin Li llvm::DIFile *Unit) {
837*67e74705SXin Li assert(Ty->isTypeAlias());
838*67e74705SXin Li llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
839*67e74705SXin Li
840*67e74705SXin Li SmallString<128> NS;
841*67e74705SXin Li llvm::raw_svector_ostream OS(NS);
842*67e74705SXin Li Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
843*67e74705SXin Li /*qualified*/ false);
844*67e74705SXin Li
845*67e74705SXin Li TemplateSpecializationType::PrintTemplateArgumentList(
846*67e74705SXin Li OS, Ty->template_arguments(),
847*67e74705SXin Li CGM.getContext().getPrintingPolicy());
848*67e74705SXin Li
849*67e74705SXin Li TypeAliasDecl *AliasDecl = cast<TypeAliasTemplateDecl>(
850*67e74705SXin Li Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
851*67e74705SXin Li
852*67e74705SXin Li SourceLocation Loc = AliasDecl->getLocation();
853*67e74705SXin Li return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc),
854*67e74705SXin Li getLineNumber(Loc),
855*67e74705SXin Li getDeclContextDescriptor(AliasDecl));
856*67e74705SXin Li }
857*67e74705SXin Li
CreateType(const TypedefType * Ty,llvm::DIFile * Unit)858*67e74705SXin Li llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
859*67e74705SXin Li llvm::DIFile *Unit) {
860*67e74705SXin Li // We don't set size information, but do specify where the typedef was
861*67e74705SXin Li // declared.
862*67e74705SXin Li SourceLocation Loc = Ty->getDecl()->getLocation();
863*67e74705SXin Li
864*67e74705SXin Li // Typedefs are derived from some other type.
865*67e74705SXin Li return DBuilder.createTypedef(
866*67e74705SXin Li getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
867*67e74705SXin Li Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
868*67e74705SXin Li getDeclContextDescriptor(Ty->getDecl()));
869*67e74705SXin Li }
870*67e74705SXin Li
getDwarfCC(CallingConv CC)871*67e74705SXin Li static unsigned getDwarfCC(CallingConv CC) {
872*67e74705SXin Li switch (CC) {
873*67e74705SXin Li case CC_C:
874*67e74705SXin Li // Avoid emitting DW_AT_calling_convention if the C convention was used.
875*67e74705SXin Li return 0;
876*67e74705SXin Li
877*67e74705SXin Li case CC_X86StdCall:
878*67e74705SXin Li return llvm::dwarf::DW_CC_BORLAND_stdcall;
879*67e74705SXin Li case CC_X86FastCall:
880*67e74705SXin Li return llvm::dwarf::DW_CC_BORLAND_msfastcall;
881*67e74705SXin Li case CC_X86ThisCall:
882*67e74705SXin Li return llvm::dwarf::DW_CC_BORLAND_thiscall;
883*67e74705SXin Li case CC_X86VectorCall:
884*67e74705SXin Li return llvm::dwarf::DW_CC_LLVM_vectorcall;
885*67e74705SXin Li case CC_X86Pascal:
886*67e74705SXin Li return llvm::dwarf::DW_CC_BORLAND_pascal;
887*67e74705SXin Li
888*67e74705SXin Li // FIXME: Create new DW_CC_ codes for these calling conventions.
889*67e74705SXin Li case CC_X86_64Win64:
890*67e74705SXin Li case CC_X86_64SysV:
891*67e74705SXin Li case CC_AAPCS:
892*67e74705SXin Li case CC_AAPCS_VFP:
893*67e74705SXin Li case CC_IntelOclBicc:
894*67e74705SXin Li case CC_SpirFunction:
895*67e74705SXin Li case CC_OpenCLKernel:
896*67e74705SXin Li case CC_Swift:
897*67e74705SXin Li case CC_PreserveMost:
898*67e74705SXin Li case CC_PreserveAll:
899*67e74705SXin Li return 0;
900*67e74705SXin Li }
901*67e74705SXin Li return 0;
902*67e74705SXin Li }
903*67e74705SXin Li
CreateType(const FunctionType * Ty,llvm::DIFile * Unit)904*67e74705SXin Li llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
905*67e74705SXin Li llvm::DIFile *Unit) {
906*67e74705SXin Li SmallVector<llvm::Metadata *, 16> EltTys;
907*67e74705SXin Li
908*67e74705SXin Li // Add the result type at least.
909*67e74705SXin Li EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
910*67e74705SXin Li
911*67e74705SXin Li // Set up remainder of arguments if there is a prototype.
912*67e74705SXin Li // otherwise emit it as a variadic function.
913*67e74705SXin Li if (isa<FunctionNoProtoType>(Ty))
914*67e74705SXin Li EltTys.push_back(DBuilder.createUnspecifiedParameter());
915*67e74705SXin Li else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
916*67e74705SXin Li for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
917*67e74705SXin Li EltTys.push_back(getOrCreateType(FPT->getParamType(i), Unit));
918*67e74705SXin Li if (FPT->isVariadic())
919*67e74705SXin Li EltTys.push_back(DBuilder.createUnspecifiedParameter());
920*67e74705SXin Li }
921*67e74705SXin Li
922*67e74705SXin Li llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
923*67e74705SXin Li return DBuilder.createSubroutineType(EltTypeArray, 0,
924*67e74705SXin Li getDwarfCC(Ty->getCallConv()));
925*67e74705SXin Li }
926*67e74705SXin Li
927*67e74705SXin Li /// Convert an AccessSpecifier into the corresponding DINode flag.
928*67e74705SXin Li /// As an optimization, return 0 if the access specifier equals the
929*67e74705SXin Li /// default for the containing type.
getAccessFlag(AccessSpecifier Access,const RecordDecl * RD)930*67e74705SXin Li static unsigned getAccessFlag(AccessSpecifier Access, const RecordDecl *RD) {
931*67e74705SXin Li AccessSpecifier Default = clang::AS_none;
932*67e74705SXin Li if (RD && RD->isClass())
933*67e74705SXin Li Default = clang::AS_private;
934*67e74705SXin Li else if (RD && (RD->isStruct() || RD->isUnion()))
935*67e74705SXin Li Default = clang::AS_public;
936*67e74705SXin Li
937*67e74705SXin Li if (Access == Default)
938*67e74705SXin Li return 0;
939*67e74705SXin Li
940*67e74705SXin Li switch (Access) {
941*67e74705SXin Li case clang::AS_private:
942*67e74705SXin Li return llvm::DINode::FlagPrivate;
943*67e74705SXin Li case clang::AS_protected:
944*67e74705SXin Li return llvm::DINode::FlagProtected;
945*67e74705SXin Li case clang::AS_public:
946*67e74705SXin Li return llvm::DINode::FlagPublic;
947*67e74705SXin Li case clang::AS_none:
948*67e74705SXin Li return 0;
949*67e74705SXin Li }
950*67e74705SXin Li llvm_unreachable("unexpected access enumerator");
951*67e74705SXin Li }
952*67e74705SXin Li
createBitFieldType(const FieldDecl * BitFieldDecl,llvm::DIScope * RecordTy,const RecordDecl * RD)953*67e74705SXin Li llvm::DIType *CGDebugInfo::createBitFieldType(const FieldDecl *BitFieldDecl,
954*67e74705SXin Li llvm::DIScope *RecordTy,
955*67e74705SXin Li const RecordDecl *RD) {
956*67e74705SXin Li StringRef Name = BitFieldDecl->getName();
957*67e74705SXin Li QualType Ty = BitFieldDecl->getType();
958*67e74705SXin Li SourceLocation Loc = BitFieldDecl->getLocation();
959*67e74705SXin Li llvm::DIFile *VUnit = getOrCreateFile(Loc);
960*67e74705SXin Li llvm::DIType *DebugType = getOrCreateType(Ty, VUnit);
961*67e74705SXin Li
962*67e74705SXin Li // Get the location for the field.
963*67e74705SXin Li llvm::DIFile *File = getOrCreateFile(Loc);
964*67e74705SXin Li unsigned Line = getLineNumber(Loc);
965*67e74705SXin Li
966*67e74705SXin Li const CGBitFieldInfo &BitFieldInfo =
967*67e74705SXin Li CGM.getTypes().getCGRecordLayout(RD).getBitFieldInfo(BitFieldDecl);
968*67e74705SXin Li uint64_t SizeInBits = BitFieldInfo.Size;
969*67e74705SXin Li assert(SizeInBits > 0 && "found named 0-width bitfield");
970*67e74705SXin Li unsigned AlignInBits = CGM.getContext().getTypeAlign(Ty);
971*67e74705SXin Li uint64_t StorageOffsetInBits =
972*67e74705SXin Li CGM.getContext().toBits(BitFieldInfo.StorageOffset);
973*67e74705SXin Li uint64_t OffsetInBits = StorageOffsetInBits + BitFieldInfo.Offset;
974*67e74705SXin Li unsigned Flags = getAccessFlag(BitFieldDecl->getAccess(), RD);
975*67e74705SXin Li return DBuilder.createBitFieldMemberType(
976*67e74705SXin Li RecordTy, Name, File, Line, SizeInBits, AlignInBits, OffsetInBits,
977*67e74705SXin Li StorageOffsetInBits, Flags, DebugType);
978*67e74705SXin Li }
979*67e74705SXin Li
980*67e74705SXin Li llvm::DIType *
createFieldType(StringRef name,QualType type,SourceLocation loc,AccessSpecifier AS,uint64_t offsetInBits,llvm::DIFile * tunit,llvm::DIScope * scope,const RecordDecl * RD)981*67e74705SXin Li CGDebugInfo::createFieldType(StringRef name, QualType type, SourceLocation loc,
982*67e74705SXin Li AccessSpecifier AS, uint64_t offsetInBits,
983*67e74705SXin Li llvm::DIFile *tunit, llvm::DIScope *scope,
984*67e74705SXin Li const RecordDecl *RD) {
985*67e74705SXin Li llvm::DIType *debugType = getOrCreateType(type, tunit);
986*67e74705SXin Li
987*67e74705SXin Li // Get the location for the field.
988*67e74705SXin Li llvm::DIFile *file = getOrCreateFile(loc);
989*67e74705SXin Li unsigned line = getLineNumber(loc);
990*67e74705SXin Li
991*67e74705SXin Li uint64_t SizeInBits = 0;
992*67e74705SXin Li unsigned AlignInBits = 0;
993*67e74705SXin Li if (!type->isIncompleteArrayType()) {
994*67e74705SXin Li TypeInfo TI = CGM.getContext().getTypeInfo(type);
995*67e74705SXin Li SizeInBits = TI.Width;
996*67e74705SXin Li AlignInBits = TI.Align;
997*67e74705SXin Li }
998*67e74705SXin Li
999*67e74705SXin Li unsigned flags = getAccessFlag(AS, RD);
1000*67e74705SXin Li return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
1001*67e74705SXin Li AlignInBits, offsetInBits, flags, debugType);
1002*67e74705SXin Li }
1003*67e74705SXin Li
CollectRecordLambdaFields(const CXXRecordDecl * CXXDecl,SmallVectorImpl<llvm::Metadata * > & elements,llvm::DIType * RecordTy)1004*67e74705SXin Li void CGDebugInfo::CollectRecordLambdaFields(
1005*67e74705SXin Li const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
1006*67e74705SXin Li llvm::DIType *RecordTy) {
1007*67e74705SXin Li // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
1008*67e74705SXin Li // has the name and the location of the variable so we should iterate over
1009*67e74705SXin Li // both concurrently.
1010*67e74705SXin Li const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
1011*67e74705SXin Li RecordDecl::field_iterator Field = CXXDecl->field_begin();
1012*67e74705SXin Li unsigned fieldno = 0;
1013*67e74705SXin Li for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
1014*67e74705SXin Li E = CXXDecl->captures_end();
1015*67e74705SXin Li I != E; ++I, ++Field, ++fieldno) {
1016*67e74705SXin Li const LambdaCapture &C = *I;
1017*67e74705SXin Li if (C.capturesVariable()) {
1018*67e74705SXin Li SourceLocation Loc = C.getLocation();
1019*67e74705SXin Li assert(!Field->isBitField() && "lambdas don't have bitfield members!");
1020*67e74705SXin Li VarDecl *V = C.getCapturedVar();
1021*67e74705SXin Li StringRef VName = V->getName();
1022*67e74705SXin Li llvm::DIFile *VUnit = getOrCreateFile(Loc);
1023*67e74705SXin Li llvm::DIType *FieldType = createFieldType(
1024*67e74705SXin Li VName, Field->getType(), Loc, Field->getAccess(),
1025*67e74705SXin Li layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
1026*67e74705SXin Li elements.push_back(FieldType);
1027*67e74705SXin Li } else if (C.capturesThis()) {
1028*67e74705SXin Li // TODO: Need to handle 'this' in some way by probably renaming the
1029*67e74705SXin Li // this of the lambda class and having a field member of 'this' or
1030*67e74705SXin Li // by using AT_object_pointer for the function and having that be
1031*67e74705SXin Li // used as 'this' for semantic references.
1032*67e74705SXin Li FieldDecl *f = *Field;
1033*67e74705SXin Li llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
1034*67e74705SXin Li QualType type = f->getType();
1035*67e74705SXin Li llvm::DIType *fieldType = createFieldType(
1036*67e74705SXin Li "this", type, f->getLocation(), f->getAccess(),
1037*67e74705SXin Li layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
1038*67e74705SXin Li
1039*67e74705SXin Li elements.push_back(fieldType);
1040*67e74705SXin Li }
1041*67e74705SXin Li }
1042*67e74705SXin Li }
1043*67e74705SXin Li
1044*67e74705SXin Li llvm::DIDerivedType *
CreateRecordStaticField(const VarDecl * Var,llvm::DIType * RecordTy,const RecordDecl * RD)1045*67e74705SXin Li CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
1046*67e74705SXin Li const RecordDecl *RD) {
1047*67e74705SXin Li // Create the descriptor for the static variable, with or without
1048*67e74705SXin Li // constant initializers.
1049*67e74705SXin Li Var = Var->getCanonicalDecl();
1050*67e74705SXin Li llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
1051*67e74705SXin Li llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
1052*67e74705SXin Li
1053*67e74705SXin Li unsigned LineNumber = getLineNumber(Var->getLocation());
1054*67e74705SXin Li StringRef VName = Var->getName();
1055*67e74705SXin Li llvm::Constant *C = nullptr;
1056*67e74705SXin Li if (Var->getInit()) {
1057*67e74705SXin Li const APValue *Value = Var->evaluateValue();
1058*67e74705SXin Li if (Value) {
1059*67e74705SXin Li if (Value->isInt())
1060*67e74705SXin Li C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
1061*67e74705SXin Li if (Value->isFloat())
1062*67e74705SXin Li C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
1063*67e74705SXin Li }
1064*67e74705SXin Li }
1065*67e74705SXin Li
1066*67e74705SXin Li unsigned Flags = getAccessFlag(Var->getAccess(), RD);
1067*67e74705SXin Li llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
1068*67e74705SXin Li RecordTy, VName, VUnit, LineNumber, VTy, Flags, C);
1069*67e74705SXin Li StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
1070*67e74705SXin Li return GV;
1071*67e74705SXin Li }
1072*67e74705SXin Li
CollectRecordNormalField(const FieldDecl * field,uint64_t OffsetInBits,llvm::DIFile * tunit,SmallVectorImpl<llvm::Metadata * > & elements,llvm::DIType * RecordTy,const RecordDecl * RD)1073*67e74705SXin Li void CGDebugInfo::CollectRecordNormalField(
1074*67e74705SXin Li const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
1075*67e74705SXin Li SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
1076*67e74705SXin Li const RecordDecl *RD) {
1077*67e74705SXin Li StringRef name = field->getName();
1078*67e74705SXin Li QualType type = field->getType();
1079*67e74705SXin Li
1080*67e74705SXin Li // Ignore unnamed fields unless they're anonymous structs/unions.
1081*67e74705SXin Li if (name.empty() && !type->isRecordType())
1082*67e74705SXin Li return;
1083*67e74705SXin Li
1084*67e74705SXin Li llvm::DIType *FieldType;
1085*67e74705SXin Li if (field->isBitField()) {
1086*67e74705SXin Li FieldType = createBitFieldType(field, RecordTy, RD);
1087*67e74705SXin Li } else {
1088*67e74705SXin Li FieldType =
1089*67e74705SXin Li createFieldType(name, type, field->getLocation(), field->getAccess(),
1090*67e74705SXin Li OffsetInBits, tunit, RecordTy, RD);
1091*67e74705SXin Li }
1092*67e74705SXin Li
1093*67e74705SXin Li elements.push_back(FieldType);
1094*67e74705SXin Li }
1095*67e74705SXin Li
CollectRecordFields(const RecordDecl * record,llvm::DIFile * tunit,SmallVectorImpl<llvm::Metadata * > & elements,llvm::DICompositeType * RecordTy)1096*67e74705SXin Li void CGDebugInfo::CollectRecordFields(
1097*67e74705SXin Li const RecordDecl *record, llvm::DIFile *tunit,
1098*67e74705SXin Li SmallVectorImpl<llvm::Metadata *> &elements,
1099*67e74705SXin Li llvm::DICompositeType *RecordTy) {
1100*67e74705SXin Li const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
1101*67e74705SXin Li
1102*67e74705SXin Li if (CXXDecl && CXXDecl->isLambda())
1103*67e74705SXin Li CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
1104*67e74705SXin Li else {
1105*67e74705SXin Li const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
1106*67e74705SXin Li
1107*67e74705SXin Li // Field number for non-static fields.
1108*67e74705SXin Li unsigned fieldNo = 0;
1109*67e74705SXin Li
1110*67e74705SXin Li // Static and non-static members should appear in the same order as
1111*67e74705SXin Li // the corresponding declarations in the source program.
1112*67e74705SXin Li for (const auto *I : record->decls())
1113*67e74705SXin Li if (const auto *V = dyn_cast<VarDecl>(I)) {
1114*67e74705SXin Li if (V->hasAttr<NoDebugAttr>())
1115*67e74705SXin Li continue;
1116*67e74705SXin Li // Reuse the existing static member declaration if one exists
1117*67e74705SXin Li auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
1118*67e74705SXin Li if (MI != StaticDataMemberCache.end()) {
1119*67e74705SXin Li assert(MI->second &&
1120*67e74705SXin Li "Static data member declaration should still exist");
1121*67e74705SXin Li elements.push_back(MI->second);
1122*67e74705SXin Li } else {
1123*67e74705SXin Li auto Field = CreateRecordStaticField(V, RecordTy, record);
1124*67e74705SXin Li elements.push_back(Field);
1125*67e74705SXin Li }
1126*67e74705SXin Li } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
1127*67e74705SXin Li CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
1128*67e74705SXin Li elements, RecordTy, record);
1129*67e74705SXin Li
1130*67e74705SXin Li // Bump field number for next field.
1131*67e74705SXin Li ++fieldNo;
1132*67e74705SXin Li }
1133*67e74705SXin Li }
1134*67e74705SXin Li }
1135*67e74705SXin Li
1136*67e74705SXin Li llvm::DISubroutineType *
getOrCreateMethodType(const CXXMethodDecl * Method,llvm::DIFile * Unit)1137*67e74705SXin Li CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
1138*67e74705SXin Li llvm::DIFile *Unit) {
1139*67e74705SXin Li const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
1140*67e74705SXin Li if (Method->isStatic())
1141*67e74705SXin Li return cast_or_null<llvm::DISubroutineType>(
1142*67e74705SXin Li getOrCreateType(QualType(Func, 0), Unit));
1143*67e74705SXin Li return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1144*67e74705SXin Li Func, Unit);
1145*67e74705SXin Li }
1146*67e74705SXin Li
getOrCreateInstanceMethodType(QualType ThisPtr,const FunctionProtoType * Func,llvm::DIFile * Unit)1147*67e74705SXin Li llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1148*67e74705SXin Li QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
1149*67e74705SXin Li // Add "this" pointer.
1150*67e74705SXin Li llvm::DITypeRefArray Args(
1151*67e74705SXin Li cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
1152*67e74705SXin Li ->getTypeArray());
1153*67e74705SXin Li assert(Args.size() && "Invalid number of arguments!");
1154*67e74705SXin Li
1155*67e74705SXin Li SmallVector<llvm::Metadata *, 16> Elts;
1156*67e74705SXin Li
1157*67e74705SXin Li // First element is always return type. For 'void' functions it is NULL.
1158*67e74705SXin Li Elts.push_back(Args[0]);
1159*67e74705SXin Li
1160*67e74705SXin Li // "this" pointer is always first argument.
1161*67e74705SXin Li const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
1162*67e74705SXin Li if (isa<ClassTemplateSpecializationDecl>(RD)) {
1163*67e74705SXin Li // Create pointer type directly in this case.
1164*67e74705SXin Li const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1165*67e74705SXin Li QualType PointeeTy = ThisPtrTy->getPointeeType();
1166*67e74705SXin Li unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
1167*67e74705SXin Li uint64_t Size = CGM.getTarget().getPointerWidth(AS);
1168*67e74705SXin Li uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
1169*67e74705SXin Li llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1170*67e74705SXin Li llvm::DIType *ThisPtrType =
1171*67e74705SXin Li DBuilder.createPointerType(PointeeType, Size, Align);
1172*67e74705SXin Li TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
1173*67e74705SXin Li // TODO: This and the artificial type below are misleading, the
1174*67e74705SXin Li // types aren't artificial the argument is, but the current
1175*67e74705SXin Li // metadata doesn't represent that.
1176*67e74705SXin Li ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1177*67e74705SXin Li Elts.push_back(ThisPtrType);
1178*67e74705SXin Li } else {
1179*67e74705SXin Li llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
1180*67e74705SXin Li TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
1181*67e74705SXin Li ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1182*67e74705SXin Li Elts.push_back(ThisPtrType);
1183*67e74705SXin Li }
1184*67e74705SXin Li
1185*67e74705SXin Li // Copy rest of the arguments.
1186*67e74705SXin Li for (unsigned i = 1, e = Args.size(); i != e; ++i)
1187*67e74705SXin Li Elts.push_back(Args[i]);
1188*67e74705SXin Li
1189*67e74705SXin Li llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
1190*67e74705SXin Li
1191*67e74705SXin Li unsigned Flags = 0;
1192*67e74705SXin Li if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
1193*67e74705SXin Li Flags |= llvm::DINode::FlagLValueReference;
1194*67e74705SXin Li if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
1195*67e74705SXin Li Flags |= llvm::DINode::FlagRValueReference;
1196*67e74705SXin Li
1197*67e74705SXin Li return DBuilder.createSubroutineType(EltTypeArray, Flags,
1198*67e74705SXin Li getDwarfCC(Func->getCallConv()));
1199*67e74705SXin Li }
1200*67e74705SXin Li
1201*67e74705SXin Li /// isFunctionLocalClass - Return true if CXXRecordDecl is defined
1202*67e74705SXin Li /// inside a function.
isFunctionLocalClass(const CXXRecordDecl * RD)1203*67e74705SXin Li static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1204*67e74705SXin Li if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1205*67e74705SXin Li return isFunctionLocalClass(NRD);
1206*67e74705SXin Li if (isa<FunctionDecl>(RD->getDeclContext()))
1207*67e74705SXin Li return true;
1208*67e74705SXin Li return false;
1209*67e74705SXin Li }
1210*67e74705SXin Li
CreateCXXMemberFunction(const CXXMethodDecl * Method,llvm::DIFile * Unit,llvm::DIType * RecordTy)1211*67e74705SXin Li llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1212*67e74705SXin Li const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
1213*67e74705SXin Li bool IsCtorOrDtor =
1214*67e74705SXin Li isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
1215*67e74705SXin Li
1216*67e74705SXin Li StringRef MethodName = getFunctionName(Method);
1217*67e74705SXin Li llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
1218*67e74705SXin Li
1219*67e74705SXin Li // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1220*67e74705SXin Li // make sense to give a single ctor/dtor a linkage name.
1221*67e74705SXin Li StringRef MethodLinkageName;
1222*67e74705SXin Li // FIXME: 'isFunctionLocalClass' seems like an arbitrary/unintentional
1223*67e74705SXin Li // property to use here. It may've been intended to model "is non-external
1224*67e74705SXin Li // type" but misses cases of non-function-local but non-external classes such
1225*67e74705SXin Li // as those in anonymous namespaces as well as the reverse - external types
1226*67e74705SXin Li // that are function local, such as those in (non-local) inline functions.
1227*67e74705SXin Li if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1228*67e74705SXin Li MethodLinkageName = CGM.getMangledName(Method);
1229*67e74705SXin Li
1230*67e74705SXin Li // Get the location for the method.
1231*67e74705SXin Li llvm::DIFile *MethodDefUnit = nullptr;
1232*67e74705SXin Li unsigned MethodLine = 0;
1233*67e74705SXin Li if (!Method->isImplicit()) {
1234*67e74705SXin Li MethodDefUnit = getOrCreateFile(Method->getLocation());
1235*67e74705SXin Li MethodLine = getLineNumber(Method->getLocation());
1236*67e74705SXin Li }
1237*67e74705SXin Li
1238*67e74705SXin Li // Collect virtual method info.
1239*67e74705SXin Li llvm::DIType *ContainingType = nullptr;
1240*67e74705SXin Li unsigned Virtuality = 0;
1241*67e74705SXin Li unsigned VIndex = 0;
1242*67e74705SXin Li unsigned Flags = 0;
1243*67e74705SXin Li int ThisAdjustment = 0;
1244*67e74705SXin Li
1245*67e74705SXin Li if (Method->isVirtual()) {
1246*67e74705SXin Li if (Method->isPure())
1247*67e74705SXin Li Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1248*67e74705SXin Li else
1249*67e74705SXin Li Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
1250*67e74705SXin Li
1251*67e74705SXin Li if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1252*67e74705SXin Li // It doesn't make sense to give a virtual destructor a vtable index,
1253*67e74705SXin Li // since a single destructor has two entries in the vtable.
1254*67e74705SXin Li if (!isa<CXXDestructorDecl>(Method))
1255*67e74705SXin Li VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
1256*67e74705SXin Li } else {
1257*67e74705SXin Li // Emit MS ABI vftable information. There is only one entry for the
1258*67e74705SXin Li // deleting dtor.
1259*67e74705SXin Li const auto *DD = dyn_cast<CXXDestructorDecl>(Method);
1260*67e74705SXin Li GlobalDecl GD = DD ? GlobalDecl(DD, Dtor_Deleting) : GlobalDecl(Method);
1261*67e74705SXin Li MicrosoftVTableContext::MethodVFTableLocation ML =
1262*67e74705SXin Li CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD);
1263*67e74705SXin Li VIndex = ML.Index;
1264*67e74705SXin Li
1265*67e74705SXin Li // CodeView only records the vftable offset in the class that introduces
1266*67e74705SXin Li // the virtual method. This is possible because, unlike Itanium, the MS
1267*67e74705SXin Li // C++ ABI does not include all virtual methods from non-primary bases in
1268*67e74705SXin Li // the vtable for the most derived class. For example, if C inherits from
1269*67e74705SXin Li // A and B, C's primary vftable will not include B's virtual methods.
1270*67e74705SXin Li if (Method->begin_overridden_methods() == Method->end_overridden_methods())
1271*67e74705SXin Li Flags |= llvm::DINode::FlagIntroducedVirtual;
1272*67e74705SXin Li
1273*67e74705SXin Li // The 'this' adjustment accounts for both the virtual and non-virtual
1274*67e74705SXin Li // portions of the adjustment. Presumably the debugger only uses it when
1275*67e74705SXin Li // it knows the dynamic type of an object.
1276*67e74705SXin Li ThisAdjustment = CGM.getCXXABI()
1277*67e74705SXin Li .getVirtualFunctionPrologueThisAdjustment(GD)
1278*67e74705SXin Li .getQuantity();
1279*67e74705SXin Li }
1280*67e74705SXin Li ContainingType = RecordTy;
1281*67e74705SXin Li }
1282*67e74705SXin Li
1283*67e74705SXin Li if (Method->isImplicit())
1284*67e74705SXin Li Flags |= llvm::DINode::FlagArtificial;
1285*67e74705SXin Li Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
1286*67e74705SXin Li if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1287*67e74705SXin Li if (CXXC->isExplicit())
1288*67e74705SXin Li Flags |= llvm::DINode::FlagExplicit;
1289*67e74705SXin Li } else if (const CXXConversionDecl *CXXC =
1290*67e74705SXin Li dyn_cast<CXXConversionDecl>(Method)) {
1291*67e74705SXin Li if (CXXC->isExplicit())
1292*67e74705SXin Li Flags |= llvm::DINode::FlagExplicit;
1293*67e74705SXin Li }
1294*67e74705SXin Li if (Method->hasPrototype())
1295*67e74705SXin Li Flags |= llvm::DINode::FlagPrototyped;
1296*67e74705SXin Li if (Method->getRefQualifier() == RQ_LValue)
1297*67e74705SXin Li Flags |= llvm::DINode::FlagLValueReference;
1298*67e74705SXin Li if (Method->getRefQualifier() == RQ_RValue)
1299*67e74705SXin Li Flags |= llvm::DINode::FlagRValueReference;
1300*67e74705SXin Li
1301*67e74705SXin Li llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1302*67e74705SXin Li llvm::DISubprogram *SP = DBuilder.createMethod(
1303*67e74705SXin Li RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
1304*67e74705SXin Li MethodTy, /*isLocalToUnit=*/false, /*isDefinition=*/false, Virtuality,
1305*67e74705SXin Li VIndex, ThisAdjustment, ContainingType, Flags, CGM.getLangOpts().Optimize,
1306*67e74705SXin Li TParamsArray.get());
1307*67e74705SXin Li
1308*67e74705SXin Li SPCache[Method->getCanonicalDecl()].reset(SP);
1309*67e74705SXin Li
1310*67e74705SXin Li return SP;
1311*67e74705SXin Li }
1312*67e74705SXin Li
CollectCXXMemberFunctions(const CXXRecordDecl * RD,llvm::DIFile * Unit,SmallVectorImpl<llvm::Metadata * > & EltTys,llvm::DIType * RecordTy)1313*67e74705SXin Li void CGDebugInfo::CollectCXXMemberFunctions(
1314*67e74705SXin Li const CXXRecordDecl *RD, llvm::DIFile *Unit,
1315*67e74705SXin Li SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
1316*67e74705SXin Li
1317*67e74705SXin Li // Since we want more than just the individual member decls if we
1318*67e74705SXin Li // have templated functions iterate over every declaration to gather
1319*67e74705SXin Li // the functions.
1320*67e74705SXin Li for (const auto *I : RD->decls()) {
1321*67e74705SXin Li const auto *Method = dyn_cast<CXXMethodDecl>(I);
1322*67e74705SXin Li // If the member is implicit, don't add it to the member list. This avoids
1323*67e74705SXin Li // the member being added to type units by LLVM, while still allowing it
1324*67e74705SXin Li // to be emitted into the type declaration/reference inside the compile
1325*67e74705SXin Li // unit.
1326*67e74705SXin Li // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
1327*67e74705SXin Li // FIXME: Handle Using(Shadow?)Decls here to create
1328*67e74705SXin Li // DW_TAG_imported_declarations inside the class for base decls brought into
1329*67e74705SXin Li // derived classes. GDB doesn't seem to notice/leverage these when I tried
1330*67e74705SXin Li // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1331*67e74705SXin Li // referenced)
1332*67e74705SXin Li if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
1333*67e74705SXin Li continue;
1334*67e74705SXin Li
1335*67e74705SXin Li if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1336*67e74705SXin Li continue;
1337*67e74705SXin Li
1338*67e74705SXin Li // Reuse the existing member function declaration if it exists.
1339*67e74705SXin Li // It may be associated with the declaration of the type & should be
1340*67e74705SXin Li // reused as we're building the definition.
1341*67e74705SXin Li //
1342*67e74705SXin Li // This situation can arise in the vtable-based debug info reduction where
1343*67e74705SXin Li // implicit members are emitted in a non-vtable TU.
1344*67e74705SXin Li auto MI = SPCache.find(Method->getCanonicalDecl());
1345*67e74705SXin Li EltTys.push_back(MI == SPCache.end()
1346*67e74705SXin Li ? CreateCXXMemberFunction(Method, Unit, RecordTy)
1347*67e74705SXin Li : static_cast<llvm::Metadata *>(MI->second));
1348*67e74705SXin Li }
1349*67e74705SXin Li }
1350*67e74705SXin Li
CollectCXXBases(const CXXRecordDecl * RD,llvm::DIFile * Unit,SmallVectorImpl<llvm::Metadata * > & EltTys,llvm::DIType * RecordTy)1351*67e74705SXin Li void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
1352*67e74705SXin Li SmallVectorImpl<llvm::Metadata *> &EltTys,
1353*67e74705SXin Li llvm::DIType *RecordTy) {
1354*67e74705SXin Li const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1355*67e74705SXin Li for (const auto &BI : RD->bases()) {
1356*67e74705SXin Li unsigned BFlags = 0;
1357*67e74705SXin Li uint64_t BaseOffset;
1358*67e74705SXin Li
1359*67e74705SXin Li const CXXRecordDecl *Base =
1360*67e74705SXin Li cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
1361*67e74705SXin Li
1362*67e74705SXin Li if (BI.isVirtual()) {
1363*67e74705SXin Li if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1364*67e74705SXin Li // virtual base offset offset is -ve. The code generator emits dwarf
1365*67e74705SXin Li // expression where it expects +ve number.
1366*67e74705SXin Li BaseOffset = 0 - CGM.getItaniumVTableContext()
1367*67e74705SXin Li .getVirtualBaseOffsetOffset(RD, Base)
1368*67e74705SXin Li .getQuantity();
1369*67e74705SXin Li } else {
1370*67e74705SXin Li // In the MS ABI, store the vbtable offset, which is analogous to the
1371*67e74705SXin Li // vbase offset offset in Itanium.
1372*67e74705SXin Li BaseOffset =
1373*67e74705SXin Li 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1374*67e74705SXin Li }
1375*67e74705SXin Li BFlags = llvm::DINode::FlagVirtual;
1376*67e74705SXin Li } else
1377*67e74705SXin Li BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1378*67e74705SXin Li // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1379*67e74705SXin Li // BI->isVirtual() and bits when not.
1380*67e74705SXin Li
1381*67e74705SXin Li BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
1382*67e74705SXin Li llvm::DIType *DTy = DBuilder.createInheritance(
1383*67e74705SXin Li RecordTy, getOrCreateType(BI.getType(), Unit), BaseOffset, BFlags);
1384*67e74705SXin Li EltTys.push_back(DTy);
1385*67e74705SXin Li }
1386*67e74705SXin Li }
1387*67e74705SXin Li
1388*67e74705SXin Li llvm::DINodeArray
CollectTemplateParams(const TemplateParameterList * TPList,ArrayRef<TemplateArgument> TAList,llvm::DIFile * Unit)1389*67e74705SXin Li CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1390*67e74705SXin Li ArrayRef<TemplateArgument> TAList,
1391*67e74705SXin Li llvm::DIFile *Unit) {
1392*67e74705SXin Li SmallVector<llvm::Metadata *, 16> TemplateParams;
1393*67e74705SXin Li for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1394*67e74705SXin Li const TemplateArgument &TA = TAList[i];
1395*67e74705SXin Li StringRef Name;
1396*67e74705SXin Li if (TPList)
1397*67e74705SXin Li Name = TPList->getParam(i)->getName();
1398*67e74705SXin Li switch (TA.getKind()) {
1399*67e74705SXin Li case TemplateArgument::Type: {
1400*67e74705SXin Li llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
1401*67e74705SXin Li TemplateParams.push_back(
1402*67e74705SXin Li DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
1403*67e74705SXin Li } break;
1404*67e74705SXin Li case TemplateArgument::Integral: {
1405*67e74705SXin Li llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
1406*67e74705SXin Li TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1407*67e74705SXin Li TheCU, Name, TTy,
1408*67e74705SXin Li llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
1409*67e74705SXin Li } break;
1410*67e74705SXin Li case TemplateArgument::Declaration: {
1411*67e74705SXin Li const ValueDecl *D = TA.getAsDecl();
1412*67e74705SXin Li QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
1413*67e74705SXin Li llvm::DIType *TTy = getOrCreateType(T, Unit);
1414*67e74705SXin Li llvm::Constant *V = nullptr;
1415*67e74705SXin Li const CXXMethodDecl *MD;
1416*67e74705SXin Li // Variable pointer template parameters have a value that is the address
1417*67e74705SXin Li // of the variable.
1418*67e74705SXin Li if (const auto *VD = dyn_cast<VarDecl>(D))
1419*67e74705SXin Li V = CGM.GetAddrOfGlobalVar(VD);
1420*67e74705SXin Li // Member function pointers have special support for building them, though
1421*67e74705SXin Li // this is currently unsupported in LLVM CodeGen.
1422*67e74705SXin Li else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
1423*67e74705SXin Li V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
1424*67e74705SXin Li else if (const auto *FD = dyn_cast<FunctionDecl>(D))
1425*67e74705SXin Li V = CGM.GetAddrOfFunction(FD);
1426*67e74705SXin Li // Member data pointers have special handling too to compute the fixed
1427*67e74705SXin Li // offset within the object.
1428*67e74705SXin Li else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
1429*67e74705SXin Li // These five lines (& possibly the above member function pointer
1430*67e74705SXin Li // handling) might be able to be refactored to use similar code in
1431*67e74705SXin Li // CodeGenModule::getMemberPointerConstant
1432*67e74705SXin Li uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1433*67e74705SXin Li CharUnits chars =
1434*67e74705SXin Li CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
1435*67e74705SXin Li V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
1436*67e74705SXin Li }
1437*67e74705SXin Li TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1438*67e74705SXin Li TheCU, Name, TTy,
1439*67e74705SXin Li cast_or_null<llvm::Constant>(V->stripPointerCasts())));
1440*67e74705SXin Li } break;
1441*67e74705SXin Li case TemplateArgument::NullPtr: {
1442*67e74705SXin Li QualType T = TA.getNullPtrType();
1443*67e74705SXin Li llvm::DIType *TTy = getOrCreateType(T, Unit);
1444*67e74705SXin Li llvm::Constant *V = nullptr;
1445*67e74705SXin Li // Special case member data pointer null values since they're actually -1
1446*67e74705SXin Li // instead of zero.
1447*67e74705SXin Li if (const MemberPointerType *MPT =
1448*67e74705SXin Li dyn_cast<MemberPointerType>(T.getTypePtr()))
1449*67e74705SXin Li // But treat member function pointers as simple zero integers because
1450*67e74705SXin Li // it's easier than having a special case in LLVM's CodeGen. If LLVM
1451*67e74705SXin Li // CodeGen grows handling for values of non-null member function
1452*67e74705SXin Li // pointers then perhaps we could remove this special case and rely on
1453*67e74705SXin Li // EmitNullMemberPointer for member function pointers.
1454*67e74705SXin Li if (MPT->isMemberDataPointer())
1455*67e74705SXin Li V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1456*67e74705SXin Li if (!V)
1457*67e74705SXin Li V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
1458*67e74705SXin Li TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1459*67e74705SXin Li TheCU, Name, TTy, cast<llvm::Constant>(V)));
1460*67e74705SXin Li } break;
1461*67e74705SXin Li case TemplateArgument::Template:
1462*67e74705SXin Li TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
1463*67e74705SXin Li TheCU, Name, nullptr,
1464*67e74705SXin Li TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1465*67e74705SXin Li break;
1466*67e74705SXin Li case TemplateArgument::Pack:
1467*67e74705SXin Li TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1468*67e74705SXin Li TheCU, Name, nullptr,
1469*67e74705SXin Li CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1470*67e74705SXin Li break;
1471*67e74705SXin Li case TemplateArgument::Expression: {
1472*67e74705SXin Li const Expr *E = TA.getAsExpr();
1473*67e74705SXin Li QualType T = E->getType();
1474*67e74705SXin Li if (E->isGLValue())
1475*67e74705SXin Li T = CGM.getContext().getLValueReferenceType(T);
1476*67e74705SXin Li llvm::Constant *V = CGM.EmitConstantExpr(E, T);
1477*67e74705SXin Li assert(V && "Expression in template argument isn't constant");
1478*67e74705SXin Li llvm::DIType *TTy = getOrCreateType(T, Unit);
1479*67e74705SXin Li TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1480*67e74705SXin Li TheCU, Name, TTy, cast<llvm::Constant>(V->stripPointerCasts())));
1481*67e74705SXin Li } break;
1482*67e74705SXin Li // And the following should never occur:
1483*67e74705SXin Li case TemplateArgument::TemplateExpansion:
1484*67e74705SXin Li case TemplateArgument::Null:
1485*67e74705SXin Li llvm_unreachable(
1486*67e74705SXin Li "These argument types shouldn't exist in concrete types");
1487*67e74705SXin Li }
1488*67e74705SXin Li }
1489*67e74705SXin Li return DBuilder.getOrCreateArray(TemplateParams);
1490*67e74705SXin Li }
1491*67e74705SXin Li
1492*67e74705SXin Li llvm::DINodeArray
CollectFunctionTemplateParams(const FunctionDecl * FD,llvm::DIFile * Unit)1493*67e74705SXin Li CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
1494*67e74705SXin Li llvm::DIFile *Unit) {
1495*67e74705SXin Li if (FD->getTemplatedKind() ==
1496*67e74705SXin Li FunctionDecl::TK_FunctionTemplateSpecialization) {
1497*67e74705SXin Li const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1498*67e74705SXin Li ->getTemplate()
1499*67e74705SXin Li ->getTemplateParameters();
1500*67e74705SXin Li return CollectTemplateParams(
1501*67e74705SXin Li TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
1502*67e74705SXin Li }
1503*67e74705SXin Li return llvm::DINodeArray();
1504*67e74705SXin Li }
1505*67e74705SXin Li
CollectCXXTemplateParams(const ClassTemplateSpecializationDecl * TSpecial,llvm::DIFile * Unit)1506*67e74705SXin Li llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1507*67e74705SXin Li const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
1508*67e74705SXin Li // Always get the full list of parameters, not just the ones from
1509*67e74705SXin Li // the specialization.
1510*67e74705SXin Li TemplateParameterList *TPList =
1511*67e74705SXin Li TSpecial->getSpecializedTemplate()->getTemplateParameters();
1512*67e74705SXin Li const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
1513*67e74705SXin Li return CollectTemplateParams(TPList, TAList.asArray(), Unit);
1514*67e74705SXin Li }
1515*67e74705SXin Li
getOrCreateVTablePtrType(llvm::DIFile * Unit)1516*67e74705SXin Li llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
1517*67e74705SXin Li if (VTablePtrType)
1518*67e74705SXin Li return VTablePtrType;
1519*67e74705SXin Li
1520*67e74705SXin Li ASTContext &Context = CGM.getContext();
1521*67e74705SXin Li
1522*67e74705SXin Li /* Function type */
1523*67e74705SXin Li llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
1524*67e74705SXin Li llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
1525*67e74705SXin Li llvm::DIType *SubTy = DBuilder.createSubroutineType(SElements);
1526*67e74705SXin Li unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
1527*67e74705SXin Li llvm::DIType *vtbl_ptr_type =
1528*67e74705SXin Li DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
1529*67e74705SXin Li VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1530*67e74705SXin Li return VTablePtrType;
1531*67e74705SXin Li }
1532*67e74705SXin Li
getVTableName(const CXXRecordDecl * RD)1533*67e74705SXin Li StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
1534*67e74705SXin Li // Copy the gdb compatible name on the side and use its reference.
1535*67e74705SXin Li return internString("_vptr$", RD->getNameAsString());
1536*67e74705SXin Li }
1537*67e74705SXin Li
CollectVTableInfo(const CXXRecordDecl * RD,llvm::DIFile * Unit,SmallVectorImpl<llvm::Metadata * > & EltTys)1538*67e74705SXin Li void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
1539*67e74705SXin Li SmallVectorImpl<llvm::Metadata *> &EltTys) {
1540*67e74705SXin Li const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1541*67e74705SXin Li
1542*67e74705SXin Li // If there is a primary base then it will hold vtable info.
1543*67e74705SXin Li if (RL.getPrimaryBase())
1544*67e74705SXin Li return;
1545*67e74705SXin Li
1546*67e74705SXin Li // If this class is not dynamic then there is not any vtable info to collect.
1547*67e74705SXin Li if (!RD->isDynamicClass())
1548*67e74705SXin Li return;
1549*67e74705SXin Li
1550*67e74705SXin Li unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
1551*67e74705SXin Li llvm::DIType *VPTR = DBuilder.createMemberType(
1552*67e74705SXin Li Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
1553*67e74705SXin Li llvm::DINode::FlagArtificial, getOrCreateVTablePtrType(Unit));
1554*67e74705SXin Li EltTys.push_back(VPTR);
1555*67e74705SXin Li }
1556*67e74705SXin Li
getOrCreateRecordType(QualType RTy,SourceLocation Loc)1557*67e74705SXin Li llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
1558*67e74705SXin Li SourceLocation Loc) {
1559*67e74705SXin Li assert(DebugKind >= codegenoptions::LimitedDebugInfo);
1560*67e74705SXin Li llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
1561*67e74705SXin Li return T;
1562*67e74705SXin Li }
1563*67e74705SXin Li
getOrCreateInterfaceType(QualType D,SourceLocation Loc)1564*67e74705SXin Li llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
1565*67e74705SXin Li SourceLocation Loc) {
1566*67e74705SXin Li return getOrCreateStandaloneType(D, Loc);
1567*67e74705SXin Li }
1568*67e74705SXin Li
getOrCreateStandaloneType(QualType D,SourceLocation Loc)1569*67e74705SXin Li llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
1570*67e74705SXin Li SourceLocation Loc) {
1571*67e74705SXin Li assert(DebugKind >= codegenoptions::LimitedDebugInfo);
1572*67e74705SXin Li assert(!D.isNull() && "null type");
1573*67e74705SXin Li llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
1574*67e74705SXin Li assert(T && "could not create debug info for type");
1575*67e74705SXin Li
1576*67e74705SXin Li RetainedTypes.push_back(D.getAsOpaquePtr());
1577*67e74705SXin Li return T;
1578*67e74705SXin Li }
1579*67e74705SXin Li
completeType(const EnumDecl * ED)1580*67e74705SXin Li void CGDebugInfo::completeType(const EnumDecl *ED) {
1581*67e74705SXin Li if (DebugKind <= codegenoptions::DebugLineTablesOnly)
1582*67e74705SXin Li return;
1583*67e74705SXin Li QualType Ty = CGM.getContext().getEnumType(ED);
1584*67e74705SXin Li void *TyPtr = Ty.getAsOpaquePtr();
1585*67e74705SXin Li auto I = TypeCache.find(TyPtr);
1586*67e74705SXin Li if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
1587*67e74705SXin Li return;
1588*67e74705SXin Li llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
1589*67e74705SXin Li assert(!Res->isForwardDecl());
1590*67e74705SXin Li TypeCache[TyPtr].reset(Res);
1591*67e74705SXin Li }
1592*67e74705SXin Li
completeType(const RecordDecl * RD)1593*67e74705SXin Li void CGDebugInfo::completeType(const RecordDecl *RD) {
1594*67e74705SXin Li if (DebugKind > codegenoptions::LimitedDebugInfo ||
1595*67e74705SXin Li !CGM.getLangOpts().CPlusPlus)
1596*67e74705SXin Li completeRequiredType(RD);
1597*67e74705SXin Li }
1598*67e74705SXin Li
completeRequiredType(const RecordDecl * RD)1599*67e74705SXin Li void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
1600*67e74705SXin Li if (DebugKind <= codegenoptions::DebugLineTablesOnly)
1601*67e74705SXin Li return;
1602*67e74705SXin Li
1603*67e74705SXin Li if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1604*67e74705SXin Li if (CXXDecl->isDynamicClass())
1605*67e74705SXin Li return;
1606*67e74705SXin Li
1607*67e74705SXin Li if (DebugTypeExtRefs && RD->isFromASTFile())
1608*67e74705SXin Li return;
1609*67e74705SXin Li
1610*67e74705SXin Li QualType Ty = CGM.getContext().getRecordType(RD);
1611*67e74705SXin Li llvm::DIType *T = getTypeOrNull(Ty);
1612*67e74705SXin Li if (T && T->isForwardDecl())
1613*67e74705SXin Li completeClassData(RD);
1614*67e74705SXin Li }
1615*67e74705SXin Li
completeClassData(const RecordDecl * RD)1616*67e74705SXin Li void CGDebugInfo::completeClassData(const RecordDecl *RD) {
1617*67e74705SXin Li if (DebugKind <= codegenoptions::DebugLineTablesOnly)
1618*67e74705SXin Li return;
1619*67e74705SXin Li QualType Ty = CGM.getContext().getRecordType(RD);
1620*67e74705SXin Li void *TyPtr = Ty.getAsOpaquePtr();
1621*67e74705SXin Li auto I = TypeCache.find(TyPtr);
1622*67e74705SXin Li if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
1623*67e74705SXin Li return;
1624*67e74705SXin Li llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
1625*67e74705SXin Li assert(!Res->isForwardDecl());
1626*67e74705SXin Li TypeCache[TyPtr].reset(Res);
1627*67e74705SXin Li }
1628*67e74705SXin Li
hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,CXXRecordDecl::method_iterator End)1629*67e74705SXin Li static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1630*67e74705SXin Li CXXRecordDecl::method_iterator End) {
1631*67e74705SXin Li for (; I != End; ++I)
1632*67e74705SXin Li if (FunctionDecl *Tmpl = I->getInstantiatedFromMemberFunction())
1633*67e74705SXin Li if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
1634*67e74705SXin Li !I->getMemberSpecializationInfo()->isExplicitSpecialization())
1635*67e74705SXin Li return true;
1636*67e74705SXin Li return false;
1637*67e74705SXin Li }
1638*67e74705SXin Li
1639*67e74705SXin Li /// Does a type definition exist in an imported clang module?
isDefinedInClangModule(const RecordDecl * RD)1640*67e74705SXin Li static bool isDefinedInClangModule(const RecordDecl *RD) {
1641*67e74705SXin Li if (!RD || !RD->isFromASTFile())
1642*67e74705SXin Li return false;
1643*67e74705SXin Li if (!RD->isExternallyVisible() && RD->getName().empty())
1644*67e74705SXin Li return false;
1645*67e74705SXin Li if (auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) {
1646*67e74705SXin Li assert(CXXDecl->isCompleteDefinition() && "incomplete record definition");
1647*67e74705SXin Li if (CXXDecl->getTemplateSpecializationKind() != TSK_Undeclared)
1648*67e74705SXin Li // Make sure the instantiation is actually in a module.
1649*67e74705SXin Li if (CXXDecl->field_begin() != CXXDecl->field_end())
1650*67e74705SXin Li return CXXDecl->field_begin()->isFromASTFile();
1651*67e74705SXin Li }
1652*67e74705SXin Li
1653*67e74705SXin Li return true;
1654*67e74705SXin Li }
1655*67e74705SXin Li
shouldOmitDefinition(codegenoptions::DebugInfoKind DebugKind,bool DebugTypeExtRefs,const RecordDecl * RD,const LangOptions & LangOpts)1656*67e74705SXin Li static bool shouldOmitDefinition(codegenoptions::DebugInfoKind DebugKind,
1657*67e74705SXin Li bool DebugTypeExtRefs, const RecordDecl *RD,
1658*67e74705SXin Li const LangOptions &LangOpts) {
1659*67e74705SXin Li if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
1660*67e74705SXin Li return true;
1661*67e74705SXin Li
1662*67e74705SXin Li if (DebugKind > codegenoptions::LimitedDebugInfo)
1663*67e74705SXin Li return false;
1664*67e74705SXin Li
1665*67e74705SXin Li if (!LangOpts.CPlusPlus)
1666*67e74705SXin Li return false;
1667*67e74705SXin Li
1668*67e74705SXin Li if (!RD->isCompleteDefinitionRequired())
1669*67e74705SXin Li return true;
1670*67e74705SXin Li
1671*67e74705SXin Li const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1672*67e74705SXin Li
1673*67e74705SXin Li if (!CXXDecl)
1674*67e74705SXin Li return false;
1675*67e74705SXin Li
1676*67e74705SXin Li if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass())
1677*67e74705SXin Li return true;
1678*67e74705SXin Li
1679*67e74705SXin Li TemplateSpecializationKind Spec = TSK_Undeclared;
1680*67e74705SXin Li if (const ClassTemplateSpecializationDecl *SD =
1681*67e74705SXin Li dyn_cast<ClassTemplateSpecializationDecl>(RD))
1682*67e74705SXin Li Spec = SD->getSpecializationKind();
1683*67e74705SXin Li
1684*67e74705SXin Li if (Spec == TSK_ExplicitInstantiationDeclaration &&
1685*67e74705SXin Li hasExplicitMemberDefinition(CXXDecl->method_begin(),
1686*67e74705SXin Li CXXDecl->method_end()))
1687*67e74705SXin Li return true;
1688*67e74705SXin Li
1689*67e74705SXin Li return false;
1690*67e74705SXin Li }
1691*67e74705SXin Li
CreateType(const RecordType * Ty)1692*67e74705SXin Li llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
1693*67e74705SXin Li RecordDecl *RD = Ty->getDecl();
1694*67e74705SXin Li llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
1695*67e74705SXin Li if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD,
1696*67e74705SXin Li CGM.getLangOpts())) {
1697*67e74705SXin Li if (!T)
1698*67e74705SXin Li T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD));
1699*67e74705SXin Li return T;
1700*67e74705SXin Li }
1701*67e74705SXin Li
1702*67e74705SXin Li return CreateTypeDefinition(Ty);
1703*67e74705SXin Li }
1704*67e74705SXin Li
CreateTypeDefinition(const RecordType * Ty)1705*67e74705SXin Li llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
1706*67e74705SXin Li RecordDecl *RD = Ty->getDecl();
1707*67e74705SXin Li
1708*67e74705SXin Li // Get overall information about the record type for the debug info.
1709*67e74705SXin Li llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
1710*67e74705SXin Li
1711*67e74705SXin Li // Records and classes and unions can all be recursive. To handle them, we
1712*67e74705SXin Li // first generate a debug descriptor for the struct as a forward declaration.
1713*67e74705SXin Li // Then (if it is a definition) we go through and get debug info for all of
1714*67e74705SXin Li // its members. Finally, we create a descriptor for the complete type (which
1715*67e74705SXin Li // may refer to the forward decl if the struct is recursive) and replace all
1716*67e74705SXin Li // uses of the forward declaration with the final definition.
1717*67e74705SXin Li llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit);
1718*67e74705SXin Li
1719*67e74705SXin Li const RecordDecl *D = RD->getDefinition();
1720*67e74705SXin Li if (!D || !D->isCompleteDefinition())
1721*67e74705SXin Li return FwdDecl;
1722*67e74705SXin Li
1723*67e74705SXin Li if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1724*67e74705SXin Li CollectContainingType(CXXDecl, FwdDecl);
1725*67e74705SXin Li
1726*67e74705SXin Li // Push the struct on region stack.
1727*67e74705SXin Li LexicalBlockStack.emplace_back(&*FwdDecl);
1728*67e74705SXin Li RegionMap[Ty->getDecl()].reset(FwdDecl);
1729*67e74705SXin Li
1730*67e74705SXin Li // Convert all the elements.
1731*67e74705SXin Li SmallVector<llvm::Metadata *, 16> EltTys;
1732*67e74705SXin Li // what about nested types?
1733*67e74705SXin Li
1734*67e74705SXin Li // Note: The split of CXXDecl information here is intentional, the
1735*67e74705SXin Li // gdb tests will depend on a certain ordering at printout. The debug
1736*67e74705SXin Li // information offsets are still correct if we merge them all together
1737*67e74705SXin Li // though.
1738*67e74705SXin Li const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1739*67e74705SXin Li if (CXXDecl) {
1740*67e74705SXin Li CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1741*67e74705SXin Li CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1742*67e74705SXin Li }
1743*67e74705SXin Li
1744*67e74705SXin Li // Collect data fields (including static variables and any initializers).
1745*67e74705SXin Li CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
1746*67e74705SXin Li if (CXXDecl)
1747*67e74705SXin Li CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
1748*67e74705SXin Li
1749*67e74705SXin Li LexicalBlockStack.pop_back();
1750*67e74705SXin Li RegionMap.erase(Ty->getDecl());
1751*67e74705SXin Li
1752*67e74705SXin Li llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
1753*67e74705SXin Li DBuilder.replaceArrays(FwdDecl, Elements);
1754*67e74705SXin Li
1755*67e74705SXin Li if (FwdDecl->isTemporary())
1756*67e74705SXin Li FwdDecl =
1757*67e74705SXin Li llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
1758*67e74705SXin Li
1759*67e74705SXin Li RegionMap[Ty->getDecl()].reset(FwdDecl);
1760*67e74705SXin Li return FwdDecl;
1761*67e74705SXin Li }
1762*67e74705SXin Li
CreateType(const ObjCObjectType * Ty,llvm::DIFile * Unit)1763*67e74705SXin Li llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1764*67e74705SXin Li llvm::DIFile *Unit) {
1765*67e74705SXin Li // Ignore protocols.
1766*67e74705SXin Li return getOrCreateType(Ty->getBaseType(), Unit);
1767*67e74705SXin Li }
1768*67e74705SXin Li
1769*67e74705SXin Li /// \return true if Getter has the default name for the property PD.
hasDefaultGetterName(const ObjCPropertyDecl * PD,const ObjCMethodDecl * Getter)1770*67e74705SXin Li static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1771*67e74705SXin Li const ObjCMethodDecl *Getter) {
1772*67e74705SXin Li assert(PD);
1773*67e74705SXin Li if (!Getter)
1774*67e74705SXin Li return true;
1775*67e74705SXin Li
1776*67e74705SXin Li assert(Getter->getDeclName().isObjCZeroArgSelector());
1777*67e74705SXin Li return PD->getName() ==
1778*67e74705SXin Li Getter->getDeclName().getObjCSelector().getNameForSlot(0);
1779*67e74705SXin Li }
1780*67e74705SXin Li
1781*67e74705SXin Li /// \return true if Setter has the default name for the property PD.
hasDefaultSetterName(const ObjCPropertyDecl * PD,const ObjCMethodDecl * Setter)1782*67e74705SXin Li static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1783*67e74705SXin Li const ObjCMethodDecl *Setter) {
1784*67e74705SXin Li assert(PD);
1785*67e74705SXin Li if (!Setter)
1786*67e74705SXin Li return true;
1787*67e74705SXin Li
1788*67e74705SXin Li assert(Setter->getDeclName().isObjCOneArgSelector());
1789*67e74705SXin Li return SelectorTable::constructSetterName(PD->getName()) ==
1790*67e74705SXin Li Setter->getDeclName().getObjCSelector().getNameForSlot(0);
1791*67e74705SXin Li }
1792*67e74705SXin Li
CreateType(const ObjCInterfaceType * Ty,llvm::DIFile * Unit)1793*67e74705SXin Li llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1794*67e74705SXin Li llvm::DIFile *Unit) {
1795*67e74705SXin Li ObjCInterfaceDecl *ID = Ty->getDecl();
1796*67e74705SXin Li if (!ID)
1797*67e74705SXin Li return nullptr;
1798*67e74705SXin Li
1799*67e74705SXin Li // Return a forward declaration if this type was imported from a clang module,
1800*67e74705SXin Li // and this is not the compile unit with the implementation of the type (which
1801*67e74705SXin Li // may contain hidden ivars).
1802*67e74705SXin Li if (DebugTypeExtRefs && ID->isFromASTFile() && ID->getDefinition() &&
1803*67e74705SXin Li !ID->getImplementation())
1804*67e74705SXin Li return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
1805*67e74705SXin Li ID->getName(),
1806*67e74705SXin Li getDeclContextDescriptor(ID), Unit, 0);
1807*67e74705SXin Li
1808*67e74705SXin Li // Get overall information about the record type for the debug info.
1809*67e74705SXin Li llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
1810*67e74705SXin Li unsigned Line = getLineNumber(ID->getLocation());
1811*67e74705SXin Li auto RuntimeLang =
1812*67e74705SXin Li static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
1813*67e74705SXin Li
1814*67e74705SXin Li // If this is just a forward declaration return a special forward-declaration
1815*67e74705SXin Li // debug type since we won't be able to lay out the entire type.
1816*67e74705SXin Li ObjCInterfaceDecl *Def = ID->getDefinition();
1817*67e74705SXin Li if (!Def || !Def->getImplementation()) {
1818*67e74705SXin Li llvm::DIScope *Mod = getParentModuleOrNull(ID);
1819*67e74705SXin Li llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
1820*67e74705SXin Li llvm::dwarf::DW_TAG_structure_type, ID->getName(), Mod ? Mod : TheCU,
1821*67e74705SXin Li DefUnit, Line, RuntimeLang);
1822*67e74705SXin Li ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
1823*67e74705SXin Li return FwdDecl;
1824*67e74705SXin Li }
1825*67e74705SXin Li
1826*67e74705SXin Li return CreateTypeDefinition(Ty, Unit);
1827*67e74705SXin Li }
1828*67e74705SXin Li
1829*67e74705SXin Li llvm::DIModule *
getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod,bool CreateSkeletonCU)1830*67e74705SXin Li CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod,
1831*67e74705SXin Li bool CreateSkeletonCU) {
1832*67e74705SXin Li // Use the Module pointer as the key into the cache. This is a
1833*67e74705SXin Li // nullptr if the "Module" is a PCH, which is safe because we don't
1834*67e74705SXin Li // support chained PCH debug info, so there can only be a single PCH.
1835*67e74705SXin Li const Module *M = Mod.getModuleOrNull();
1836*67e74705SXin Li auto ModRef = ModuleCache.find(M);
1837*67e74705SXin Li if (ModRef != ModuleCache.end())
1838*67e74705SXin Li return cast<llvm::DIModule>(ModRef->second);
1839*67e74705SXin Li
1840*67e74705SXin Li // Macro definitions that were defined with "-D" on the command line.
1841*67e74705SXin Li SmallString<128> ConfigMacros;
1842*67e74705SXin Li {
1843*67e74705SXin Li llvm::raw_svector_ostream OS(ConfigMacros);
1844*67e74705SXin Li const auto &PPOpts = CGM.getPreprocessorOpts();
1845*67e74705SXin Li unsigned I = 0;
1846*67e74705SXin Li // Translate the macro definitions back into a commmand line.
1847*67e74705SXin Li for (auto &M : PPOpts.Macros) {
1848*67e74705SXin Li if (++I > 1)
1849*67e74705SXin Li OS << " ";
1850*67e74705SXin Li const std::string &Macro = M.first;
1851*67e74705SXin Li bool Undef = M.second;
1852*67e74705SXin Li OS << "\"-" << (Undef ? 'U' : 'D');
1853*67e74705SXin Li for (char c : Macro)
1854*67e74705SXin Li switch (c) {
1855*67e74705SXin Li case '\\' : OS << "\\\\"; break;
1856*67e74705SXin Li case '"' : OS << "\\\""; break;
1857*67e74705SXin Li default: OS << c;
1858*67e74705SXin Li }
1859*67e74705SXin Li OS << '\"';
1860*67e74705SXin Li }
1861*67e74705SXin Li }
1862*67e74705SXin Li
1863*67e74705SXin Li bool IsRootModule = M ? !M->Parent : true;
1864*67e74705SXin Li if (CreateSkeletonCU && IsRootModule) {
1865*67e74705SXin Li // PCH files don't have a signature field in the control block,
1866*67e74705SXin Li // but LLVM detects skeleton CUs by looking for a non-zero DWO id.
1867*67e74705SXin Li uint64_t Signature = Mod.getSignature() ? Mod.getSignature() : ~1ULL;
1868*67e74705SXin Li llvm::DIBuilder DIB(CGM.getModule());
1869*67e74705SXin Li DIB.createCompileUnit(TheCU->getSourceLanguage(), Mod.getModuleName(),
1870*67e74705SXin Li Mod.getPath(), TheCU->getProducer(), true,
1871*67e74705SXin Li StringRef(), 0, Mod.getASTFile(),
1872*67e74705SXin Li llvm::DICompileUnit::FullDebug, Signature);
1873*67e74705SXin Li DIB.finalize();
1874*67e74705SXin Li }
1875*67e74705SXin Li llvm::DIModule *Parent =
1876*67e74705SXin Li IsRootModule ? nullptr
1877*67e74705SXin Li : getOrCreateModuleRef(
1878*67e74705SXin Li ExternalASTSource::ASTSourceDescriptor(*M->Parent),
1879*67e74705SXin Li CreateSkeletonCU);
1880*67e74705SXin Li llvm::DIModule *DIMod =
1881*67e74705SXin Li DBuilder.createModule(Parent, Mod.getModuleName(), ConfigMacros,
1882*67e74705SXin Li Mod.getPath(), CGM.getHeaderSearchOpts().Sysroot);
1883*67e74705SXin Li ModuleCache[M].reset(DIMod);
1884*67e74705SXin Li return DIMod;
1885*67e74705SXin Li }
1886*67e74705SXin Li
CreateTypeDefinition(const ObjCInterfaceType * Ty,llvm::DIFile * Unit)1887*67e74705SXin Li llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
1888*67e74705SXin Li llvm::DIFile *Unit) {
1889*67e74705SXin Li ObjCInterfaceDecl *ID = Ty->getDecl();
1890*67e74705SXin Li llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
1891*67e74705SXin Li unsigned Line = getLineNumber(ID->getLocation());
1892*67e74705SXin Li unsigned RuntimeLang = TheCU->getSourceLanguage();
1893*67e74705SXin Li
1894*67e74705SXin Li // Bit size, align and offset of the type.
1895*67e74705SXin Li uint64_t Size = CGM.getContext().getTypeSize(Ty);
1896*67e74705SXin Li uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1897*67e74705SXin Li
1898*67e74705SXin Li unsigned Flags = 0;
1899*67e74705SXin Li if (ID->getImplementation())
1900*67e74705SXin Li Flags |= llvm::DINode::FlagObjcClassComplete;
1901*67e74705SXin Li
1902*67e74705SXin Li llvm::DIScope *Mod = getParentModuleOrNull(ID);
1903*67e74705SXin Li llvm::DICompositeType *RealDecl = DBuilder.createStructType(
1904*67e74705SXin Li Mod ? Mod : Unit, ID->getName(), DefUnit, Line, Size, Align, Flags,
1905*67e74705SXin Li nullptr, llvm::DINodeArray(), RuntimeLang);
1906*67e74705SXin Li
1907*67e74705SXin Li QualType QTy(Ty, 0);
1908*67e74705SXin Li TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
1909*67e74705SXin Li
1910*67e74705SXin Li // Push the struct on region stack.
1911*67e74705SXin Li LexicalBlockStack.emplace_back(RealDecl);
1912*67e74705SXin Li RegionMap[Ty->getDecl()].reset(RealDecl);
1913*67e74705SXin Li
1914*67e74705SXin Li // Convert all the elements.
1915*67e74705SXin Li SmallVector<llvm::Metadata *, 16> EltTys;
1916*67e74705SXin Li
1917*67e74705SXin Li ObjCInterfaceDecl *SClass = ID->getSuperClass();
1918*67e74705SXin Li if (SClass) {
1919*67e74705SXin Li llvm::DIType *SClassTy =
1920*67e74705SXin Li getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
1921*67e74705SXin Li if (!SClassTy)
1922*67e74705SXin Li return nullptr;
1923*67e74705SXin Li
1924*67e74705SXin Li llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
1925*67e74705SXin Li EltTys.push_back(InhTag);
1926*67e74705SXin Li }
1927*67e74705SXin Li
1928*67e74705SXin Li // Create entries for all of the properties.
1929*67e74705SXin Li auto AddProperty = [&](const ObjCPropertyDecl *PD) {
1930*67e74705SXin Li SourceLocation Loc = PD->getLocation();
1931*67e74705SXin Li llvm::DIFile *PUnit = getOrCreateFile(Loc);
1932*67e74705SXin Li unsigned PLine = getLineNumber(Loc);
1933*67e74705SXin Li ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1934*67e74705SXin Li ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
1935*67e74705SXin Li llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
1936*67e74705SXin Li PD->getName(), PUnit, PLine,
1937*67e74705SXin Li hasDefaultGetterName(PD, Getter) ? ""
1938*67e74705SXin Li : getSelectorName(PD->getGetterName()),
1939*67e74705SXin Li hasDefaultSetterName(PD, Setter) ? ""
1940*67e74705SXin Li : getSelectorName(PD->getSetterName()),
1941*67e74705SXin Li PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
1942*67e74705SXin Li EltTys.push_back(PropertyNode);
1943*67e74705SXin Li };
1944*67e74705SXin Li {
1945*67e74705SXin Li llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
1946*67e74705SXin Li for (const ObjCCategoryDecl *ClassExt : ID->known_extensions())
1947*67e74705SXin Li for (auto *PD : ClassExt->properties()) {
1948*67e74705SXin Li PropertySet.insert(PD->getIdentifier());
1949*67e74705SXin Li AddProperty(PD);
1950*67e74705SXin Li }
1951*67e74705SXin Li for (const auto *PD : ID->properties()) {
1952*67e74705SXin Li // Don't emit duplicate metadata for properties that were already in a
1953*67e74705SXin Li // class extension.
1954*67e74705SXin Li if (!PropertySet.insert(PD->getIdentifier()).second)
1955*67e74705SXin Li continue;
1956*67e74705SXin Li AddProperty(PD);
1957*67e74705SXin Li }
1958*67e74705SXin Li }
1959*67e74705SXin Li
1960*67e74705SXin Li const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1961*67e74705SXin Li unsigned FieldNo = 0;
1962*67e74705SXin Li for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1963*67e74705SXin Li Field = Field->getNextIvar(), ++FieldNo) {
1964*67e74705SXin Li llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
1965*67e74705SXin Li if (!FieldTy)
1966*67e74705SXin Li return nullptr;
1967*67e74705SXin Li
1968*67e74705SXin Li StringRef FieldName = Field->getName();
1969*67e74705SXin Li
1970*67e74705SXin Li // Ignore unnamed fields.
1971*67e74705SXin Li if (FieldName.empty())
1972*67e74705SXin Li continue;
1973*67e74705SXin Li
1974*67e74705SXin Li // Get the location for the field.
1975*67e74705SXin Li llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
1976*67e74705SXin Li unsigned FieldLine = getLineNumber(Field->getLocation());
1977*67e74705SXin Li QualType FType = Field->getType();
1978*67e74705SXin Li uint64_t FieldSize = 0;
1979*67e74705SXin Li unsigned FieldAlign = 0;
1980*67e74705SXin Li
1981*67e74705SXin Li if (!FType->isIncompleteArrayType()) {
1982*67e74705SXin Li
1983*67e74705SXin Li // Bit size, align and offset of the type.
1984*67e74705SXin Li FieldSize = Field->isBitField()
1985*67e74705SXin Li ? Field->getBitWidthValue(CGM.getContext())
1986*67e74705SXin Li : CGM.getContext().getTypeSize(FType);
1987*67e74705SXin Li FieldAlign = CGM.getContext().getTypeAlign(FType);
1988*67e74705SXin Li }
1989*67e74705SXin Li
1990*67e74705SXin Li uint64_t FieldOffset;
1991*67e74705SXin Li if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1992*67e74705SXin Li // We don't know the runtime offset of an ivar if we're using the
1993*67e74705SXin Li // non-fragile ABI. For bitfields, use the bit offset into the first
1994*67e74705SXin Li // byte of storage of the bitfield. For other fields, use zero.
1995*67e74705SXin Li if (Field->isBitField()) {
1996*67e74705SXin Li FieldOffset =
1997*67e74705SXin Li CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
1998*67e74705SXin Li FieldOffset %= CGM.getContext().getCharWidth();
1999*67e74705SXin Li } else {
2000*67e74705SXin Li FieldOffset = 0;
2001*67e74705SXin Li }
2002*67e74705SXin Li } else {
2003*67e74705SXin Li FieldOffset = RL.getFieldOffset(FieldNo);
2004*67e74705SXin Li }
2005*67e74705SXin Li
2006*67e74705SXin Li unsigned Flags = 0;
2007*67e74705SXin Li if (Field->getAccessControl() == ObjCIvarDecl::Protected)
2008*67e74705SXin Li Flags = llvm::DINode::FlagProtected;
2009*67e74705SXin Li else if (Field->getAccessControl() == ObjCIvarDecl::Private)
2010*67e74705SXin Li Flags = llvm::DINode::FlagPrivate;
2011*67e74705SXin Li else if (Field->getAccessControl() == ObjCIvarDecl::Public)
2012*67e74705SXin Li Flags = llvm::DINode::FlagPublic;
2013*67e74705SXin Li
2014*67e74705SXin Li llvm::MDNode *PropertyNode = nullptr;
2015*67e74705SXin Li if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
2016*67e74705SXin Li if (ObjCPropertyImplDecl *PImpD =
2017*67e74705SXin Li ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
2018*67e74705SXin Li if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
2019*67e74705SXin Li SourceLocation Loc = PD->getLocation();
2020*67e74705SXin Li llvm::DIFile *PUnit = getOrCreateFile(Loc);
2021*67e74705SXin Li unsigned PLine = getLineNumber(Loc);
2022*67e74705SXin Li ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
2023*67e74705SXin Li ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
2024*67e74705SXin Li PropertyNode = DBuilder.createObjCProperty(
2025*67e74705SXin Li PD->getName(), PUnit, PLine,
2026*67e74705SXin Li hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
2027*67e74705SXin Li PD->getGetterName()),
2028*67e74705SXin Li hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
2029*67e74705SXin Li PD->getSetterName()),
2030*67e74705SXin Li PD->getPropertyAttributes(),
2031*67e74705SXin Li getOrCreateType(PD->getType(), PUnit));
2032*67e74705SXin Li }
2033*67e74705SXin Li }
2034*67e74705SXin Li }
2035*67e74705SXin Li FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
2036*67e74705SXin Li FieldSize, FieldAlign, FieldOffset, Flags,
2037*67e74705SXin Li FieldTy, PropertyNode);
2038*67e74705SXin Li EltTys.push_back(FieldTy);
2039*67e74705SXin Li }
2040*67e74705SXin Li
2041*67e74705SXin Li llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
2042*67e74705SXin Li DBuilder.replaceArrays(RealDecl, Elements);
2043*67e74705SXin Li
2044*67e74705SXin Li LexicalBlockStack.pop_back();
2045*67e74705SXin Li return RealDecl;
2046*67e74705SXin Li }
2047*67e74705SXin Li
CreateType(const VectorType * Ty,llvm::DIFile * Unit)2048*67e74705SXin Li llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
2049*67e74705SXin Li llvm::DIFile *Unit) {
2050*67e74705SXin Li llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
2051*67e74705SXin Li int64_t Count = Ty->getNumElements();
2052*67e74705SXin Li if (Count == 0)
2053*67e74705SXin Li // If number of elements are not known then this is an unbounded array.
2054*67e74705SXin Li // Use Count == -1 to express such arrays.
2055*67e74705SXin Li Count = -1;
2056*67e74705SXin Li
2057*67e74705SXin Li llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
2058*67e74705SXin Li llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
2059*67e74705SXin Li
2060*67e74705SXin Li uint64_t Size = CGM.getContext().getTypeSize(Ty);
2061*67e74705SXin Li uint64_t Align = CGM.getContext().getTypeAlign(Ty);
2062*67e74705SXin Li
2063*67e74705SXin Li return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
2064*67e74705SXin Li }
2065*67e74705SXin Li
CreateType(const ArrayType * Ty,llvm::DIFile * Unit)2066*67e74705SXin Li llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
2067*67e74705SXin Li uint64_t Size;
2068*67e74705SXin Li uint64_t Align;
2069*67e74705SXin Li
2070*67e74705SXin Li // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
2071*67e74705SXin Li if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
2072*67e74705SXin Li Size = 0;
2073*67e74705SXin Li Align =
2074*67e74705SXin Li CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
2075*67e74705SXin Li } else if (Ty->isIncompleteArrayType()) {
2076*67e74705SXin Li Size = 0;
2077*67e74705SXin Li if (Ty->getElementType()->isIncompleteType())
2078*67e74705SXin Li Align = 0;
2079*67e74705SXin Li else
2080*67e74705SXin Li Align = CGM.getContext().getTypeAlign(Ty->getElementType());
2081*67e74705SXin Li } else if (Ty->isIncompleteType()) {
2082*67e74705SXin Li Size = 0;
2083*67e74705SXin Li Align = 0;
2084*67e74705SXin Li } else {
2085*67e74705SXin Li // Size and align of the whole array, not the element type.
2086*67e74705SXin Li Size = CGM.getContext().getTypeSize(Ty);
2087*67e74705SXin Li Align = CGM.getContext().getTypeAlign(Ty);
2088*67e74705SXin Li }
2089*67e74705SXin Li
2090*67e74705SXin Li // Add the dimensions of the array. FIXME: This loses CV qualifiers from
2091*67e74705SXin Li // interior arrays, do we care? Why aren't nested arrays represented the
2092*67e74705SXin Li // obvious/recursive way?
2093*67e74705SXin Li SmallVector<llvm::Metadata *, 8> Subscripts;
2094*67e74705SXin Li QualType EltTy(Ty, 0);
2095*67e74705SXin Li while ((Ty = dyn_cast<ArrayType>(EltTy))) {
2096*67e74705SXin Li // If the number of elements is known, then count is that number. Otherwise,
2097*67e74705SXin Li // it's -1. This allows us to represent a subrange with an array of 0
2098*67e74705SXin Li // elements, like this:
2099*67e74705SXin Li //
2100*67e74705SXin Li // struct foo {
2101*67e74705SXin Li // int x[0];
2102*67e74705SXin Li // };
2103*67e74705SXin Li int64_t Count = -1; // Count == -1 is an unbounded array.
2104*67e74705SXin Li if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
2105*67e74705SXin Li Count = CAT->getSize().getZExtValue();
2106*67e74705SXin Li
2107*67e74705SXin Li // FIXME: Verify this is right for VLAs.
2108*67e74705SXin Li Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
2109*67e74705SXin Li EltTy = Ty->getElementType();
2110*67e74705SXin Li }
2111*67e74705SXin Li
2112*67e74705SXin Li llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
2113*67e74705SXin Li
2114*67e74705SXin Li return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
2115*67e74705SXin Li SubscriptArray);
2116*67e74705SXin Li }
2117*67e74705SXin Li
CreateType(const LValueReferenceType * Ty,llvm::DIFile * Unit)2118*67e74705SXin Li llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
2119*67e74705SXin Li llvm::DIFile *Unit) {
2120*67e74705SXin Li return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
2121*67e74705SXin Li Ty->getPointeeType(), Unit);
2122*67e74705SXin Li }
2123*67e74705SXin Li
CreateType(const RValueReferenceType * Ty,llvm::DIFile * Unit)2124*67e74705SXin Li llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
2125*67e74705SXin Li llvm::DIFile *Unit) {
2126*67e74705SXin Li return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
2127*67e74705SXin Li Ty->getPointeeType(), Unit);
2128*67e74705SXin Li }
2129*67e74705SXin Li
CreateType(const MemberPointerType * Ty,llvm::DIFile * U)2130*67e74705SXin Li llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
2131*67e74705SXin Li llvm::DIFile *U) {
2132*67e74705SXin Li unsigned Flags = 0;
2133*67e74705SXin Li uint64_t Size = 0;
2134*67e74705SXin Li
2135*67e74705SXin Li if (!Ty->isIncompleteType()) {
2136*67e74705SXin Li Size = CGM.getContext().getTypeSize(Ty);
2137*67e74705SXin Li
2138*67e74705SXin Li // Set the MS inheritance model. There is no flag for the unspecified model.
2139*67e74705SXin Li if (CGM.getTarget().getCXXABI().isMicrosoft()) {
2140*67e74705SXin Li switch (Ty->getMostRecentCXXRecordDecl()->getMSInheritanceModel()) {
2141*67e74705SXin Li case MSInheritanceAttr::Keyword_single_inheritance:
2142*67e74705SXin Li Flags |= llvm::DINode::FlagSingleInheritance;
2143*67e74705SXin Li break;
2144*67e74705SXin Li case MSInheritanceAttr::Keyword_multiple_inheritance:
2145*67e74705SXin Li Flags |= llvm::DINode::FlagMultipleInheritance;
2146*67e74705SXin Li break;
2147*67e74705SXin Li case MSInheritanceAttr::Keyword_virtual_inheritance:
2148*67e74705SXin Li Flags |= llvm::DINode::FlagVirtualInheritance;
2149*67e74705SXin Li break;
2150*67e74705SXin Li case MSInheritanceAttr::Keyword_unspecified_inheritance:
2151*67e74705SXin Li break;
2152*67e74705SXin Li }
2153*67e74705SXin Li }
2154*67e74705SXin Li }
2155*67e74705SXin Li
2156*67e74705SXin Li llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
2157*67e74705SXin Li if (Ty->isMemberDataPointerType())
2158*67e74705SXin Li return DBuilder.createMemberPointerType(
2159*67e74705SXin Li getOrCreateType(Ty->getPointeeType(), U), ClassType, Size, /*Align=*/0,
2160*67e74705SXin Li Flags);
2161*67e74705SXin Li
2162*67e74705SXin Li const FunctionProtoType *FPT =
2163*67e74705SXin Li Ty->getPointeeType()->getAs<FunctionProtoType>();
2164*67e74705SXin Li return DBuilder.createMemberPointerType(
2165*67e74705SXin Li getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
2166*67e74705SXin Li Ty->getClass(), FPT->getTypeQuals())),
2167*67e74705SXin Li FPT, U),
2168*67e74705SXin Li ClassType, Size, /*Align=*/0, Flags);
2169*67e74705SXin Li }
2170*67e74705SXin Li
CreateType(const AtomicType * Ty,llvm::DIFile * U)2171*67e74705SXin Li llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
2172*67e74705SXin Li // Ignore the atomic wrapping
2173*67e74705SXin Li // FIXME: What is the correct representation?
2174*67e74705SXin Li return getOrCreateType(Ty->getValueType(), U);
2175*67e74705SXin Li }
2176*67e74705SXin Li
CreateType(const PipeType * Ty,llvm::DIFile * U)2177*67e74705SXin Li llvm::DIType* CGDebugInfo::CreateType(const PipeType *Ty,
2178*67e74705SXin Li llvm::DIFile *U) {
2179*67e74705SXin Li return getOrCreateType(Ty->getElementType(), U);
2180*67e74705SXin Li }
2181*67e74705SXin Li
CreateEnumType(const EnumType * Ty)2182*67e74705SXin Li llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
2183*67e74705SXin Li const EnumDecl *ED = Ty->getDecl();
2184*67e74705SXin Li
2185*67e74705SXin Li uint64_t Size = 0;
2186*67e74705SXin Li uint64_t Align = 0;
2187*67e74705SXin Li if (!ED->getTypeForDecl()->isIncompleteType()) {
2188*67e74705SXin Li Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
2189*67e74705SXin Li Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
2190*67e74705SXin Li }
2191*67e74705SXin Li
2192*67e74705SXin Li SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2193*67e74705SXin Li
2194*67e74705SXin Li bool isImportedFromModule =
2195*67e74705SXin Li DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition();
2196*67e74705SXin Li
2197*67e74705SXin Li // If this is just a forward declaration, construct an appropriately
2198*67e74705SXin Li // marked node and just return it.
2199*67e74705SXin Li if (isImportedFromModule || !ED->getDefinition()) {
2200*67e74705SXin Li // Note that it is possible for enums to be created as part of
2201*67e74705SXin Li // their own declcontext. In this case a FwdDecl will be created
2202*67e74705SXin Li // twice. This doesn't cause a problem because both FwdDecls are
2203*67e74705SXin Li // entered into the ReplaceMap: finalize() will replace the first
2204*67e74705SXin Li // FwdDecl with the second and then replace the second with
2205*67e74705SXin Li // complete type.
2206*67e74705SXin Li llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
2207*67e74705SXin Li llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
2208*67e74705SXin Li llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType(
2209*67e74705SXin Li llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0));
2210*67e74705SXin Li
2211*67e74705SXin Li unsigned Line = getLineNumber(ED->getLocation());
2212*67e74705SXin Li StringRef EDName = ED->getName();
2213*67e74705SXin Li llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
2214*67e74705SXin Li llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
2215*67e74705SXin Li 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
2216*67e74705SXin Li
2217*67e74705SXin Li ReplaceMap.emplace_back(
2218*67e74705SXin Li std::piecewise_construct, std::make_tuple(Ty),
2219*67e74705SXin Li std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
2220*67e74705SXin Li return RetTy;
2221*67e74705SXin Li }
2222*67e74705SXin Li
2223*67e74705SXin Li return CreateTypeDefinition(Ty);
2224*67e74705SXin Li }
2225*67e74705SXin Li
CreateTypeDefinition(const EnumType * Ty)2226*67e74705SXin Li llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
2227*67e74705SXin Li const EnumDecl *ED = Ty->getDecl();
2228*67e74705SXin Li uint64_t Size = 0;
2229*67e74705SXin Li uint64_t Align = 0;
2230*67e74705SXin Li if (!ED->getTypeForDecl()->isIncompleteType()) {
2231*67e74705SXin Li Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
2232*67e74705SXin Li Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
2233*67e74705SXin Li }
2234*67e74705SXin Li
2235*67e74705SXin Li SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2236*67e74705SXin Li
2237*67e74705SXin Li // Create elements for each enumerator.
2238*67e74705SXin Li SmallVector<llvm::Metadata *, 16> Enumerators;
2239*67e74705SXin Li ED = ED->getDefinition();
2240*67e74705SXin Li for (const auto *Enum : ED->enumerators()) {
2241*67e74705SXin Li Enumerators.push_back(DBuilder.createEnumerator(
2242*67e74705SXin Li Enum->getName(), Enum->getInitVal().getSExtValue()));
2243*67e74705SXin Li }
2244*67e74705SXin Li
2245*67e74705SXin Li // Return a CompositeType for the enum itself.
2246*67e74705SXin Li llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
2247*67e74705SXin Li
2248*67e74705SXin Li llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
2249*67e74705SXin Li unsigned Line = getLineNumber(ED->getLocation());
2250*67e74705SXin Li llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
2251*67e74705SXin Li llvm::DIType *ClassTy =
2252*67e74705SXin Li ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
2253*67e74705SXin Li return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
2254*67e74705SXin Li Line, Size, Align, EltArray, ClassTy,
2255*67e74705SXin Li FullName);
2256*67e74705SXin Li }
2257*67e74705SXin Li
UnwrapTypeForDebugInfo(QualType T,const ASTContext & C)2258*67e74705SXin Li static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
2259*67e74705SXin Li Qualifiers Quals;
2260*67e74705SXin Li do {
2261*67e74705SXin Li Qualifiers InnerQuals = T.getLocalQualifiers();
2262*67e74705SXin Li // Qualifiers::operator+() doesn't like it if you add a Qualifier
2263*67e74705SXin Li // that is already there.
2264*67e74705SXin Li Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2265*67e74705SXin Li Quals += InnerQuals;
2266*67e74705SXin Li QualType LastT = T;
2267*67e74705SXin Li switch (T->getTypeClass()) {
2268*67e74705SXin Li default:
2269*67e74705SXin Li return C.getQualifiedType(T.getTypePtr(), Quals);
2270*67e74705SXin Li case Type::TemplateSpecialization: {
2271*67e74705SXin Li const auto *Spec = cast<TemplateSpecializationType>(T);
2272*67e74705SXin Li if (Spec->isTypeAlias())
2273*67e74705SXin Li return C.getQualifiedType(T.getTypePtr(), Quals);
2274*67e74705SXin Li T = Spec->desugar();
2275*67e74705SXin Li break;
2276*67e74705SXin Li }
2277*67e74705SXin Li case Type::TypeOfExpr:
2278*67e74705SXin Li T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2279*67e74705SXin Li break;
2280*67e74705SXin Li case Type::TypeOf:
2281*67e74705SXin Li T = cast<TypeOfType>(T)->getUnderlyingType();
2282*67e74705SXin Li break;
2283*67e74705SXin Li case Type::Decltype:
2284*67e74705SXin Li T = cast<DecltypeType>(T)->getUnderlyingType();
2285*67e74705SXin Li break;
2286*67e74705SXin Li case Type::UnaryTransform:
2287*67e74705SXin Li T = cast<UnaryTransformType>(T)->getUnderlyingType();
2288*67e74705SXin Li break;
2289*67e74705SXin Li case Type::Attributed:
2290*67e74705SXin Li T = cast<AttributedType>(T)->getEquivalentType();
2291*67e74705SXin Li break;
2292*67e74705SXin Li case Type::Elaborated:
2293*67e74705SXin Li T = cast<ElaboratedType>(T)->getNamedType();
2294*67e74705SXin Li break;
2295*67e74705SXin Li case Type::Paren:
2296*67e74705SXin Li T = cast<ParenType>(T)->getInnerType();
2297*67e74705SXin Li break;
2298*67e74705SXin Li case Type::SubstTemplateTypeParm:
2299*67e74705SXin Li T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
2300*67e74705SXin Li break;
2301*67e74705SXin Li case Type::Auto:
2302*67e74705SXin Li QualType DT = cast<AutoType>(T)->getDeducedType();
2303*67e74705SXin Li assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
2304*67e74705SXin Li T = DT;
2305*67e74705SXin Li break;
2306*67e74705SXin Li }
2307*67e74705SXin Li
2308*67e74705SXin Li assert(T != LastT && "Type unwrapping failed to unwrap!");
2309*67e74705SXin Li (void)LastT;
2310*67e74705SXin Li } while (true);
2311*67e74705SXin Li }
2312*67e74705SXin Li
getTypeOrNull(QualType Ty)2313*67e74705SXin Li llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
2314*67e74705SXin Li
2315*67e74705SXin Li // Unwrap the type as needed for debug information.
2316*67e74705SXin Li Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
2317*67e74705SXin Li
2318*67e74705SXin Li auto it = TypeCache.find(Ty.getAsOpaquePtr());
2319*67e74705SXin Li if (it != TypeCache.end()) {
2320*67e74705SXin Li // Verify that the debug info still exists.
2321*67e74705SXin Li if (llvm::Metadata *V = it->second)
2322*67e74705SXin Li return cast<llvm::DIType>(V);
2323*67e74705SXin Li }
2324*67e74705SXin Li
2325*67e74705SXin Li return nullptr;
2326*67e74705SXin Li }
2327*67e74705SXin Li
completeTemplateDefinition(const ClassTemplateSpecializationDecl & SD)2328*67e74705SXin Li void CGDebugInfo::completeTemplateDefinition(
2329*67e74705SXin Li const ClassTemplateSpecializationDecl &SD) {
2330*67e74705SXin Li if (DebugKind <= codegenoptions::DebugLineTablesOnly)
2331*67e74705SXin Li return;
2332*67e74705SXin Li
2333*67e74705SXin Li completeClassData(&SD);
2334*67e74705SXin Li // In case this type has no member function definitions being emitted, ensure
2335*67e74705SXin Li // it is retained
2336*67e74705SXin Li RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2337*67e74705SXin Li }
2338*67e74705SXin Li
getOrCreateType(QualType Ty,llvm::DIFile * Unit)2339*67e74705SXin Li llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
2340*67e74705SXin Li if (Ty.isNull())
2341*67e74705SXin Li return nullptr;
2342*67e74705SXin Li
2343*67e74705SXin Li // Unwrap the type as needed for debug information.
2344*67e74705SXin Li Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
2345*67e74705SXin Li
2346*67e74705SXin Li if (auto *T = getTypeOrNull(Ty))
2347*67e74705SXin Li return T;
2348*67e74705SXin Li
2349*67e74705SXin Li llvm::DIType *Res = CreateTypeNode(Ty, Unit);
2350*67e74705SXin Li void* TyPtr = Ty.getAsOpaquePtr();
2351*67e74705SXin Li
2352*67e74705SXin Li // And update the type cache.
2353*67e74705SXin Li TypeCache[TyPtr].reset(Res);
2354*67e74705SXin Li
2355*67e74705SXin Li return Res;
2356*67e74705SXin Li }
2357*67e74705SXin Li
getParentModuleOrNull(const Decl * D)2358*67e74705SXin Li llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
2359*67e74705SXin Li // A forward declaration inside a module header does not belong to the module.
2360*67e74705SXin Li if (isa<RecordDecl>(D) && !cast<RecordDecl>(D)->getDefinition())
2361*67e74705SXin Li return nullptr;
2362*67e74705SXin Li if (DebugTypeExtRefs && D->isFromASTFile()) {
2363*67e74705SXin Li // Record a reference to an imported clang module or precompiled header.
2364*67e74705SXin Li auto *Reader = CGM.getContext().getExternalSource();
2365*67e74705SXin Li auto Idx = D->getOwningModuleID();
2366*67e74705SXin Li auto Info = Reader->getSourceDescriptor(Idx);
2367*67e74705SXin Li if (Info)
2368*67e74705SXin Li return getOrCreateModuleRef(*Info, /*SkeletonCU=*/true);
2369*67e74705SXin Li } else if (ClangModuleMap) {
2370*67e74705SXin Li // We are building a clang module or a precompiled header.
2371*67e74705SXin Li //
2372*67e74705SXin Li // TODO: When D is a CXXRecordDecl or a C++ Enum, the ODR applies
2373*67e74705SXin Li // and it wouldn't be necessary to specify the parent scope
2374*67e74705SXin Li // because the type is already unique by definition (it would look
2375*67e74705SXin Li // like the output of -fno-standalone-debug). On the other hand,
2376*67e74705SXin Li // the parent scope helps a consumer to quickly locate the object
2377*67e74705SXin Li // file where the type's definition is located, so it might be
2378*67e74705SXin Li // best to make this behavior a command line or debugger tuning
2379*67e74705SXin Li // option.
2380*67e74705SXin Li FullSourceLoc Loc(D->getLocation(), CGM.getContext().getSourceManager());
2381*67e74705SXin Li if (Module *M = ClangModuleMap->inferModuleFromLocation(Loc)) {
2382*67e74705SXin Li // This is a (sub-)module.
2383*67e74705SXin Li auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
2384*67e74705SXin Li return getOrCreateModuleRef(Info, /*SkeletonCU=*/false);
2385*67e74705SXin Li } else {
2386*67e74705SXin Li // This the precompiled header being built.
2387*67e74705SXin Li return getOrCreateModuleRef(PCHDescriptor, /*SkeletonCU=*/false);
2388*67e74705SXin Li }
2389*67e74705SXin Li }
2390*67e74705SXin Li
2391*67e74705SXin Li return nullptr;
2392*67e74705SXin Li }
2393*67e74705SXin Li
CreateTypeNode(QualType Ty,llvm::DIFile * Unit)2394*67e74705SXin Li llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
2395*67e74705SXin Li // Handle qualifiers, which recursively handles what they refer to.
2396*67e74705SXin Li if (Ty.hasLocalQualifiers())
2397*67e74705SXin Li return CreateQualifiedType(Ty, Unit);
2398*67e74705SXin Li
2399*67e74705SXin Li // Work out details of type.
2400*67e74705SXin Li switch (Ty->getTypeClass()) {
2401*67e74705SXin Li #define TYPE(Class, Base)
2402*67e74705SXin Li #define ABSTRACT_TYPE(Class, Base)
2403*67e74705SXin Li #define NON_CANONICAL_TYPE(Class, Base)
2404*67e74705SXin Li #define DEPENDENT_TYPE(Class, Base) case Type::Class:
2405*67e74705SXin Li #include "clang/AST/TypeNodes.def"
2406*67e74705SXin Li llvm_unreachable("Dependent types cannot show up in debug information");
2407*67e74705SXin Li
2408*67e74705SXin Li case Type::ExtVector:
2409*67e74705SXin Li case Type::Vector:
2410*67e74705SXin Li return CreateType(cast<VectorType>(Ty), Unit);
2411*67e74705SXin Li case Type::ObjCObjectPointer:
2412*67e74705SXin Li return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2413*67e74705SXin Li case Type::ObjCObject:
2414*67e74705SXin Li return CreateType(cast<ObjCObjectType>(Ty), Unit);
2415*67e74705SXin Li case Type::ObjCInterface:
2416*67e74705SXin Li return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2417*67e74705SXin Li case Type::Builtin:
2418*67e74705SXin Li return CreateType(cast<BuiltinType>(Ty));
2419*67e74705SXin Li case Type::Complex:
2420*67e74705SXin Li return CreateType(cast<ComplexType>(Ty));
2421*67e74705SXin Li case Type::Pointer:
2422*67e74705SXin Li return CreateType(cast<PointerType>(Ty), Unit);
2423*67e74705SXin Li case Type::Adjusted:
2424*67e74705SXin Li case Type::Decayed:
2425*67e74705SXin Li // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
2426*67e74705SXin Li return CreateType(
2427*67e74705SXin Li cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
2428*67e74705SXin Li case Type::BlockPointer:
2429*67e74705SXin Li return CreateType(cast<BlockPointerType>(Ty), Unit);
2430*67e74705SXin Li case Type::Typedef:
2431*67e74705SXin Li return CreateType(cast<TypedefType>(Ty), Unit);
2432*67e74705SXin Li case Type::Record:
2433*67e74705SXin Li return CreateType(cast<RecordType>(Ty));
2434*67e74705SXin Li case Type::Enum:
2435*67e74705SXin Li return CreateEnumType(cast<EnumType>(Ty));
2436*67e74705SXin Li case Type::FunctionProto:
2437*67e74705SXin Li case Type::FunctionNoProto:
2438*67e74705SXin Li return CreateType(cast<FunctionType>(Ty), Unit);
2439*67e74705SXin Li case Type::ConstantArray:
2440*67e74705SXin Li case Type::VariableArray:
2441*67e74705SXin Li case Type::IncompleteArray:
2442*67e74705SXin Li return CreateType(cast<ArrayType>(Ty), Unit);
2443*67e74705SXin Li
2444*67e74705SXin Li case Type::LValueReference:
2445*67e74705SXin Li return CreateType(cast<LValueReferenceType>(Ty), Unit);
2446*67e74705SXin Li case Type::RValueReference:
2447*67e74705SXin Li return CreateType(cast<RValueReferenceType>(Ty), Unit);
2448*67e74705SXin Li
2449*67e74705SXin Li case Type::MemberPointer:
2450*67e74705SXin Li return CreateType(cast<MemberPointerType>(Ty), Unit);
2451*67e74705SXin Li
2452*67e74705SXin Li case Type::Atomic:
2453*67e74705SXin Li return CreateType(cast<AtomicType>(Ty), Unit);
2454*67e74705SXin Li
2455*67e74705SXin Li case Type::Pipe:
2456*67e74705SXin Li return CreateType(cast<PipeType>(Ty), Unit);
2457*67e74705SXin Li
2458*67e74705SXin Li case Type::TemplateSpecialization:
2459*67e74705SXin Li return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2460*67e74705SXin Li
2461*67e74705SXin Li case Type::Auto:
2462*67e74705SXin Li case Type::Attributed:
2463*67e74705SXin Li case Type::Elaborated:
2464*67e74705SXin Li case Type::Paren:
2465*67e74705SXin Li case Type::SubstTemplateTypeParm:
2466*67e74705SXin Li case Type::TypeOfExpr:
2467*67e74705SXin Li case Type::TypeOf:
2468*67e74705SXin Li case Type::Decltype:
2469*67e74705SXin Li case Type::UnaryTransform:
2470*67e74705SXin Li case Type::PackExpansion:
2471*67e74705SXin Li break;
2472*67e74705SXin Li }
2473*67e74705SXin Li
2474*67e74705SXin Li llvm_unreachable("type should have been unwrapped!");
2475*67e74705SXin Li }
2476*67e74705SXin Li
getOrCreateLimitedType(const RecordType * Ty,llvm::DIFile * Unit)2477*67e74705SXin Li llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2478*67e74705SXin Li llvm::DIFile *Unit) {
2479*67e74705SXin Li QualType QTy(Ty, 0);
2480*67e74705SXin Li
2481*67e74705SXin Li auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
2482*67e74705SXin Li
2483*67e74705SXin Li // We may have cached a forward decl when we could have created
2484*67e74705SXin Li // a non-forward decl. Go ahead and create a non-forward decl
2485*67e74705SXin Li // now.
2486*67e74705SXin Li if (T && !T->isForwardDecl())
2487*67e74705SXin Li return T;
2488*67e74705SXin Li
2489*67e74705SXin Li // Otherwise create the type.
2490*67e74705SXin Li llvm::DICompositeType *Res = CreateLimitedType(Ty);
2491*67e74705SXin Li
2492*67e74705SXin Li // Propagate members from the declaration to the definition
2493*67e74705SXin Li // CreateType(const RecordType*) will overwrite this with the members in the
2494*67e74705SXin Li // correct order if the full type is needed.
2495*67e74705SXin Li DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
2496*67e74705SXin Li
2497*67e74705SXin Li // And update the type cache.
2498*67e74705SXin Li TypeCache[QTy.getAsOpaquePtr()].reset(Res);
2499*67e74705SXin Li return Res;
2500*67e74705SXin Li }
2501*67e74705SXin Li
2502*67e74705SXin Li // TODO: Currently used for context chains when limiting debug info.
CreateLimitedType(const RecordType * Ty)2503*67e74705SXin Li llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
2504*67e74705SXin Li RecordDecl *RD = Ty->getDecl();
2505*67e74705SXin Li
2506*67e74705SXin Li // Get overall information about the record type for the debug info.
2507*67e74705SXin Li llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
2508*67e74705SXin Li unsigned Line = getLineNumber(RD->getLocation());
2509*67e74705SXin Li StringRef RDName = getClassName(RD);
2510*67e74705SXin Li
2511*67e74705SXin Li llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
2512*67e74705SXin Li
2513*67e74705SXin Li // If we ended up creating the type during the context chain construction,
2514*67e74705SXin Li // just return that.
2515*67e74705SXin Li auto *T = cast_or_null<llvm::DICompositeType>(
2516*67e74705SXin Li getTypeOrNull(CGM.getContext().getRecordType(RD)));
2517*67e74705SXin Li if (T && (!T->isForwardDecl() || !RD->getDefinition()))
2518*67e74705SXin Li return T;
2519*67e74705SXin Li
2520*67e74705SXin Li // If this is just a forward or incomplete declaration, construct an
2521*67e74705SXin Li // appropriately marked node and just return it.
2522*67e74705SXin Li const RecordDecl *D = RD->getDefinition();
2523*67e74705SXin Li if (!D || !D->isCompleteDefinition())
2524*67e74705SXin Li return getOrCreateRecordFwdDecl(Ty, RDContext);
2525*67e74705SXin Li
2526*67e74705SXin Li uint64_t Size = CGM.getContext().getTypeSize(Ty);
2527*67e74705SXin Li uint64_t Align = CGM.getContext().getTypeAlign(Ty);
2528*67e74705SXin Li
2529*67e74705SXin Li SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2530*67e74705SXin Li
2531*67e74705SXin Li llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
2532*67e74705SXin Li getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align, 0,
2533*67e74705SXin Li FullName);
2534*67e74705SXin Li
2535*67e74705SXin Li // Elements of composite types usually have back to the type, creating
2536*67e74705SXin Li // uniquing cycles. Distinct nodes are more efficient.
2537*67e74705SXin Li switch (RealDecl->getTag()) {
2538*67e74705SXin Li default:
2539*67e74705SXin Li llvm_unreachable("invalid composite type tag");
2540*67e74705SXin Li
2541*67e74705SXin Li case llvm::dwarf::DW_TAG_array_type:
2542*67e74705SXin Li case llvm::dwarf::DW_TAG_enumeration_type:
2543*67e74705SXin Li // Array elements and most enumeration elements don't have back references,
2544*67e74705SXin Li // so they don't tend to be involved in uniquing cycles and there is some
2545*67e74705SXin Li // chance of merging them when linking together two modules. Only make
2546*67e74705SXin Li // them distinct if they are ODR-uniqued.
2547*67e74705SXin Li if (FullName.empty())
2548*67e74705SXin Li break;
2549*67e74705SXin Li
2550*67e74705SXin Li case llvm::dwarf::DW_TAG_structure_type:
2551*67e74705SXin Li case llvm::dwarf::DW_TAG_union_type:
2552*67e74705SXin Li case llvm::dwarf::DW_TAG_class_type:
2553*67e74705SXin Li // Immediatley resolve to a distinct node.
2554*67e74705SXin Li RealDecl =
2555*67e74705SXin Li llvm::MDNode::replaceWithDistinct(llvm::TempDICompositeType(RealDecl));
2556*67e74705SXin Li break;
2557*67e74705SXin Li }
2558*67e74705SXin Li
2559*67e74705SXin Li RegionMap[Ty->getDecl()].reset(RealDecl);
2560*67e74705SXin Li TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
2561*67e74705SXin Li
2562*67e74705SXin Li if (const ClassTemplateSpecializationDecl *TSpecial =
2563*67e74705SXin Li dyn_cast<ClassTemplateSpecializationDecl>(RD))
2564*67e74705SXin Li DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
2565*67e74705SXin Li CollectCXXTemplateParams(TSpecial, DefUnit));
2566*67e74705SXin Li return RealDecl;
2567*67e74705SXin Li }
2568*67e74705SXin Li
CollectContainingType(const CXXRecordDecl * RD,llvm::DICompositeType * RealDecl)2569*67e74705SXin Li void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
2570*67e74705SXin Li llvm::DICompositeType *RealDecl) {
2571*67e74705SXin Li // A class's primary base or the class itself contains the vtable.
2572*67e74705SXin Li llvm::DICompositeType *ContainingType = nullptr;
2573*67e74705SXin Li const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2574*67e74705SXin Li if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
2575*67e74705SXin Li // Seek non-virtual primary base root.
2576*67e74705SXin Li while (1) {
2577*67e74705SXin Li const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2578*67e74705SXin Li const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2579*67e74705SXin Li if (PBT && !BRL.isPrimaryBaseVirtual())
2580*67e74705SXin Li PBase = PBT;
2581*67e74705SXin Li else
2582*67e74705SXin Li break;
2583*67e74705SXin Li }
2584*67e74705SXin Li ContainingType = cast<llvm::DICompositeType>(
2585*67e74705SXin Li getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2586*67e74705SXin Li getOrCreateFile(RD->getLocation())));
2587*67e74705SXin Li } else if (RD->isDynamicClass())
2588*67e74705SXin Li ContainingType = RealDecl;
2589*67e74705SXin Li
2590*67e74705SXin Li DBuilder.replaceVTableHolder(RealDecl, ContainingType);
2591*67e74705SXin Li }
2592*67e74705SXin Li
CreateMemberType(llvm::DIFile * Unit,QualType FType,StringRef Name,uint64_t * Offset)2593*67e74705SXin Li llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
2594*67e74705SXin Li StringRef Name, uint64_t *Offset) {
2595*67e74705SXin Li llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
2596*67e74705SXin Li uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2597*67e74705SXin Li unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
2598*67e74705SXin Li llvm::DIType *Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
2599*67e74705SXin Li FieldAlign, *Offset, 0, FieldTy);
2600*67e74705SXin Li *Offset += FieldSize;
2601*67e74705SXin Li return Ty;
2602*67e74705SXin Li }
2603*67e74705SXin Li
collectFunctionDeclProps(GlobalDecl GD,llvm::DIFile * Unit,StringRef & Name,StringRef & LinkageName,llvm::DIScope * & FDContext,llvm::DINodeArray & TParamsArray,unsigned & Flags)2604*67e74705SXin Li void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
2605*67e74705SXin Li StringRef &Name,
2606*67e74705SXin Li StringRef &LinkageName,
2607*67e74705SXin Li llvm::DIScope *&FDContext,
2608*67e74705SXin Li llvm::DINodeArray &TParamsArray,
2609*67e74705SXin Li unsigned &Flags) {
2610*67e74705SXin Li const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2611*67e74705SXin Li Name = getFunctionName(FD);
2612*67e74705SXin Li // Use mangled name as linkage name for C/C++ functions.
2613*67e74705SXin Li if (FD->hasPrototype()) {
2614*67e74705SXin Li LinkageName = CGM.getMangledName(GD);
2615*67e74705SXin Li Flags |= llvm::DINode::FlagPrototyped;
2616*67e74705SXin Li }
2617*67e74705SXin Li // No need to replicate the linkage name if it isn't different from the
2618*67e74705SXin Li // subprogram name, no need to have it at all unless coverage is enabled or
2619*67e74705SXin Li // debug is set to more than just line tables.
2620*67e74705SXin Li if (LinkageName == Name || (!CGM.getCodeGenOpts().EmitGcovArcs &&
2621*67e74705SXin Li !CGM.getCodeGenOpts().EmitGcovNotes &&
2622*67e74705SXin Li DebugKind <= codegenoptions::DebugLineTablesOnly))
2623*67e74705SXin Li LinkageName = StringRef();
2624*67e74705SXin Li
2625*67e74705SXin Li if (DebugKind >= codegenoptions::LimitedDebugInfo) {
2626*67e74705SXin Li if (const NamespaceDecl *NSDecl =
2627*67e74705SXin Li dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2628*67e74705SXin Li FDContext = getOrCreateNameSpace(NSDecl);
2629*67e74705SXin Li else if (const RecordDecl *RDecl =
2630*67e74705SXin Li dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) {
2631*67e74705SXin Li llvm::DIScope *Mod = getParentModuleOrNull(RDecl);
2632*67e74705SXin Li FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU);
2633*67e74705SXin Li }
2634*67e74705SXin Li // Collect template parameters.
2635*67e74705SXin Li TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2636*67e74705SXin Li }
2637*67e74705SXin Li }
2638*67e74705SXin Li
collectVarDeclProps(const VarDecl * VD,llvm::DIFile * & Unit,unsigned & LineNo,QualType & T,StringRef & Name,StringRef & LinkageName,llvm::DIScope * & VDContext)2639*67e74705SXin Li void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
2640*67e74705SXin Li unsigned &LineNo, QualType &T,
2641*67e74705SXin Li StringRef &Name, StringRef &LinkageName,
2642*67e74705SXin Li llvm::DIScope *&VDContext) {
2643*67e74705SXin Li Unit = getOrCreateFile(VD->getLocation());
2644*67e74705SXin Li LineNo = getLineNumber(VD->getLocation());
2645*67e74705SXin Li
2646*67e74705SXin Li setLocation(VD->getLocation());
2647*67e74705SXin Li
2648*67e74705SXin Li T = VD->getType();
2649*67e74705SXin Li if (T->isIncompleteArrayType()) {
2650*67e74705SXin Li // CodeGen turns int[] into int[1] so we'll do the same here.
2651*67e74705SXin Li llvm::APInt ConstVal(32, 1);
2652*67e74705SXin Li QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2653*67e74705SXin Li
2654*67e74705SXin Li T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2655*67e74705SXin Li ArrayType::Normal, 0);
2656*67e74705SXin Li }
2657*67e74705SXin Li
2658*67e74705SXin Li Name = VD->getName();
2659*67e74705SXin Li if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2660*67e74705SXin Li !isa<ObjCMethodDecl>(VD->getDeclContext()))
2661*67e74705SXin Li LinkageName = CGM.getMangledName(VD);
2662*67e74705SXin Li if (LinkageName == Name)
2663*67e74705SXin Li LinkageName = StringRef();
2664*67e74705SXin Li
2665*67e74705SXin Li // Since we emit declarations (DW_AT_members) for static members, place the
2666*67e74705SXin Li // definition of those static members in the namespace they were declared in
2667*67e74705SXin Li // in the source code (the lexical decl context).
2668*67e74705SXin Li // FIXME: Generalize this for even non-member global variables where the
2669*67e74705SXin Li // declaration and definition may have different lexical decl contexts, once
2670*67e74705SXin Li // we have support for emitting declarations of (non-member) global variables.
2671*67e74705SXin Li const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2672*67e74705SXin Li : VD->getDeclContext();
2673*67e74705SXin Li // When a record type contains an in-line initialization of a static data
2674*67e74705SXin Li // member, and the record type is marked as __declspec(dllexport), an implicit
2675*67e74705SXin Li // definition of the member will be created in the record context. DWARF
2676*67e74705SXin Li // doesn't seem to have a nice way to describe this in a form that consumers
2677*67e74705SXin Li // are likely to understand, so fake the "normal" situation of a definition
2678*67e74705SXin Li // outside the class by putting it in the global scope.
2679*67e74705SXin Li if (DC->isRecord())
2680*67e74705SXin Li DC = CGM.getContext().getTranslationUnitDecl();
2681*67e74705SXin Li
2682*67e74705SXin Li llvm::DIScope *Mod = getParentModuleOrNull(VD);
2683*67e74705SXin Li VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
2684*67e74705SXin Li }
2685*67e74705SXin Li
2686*67e74705SXin Li llvm::DISubprogram *
getFunctionForwardDeclaration(const FunctionDecl * FD)2687*67e74705SXin Li CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
2688*67e74705SXin Li llvm::DINodeArray TParamsArray;
2689*67e74705SXin Li StringRef Name, LinkageName;
2690*67e74705SXin Li unsigned Flags = 0;
2691*67e74705SXin Li SourceLocation Loc = FD->getLocation();
2692*67e74705SXin Li llvm::DIFile *Unit = getOrCreateFile(Loc);
2693*67e74705SXin Li llvm::DIScope *DContext = Unit;
2694*67e74705SXin Li unsigned Line = getLineNumber(Loc);
2695*67e74705SXin Li
2696*67e74705SXin Li collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2697*67e74705SXin Li TParamsArray, Flags);
2698*67e74705SXin Li // Build function type.
2699*67e74705SXin Li SmallVector<QualType, 16> ArgTypes;
2700*67e74705SXin Li for (const ParmVarDecl *Parm: FD->parameters())
2701*67e74705SXin Li ArgTypes.push_back(Parm->getType());
2702*67e74705SXin Li CallingConv CC = FD->getType()->castAs<FunctionType>()->getCallConv();
2703*67e74705SXin Li QualType FnType = CGM.getContext().getFunctionType(
2704*67e74705SXin Li FD->getReturnType(), ArgTypes, FunctionProtoType::ExtProtoInfo(CC));
2705*67e74705SXin Li llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
2706*67e74705SXin Li DContext, Name, LinkageName, Unit, Line,
2707*67e74705SXin Li getOrCreateFunctionType(FD, FnType, Unit), !FD->isExternallyVisible(),
2708*67e74705SXin Li /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize,
2709*67e74705SXin Li TParamsArray.get(), getFunctionDeclaration(FD));
2710*67e74705SXin Li const FunctionDecl *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
2711*67e74705SXin Li FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2712*67e74705SXin Li std::make_tuple(CanonDecl),
2713*67e74705SXin Li std::make_tuple(SP));
2714*67e74705SXin Li return SP;
2715*67e74705SXin Li }
2716*67e74705SXin Li
2717*67e74705SXin Li llvm::DIGlobalVariable *
getGlobalVariableForwardDeclaration(const VarDecl * VD)2718*67e74705SXin Li CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2719*67e74705SXin Li QualType T;
2720*67e74705SXin Li StringRef Name, LinkageName;
2721*67e74705SXin Li SourceLocation Loc = VD->getLocation();
2722*67e74705SXin Li llvm::DIFile *Unit = getOrCreateFile(Loc);
2723*67e74705SXin Li llvm::DIScope *DContext = Unit;
2724*67e74705SXin Li unsigned Line = getLineNumber(Loc);
2725*67e74705SXin Li
2726*67e74705SXin Li collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
2727*67e74705SXin Li auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
2728*67e74705SXin Li DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
2729*67e74705SXin Li !VD->isExternallyVisible(), nullptr, nullptr);
2730*67e74705SXin Li FwdDeclReplaceMap.emplace_back(
2731*67e74705SXin Li std::piecewise_construct,
2732*67e74705SXin Li std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2733*67e74705SXin Li std::make_tuple(static_cast<llvm::Metadata *>(GV)));
2734*67e74705SXin Li return GV;
2735*67e74705SXin Li }
2736*67e74705SXin Li
getDeclarationOrDefinition(const Decl * D)2737*67e74705SXin Li llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
2738*67e74705SXin Li // We only need a declaration (not a definition) of the type - so use whatever
2739*67e74705SXin Li // we would otherwise do to get a type for a pointee. (forward declarations in
2740*67e74705SXin Li // limited debug info, full definitions (if the type definition is available)
2741*67e74705SXin Li // in unlimited debug info)
2742*67e74705SXin Li if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2743*67e74705SXin Li return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
2744*67e74705SXin Li getOrCreateFile(TD->getLocation()));
2745*67e74705SXin Li auto I = DeclCache.find(D->getCanonicalDecl());
2746*67e74705SXin Li
2747*67e74705SXin Li if (I != DeclCache.end())
2748*67e74705SXin Li return dyn_cast_or_null<llvm::DINode>(I->second);
2749*67e74705SXin Li
2750*67e74705SXin Li // No definition for now. Emit a forward definition that might be
2751*67e74705SXin Li // merged with a potential upcoming definition.
2752*67e74705SXin Li if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
2753*67e74705SXin Li return getFunctionForwardDeclaration(FD);
2754*67e74705SXin Li else if (const auto *VD = dyn_cast<VarDecl>(D))
2755*67e74705SXin Li return getGlobalVariableForwardDeclaration(VD);
2756*67e74705SXin Li
2757*67e74705SXin Li return nullptr;
2758*67e74705SXin Li }
2759*67e74705SXin Li
getFunctionDeclaration(const Decl * D)2760*67e74705SXin Li llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
2761*67e74705SXin Li if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
2762*67e74705SXin Li return nullptr;
2763*67e74705SXin Li
2764*67e74705SXin Li const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2765*67e74705SXin Li if (!FD)
2766*67e74705SXin Li return nullptr;
2767*67e74705SXin Li
2768*67e74705SXin Li // Setup context.
2769*67e74705SXin Li auto *S = getDeclContextDescriptor(D);
2770*67e74705SXin Li
2771*67e74705SXin Li auto MI = SPCache.find(FD->getCanonicalDecl());
2772*67e74705SXin Li if (MI == SPCache.end()) {
2773*67e74705SXin Li if (const CXXMethodDecl *MD =
2774*67e74705SXin Li dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
2775*67e74705SXin Li return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
2776*67e74705SXin Li cast<llvm::DICompositeType>(S));
2777*67e74705SXin Li }
2778*67e74705SXin Li }
2779*67e74705SXin Li if (MI != SPCache.end()) {
2780*67e74705SXin Li auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
2781*67e74705SXin Li if (SP && !SP->isDefinition())
2782*67e74705SXin Li return SP;
2783*67e74705SXin Li }
2784*67e74705SXin Li
2785*67e74705SXin Li for (auto NextFD : FD->redecls()) {
2786*67e74705SXin Li auto MI = SPCache.find(NextFD->getCanonicalDecl());
2787*67e74705SXin Li if (MI != SPCache.end()) {
2788*67e74705SXin Li auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
2789*67e74705SXin Li if (SP && !SP->isDefinition())
2790*67e74705SXin Li return SP;
2791*67e74705SXin Li }
2792*67e74705SXin Li }
2793*67e74705SXin Li return nullptr;
2794*67e74705SXin Li }
2795*67e74705SXin Li
2796*67e74705SXin Li // getOrCreateFunctionType - Construct type. If it is a c++ method, include
2797*67e74705SXin Li // implicit parameter "this".
getOrCreateFunctionType(const Decl * D,QualType FnType,llvm::DIFile * F)2798*67e74705SXin Li llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
2799*67e74705SXin Li QualType FnType,
2800*67e74705SXin Li llvm::DIFile *F) {
2801*67e74705SXin Li if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
2802*67e74705SXin Li // Create fake but valid subroutine type. Otherwise -verify would fail, and
2803*67e74705SXin Li // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
2804*67e74705SXin Li return DBuilder.createSubroutineType(DBuilder.getOrCreateTypeArray(None));
2805*67e74705SXin Li
2806*67e74705SXin Li if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2807*67e74705SXin Li return getOrCreateMethodType(Method, F);
2808*67e74705SXin Li
2809*67e74705SXin Li const auto *FTy = FnType->getAs<FunctionType>();
2810*67e74705SXin Li CallingConv CC = FTy ? FTy->getCallConv() : CallingConv::CC_C;
2811*67e74705SXin Li
2812*67e74705SXin Li if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2813*67e74705SXin Li // Add "self" and "_cmd"
2814*67e74705SXin Li SmallVector<llvm::Metadata *, 16> Elts;
2815*67e74705SXin Li
2816*67e74705SXin Li // First element is always return type. For 'void' functions it is NULL.
2817*67e74705SXin Li QualType ResultTy = OMethod->getReturnType();
2818*67e74705SXin Li
2819*67e74705SXin Li // Replace the instancetype keyword with the actual type.
2820*67e74705SXin Li if (ResultTy == CGM.getContext().getObjCInstanceType())
2821*67e74705SXin Li ResultTy = CGM.getContext().getPointerType(
2822*67e74705SXin Li QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
2823*67e74705SXin Li
2824*67e74705SXin Li Elts.push_back(getOrCreateType(ResultTy, F));
2825*67e74705SXin Li // "self" pointer is always first argument.
2826*67e74705SXin Li QualType SelfDeclTy;
2827*67e74705SXin Li if (auto *SelfDecl = OMethod->getSelfDecl())
2828*67e74705SXin Li SelfDeclTy = SelfDecl->getType();
2829*67e74705SXin Li else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType))
2830*67e74705SXin Li if (FPT->getNumParams() > 1)
2831*67e74705SXin Li SelfDeclTy = FPT->getParamType(0);
2832*67e74705SXin Li if (!SelfDeclTy.isNull())
2833*67e74705SXin Li Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
2834*67e74705SXin Li // "_cmd" pointer is always second argument.
2835*67e74705SXin Li Elts.push_back(DBuilder.createArtificialType(
2836*67e74705SXin Li getOrCreateType(CGM.getContext().getObjCSelType(), F)));
2837*67e74705SXin Li // Get rest of the arguments.
2838*67e74705SXin Li for (const auto *PI : OMethod->parameters())
2839*67e74705SXin Li Elts.push_back(getOrCreateType(PI->getType(), F));
2840*67e74705SXin Li // Variadic methods need a special marker at the end of the type list.
2841*67e74705SXin Li if (OMethod->isVariadic())
2842*67e74705SXin Li Elts.push_back(DBuilder.createUnspecifiedParameter());
2843*67e74705SXin Li
2844*67e74705SXin Li llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
2845*67e74705SXin Li return DBuilder.createSubroutineType(EltTypeArray, 0, getDwarfCC(CC));
2846*67e74705SXin Li }
2847*67e74705SXin Li
2848*67e74705SXin Li // Handle variadic function types; they need an additional
2849*67e74705SXin Li // unspecified parameter.
2850*67e74705SXin Li if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2851*67e74705SXin Li if (FD->isVariadic()) {
2852*67e74705SXin Li SmallVector<llvm::Metadata *, 16> EltTys;
2853*67e74705SXin Li EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2854*67e74705SXin Li if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2855*67e74705SXin Li for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2856*67e74705SXin Li EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2857*67e74705SXin Li EltTys.push_back(DBuilder.createUnspecifiedParameter());
2858*67e74705SXin Li llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
2859*67e74705SXin Li return DBuilder.createSubroutineType(EltTypeArray, 0, getDwarfCC(CC));
2860*67e74705SXin Li }
2861*67e74705SXin Li
2862*67e74705SXin Li return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
2863*67e74705SXin Li }
2864*67e74705SXin Li
EmitFunctionStart(GlobalDecl GD,SourceLocation Loc,SourceLocation ScopeLoc,QualType FnType,llvm::Function * Fn,CGBuilderTy & Builder)2865*67e74705SXin Li void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2866*67e74705SXin Li SourceLocation ScopeLoc, QualType FnType,
2867*67e74705SXin Li llvm::Function *Fn, CGBuilderTy &Builder) {
2868*67e74705SXin Li
2869*67e74705SXin Li StringRef Name;
2870*67e74705SXin Li StringRef LinkageName;
2871*67e74705SXin Li
2872*67e74705SXin Li FnBeginRegionCount.push_back(LexicalBlockStack.size());
2873*67e74705SXin Li
2874*67e74705SXin Li const Decl *D = GD.getDecl();
2875*67e74705SXin Li bool HasDecl = (D != nullptr);
2876*67e74705SXin Li
2877*67e74705SXin Li unsigned Flags = 0;
2878*67e74705SXin Li llvm::DIFile *Unit = getOrCreateFile(Loc);
2879*67e74705SXin Li llvm::DIScope *FDContext = Unit;
2880*67e74705SXin Li llvm::DINodeArray TParamsArray;
2881*67e74705SXin Li if (!HasDecl) {
2882*67e74705SXin Li // Use llvm function name.
2883*67e74705SXin Li LinkageName = Fn->getName();
2884*67e74705SXin Li } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2885*67e74705SXin Li // If there is a subprogram for this function available then use it.
2886*67e74705SXin Li auto FI = SPCache.find(FD->getCanonicalDecl());
2887*67e74705SXin Li if (FI != SPCache.end()) {
2888*67e74705SXin Li auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
2889*67e74705SXin Li if (SP && SP->isDefinition()) {
2890*67e74705SXin Li LexicalBlockStack.emplace_back(SP);
2891*67e74705SXin Li RegionMap[D].reset(SP);
2892*67e74705SXin Li return;
2893*67e74705SXin Li }
2894*67e74705SXin Li }
2895*67e74705SXin Li collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2896*67e74705SXin Li TParamsArray, Flags);
2897*67e74705SXin Li } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2898*67e74705SXin Li Name = getObjCMethodName(OMD);
2899*67e74705SXin Li Flags |= llvm::DINode::FlagPrototyped;
2900*67e74705SXin Li } else {
2901*67e74705SXin Li // Use llvm function name.
2902*67e74705SXin Li Name = Fn->getName();
2903*67e74705SXin Li Flags |= llvm::DINode::FlagPrototyped;
2904*67e74705SXin Li }
2905*67e74705SXin Li if (!Name.empty() && Name[0] == '\01')
2906*67e74705SXin Li Name = Name.substr(1);
2907*67e74705SXin Li
2908*67e74705SXin Li if (!HasDecl || D->isImplicit()) {
2909*67e74705SXin Li Flags |= llvm::DINode::FlagArtificial;
2910*67e74705SXin Li // Artificial functions without a location should not silently reuse CurLoc.
2911*67e74705SXin Li if (Loc.isInvalid())
2912*67e74705SXin Li CurLoc = SourceLocation();
2913*67e74705SXin Li }
2914*67e74705SXin Li unsigned LineNo = getLineNumber(Loc);
2915*67e74705SXin Li unsigned ScopeLine = getLineNumber(ScopeLoc);
2916*67e74705SXin Li
2917*67e74705SXin Li // FIXME: The function declaration we're constructing here is mostly reusing
2918*67e74705SXin Li // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2919*67e74705SXin Li // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2920*67e74705SXin Li // all subprograms instead of the actual context since subprogram definitions
2921*67e74705SXin Li // are emitted as CU level entities by the backend.
2922*67e74705SXin Li llvm::DISubprogram *SP = DBuilder.createFunction(
2923*67e74705SXin Li FDContext, Name, LinkageName, Unit, LineNo,
2924*67e74705SXin Li getOrCreateFunctionType(D, FnType, Unit), Fn->hasLocalLinkage(),
2925*67e74705SXin Li true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
2926*67e74705SXin Li TParamsArray.get(), getFunctionDeclaration(D));
2927*67e74705SXin Li Fn->setSubprogram(SP);
2928*67e74705SXin Li // We might get here with a VarDecl in the case we're generating
2929*67e74705SXin Li // code for the initialization of globals. Do not record these decls
2930*67e74705SXin Li // as they will overwrite the actual VarDecl Decl in the cache.
2931*67e74705SXin Li if (HasDecl && isa<FunctionDecl>(D))
2932*67e74705SXin Li DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(SP));
2933*67e74705SXin Li
2934*67e74705SXin Li // Push the function onto the lexical block stack.
2935*67e74705SXin Li LexicalBlockStack.emplace_back(SP);
2936*67e74705SXin Li
2937*67e74705SXin Li if (HasDecl)
2938*67e74705SXin Li RegionMap[D].reset(SP);
2939*67e74705SXin Li }
2940*67e74705SXin Li
EmitFunctionDecl(GlobalDecl GD,SourceLocation Loc,QualType FnType)2941*67e74705SXin Li void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
2942*67e74705SXin Li QualType FnType) {
2943*67e74705SXin Li StringRef Name;
2944*67e74705SXin Li StringRef LinkageName;
2945*67e74705SXin Li
2946*67e74705SXin Li const Decl *D = GD.getDecl();
2947*67e74705SXin Li if (!D)
2948*67e74705SXin Li return;
2949*67e74705SXin Li
2950*67e74705SXin Li unsigned Flags = 0;
2951*67e74705SXin Li llvm::DIFile *Unit = getOrCreateFile(Loc);
2952*67e74705SXin Li llvm::DIScope *FDContext = getDeclContextDescriptor(D);
2953*67e74705SXin Li llvm::DINodeArray TParamsArray;
2954*67e74705SXin Li if (isa<FunctionDecl>(D)) {
2955*67e74705SXin Li // If there is a DISubprogram for this function available then use it.
2956*67e74705SXin Li collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2957*67e74705SXin Li TParamsArray, Flags);
2958*67e74705SXin Li } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2959*67e74705SXin Li Name = getObjCMethodName(OMD);
2960*67e74705SXin Li Flags |= llvm::DINode::FlagPrototyped;
2961*67e74705SXin Li } else {
2962*67e74705SXin Li llvm_unreachable("not a function or ObjC method");
2963*67e74705SXin Li }
2964*67e74705SXin Li if (!Name.empty() && Name[0] == '\01')
2965*67e74705SXin Li Name = Name.substr(1);
2966*67e74705SXin Li
2967*67e74705SXin Li if (D->isImplicit()) {
2968*67e74705SXin Li Flags |= llvm::DINode::FlagArtificial;
2969*67e74705SXin Li // Artificial functions without a location should not silently reuse CurLoc.
2970*67e74705SXin Li if (Loc.isInvalid())
2971*67e74705SXin Li CurLoc = SourceLocation();
2972*67e74705SXin Li }
2973*67e74705SXin Li unsigned LineNo = getLineNumber(Loc);
2974*67e74705SXin Li unsigned ScopeLine = 0;
2975*67e74705SXin Li
2976*67e74705SXin Li DBuilder.retainType(DBuilder.createFunction(
2977*67e74705SXin Li FDContext, Name, LinkageName, Unit, LineNo,
2978*67e74705SXin Li getOrCreateFunctionType(D, FnType, Unit), false /*internalLinkage*/,
2979*67e74705SXin Li false /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
2980*67e74705SXin Li TParamsArray.get(), getFunctionDeclaration(D)));
2981*67e74705SXin Li }
2982*67e74705SXin Li
EmitLocation(CGBuilderTy & Builder,SourceLocation Loc)2983*67e74705SXin Li void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
2984*67e74705SXin Li // Update our current location
2985*67e74705SXin Li setLocation(Loc);
2986*67e74705SXin Li
2987*67e74705SXin Li if (CurLoc.isInvalid() || CurLoc.isMacroID())
2988*67e74705SXin Li return;
2989*67e74705SXin Li
2990*67e74705SXin Li llvm::MDNode *Scope = LexicalBlockStack.back();
2991*67e74705SXin Li Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2992*67e74705SXin Li getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope));
2993*67e74705SXin Li }
2994*67e74705SXin Li
CreateLexicalBlock(SourceLocation Loc)2995*67e74705SXin Li void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
2996*67e74705SXin Li llvm::MDNode *Back = nullptr;
2997*67e74705SXin Li if (!LexicalBlockStack.empty())
2998*67e74705SXin Li Back = LexicalBlockStack.back().get();
2999*67e74705SXin Li LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
3000*67e74705SXin Li cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
3001*67e74705SXin Li getColumnNumber(CurLoc)));
3002*67e74705SXin Li }
3003*67e74705SXin Li
EmitLexicalBlockStart(CGBuilderTy & Builder,SourceLocation Loc)3004*67e74705SXin Li void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
3005*67e74705SXin Li SourceLocation Loc) {
3006*67e74705SXin Li // Set our current location.
3007*67e74705SXin Li setLocation(Loc);
3008*67e74705SXin Li
3009*67e74705SXin Li // Emit a line table change for the current location inside the new scope.
3010*67e74705SXin Li Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
3011*67e74705SXin Li getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
3012*67e74705SXin Li
3013*67e74705SXin Li if (DebugKind <= codegenoptions::DebugLineTablesOnly)
3014*67e74705SXin Li return;
3015*67e74705SXin Li
3016*67e74705SXin Li // Create a new lexical block and push it on the stack.
3017*67e74705SXin Li CreateLexicalBlock(Loc);
3018*67e74705SXin Li }
3019*67e74705SXin Li
EmitLexicalBlockEnd(CGBuilderTy & Builder,SourceLocation Loc)3020*67e74705SXin Li void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
3021*67e74705SXin Li SourceLocation Loc) {
3022*67e74705SXin Li assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3023*67e74705SXin Li
3024*67e74705SXin Li // Provide an entry in the line table for the end of the block.
3025*67e74705SXin Li EmitLocation(Builder, Loc);
3026*67e74705SXin Li
3027*67e74705SXin Li if (DebugKind <= codegenoptions::DebugLineTablesOnly)
3028*67e74705SXin Li return;
3029*67e74705SXin Li
3030*67e74705SXin Li LexicalBlockStack.pop_back();
3031*67e74705SXin Li }
3032*67e74705SXin Li
EmitFunctionEnd(CGBuilderTy & Builder)3033*67e74705SXin Li void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
3034*67e74705SXin Li assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3035*67e74705SXin Li unsigned RCount = FnBeginRegionCount.back();
3036*67e74705SXin Li assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
3037*67e74705SXin Li
3038*67e74705SXin Li // Pop all regions for this function.
3039*67e74705SXin Li while (LexicalBlockStack.size() != RCount) {
3040*67e74705SXin Li // Provide an entry in the line table for the end of the block.
3041*67e74705SXin Li EmitLocation(Builder, CurLoc);
3042*67e74705SXin Li LexicalBlockStack.pop_back();
3043*67e74705SXin Li }
3044*67e74705SXin Li FnBeginRegionCount.pop_back();
3045*67e74705SXin Li }
3046*67e74705SXin Li
EmitTypeForVarWithBlocksAttr(const VarDecl * VD,uint64_t * XOffset)3047*67e74705SXin Li llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
3048*67e74705SXin Li uint64_t *XOffset) {
3049*67e74705SXin Li
3050*67e74705SXin Li SmallVector<llvm::Metadata *, 5> EltTys;
3051*67e74705SXin Li QualType FType;
3052*67e74705SXin Li uint64_t FieldSize, FieldOffset;
3053*67e74705SXin Li unsigned FieldAlign;
3054*67e74705SXin Li
3055*67e74705SXin Li llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
3056*67e74705SXin Li QualType Type = VD->getType();
3057*67e74705SXin Li
3058*67e74705SXin Li FieldOffset = 0;
3059*67e74705SXin Li FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
3060*67e74705SXin Li EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
3061*67e74705SXin Li EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
3062*67e74705SXin Li FType = CGM.getContext().IntTy;
3063*67e74705SXin Li EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
3064*67e74705SXin Li EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
3065*67e74705SXin Li
3066*67e74705SXin Li bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
3067*67e74705SXin Li if (HasCopyAndDispose) {
3068*67e74705SXin Li FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
3069*67e74705SXin Li EltTys.push_back(
3070*67e74705SXin Li CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
3071*67e74705SXin Li EltTys.push_back(
3072*67e74705SXin Li CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
3073*67e74705SXin Li }
3074*67e74705SXin Li bool HasByrefExtendedLayout;
3075*67e74705SXin Li Qualifiers::ObjCLifetime Lifetime;
3076*67e74705SXin Li if (CGM.getContext().getByrefLifetime(Type, Lifetime,
3077*67e74705SXin Li HasByrefExtendedLayout) &&
3078*67e74705SXin Li HasByrefExtendedLayout) {
3079*67e74705SXin Li FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
3080*67e74705SXin Li EltTys.push_back(
3081*67e74705SXin Li CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
3082*67e74705SXin Li }
3083*67e74705SXin Li
3084*67e74705SXin Li CharUnits Align = CGM.getContext().getDeclAlign(VD);
3085*67e74705SXin Li if (Align > CGM.getContext().toCharUnitsFromBits(
3086*67e74705SXin Li CGM.getTarget().getPointerAlign(0))) {
3087*67e74705SXin Li CharUnits FieldOffsetInBytes =
3088*67e74705SXin Li CGM.getContext().toCharUnitsFromBits(FieldOffset);
3089*67e74705SXin Li CharUnits AlignedOffsetInBytes = FieldOffsetInBytes.alignTo(Align);
3090*67e74705SXin Li CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
3091*67e74705SXin Li
3092*67e74705SXin Li if (NumPaddingBytes.isPositive()) {
3093*67e74705SXin Li llvm::APInt pad(32, NumPaddingBytes.getQuantity());
3094*67e74705SXin Li FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
3095*67e74705SXin Li pad, ArrayType::Normal, 0);
3096*67e74705SXin Li EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
3097*67e74705SXin Li }
3098*67e74705SXin Li }
3099*67e74705SXin Li
3100*67e74705SXin Li FType = Type;
3101*67e74705SXin Li llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
3102*67e74705SXin Li FieldSize = CGM.getContext().getTypeSize(FType);
3103*67e74705SXin Li FieldAlign = CGM.getContext().toBits(Align);
3104*67e74705SXin Li
3105*67e74705SXin Li *XOffset = FieldOffset;
3106*67e74705SXin Li FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
3107*67e74705SXin Li FieldAlign, FieldOffset, 0, FieldTy);
3108*67e74705SXin Li EltTys.push_back(FieldTy);
3109*67e74705SXin Li FieldOffset += FieldSize;
3110*67e74705SXin Li
3111*67e74705SXin Li llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
3112*67e74705SXin Li
3113*67e74705SXin Li unsigned Flags = llvm::DINode::FlagBlockByrefStruct;
3114*67e74705SXin Li
3115*67e74705SXin Li return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
3116*67e74705SXin Li nullptr, Elements);
3117*67e74705SXin Li }
3118*67e74705SXin Li
EmitDeclare(const VarDecl * VD,llvm::Value * Storage,llvm::Optional<unsigned> ArgNo,CGBuilderTy & Builder)3119*67e74705SXin Li void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::Value *Storage,
3120*67e74705SXin Li llvm::Optional<unsigned> ArgNo,
3121*67e74705SXin Li CGBuilderTy &Builder) {
3122*67e74705SXin Li assert(DebugKind >= codegenoptions::LimitedDebugInfo);
3123*67e74705SXin Li assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3124*67e74705SXin Li if (VD->hasAttr<NoDebugAttr>())
3125*67e74705SXin Li return;
3126*67e74705SXin Li
3127*67e74705SXin Li bool Unwritten =
3128*67e74705SXin Li VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
3129*67e74705SXin Li cast<Decl>(VD->getDeclContext())->isImplicit());
3130*67e74705SXin Li llvm::DIFile *Unit = nullptr;
3131*67e74705SXin Li if (!Unwritten)
3132*67e74705SXin Li Unit = getOrCreateFile(VD->getLocation());
3133*67e74705SXin Li llvm::DIType *Ty;
3134*67e74705SXin Li uint64_t XOffset = 0;
3135*67e74705SXin Li if (VD->hasAttr<BlocksAttr>())
3136*67e74705SXin Li Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
3137*67e74705SXin Li else
3138*67e74705SXin Li Ty = getOrCreateType(VD->getType(), Unit);
3139*67e74705SXin Li
3140*67e74705SXin Li // If there is no debug info for this type then do not emit debug info
3141*67e74705SXin Li // for this variable.
3142*67e74705SXin Li if (!Ty)
3143*67e74705SXin Li return;
3144*67e74705SXin Li
3145*67e74705SXin Li // Get location information.
3146*67e74705SXin Li unsigned Line = 0;
3147*67e74705SXin Li unsigned Column = 0;
3148*67e74705SXin Li if (!Unwritten) {
3149*67e74705SXin Li Line = getLineNumber(VD->getLocation());
3150*67e74705SXin Li Column = getColumnNumber(VD->getLocation());
3151*67e74705SXin Li }
3152*67e74705SXin Li SmallVector<int64_t, 9> Expr;
3153*67e74705SXin Li unsigned Flags = 0;
3154*67e74705SXin Li if (VD->isImplicit())
3155*67e74705SXin Li Flags |= llvm::DINode::FlagArtificial;
3156*67e74705SXin Li // If this is the first argument and it is implicit then
3157*67e74705SXin Li // give it an object pointer flag.
3158*67e74705SXin Li // FIXME: There has to be a better way to do this, but for static
3159*67e74705SXin Li // functions there won't be an implicit param at arg1 and
3160*67e74705SXin Li // otherwise it is 'self' or 'this'.
3161*67e74705SXin Li if (isa<ImplicitParamDecl>(VD) && ArgNo && *ArgNo == 1)
3162*67e74705SXin Li Flags |= llvm::DINode::FlagObjectPointer;
3163*67e74705SXin Li if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
3164*67e74705SXin Li if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
3165*67e74705SXin Li !VD->getType()->isPointerType())
3166*67e74705SXin Li Expr.push_back(llvm::dwarf::DW_OP_deref);
3167*67e74705SXin Li
3168*67e74705SXin Li auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
3169*67e74705SXin Li
3170*67e74705SXin Li StringRef Name = VD->getName();
3171*67e74705SXin Li if (!Name.empty()) {
3172*67e74705SXin Li if (VD->hasAttr<BlocksAttr>()) {
3173*67e74705SXin Li CharUnits offset = CharUnits::fromQuantity(32);
3174*67e74705SXin Li Expr.push_back(llvm::dwarf::DW_OP_plus);
3175*67e74705SXin Li // offset of __forwarding field
3176*67e74705SXin Li offset = CGM.getContext().toCharUnitsFromBits(
3177*67e74705SXin Li CGM.getTarget().getPointerWidth(0));
3178*67e74705SXin Li Expr.push_back(offset.getQuantity());
3179*67e74705SXin Li Expr.push_back(llvm::dwarf::DW_OP_deref);
3180*67e74705SXin Li Expr.push_back(llvm::dwarf::DW_OP_plus);
3181*67e74705SXin Li // offset of x field
3182*67e74705SXin Li offset = CGM.getContext().toCharUnitsFromBits(XOffset);
3183*67e74705SXin Li Expr.push_back(offset.getQuantity());
3184*67e74705SXin Li
3185*67e74705SXin Li // Create the descriptor for the variable.
3186*67e74705SXin Li auto *D = ArgNo
3187*67e74705SXin Li ? DBuilder.createParameterVariable(Scope, VD->getName(),
3188*67e74705SXin Li *ArgNo, Unit, Line, Ty)
3189*67e74705SXin Li : DBuilder.createAutoVariable(Scope, VD->getName(), Unit,
3190*67e74705SXin Li Line, Ty);
3191*67e74705SXin Li
3192*67e74705SXin Li // Insert an llvm.dbg.declare into the current block.
3193*67e74705SXin Li DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3194*67e74705SXin Li llvm::DebugLoc::get(Line, Column, Scope),
3195*67e74705SXin Li Builder.GetInsertBlock());
3196*67e74705SXin Li return;
3197*67e74705SXin Li } else if (isa<VariableArrayType>(VD->getType()))
3198*67e74705SXin Li Expr.push_back(llvm::dwarf::DW_OP_deref);
3199*67e74705SXin Li } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
3200*67e74705SXin Li // If VD is an anonymous union then Storage represents value for
3201*67e74705SXin Li // all union fields.
3202*67e74705SXin Li const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
3203*67e74705SXin Li if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
3204*67e74705SXin Li // GDB has trouble finding local variables in anonymous unions, so we emit
3205*67e74705SXin Li // artifical local variables for each of the members.
3206*67e74705SXin Li //
3207*67e74705SXin Li // FIXME: Remove this code as soon as GDB supports this.
3208*67e74705SXin Li // The debug info verifier in LLVM operates based on the assumption that a
3209*67e74705SXin Li // variable has the same size as its storage and we had to disable the check
3210*67e74705SXin Li // for artificial variables.
3211*67e74705SXin Li for (const auto *Field : RD->fields()) {
3212*67e74705SXin Li llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
3213*67e74705SXin Li StringRef FieldName = Field->getName();
3214*67e74705SXin Li
3215*67e74705SXin Li // Ignore unnamed fields. Do not ignore unnamed records.
3216*67e74705SXin Li if (FieldName.empty() && !isa<RecordType>(Field->getType()))
3217*67e74705SXin Li continue;
3218*67e74705SXin Li
3219*67e74705SXin Li // Use VarDecl's Tag, Scope and Line number.
3220*67e74705SXin Li auto *D = DBuilder.createAutoVariable(
3221*67e74705SXin Li Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
3222*67e74705SXin Li Flags | llvm::DINode::FlagArtificial);
3223*67e74705SXin Li
3224*67e74705SXin Li // Insert an llvm.dbg.declare into the current block.
3225*67e74705SXin Li DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3226*67e74705SXin Li llvm::DebugLoc::get(Line, Column, Scope),
3227*67e74705SXin Li Builder.GetInsertBlock());
3228*67e74705SXin Li }
3229*67e74705SXin Li }
3230*67e74705SXin Li }
3231*67e74705SXin Li
3232*67e74705SXin Li // Create the descriptor for the variable.
3233*67e74705SXin Li auto *D =
3234*67e74705SXin Li ArgNo
3235*67e74705SXin Li ? DBuilder.createParameterVariable(Scope, Name, *ArgNo, Unit, Line,
3236*67e74705SXin Li Ty, CGM.getLangOpts().Optimize,
3237*67e74705SXin Li Flags)
3238*67e74705SXin Li : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
3239*67e74705SXin Li CGM.getLangOpts().Optimize, Flags);
3240*67e74705SXin Li
3241*67e74705SXin Li // Insert an llvm.dbg.declare into the current block.
3242*67e74705SXin Li DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3243*67e74705SXin Li llvm::DebugLoc::get(Line, Column, Scope),
3244*67e74705SXin Li Builder.GetInsertBlock());
3245*67e74705SXin Li }
3246*67e74705SXin Li
EmitDeclareOfAutoVariable(const VarDecl * VD,llvm::Value * Storage,CGBuilderTy & Builder)3247*67e74705SXin Li void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
3248*67e74705SXin Li llvm::Value *Storage,
3249*67e74705SXin Li CGBuilderTy &Builder) {
3250*67e74705SXin Li assert(DebugKind >= codegenoptions::LimitedDebugInfo);
3251*67e74705SXin Li EmitDeclare(VD, Storage, llvm::None, Builder);
3252*67e74705SXin Li }
3253*67e74705SXin Li
CreateSelfType(const QualType & QualTy,llvm::DIType * Ty)3254*67e74705SXin Li llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
3255*67e74705SXin Li llvm::DIType *Ty) {
3256*67e74705SXin Li llvm::DIType *CachedTy = getTypeOrNull(QualTy);
3257*67e74705SXin Li if (CachedTy)
3258*67e74705SXin Li Ty = CachedTy;
3259*67e74705SXin Li return DBuilder.createObjectPointerType(Ty);
3260*67e74705SXin Li }
3261*67e74705SXin Li
EmitDeclareOfBlockDeclRefVariable(const VarDecl * VD,llvm::Value * Storage,CGBuilderTy & Builder,const CGBlockInfo & blockInfo,llvm::Instruction * InsertPoint)3262*67e74705SXin Li void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
3263*67e74705SXin Li const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
3264*67e74705SXin Li const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
3265*67e74705SXin Li assert(DebugKind >= codegenoptions::LimitedDebugInfo);
3266*67e74705SXin Li assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3267*67e74705SXin Li
3268*67e74705SXin Li if (Builder.GetInsertBlock() == nullptr)
3269*67e74705SXin Li return;
3270*67e74705SXin Li if (VD->hasAttr<NoDebugAttr>())
3271*67e74705SXin Li return;
3272*67e74705SXin Li
3273*67e74705SXin Li bool isByRef = VD->hasAttr<BlocksAttr>();
3274*67e74705SXin Li
3275*67e74705SXin Li uint64_t XOffset = 0;
3276*67e74705SXin Li llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
3277*67e74705SXin Li llvm::DIType *Ty;
3278*67e74705SXin Li if (isByRef)
3279*67e74705SXin Li Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
3280*67e74705SXin Li else
3281*67e74705SXin Li Ty = getOrCreateType(VD->getType(), Unit);
3282*67e74705SXin Li
3283*67e74705SXin Li // Self is passed along as an implicit non-arg variable in a
3284*67e74705SXin Li // block. Mark it as the object pointer.
3285*67e74705SXin Li if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
3286*67e74705SXin Li Ty = CreateSelfType(VD->getType(), Ty);
3287*67e74705SXin Li
3288*67e74705SXin Li // Get location information.
3289*67e74705SXin Li unsigned Line = getLineNumber(VD->getLocation());
3290*67e74705SXin Li unsigned Column = getColumnNumber(VD->getLocation());
3291*67e74705SXin Li
3292*67e74705SXin Li const llvm::DataLayout &target = CGM.getDataLayout();
3293*67e74705SXin Li
3294*67e74705SXin Li CharUnits offset = CharUnits::fromQuantity(
3295*67e74705SXin Li target.getStructLayout(blockInfo.StructureType)
3296*67e74705SXin Li ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
3297*67e74705SXin Li
3298*67e74705SXin Li SmallVector<int64_t, 9> addr;
3299*67e74705SXin Li if (isa<llvm::AllocaInst>(Storage))
3300*67e74705SXin Li addr.push_back(llvm::dwarf::DW_OP_deref);
3301*67e74705SXin Li addr.push_back(llvm::dwarf::DW_OP_plus);
3302*67e74705SXin Li addr.push_back(offset.getQuantity());
3303*67e74705SXin Li if (isByRef) {
3304*67e74705SXin Li addr.push_back(llvm::dwarf::DW_OP_deref);
3305*67e74705SXin Li addr.push_back(llvm::dwarf::DW_OP_plus);
3306*67e74705SXin Li // offset of __forwarding field
3307*67e74705SXin Li offset =
3308*67e74705SXin Li CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
3309*67e74705SXin Li addr.push_back(offset.getQuantity());
3310*67e74705SXin Li addr.push_back(llvm::dwarf::DW_OP_deref);
3311*67e74705SXin Li addr.push_back(llvm::dwarf::DW_OP_plus);
3312*67e74705SXin Li // offset of x field
3313*67e74705SXin Li offset = CGM.getContext().toCharUnitsFromBits(XOffset);
3314*67e74705SXin Li addr.push_back(offset.getQuantity());
3315*67e74705SXin Li }
3316*67e74705SXin Li
3317*67e74705SXin Li // Create the descriptor for the variable.
3318*67e74705SXin Li auto *D = DBuilder.createAutoVariable(
3319*67e74705SXin Li cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
3320*67e74705SXin Li Line, Ty);
3321*67e74705SXin Li
3322*67e74705SXin Li // Insert an llvm.dbg.declare into the current block.
3323*67e74705SXin Li auto DL = llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back());
3324*67e74705SXin Li if (InsertPoint)
3325*67e74705SXin Li DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3326*67e74705SXin Li InsertPoint);
3327*67e74705SXin Li else
3328*67e74705SXin Li DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3329*67e74705SXin Li Builder.GetInsertBlock());
3330*67e74705SXin Li }
3331*67e74705SXin Li
EmitDeclareOfArgVariable(const VarDecl * VD,llvm::Value * AI,unsigned ArgNo,CGBuilderTy & Builder)3332*67e74705SXin Li void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3333*67e74705SXin Li unsigned ArgNo,
3334*67e74705SXin Li CGBuilderTy &Builder) {
3335*67e74705SXin Li assert(DebugKind >= codegenoptions::LimitedDebugInfo);
3336*67e74705SXin Li EmitDeclare(VD, AI, ArgNo, Builder);
3337*67e74705SXin Li }
3338*67e74705SXin Li
3339*67e74705SXin Li namespace {
3340*67e74705SXin Li struct BlockLayoutChunk {
3341*67e74705SXin Li uint64_t OffsetInBits;
3342*67e74705SXin Li const BlockDecl::Capture *Capture;
3343*67e74705SXin Li };
operator <(const BlockLayoutChunk & l,const BlockLayoutChunk & r)3344*67e74705SXin Li bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3345*67e74705SXin Li return l.OffsetInBits < r.OffsetInBits;
3346*67e74705SXin Li }
3347*67e74705SXin Li }
3348*67e74705SXin Li
EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo & block,llvm::Value * Arg,unsigned ArgNo,llvm::Value * LocalAddr,CGBuilderTy & Builder)3349*67e74705SXin Li void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
3350*67e74705SXin Li llvm::Value *Arg,
3351*67e74705SXin Li unsigned ArgNo,
3352*67e74705SXin Li llvm::Value *LocalAddr,
3353*67e74705SXin Li CGBuilderTy &Builder) {
3354*67e74705SXin Li assert(DebugKind >= codegenoptions::LimitedDebugInfo);
3355*67e74705SXin Li ASTContext &C = CGM.getContext();
3356*67e74705SXin Li const BlockDecl *blockDecl = block.getBlockDecl();
3357*67e74705SXin Li
3358*67e74705SXin Li // Collect some general information about the block's location.
3359*67e74705SXin Li SourceLocation loc = blockDecl->getCaretLocation();
3360*67e74705SXin Li llvm::DIFile *tunit = getOrCreateFile(loc);
3361*67e74705SXin Li unsigned line = getLineNumber(loc);
3362*67e74705SXin Li unsigned column = getColumnNumber(loc);
3363*67e74705SXin Li
3364*67e74705SXin Li // Build the debug-info type for the block literal.
3365*67e74705SXin Li getDeclContextDescriptor(blockDecl);
3366*67e74705SXin Li
3367*67e74705SXin Li const llvm::StructLayout *blockLayout =
3368*67e74705SXin Li CGM.getDataLayout().getStructLayout(block.StructureType);
3369*67e74705SXin Li
3370*67e74705SXin Li SmallVector<llvm::Metadata *, 16> fields;
3371*67e74705SXin Li fields.push_back(createFieldType("__isa", C.VoidPtrTy, loc, AS_public,
3372*67e74705SXin Li blockLayout->getElementOffsetInBits(0),
3373*67e74705SXin Li tunit, tunit));
3374*67e74705SXin Li fields.push_back(createFieldType("__flags", C.IntTy, loc, AS_public,
3375*67e74705SXin Li blockLayout->getElementOffsetInBits(1),
3376*67e74705SXin Li tunit, tunit));
3377*67e74705SXin Li fields.push_back(createFieldType("__reserved", C.IntTy, loc, AS_public,
3378*67e74705SXin Li blockLayout->getElementOffsetInBits(2),
3379*67e74705SXin Li tunit, tunit));
3380*67e74705SXin Li auto *FnTy = block.getBlockExpr()->getFunctionType();
3381*67e74705SXin Li auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
3382*67e74705SXin Li fields.push_back(createFieldType("__FuncPtr", FnPtrType, loc, AS_public,
3383*67e74705SXin Li blockLayout->getElementOffsetInBits(3),
3384*67e74705SXin Li tunit, tunit));
3385*67e74705SXin Li fields.push_back(createFieldType(
3386*67e74705SXin Li "__descriptor", C.getPointerType(block.NeedsCopyDispose
3387*67e74705SXin Li ? C.getBlockDescriptorExtendedType()
3388*67e74705SXin Li : C.getBlockDescriptorType()),
3389*67e74705SXin Li loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
3390*67e74705SXin Li
3391*67e74705SXin Li // We want to sort the captures by offset, not because DWARF
3392*67e74705SXin Li // requires this, but because we're paranoid about debuggers.
3393*67e74705SXin Li SmallVector<BlockLayoutChunk, 8> chunks;
3394*67e74705SXin Li
3395*67e74705SXin Li // 'this' capture.
3396*67e74705SXin Li if (blockDecl->capturesCXXThis()) {
3397*67e74705SXin Li BlockLayoutChunk chunk;
3398*67e74705SXin Li chunk.OffsetInBits =
3399*67e74705SXin Li blockLayout->getElementOffsetInBits(block.CXXThisIndex);
3400*67e74705SXin Li chunk.Capture = nullptr;
3401*67e74705SXin Li chunks.push_back(chunk);
3402*67e74705SXin Li }
3403*67e74705SXin Li
3404*67e74705SXin Li // Variable captures.
3405*67e74705SXin Li for (const auto &capture : blockDecl->captures()) {
3406*67e74705SXin Li const VarDecl *variable = capture.getVariable();
3407*67e74705SXin Li const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3408*67e74705SXin Li
3409*67e74705SXin Li // Ignore constant captures.
3410*67e74705SXin Li if (captureInfo.isConstant())
3411*67e74705SXin Li continue;
3412*67e74705SXin Li
3413*67e74705SXin Li BlockLayoutChunk chunk;
3414*67e74705SXin Li chunk.OffsetInBits =
3415*67e74705SXin Li blockLayout->getElementOffsetInBits(captureInfo.getIndex());
3416*67e74705SXin Li chunk.Capture = &capture;
3417*67e74705SXin Li chunks.push_back(chunk);
3418*67e74705SXin Li }
3419*67e74705SXin Li
3420*67e74705SXin Li // Sort by offset.
3421*67e74705SXin Li llvm::array_pod_sort(chunks.begin(), chunks.end());
3422*67e74705SXin Li
3423*67e74705SXin Li for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
3424*67e74705SXin Li e = chunks.end();
3425*67e74705SXin Li i != e; ++i) {
3426*67e74705SXin Li uint64_t offsetInBits = i->OffsetInBits;
3427*67e74705SXin Li const BlockDecl::Capture *capture = i->Capture;
3428*67e74705SXin Li
3429*67e74705SXin Li // If we have a null capture, this must be the C++ 'this' capture.
3430*67e74705SXin Li if (!capture) {
3431*67e74705SXin Li QualType type;
3432*67e74705SXin Li if (auto *Method =
3433*67e74705SXin Li cast_or_null<CXXMethodDecl>(blockDecl->getNonClosureContext()))
3434*67e74705SXin Li type = Method->getThisType(C);
3435*67e74705SXin Li else if (auto *RDecl = dyn_cast<CXXRecordDecl>(blockDecl->getParent()))
3436*67e74705SXin Li type = QualType(RDecl->getTypeForDecl(), 0);
3437*67e74705SXin Li else
3438*67e74705SXin Li llvm_unreachable("unexpected block declcontext");
3439*67e74705SXin Li
3440*67e74705SXin Li fields.push_back(createFieldType("this", type, loc, AS_public,
3441*67e74705SXin Li offsetInBits, tunit, tunit));
3442*67e74705SXin Li continue;
3443*67e74705SXin Li }
3444*67e74705SXin Li
3445*67e74705SXin Li const VarDecl *variable = capture->getVariable();
3446*67e74705SXin Li StringRef name = variable->getName();
3447*67e74705SXin Li
3448*67e74705SXin Li llvm::DIType *fieldType;
3449*67e74705SXin Li if (capture->isByRef()) {
3450*67e74705SXin Li TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
3451*67e74705SXin Li
3452*67e74705SXin Li // FIXME: this creates a second copy of this type!
3453*67e74705SXin Li uint64_t xoffset;
3454*67e74705SXin Li fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
3455*67e74705SXin Li fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3456*67e74705SXin Li fieldType =
3457*67e74705SXin Li DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3458*67e74705SXin Li PtrInfo.Align, offsetInBits, 0, fieldType);
3459*67e74705SXin Li } else {
3460*67e74705SXin Li fieldType = createFieldType(name, variable->getType(), loc, AS_public,
3461*67e74705SXin Li offsetInBits, tunit, tunit);
3462*67e74705SXin Li }
3463*67e74705SXin Li fields.push_back(fieldType);
3464*67e74705SXin Li }
3465*67e74705SXin Li
3466*67e74705SXin Li SmallString<36> typeName;
3467*67e74705SXin Li llvm::raw_svector_ostream(typeName) << "__block_literal_"
3468*67e74705SXin Li << CGM.getUniqueBlockCount();
3469*67e74705SXin Li
3470*67e74705SXin Li llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
3471*67e74705SXin Li
3472*67e74705SXin Li llvm::DIType *type = DBuilder.createStructType(
3473*67e74705SXin Li tunit, typeName.str(), tunit, line,
3474*67e74705SXin Li CGM.getContext().toBits(block.BlockSize),
3475*67e74705SXin Li CGM.getContext().toBits(block.BlockAlign), 0, nullptr, fieldsArray);
3476*67e74705SXin Li type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3477*67e74705SXin Li
3478*67e74705SXin Li // Get overall information about the block.
3479*67e74705SXin Li unsigned flags = llvm::DINode::FlagArtificial;
3480*67e74705SXin Li auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
3481*67e74705SXin Li
3482*67e74705SXin Li // Create the descriptor for the parameter.
3483*67e74705SXin Li auto *debugVar = DBuilder.createParameterVariable(
3484*67e74705SXin Li scope, Arg->getName(), ArgNo, tunit, line, type,
3485*67e74705SXin Li CGM.getLangOpts().Optimize, flags);
3486*67e74705SXin Li
3487*67e74705SXin Li if (LocalAddr) {
3488*67e74705SXin Li // Insert an llvm.dbg.value into the current block.
3489*67e74705SXin Li DBuilder.insertDbgValueIntrinsic(
3490*67e74705SXin Li LocalAddr, 0, debugVar, DBuilder.createExpression(),
3491*67e74705SXin Li llvm::DebugLoc::get(line, column, scope), Builder.GetInsertBlock());
3492*67e74705SXin Li }
3493*67e74705SXin Li
3494*67e74705SXin Li // Insert an llvm.dbg.declare into the current block.
3495*67e74705SXin Li DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
3496*67e74705SXin Li llvm::DebugLoc::get(line, column, scope),
3497*67e74705SXin Li Builder.GetInsertBlock());
3498*67e74705SXin Li }
3499*67e74705SXin Li
3500*67e74705SXin Li llvm::DIDerivedType *
getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl * D)3501*67e74705SXin Li CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3502*67e74705SXin Li if (!D->isStaticDataMember())
3503*67e74705SXin Li return nullptr;
3504*67e74705SXin Li
3505*67e74705SXin Li auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
3506*67e74705SXin Li if (MI != StaticDataMemberCache.end()) {
3507*67e74705SXin Li assert(MI->second && "Static data member declaration should still exist");
3508*67e74705SXin Li return MI->second;
3509*67e74705SXin Li }
3510*67e74705SXin Li
3511*67e74705SXin Li // If the member wasn't found in the cache, lazily construct and add it to the
3512*67e74705SXin Li // type (used when a limited form of the type is emitted).
3513*67e74705SXin Li auto DC = D->getDeclContext();
3514*67e74705SXin Li auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D));
3515*67e74705SXin Li return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
3516*67e74705SXin Li }
3517*67e74705SXin Li
CollectAnonRecordDecls(const RecordDecl * RD,llvm::DIFile * Unit,unsigned LineNo,StringRef LinkageName,llvm::GlobalVariable * Var,llvm::DIScope * DContext)3518*67e74705SXin Li llvm::DIGlobalVariable *CGDebugInfo::CollectAnonRecordDecls(
3519*67e74705SXin Li const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3520*67e74705SXin Li StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
3521*67e74705SXin Li llvm::DIGlobalVariable *GV = nullptr;
3522*67e74705SXin Li
3523*67e74705SXin Li for (const auto *Field : RD->fields()) {
3524*67e74705SXin Li llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
3525*67e74705SXin Li StringRef FieldName = Field->getName();
3526*67e74705SXin Li
3527*67e74705SXin Li // Ignore unnamed fields, but recurse into anonymous records.
3528*67e74705SXin Li if (FieldName.empty()) {
3529*67e74705SXin Li const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3530*67e74705SXin Li if (RT)
3531*67e74705SXin Li GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3532*67e74705SXin Li Var, DContext);
3533*67e74705SXin Li continue;
3534*67e74705SXin Li }
3535*67e74705SXin Li // Use VarDecl's Tag, Scope and Line number.
3536*67e74705SXin Li GV = DBuilder.createGlobalVariable(DContext, FieldName, LinkageName, Unit,
3537*67e74705SXin Li LineNo, FieldTy,
3538*67e74705SXin Li Var->hasLocalLinkage(), Var, nullptr);
3539*67e74705SXin Li }
3540*67e74705SXin Li return GV;
3541*67e74705SXin Li }
3542*67e74705SXin Li
EmitGlobalVariable(llvm::GlobalVariable * Var,const VarDecl * D)3543*67e74705SXin Li void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
3544*67e74705SXin Li const VarDecl *D) {
3545*67e74705SXin Li assert(DebugKind >= codegenoptions::LimitedDebugInfo);
3546*67e74705SXin Li if (D->hasAttr<NoDebugAttr>())
3547*67e74705SXin Li return;
3548*67e74705SXin Li // Create global variable debug descriptor.
3549*67e74705SXin Li llvm::DIFile *Unit = nullptr;
3550*67e74705SXin Li llvm::DIScope *DContext = nullptr;
3551*67e74705SXin Li unsigned LineNo;
3552*67e74705SXin Li StringRef DeclName, LinkageName;
3553*67e74705SXin Li QualType T;
3554*67e74705SXin Li collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
3555*67e74705SXin Li
3556*67e74705SXin Li // Attempt to store one global variable for the declaration - even if we
3557*67e74705SXin Li // emit a lot of fields.
3558*67e74705SXin Li llvm::DIGlobalVariable *GV = nullptr;
3559*67e74705SXin Li
3560*67e74705SXin Li // If this is an anonymous union then we'll want to emit a global
3561*67e74705SXin Li // variable for each member of the anonymous union so that it's possible
3562*67e74705SXin Li // to find the name of any field in the union.
3563*67e74705SXin Li if (T->isUnionType() && DeclName.empty()) {
3564*67e74705SXin Li const RecordDecl *RD = T->castAs<RecordType>()->getDecl();
3565*67e74705SXin Li assert(RD->isAnonymousStructOrUnion() &&
3566*67e74705SXin Li "unnamed non-anonymous struct or union?");
3567*67e74705SXin Li GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3568*67e74705SXin Li } else {
3569*67e74705SXin Li GV = DBuilder.createGlobalVariable(
3570*67e74705SXin Li DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3571*67e74705SXin Li Var->hasLocalLinkage(), Var,
3572*67e74705SXin Li getOrCreateStaticDataMemberDeclarationOrNull(D));
3573*67e74705SXin Li }
3574*67e74705SXin Li DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(GV));
3575*67e74705SXin Li }
3576*67e74705SXin Li
EmitGlobalVariable(const ValueDecl * VD,llvm::Constant * Init)3577*67e74705SXin Li void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3578*67e74705SXin Li llvm::Constant *Init) {
3579*67e74705SXin Li assert(DebugKind >= codegenoptions::LimitedDebugInfo);
3580*67e74705SXin Li if (VD->hasAttr<NoDebugAttr>())
3581*67e74705SXin Li return;
3582*67e74705SXin Li // Create the descriptor for the variable.
3583*67e74705SXin Li llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
3584*67e74705SXin Li StringRef Name = VD->getName();
3585*67e74705SXin Li llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
3586*67e74705SXin Li if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3587*67e74705SXin Li const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3588*67e74705SXin Li assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3589*67e74705SXin Li Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3590*67e74705SXin Li }
3591*67e74705SXin Li // Do not use global variables for enums.
3592*67e74705SXin Li //
3593*67e74705SXin Li // FIXME: why not?
3594*67e74705SXin Li if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
3595*67e74705SXin Li return;
3596*67e74705SXin Li // Do not emit separate definitions for function local const/statics.
3597*67e74705SXin Li if (isa<FunctionDecl>(VD->getDeclContext()))
3598*67e74705SXin Li return;
3599*67e74705SXin Li VD = cast<ValueDecl>(VD->getCanonicalDecl());
3600*67e74705SXin Li auto *VarD = cast<VarDecl>(VD);
3601*67e74705SXin Li if (VarD->isStaticDataMember()) {
3602*67e74705SXin Li auto *RD = cast<RecordDecl>(VarD->getDeclContext());
3603*67e74705SXin Li getDeclContextDescriptor(VarD);
3604*67e74705SXin Li // Ensure that the type is retained even though it's otherwise unreferenced.
3605*67e74705SXin Li //
3606*67e74705SXin Li // FIXME: This is probably unnecessary, since Ty should reference RD
3607*67e74705SXin Li // through its scope.
3608*67e74705SXin Li RetainedTypes.push_back(
3609*67e74705SXin Li CGM.getContext().getRecordType(RD).getAsOpaquePtr());
3610*67e74705SXin Li return;
3611*67e74705SXin Li }
3612*67e74705SXin Li
3613*67e74705SXin Li llvm::DIScope *DContext = getDeclContextDescriptor(VD);
3614*67e74705SXin Li
3615*67e74705SXin Li auto &GV = DeclCache[VD];
3616*67e74705SXin Li if (GV)
3617*67e74705SXin Li return;
3618*67e74705SXin Li GV.reset(DBuilder.createGlobalVariable(
3619*67e74705SXin Li DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
3620*67e74705SXin Li true, Init, getOrCreateStaticDataMemberDeclarationOrNull(VarD)));
3621*67e74705SXin Li }
3622*67e74705SXin Li
getCurrentContextDescriptor(const Decl * D)3623*67e74705SXin Li llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
3624*67e74705SXin Li if (!LexicalBlockStack.empty())
3625*67e74705SXin Li return LexicalBlockStack.back();
3626*67e74705SXin Li llvm::DIScope *Mod = getParentModuleOrNull(D);
3627*67e74705SXin Li return getContextDescriptor(D, Mod ? Mod : TheCU);
3628*67e74705SXin Li }
3629*67e74705SXin Li
EmitUsingDirective(const UsingDirectiveDecl & UD)3630*67e74705SXin Li void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
3631*67e74705SXin Li if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
3632*67e74705SXin Li return;
3633*67e74705SXin Li const NamespaceDecl *NSDecl = UD.getNominatedNamespace();
3634*67e74705SXin Li if (!NSDecl->isAnonymousNamespace() ||
3635*67e74705SXin Li CGM.getCodeGenOpts().DebugExplicitImport) {
3636*67e74705SXin Li DBuilder.createImportedModule(
3637*67e74705SXin Li getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3638*67e74705SXin Li getOrCreateNameSpace(NSDecl),
3639*67e74705SXin Li getLineNumber(UD.getLocation()));
3640*67e74705SXin Li }
3641*67e74705SXin Li }
3642*67e74705SXin Li
EmitUsingDecl(const UsingDecl & UD)3643*67e74705SXin Li void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
3644*67e74705SXin Li if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
3645*67e74705SXin Li return;
3646*67e74705SXin Li assert(UD.shadow_size() &&
3647*67e74705SXin Li "We shouldn't be codegening an invalid UsingDecl containing no decls");
3648*67e74705SXin Li // Emitting one decl is sufficient - debuggers can detect that this is an
3649*67e74705SXin Li // overloaded name & provide lookup for all the overloads.
3650*67e74705SXin Li const UsingShadowDecl &USD = **UD.shadow_begin();
3651*67e74705SXin Li if (llvm::DINode *Target =
3652*67e74705SXin Li getDeclarationOrDefinition(USD.getUnderlyingDecl()))
3653*67e74705SXin Li DBuilder.createImportedDeclaration(
3654*67e74705SXin Li getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3655*67e74705SXin Li getLineNumber(USD.getLocation()));
3656*67e74705SXin Li }
3657*67e74705SXin Li
EmitImportDecl(const ImportDecl & ID)3658*67e74705SXin Li void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
3659*67e74705SXin Li if (CGM.getCodeGenOpts().getDebuggerTuning() != llvm::DebuggerKind::LLDB)
3660*67e74705SXin Li return;
3661*67e74705SXin Li if (Module *M = ID.getImportedModule()) {
3662*67e74705SXin Li auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
3663*67e74705SXin Li DBuilder.createImportedDeclaration(
3664*67e74705SXin Li getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
3665*67e74705SXin Li getOrCreateModuleRef(Info, DebugTypeExtRefs),
3666*67e74705SXin Li getLineNumber(ID.getLocation()));
3667*67e74705SXin Li }
3668*67e74705SXin Li }
3669*67e74705SXin Li
3670*67e74705SXin Li llvm::DIImportedEntity *
EmitNamespaceAlias(const NamespaceAliasDecl & NA)3671*67e74705SXin Li CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
3672*67e74705SXin Li if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
3673*67e74705SXin Li return nullptr;
3674*67e74705SXin Li auto &VH = NamespaceAliasCache[&NA];
3675*67e74705SXin Li if (VH)
3676*67e74705SXin Li return cast<llvm::DIImportedEntity>(VH);
3677*67e74705SXin Li llvm::DIImportedEntity *R;
3678*67e74705SXin Li if (const NamespaceAliasDecl *Underlying =
3679*67e74705SXin Li dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3680*67e74705SXin Li // This could cache & dedup here rather than relying on metadata deduping.
3681*67e74705SXin Li R = DBuilder.createImportedDeclaration(
3682*67e74705SXin Li getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3683*67e74705SXin Li EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3684*67e74705SXin Li NA.getName());
3685*67e74705SXin Li else
3686*67e74705SXin Li R = DBuilder.createImportedDeclaration(
3687*67e74705SXin Li getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3688*67e74705SXin Li getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3689*67e74705SXin Li getLineNumber(NA.getLocation()), NA.getName());
3690*67e74705SXin Li VH.reset(R);
3691*67e74705SXin Li return R;
3692*67e74705SXin Li }
3693*67e74705SXin Li
3694*67e74705SXin Li llvm::DINamespace *
getOrCreateNameSpace(const NamespaceDecl * NSDecl)3695*67e74705SXin Li CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
3696*67e74705SXin Li NSDecl = NSDecl->getCanonicalDecl();
3697*67e74705SXin Li auto I = NameSpaceCache.find(NSDecl);
3698*67e74705SXin Li if (I != NameSpaceCache.end())
3699*67e74705SXin Li return cast<llvm::DINamespace>(I->second);
3700*67e74705SXin Li
3701*67e74705SXin Li unsigned LineNo = getLineNumber(NSDecl->getLocation());
3702*67e74705SXin Li llvm::DIFile *FileD = getOrCreateFile(NSDecl->getLocation());
3703*67e74705SXin Li llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
3704*67e74705SXin Li llvm::DINamespace *NS =
3705*67e74705SXin Li DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
3706*67e74705SXin Li NameSpaceCache[NSDecl].reset(NS);
3707*67e74705SXin Li return NS;
3708*67e74705SXin Li }
3709*67e74705SXin Li
setDwoId(uint64_t Signature)3710*67e74705SXin Li void CGDebugInfo::setDwoId(uint64_t Signature) {
3711*67e74705SXin Li assert(TheCU && "no main compile unit");
3712*67e74705SXin Li TheCU->setDWOId(Signature);
3713*67e74705SXin Li }
3714*67e74705SXin Li
3715*67e74705SXin Li
finalize()3716*67e74705SXin Li void CGDebugInfo::finalize() {
3717*67e74705SXin Li // Creating types might create further types - invalidating the current
3718*67e74705SXin Li // element and the size(), so don't cache/reference them.
3719*67e74705SXin Li for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3720*67e74705SXin Li ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
3721*67e74705SXin Li llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
3722*67e74705SXin Li ? CreateTypeDefinition(E.Type, E.Unit)
3723*67e74705SXin Li : E.Decl;
3724*67e74705SXin Li DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
3725*67e74705SXin Li }
3726*67e74705SXin Li
3727*67e74705SXin Li for (auto p : ReplaceMap) {
3728*67e74705SXin Li assert(p.second);
3729*67e74705SXin Li auto *Ty = cast<llvm::DIType>(p.second);
3730*67e74705SXin Li assert(Ty->isForwardDecl());
3731*67e74705SXin Li
3732*67e74705SXin Li auto it = TypeCache.find(p.first);
3733*67e74705SXin Li assert(it != TypeCache.end());
3734*67e74705SXin Li assert(it->second);
3735*67e74705SXin Li
3736*67e74705SXin Li DBuilder.replaceTemporary(llvm::TempDIType(Ty),
3737*67e74705SXin Li cast<llvm::DIType>(it->second));
3738*67e74705SXin Li }
3739*67e74705SXin Li
3740*67e74705SXin Li for (const auto &p : FwdDeclReplaceMap) {
3741*67e74705SXin Li assert(p.second);
3742*67e74705SXin Li llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
3743*67e74705SXin Li llvm::Metadata *Repl;
3744*67e74705SXin Li
3745*67e74705SXin Li auto it = DeclCache.find(p.first);
3746*67e74705SXin Li // If there has been no definition for the declaration, call RAUW
3747*67e74705SXin Li // with ourselves, that will destroy the temporary MDNode and
3748*67e74705SXin Li // replace it with a standard one, avoiding leaking memory.
3749*67e74705SXin Li if (it == DeclCache.end())
3750*67e74705SXin Li Repl = p.second;
3751*67e74705SXin Li else
3752*67e74705SXin Li Repl = it->second;
3753*67e74705SXin Li
3754*67e74705SXin Li DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
3755*67e74705SXin Li }
3756*67e74705SXin Li
3757*67e74705SXin Li // We keep our own list of retained types, because we need to look
3758*67e74705SXin Li // up the final type in the type cache.
3759*67e74705SXin Li for (auto &RT : RetainedTypes)
3760*67e74705SXin Li if (auto MD = TypeCache[RT])
3761*67e74705SXin Li DBuilder.retainType(cast<llvm::DIType>(MD));
3762*67e74705SXin Li
3763*67e74705SXin Li DBuilder.finalize();
3764*67e74705SXin Li }
3765*67e74705SXin Li
EmitExplicitCastType(QualType Ty)3766*67e74705SXin Li void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
3767*67e74705SXin Li if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
3768*67e74705SXin Li return;
3769*67e74705SXin Li
3770*67e74705SXin Li if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
3771*67e74705SXin Li // Don't ignore in case of explicit cast where it is referenced indirectly.
3772*67e74705SXin Li DBuilder.retainType(DieTy);
3773*67e74705SXin Li }
3774