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