blob: c3e1c5bf2bf2c3457c184cb3ff24b0eba880eac8 [file] [log] [blame]
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the debug information generation while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGDebugInfo.h"
15#include "CGBlocks.h"
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -070016#include "CGRecordLayout.h"
David Blaikie9dfd2432013-05-10 21:53:14 +000017#include "CGCXXABI.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000018#include "CGObjCRuntime.h"
19#include "CodeGenFunction.h"
20#include "CodeGenModule.h"
21#include "clang/AST/ASTContext.h"
22#include "clang/AST/DeclFriend.h"
23#include "clang/AST/DeclObjC.h"
24#include "clang/AST/DeclTemplate.h"
25#include "clang/AST/Expr.h"
26#include "clang/AST/RecordLayout.h"
27#include "clang/Basic/FileManager.h"
28#include "clang/Basic/SourceManager.h"
29#include "clang/Basic/Version.h"
30#include "clang/Frontend/CodeGenOptions.h"
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -080031#include "clang/Lex/HeaderSearchOptions.h"
32#include "clang/Lex/ModuleMap.h"
33#include "clang/Lex/PreprocessorOptions.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000034#include "llvm/ADT/SmallVector.h"
35#include "llvm/ADT/StringExtras.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000036#include "llvm/IR/Constants.h"
37#include "llvm/IR/DataLayout.h"
38#include "llvm/IR/DerivedTypes.h"
39#include "llvm/IR/Instructions.h"
40#include "llvm/IR/Intrinsics.h"
41#include "llvm/IR/Module.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000042#include "llvm/Support/FileSystem.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070043#include "llvm/Support/Path.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000044using namespace clang;
45using namespace clang::CodeGen;
46
47CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Eric Christopher688cf5b2013-07-14 21:12:44 +000048 : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -080049 DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs),
Eric Christopher688cf5b2013-07-14 21:12:44 +000050 DBuilder(CGM.getModule()) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -080051 for (const auto &KV : CGM.getCodeGenOpts().DebugPrefixMap)
52 DebugPrefixMap[KV.first] = KV.second;
Guy Benyei7f92f2d2012-12-18 14:30:41 +000053 CreateCompileUnit();
54}
55
56CGDebugInfo::~CGDebugInfo() {
57 assert(LexicalBlockStack.empty() &&
58 "Region stack mismatch, stack not empty!");
59}
60
Stephen Hines0e2c34f2015-03-23 12:09:02 -070061ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
62 SourceLocation TemporaryLocation)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -080063 : CGF(&CGF) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -070064 init(TemporaryLocation);
65}
66
67ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
68 bool DefaultToEmpty,
69 SourceLocation TemporaryLocation)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -080070 : CGF(&CGF) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -070071 init(TemporaryLocation, DefaultToEmpty);
72}
73
74void ApplyDebugLocation::init(SourceLocation TemporaryLocation,
75 bool DefaultToEmpty) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -080076 auto *DI = CGF->getDebugInfo();
77 if (!DI) {
78 CGF = nullptr;
79 return;
Adrian Prantled6bbe42013-07-18 00:28:02 +000080 }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -080081
82 OriginalLocation = CGF->Builder.getCurrentDebugLocation();
83 if (TemporaryLocation.isValid()) {
84 DI->EmitLocation(CGF->Builder, TemporaryLocation);
85 return;
86 }
87
88 if (DefaultToEmpty) {
89 CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc());
90 return;
91 }
92
93 // Construct a location that has a valid scope, but no line info.
94 assert(!DI->LexicalBlockStack.empty());
95 CGF->Builder.SetCurrentDebugLocation(
96 llvm::DebugLoc::get(0, 0, DI->LexicalBlockStack.back()));
Adrian Prantled6bbe42013-07-18 00:28:02 +000097}
98
Stephen Hines0e2c34f2015-03-23 12:09:02 -070099ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800100 : CGF(&CGF) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700101 init(E->getExprLoc());
Stephen Hines651f13c2014-04-23 16:59:28 -0700102}
103
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700104ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800105 : CGF(&CGF) {
106 if (!CGF.getDebugInfo()) {
107 this->CGF = nullptr;
108 return;
Adrian Prantled6bbe42013-07-18 00:28:02 +0000109 }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800110 OriginalLocation = CGF.Builder.getCurrentDebugLocation();
111 if (Loc)
112 CGF.Builder.SetCurrentDebugLocation(std::move(Loc));
Adrian Prantled6bbe42013-07-18 00:28:02 +0000113}
114
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700115ApplyDebugLocation::~ApplyDebugLocation() {
116 // Query CGF so the location isn't overwritten when location updates are
117 // temporarily disabled (for C++ default function arguments)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800118 if (CGF)
119 CGF->Builder.SetCurrentDebugLocation(std::move(OriginalLocation));
Adrian Prantled6bbe42013-07-18 00:28:02 +0000120}
121
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000122void CGDebugInfo::setLocation(SourceLocation Loc) {
123 // If the new location isn't valid return.
Stephen Hines176edba2014-12-01 14:53:08 -0800124 if (Loc.isInvalid())
125 return;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000126
127 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
128
129 // If we've changed files in the middle of a lexical scope go ahead
130 // and create a new lexical scope with file node if it's different
131 // from the one in the scope.
Stephen Hines176edba2014-12-01 14:53:08 -0800132 if (LexicalBlockStack.empty())
133 return;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000134
135 SourceManager &SM = CGM.getContext().getSourceManager();
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700136 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000137 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000138
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -0700139 if (PCLoc.isInvalid() || Scope->getFilename() == PCLoc.getFilename())
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000140 return;
141
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700142 if (auto *LBF = dyn_cast<llvm::DILexicalBlockFile>(Scope)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000143 LexicalBlockStack.pop_back();
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700144 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlockFile(
145 LBF->getScope(), getOrCreateFile(CurLoc)));
146 } else if (isa<llvm::DILexicalBlock>(Scope) ||
147 isa<llvm::DISubprogram>(Scope)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000148 LexicalBlockStack.pop_back();
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700149 LexicalBlockStack.emplace_back(
150 DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc)));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000151 }
152}
153
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800154llvm::DIScope *CGDebugInfo::getDeclContextDescriptor(const Decl *D) {
155 llvm::DIScope *Mod = getParentModuleOrNull(D);
156 return getContextDescriptor(cast<Decl>(D->getDeclContext()),
157 Mod ? Mod : TheCU);
158}
159
160llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context,
161 llvm::DIScope *Default) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000162 if (!Context)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800163 return Default;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000164
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700165 auto I = RegionMap.find(Context);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000166 if (I != RegionMap.end()) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700167 llvm::Metadata *V = I->second;
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700168 return dyn_cast_or_null<llvm::DIScope>(V);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000169 }
170
171 // Check namespace.
172 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
David Blaikiebb000792013-04-19 06:56:38 +0000173 return getOrCreateNameSpace(NSDecl);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000174
David Blaikiebb000792013-04-19 06:56:38 +0000175 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context))
176 if (!RDecl->isDependentType())
177 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Stephen Hines176edba2014-12-01 14:53:08 -0800178 getOrCreateMainFile());
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800179 return Default;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000180}
181
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000182StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Stephen Hines176edba2014-12-01 14:53:08 -0800183 assert(FD && "Invalid FunctionDecl!");
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000184 IdentifierInfo *FII = FD->getIdentifier();
Stephen Hines176edba2014-12-01 14:53:08 -0800185 FunctionTemplateSpecializationInfo *Info =
186 FD->getTemplateSpecializationInfo();
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800187
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700188 // Emit the unqualified name in normal operation. LLVM and the debugger can
189 // compute the fully qualified name from the scope chain. If we're only
190 // emitting line table info, there won't be any scope chains, so emit the
191 // fully qualified name here so that stack traces are more accurate.
192 // FIXME: Do this when emitting DWARF as well as when emitting CodeView after
193 // evaluating the size impact.
194 bool UseQualifiedName = DebugKind == codegenoptions::DebugLineTablesOnly &&
195 CGM.getCodeGenOpts().EmitCodeView;
196
197 if (!Info && FII && !UseQualifiedName)
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000198 return FII->getName();
199
Benjamin Kramer5eada842013-02-22 15:46:01 +0000200 SmallString<128> NS;
201 llvm::raw_svector_ostream OS(NS);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800202 PrintingPolicy Policy(CGM.getLangOpts());
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700203 Policy.MSVCFormatting = CGM.getCodeGenOpts().EmitCodeView;
204 if (!UseQualifiedName)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800205 FD->printName(OS);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700206 else
207 FD->printQualifiedName(OS, Policy);
208
209 // Add any template specialization args.
210 if (Info) {
211 const TemplateArgumentList *TArgs = Info->TemplateArguments;
212 TemplateSpecializationType::PrintTemplateArgumentList(OS, TArgs->asArray(),
213 Policy);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000214 }
215
216 // Copy this name on the side and use its reference.
Benjamin Kramer84953792013-09-09 16:39:06 +0000217 return internString(OS.str());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000218}
219
220StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
221 SmallString<256> MethodName;
222 llvm::raw_svector_ostream OS(MethodName);
223 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
224 const DeclContext *DC = OMD->getDeclContext();
Eric Christopher6537f082013-05-16 00:45:12 +0000225 if (const ObjCImplementationDecl *OID =
Stephen Hines176edba2014-12-01 14:53:08 -0800226 dyn_cast<const ObjCImplementationDecl>(DC)) {
227 OS << OID->getName();
Eric Christopher6537f082013-05-16 00:45:12 +0000228 } else if (const ObjCInterfaceDecl *OID =
Stephen Hines176edba2014-12-01 14:53:08 -0800229 dyn_cast<const ObjCInterfaceDecl>(DC)) {
230 OS << OID->getName();
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800231 } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(DC)) {
232 if (OC->IsClassExtension()) {
233 OS << OC->getClassInterface()->getName();
234 } else {
235 OS << ((const NamedDecl *)OC)->getIdentifier()->getNameStart() << '('
236 << OC->getIdentifier()->getNameStart() << ')';
237 }
Eric Christopher6537f082013-05-16 00:45:12 +0000238 } else if (const ObjCCategoryImplDecl *OCD =
Stephen Hines176edba2014-12-01 14:53:08 -0800239 dyn_cast<const ObjCCategoryImplDecl>(DC)) {
240 OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '('
241 << OCD->getIdentifier()->getNameStart() << ')';
Adrian Prantlb5092242013-05-17 23:58:45 +0000242 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl687ecae2013-05-17 23:49:10 +0000243 // We can extract the type of the class from the self pointer.
Stephen Hines176edba2014-12-01 14:53:08 -0800244 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl687ecae2013-05-17 23:49:10 +0000245 QualType ClassTy =
Stephen Hines176edba2014-12-01 14:53:08 -0800246 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl687ecae2013-05-17 23:49:10 +0000247 ClassTy.print(OS, PrintingPolicy(LangOptions()));
248 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000249 }
250 OS << ' ' << OMD->getSelector().getAsString() << ']';
251
Benjamin Kramer84953792013-09-09 16:39:06 +0000252 return internString(OS.str());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000253}
254
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000255StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer84953792013-09-09 16:39:06 +0000256 return internString(S.getAsString());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000257}
258
Stephen Hines176edba2014-12-01 14:53:08 -0800259StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700260 if (isa<ClassTemplateSpecializationDecl>(RD)) {
261 SmallString<128> Name;
Stephen Hines651f13c2014-04-23 16:59:28 -0700262 llvm::raw_svector_ostream OS(Name);
263 RD->getNameForDiagnostic(OS, CGM.getContext().getPrintingPolicy(),
264 /*Qualified*/ false);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700265
266 // Copy this name on the side and use its reference.
267 return internString(Name);
Benjamin Kramer5eada842013-02-22 15:46:01 +0000268 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000269
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700270 // quick optimization to avoid having to intern strings that are already
271 // stored reliably elsewhere
272 if (const IdentifierInfo *II = RD->getIdentifier())
273 return II->getName();
274
275 // The CodeView printer in LLVM wants to see the names of unnamed types: it is
276 // used to reconstruct the fully qualified type names.
277 if (CGM.getCodeGenOpts().EmitCodeView) {
278 if (const TypedefNameDecl *D = RD->getTypedefNameForAnonDecl()) {
279 assert(RD->getDeclContext() == D->getDeclContext() &&
280 "Typedef should not be in another decl context!");
281 assert(D->getDeclName().getAsIdentifierInfo() &&
282 "Typedef was not named!");
283 return D->getDeclName().getAsIdentifierInfo()->getName();
284 }
285
286 if (CGM.getLangOpts().CPlusPlus) {
287 StringRef Name;
288
289 ASTContext &Context = CGM.getContext();
290 if (const DeclaratorDecl *DD = Context.getDeclaratorForUnnamedTagDecl(RD))
291 // Anonymous types without a name for linkage purposes have their
292 // declarator mangled in if they have one.
293 Name = DD->getName();
294 else if (const TypedefNameDecl *TND =
295 Context.getTypedefNameForUnnamedTagDecl(RD))
296 // Anonymous types without a name for linkage purposes have their
297 // associate typedef mangled in if they have one.
298 Name = TND->getName();
299
300 if (!Name.empty()) {
301 SmallString<256> UnnamedType("<unnamed-type-");
302 UnnamedType += Name;
303 UnnamedType += '>';
304 return internString(UnnamedType);
305 }
306 }
307 }
308
309 return StringRef();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000310}
311
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700312llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000313 if (!Loc.isValid())
314 // If Location is not valid then use main input file.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800315 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
316 remapDIPath(TheCU->getDirectory()));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000317
318 SourceManager &SM = CGM.getContext().getSourceManager();
319 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
320
321 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
322 // If the location is not valid then use main input file.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800323 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
324 remapDIPath(TheCU->getDirectory()));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000325
326 // Cache the results.
327 const char *fname = PLoc.getFilename();
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700328 auto it = DIFileCache.find(fname);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000329
330 if (it != DIFileCache.end()) {
331 // Verify that the information still exists.
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700332 if (llvm::Metadata *V = it->second)
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700333 return cast<llvm::DIFile>(V);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000334 }
335
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800336 llvm::DIFile *F = DBuilder.createFile(remapDIPath(PLoc.getFilename()),
337 remapDIPath(getCurrentDirname()));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000338
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700339 DIFileCache[fname].reset(F);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000340 return F;
341}
342
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700343llvm::DIFile *CGDebugInfo::getOrCreateMainFile() {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800344 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
345 remapDIPath(TheCU->getDirectory()));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000346}
347
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800348std::string CGDebugInfo::remapDIPath(StringRef Path) const {
349 for (const auto &Entry : DebugPrefixMap)
350 if (Path.startswith(Entry.first))
351 return (Twine(Entry.second) + Path.substr(Entry.first.size())).str();
352 return Path.str();
353}
354
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000355unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
356 if (Loc.isInvalid() && CurLoc.isInvalid())
357 return 0;
358 SourceManager &SM = CGM.getContext().getSourceManager();
359 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Stephen Hines176edba2014-12-01 14:53:08 -0800360 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000361}
362
Adrian Prantl00df5ea2013-03-12 20:43:25 +0000363unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000364 // We may not want column information at all.
Adrian Prantl00df5ea2013-03-12 20:43:25 +0000365 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000366 return 0;
367
368 // If the location is invalid then use the current column.
369 if (Loc.isInvalid() && CurLoc.isInvalid())
370 return 0;
371 SourceManager &SM = CGM.getContext().getSourceManager();
372 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Stephen Hines176edba2014-12-01 14:53:08 -0800373 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000374}
375
376StringRef CGDebugInfo::getCurrentDirname() {
377 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
378 return CGM.getCodeGenOpts().DebugCompilationDir;
379
380 if (!CWDName.empty())
381 return CWDName;
382 SmallString<256> CWD;
383 llvm::sys::fs::current_path(CWD);
Benjamin Kramer84953792013-09-09 16:39:06 +0000384 return CWDName = internString(CWD);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000385}
386
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000387void CGDebugInfo::CreateCompileUnit() {
388
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700389 // Should we be asking the SourceManager for the main file name, instead of
390 // accepting it as an argument? This just causes the main file name to
391 // mismatch with source locations and create extra lexical scopes or
392 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
393 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
394 // because that's what the SourceManager says)
395
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000396 // Get absolute path name.
397 SourceManager &SM = CGM.getContext().getSourceManager();
398 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
399 if (MainFileName.empty())
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700400 MainFileName = "<stdin>";
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000401
402 // The main file name provided via the "-main-file-name" option contains just
403 // the file name itself with no path information. This file name may have had
404 // a relative path, so we look into the actual file entry for the main
405 // file to determine the real absolute path for the file.
406 std::string MainFileDir;
407 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800408 MainFileDir = remapDIPath(MainFile->getDir()->getName());
Yaron Keren921ac4d2013-10-21 20:07:37 +0000409 if (MainFileDir != ".") {
Stephen Hines651f13c2014-04-23 16:59:28 -0700410 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
411 llvm::sys::path::append(MainFileDirSS, MainFileName);
412 MainFileName = MainFileDirSS.str();
Yaron Keren921ac4d2013-10-21 20:07:37 +0000413 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000414 }
415
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700416 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000417 const LangOptions &LO = CGM.getLangOpts();
418 if (LO.CPlusPlus) {
419 if (LO.ObjC1)
420 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
421 else
422 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
423 } else if (LO.ObjC1) {
424 LangTag = llvm::dwarf::DW_LANG_ObjC;
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700425 } else if (LO.RenderScript) {
Ewan Crawfordf6ea0212016-02-01 16:56:53 +0000426 LangTag = llvm::dwarf::DW_LANG_GOOGLE_RenderScript;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000427 } else if (LO.C99) {
428 LangTag = llvm::dwarf::DW_LANG_C99;
429 } else {
430 LangTag = llvm::dwarf::DW_LANG_C89;
431 }
432
433 std::string Producer = getClangFullVersion();
434
435 // Figure out which version of the ObjC runtime we have.
436 unsigned RuntimeVers = 0;
437 if (LO.ObjC1)
438 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
439
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700440 llvm::DICompileUnit::DebugEmissionKind EmissionKind;
441 switch (DebugKind) {
442 case codegenoptions::NoDebugInfo:
443 case codegenoptions::LocTrackingOnly:
444 EmissionKind = llvm::DICompileUnit::NoDebug;
445 break;
446 case codegenoptions::DebugLineTablesOnly:
447 EmissionKind = llvm::DICompileUnit::LineTablesOnly;
448 break;
449 case codegenoptions::LimitedDebugInfo:
450 case codegenoptions::FullDebugInfo:
451 EmissionKind = llvm::DICompileUnit::FullDebug;
452 break;
453 }
454
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000455 // Create new compile unit.
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000456 // FIXME - Eliminate TheCU.
Stephen Hines651f13c2014-04-23 16:59:28 -0700457 TheCU = DBuilder.createCompileUnit(
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800458 LangTag, remapDIPath(MainFileName), remapDIPath(getCurrentDirname()),
459 Producer, LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers,
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700460 CGM.getCodeGenOpts().SplitDwarfFile, EmissionKind, 0 /* DWOid */);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000461}
462
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700463llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700464 llvm::dwarf::TypeKind Encoding;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000465 StringRef BTName;
466 switch (BT->getKind()) {
467#define BUILTIN_TYPE(Id, SingletonId)
Stephen Hines176edba2014-12-01 14:53:08 -0800468#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000469#include "clang/AST/BuiltinTypes.def"
470 case BuiltinType::Dependent:
471 llvm_unreachable("Unexpected builtin type");
472 case BuiltinType::NullPtr:
Peter Collingbourne24118f52013-06-27 22:51:01 +0000473 return DBuilder.createNullPtrType();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000474 case BuiltinType::Void:
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700475 return nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000476 case BuiltinType::ObjCClass:
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700477 if (!ClassTy)
478 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
479 "objc_class", TheCU,
480 getOrCreateMainFile(), 0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000481 return ClassTy;
482 case BuiltinType::ObjCId: {
483 // typedef struct objc_class *Class;
484 // typedef struct objc_object {
485 // Class isa;
486 // } *id;
487
Eric Christopherb2d13922013-07-18 00:52:50 +0000488 if (ObjTy)
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000489 return ObjTy;
490
Eric Christopherb2d13922013-07-18 00:52:50 +0000491 if (!ClassTy)
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000492 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
493 "objc_class", TheCU,
494 getOrCreateMainFile(), 0);
495
496 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopher6537f082013-05-16 00:45:12 +0000497
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700498 auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000499
Eric Christopherf068c922013-04-02 22:59:11 +0000500 ObjTy =
David Blaikiec1d0af12013-02-25 01:07:08 +0000501 DBuilder.createStructType(TheCU, "objc_object", getOrCreateMainFile(),
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700502 0, 0, 0, 0, nullptr, llvm::DINodeArray());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000503
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700504 DBuilder.replaceArrays(
505 ObjTy,
506 DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
507 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0, 0, ISATy)));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000508 return ObjTy;
509 }
510 case BuiltinType::ObjCSel: {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700511 if (!SelTy)
512 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
513 "objc_selector", TheCU,
514 getOrCreateMainFile(), 0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000515 return SelTy;
516 }
Guy Benyeib13621d2012-12-18 14:38:23 +0000517
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700518#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
519 case BuiltinType::Id: \
520 return getOrCreateStructPtrType("opencl_" #ImgType "_" #Suffix "_t", \
521 SingletonId);
522#include "clang/Basic/OpenCLImageTypes.def"
Guy Benyei21f18c42013-02-07 10:55:47 +0000523 case BuiltinType::OCLSampler:
Stephen Hines176edba2014-12-01 14:53:08 -0800524 return DBuilder.createBasicType(
525 "opencl_sampler_t", CGM.getContext().getTypeSize(BT),
526 CGM.getContext().getTypeAlign(BT), llvm::dwarf::DW_ATE_unsigned);
Guy Benyeie6b9d802013-01-20 12:31:11 +0000527 case BuiltinType::OCLEvent:
Stephen Hines176edba2014-12-01 14:53:08 -0800528 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800529 case BuiltinType::OCLClkEvent:
530 return getOrCreateStructPtrType("opencl_clk_event_t", OCLClkEventDITy);
531 case BuiltinType::OCLQueue:
532 return getOrCreateStructPtrType("opencl_queue_t", OCLQueueDITy);
533 case BuiltinType::OCLNDRange:
534 return getOrCreateStructPtrType("opencl_ndrange_t", OCLNDRangeDITy);
535 case BuiltinType::OCLReserveID:
536 return getOrCreateStructPtrType("opencl_reserve_id_t", OCLReserveIDDITy);
Guy Benyeib13621d2012-12-18 14:38:23 +0000537
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000538 case BuiltinType::UChar:
Stephen Hines176edba2014-12-01 14:53:08 -0800539 case BuiltinType::Char_U:
540 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
541 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000542 case BuiltinType::Char_S:
Stephen Hines176edba2014-12-01 14:53:08 -0800543 case BuiltinType::SChar:
544 Encoding = llvm::dwarf::DW_ATE_signed_char;
545 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000546 case BuiltinType::Char16:
Stephen Hines176edba2014-12-01 14:53:08 -0800547 case BuiltinType::Char32:
548 Encoding = llvm::dwarf::DW_ATE_UTF;
549 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000550 case BuiltinType::UShort:
551 case BuiltinType::UInt:
552 case BuiltinType::UInt128:
553 case BuiltinType::ULong:
554 case BuiltinType::WChar_U:
Stephen Hines176edba2014-12-01 14:53:08 -0800555 case BuiltinType::ULongLong:
556 Encoding = llvm::dwarf::DW_ATE_unsigned;
557 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000558 case BuiltinType::Short:
559 case BuiltinType::Int:
560 case BuiltinType::Int128:
561 case BuiltinType::Long:
562 case BuiltinType::WChar_S:
Stephen Hines176edba2014-12-01 14:53:08 -0800563 case BuiltinType::LongLong:
564 Encoding = llvm::dwarf::DW_ATE_signed;
565 break;
566 case BuiltinType::Bool:
567 Encoding = llvm::dwarf::DW_ATE_boolean;
568 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000569 case BuiltinType::Half:
570 case BuiltinType::Float:
571 case BuiltinType::LongDouble:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700572 case BuiltinType::Float128:
Stephen Hines176edba2014-12-01 14:53:08 -0800573 case BuiltinType::Double:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700574 // FIXME: For targets where long double and __float128 have the same size,
575 // they are currently indistinguishable in the debugger without some
576 // special treatment. However, there is currently no consensus on encoding
577 // and this should be updated once a DWARF encoding exists for distinct
578 // floating point types of the same size.
Stephen Hines176edba2014-12-01 14:53:08 -0800579 Encoding = llvm::dwarf::DW_ATE_float;
580 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000581 }
582
583 switch (BT->getKind()) {
Stephen Hines176edba2014-12-01 14:53:08 -0800584 case BuiltinType::Long:
585 BTName = "long int";
586 break;
587 case BuiltinType::LongLong:
588 BTName = "long long int";
589 break;
590 case BuiltinType::ULong:
591 BTName = "long unsigned int";
592 break;
593 case BuiltinType::ULongLong:
594 BTName = "long long unsigned int";
595 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000596 default:
597 BTName = BT->getName(CGM.getLangOpts());
598 break;
599 }
600 // Bit size, align and offset of the type.
601 uint64_t Size = CGM.getContext().getTypeSize(BT);
602 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700603 return DBuilder.createBasicType(BTName, Size, Align, Encoding);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000604}
605
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700606llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000607 // Bit size, align and offset of the type.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700608 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000609 if (Ty->isComplexIntegerType())
610 Encoding = llvm::dwarf::DW_ATE_lo_user;
611
612 uint64_t Size = CGM.getContext().getTypeSize(Ty);
613 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700614 return DBuilder.createBasicType("complex", Size, Align, Encoding);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000615}
616
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700617llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
618 llvm::DIFile *Unit) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000619 QualifierCollector Qc;
620 const Type *T = Qc.strip(Ty);
621
622 // Ignore these qualifiers for now.
623 Qc.removeObjCGCAttr();
624 Qc.removeAddressSpace();
625 Qc.removeObjCLifetime();
626
627 // We will create one Derived type for one qualifier and recurse to handle any
628 // additional ones.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700629 llvm::dwarf::Tag Tag;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000630 if (Qc.hasConst()) {
631 Tag = llvm::dwarf::DW_TAG_const_type;
632 Qc.removeConst();
633 } else if (Qc.hasVolatile()) {
634 Tag = llvm::dwarf::DW_TAG_volatile_type;
635 Qc.removeVolatile();
636 } else if (Qc.hasRestrict()) {
637 Tag = llvm::dwarf::DW_TAG_restrict_type;
638 Qc.removeRestrict();
639 } else {
640 assert(Qc.empty() && "Unknown type qualifier for debug info");
641 return getOrCreateType(QualType(T, 0), Unit);
642 }
643
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700644 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000645
646 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
647 // CVR derived types.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700648 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000649}
650
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700651llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
652 llvm::DIFile *Unit) {
Fariborz Jahanian05f8ff12013-02-21 20:42:11 +0000653
654 // The frontend treats 'id' as a typedef to an ObjCObjectType,
655 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
656 // debug info, we want to emit 'id' in both cases.
657 if (Ty->isObjCQualifiedIdType())
Stephen Hines176edba2014-12-01 14:53:08 -0800658 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian05f8ff12013-02-21 20:42:11 +0000659
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700660 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
661 Ty->getPointeeType(), Unit);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000662}
663
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700664llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
665 llvm::DIFile *Unit) {
Eric Christopher6537f082013-05-16 00:45:12 +0000666 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000667 Ty->getPointeeType(), Unit);
668}
669
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800670/// \return whether a C++ mangling exists for the type defined by TD.
671static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
672 switch (TheCU->getSourceLanguage()) {
673 case llvm::dwarf::DW_LANG_C_plus_plus:
674 return true;
675 case llvm::dwarf::DW_LANG_ObjC_plus_plus:
676 return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
677 default:
678 return false;
679 }
680}
681
Manman Ren83369bf2013-08-29 23:19:58 +0000682/// In C++ mode, types have linkage, so we can rely on the ODR and
683/// on their mangled names, if they're external.
Stephen Hines176edba2014-12-01 14:53:08 -0800684static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
685 CodeGenModule &CGM,
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700686 llvm::DICompileUnit *TheCU) {
Manman Ren83369bf2013-08-29 23:19:58 +0000687 SmallString<256> FullName;
Manman Ren83369bf2013-08-29 23:19:58 +0000688 const TagDecl *TD = Ty->getDecl();
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800689
690 if (!hasCXXMangling(TD, TheCU) || !TD->isExternallyVisible())
Manman Ren83369bf2013-08-29 23:19:58 +0000691 return FullName;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800692
Manman Ren83369bf2013-08-29 23:19:58 +0000693 // TODO: This is using the RTTI name. Is there a better way to get
694 // a unique string for a type?
695 llvm::raw_svector_ostream Out(FullName);
696 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
Manman Ren83369bf2013-08-29 23:19:58 +0000697 return FullName;
698}
699
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800700/// \return the approproate DWARF tag for a composite type.
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700701static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
702 llvm::dwarf::Tag Tag;
703 if (RD->isStruct() || RD->isInterface())
704 Tag = llvm::dwarf::DW_TAG_structure_type;
705 else if (RD->isUnion())
706 Tag = llvm::dwarf::DW_TAG_union_type;
707 else {
708 // FIXME: This could be a struct type giving a default visibility different
709 // than C++ class type, but needs llvm metadata changes first.
710 assert(RD->isClass());
711 Tag = llvm::dwarf::DW_TAG_class_type;
712 }
713 return Tag;
714}
715
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700716llvm::DICompositeType *
Manman Renf3327332013-08-28 21:20:28 +0000717CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700718 llvm::DIScope *Ctx) {
Manman Renf3327332013-08-28 21:20:28 +0000719 const RecordDecl *RD = Ty->getDecl();
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700720 if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
721 return cast<llvm::DICompositeType>(T);
722 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000723 unsigned Line = getLineNumber(RD->getLocation());
724 StringRef RDName = getClassName(RD);
725
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700726 uint64_t Size = 0;
727 uint64_t Align = 0;
728
729 const RecordDecl *D = RD->getDefinition();
730 if (D && D->isCompleteDefinition()) {
731 Size = CGM.getContext().getTypeSize(Ty);
732 Align = CGM.getContext().getTypeAlign(Ty);
733 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000734
735 // Create the type.
Manman Ren83369bf2013-08-29 23:19:58 +0000736 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700737 llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700738 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700739 llvm::DINode::FlagFwdDecl, FullName);
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700740 ReplaceMap.emplace_back(
741 std::piecewise_construct, std::make_tuple(Ty),
742 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700743 return RetTy;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000744}
745
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700746llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
747 const Type *Ty,
748 QualType PointeeTy,
749 llvm::DIFile *Unit) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000750 // Bit size, align and offset of the type.
751 // Size is always the size of a pointer. We can't use getTypeSize here
752 // because that does not return the correct value for references.
753 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCall64aa4b32013-04-16 22:48:15 +0000754 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000755 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
756
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800757 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
758 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
759 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit),
760 Size, Align);
761 else
762 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
763 Align);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000764}
765
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700766llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
767 llvm::DIType *&Cache) {
Eric Christopherb2d13922013-07-18 00:52:50 +0000768 if (Cache)
Guy Benyeib13621d2012-12-18 14:38:23 +0000769 return Cache;
David Blaikie1e97c1e2013-05-21 17:58:54 +0000770 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
771 TheCU, getOrCreateMainFile(), 0);
772 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
773 Cache = DBuilder.createPointerType(Cache, Size);
774 return Cache;
Guy Benyeib13621d2012-12-18 14:38:23 +0000775}
776
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700777llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
778 llvm::DIFile *Unit) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700779 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000780 QualType FType;
781 uint64_t FieldSize, FieldOffset;
782 unsigned FieldAlign;
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700783 llvm::DINodeArray Elements;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000784
785 FieldOffset = 0;
786 FType = CGM.getContext().UnsignedLongTy;
787 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
788 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
789
790 Elements = DBuilder.getOrCreateArray(EltTys);
791 EltTys.clear();
792
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700793 unsigned Flags = llvm::DINode::FlagAppleBlock;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800794 unsigned LineNo = 0;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000795
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700796 auto *EltTy =
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800797 DBuilder.createStructType(Unit, "__block_descriptor", nullptr, LineNo,
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700798 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000799
800 // Bit size, align and offset of the type.
801 uint64_t Size = CGM.getContext().getTypeSize(Ty);
802
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700803 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000804
805 FieldOffset = 0;
806 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
807 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
808 FType = CGM.getContext().IntTy;
809 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
810 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Stephen Hines176edba2014-12-01 14:53:08 -0800811 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000812 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
813
814 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000815 FieldSize = CGM.getContext().getTypeSize(Ty);
816 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800817 EltTys.push_back(DBuilder.createMemberType(Unit, "__descriptor", nullptr, LineNo,
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700818 FieldSize, FieldAlign, FieldOffset,
819 0, DescTy));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000820
821 FieldOffset += FieldSize;
822 Elements = DBuilder.getOrCreateArray(EltTys);
823
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800824 // The __block_literal_generic structs are marked with a special
825 // DW_AT_APPLE_BLOCK attribute and are an implementation detail only
826 // the debugger needs to know about. To allow type uniquing, emit
827 // them without a name or a location.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700828 EltTy =
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800829 DBuilder.createStructType(Unit, "", nullptr, LineNo,
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700830 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000831
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800832 return DBuilder.createPointerType(EltTy, Size);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000833}
834
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700835llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
836 llvm::DIFile *Unit) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700837 assert(Ty->isTypeAlias());
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700838 llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700839
840 SmallString<128> NS;
841 llvm::raw_svector_ostream OS(NS);
Stephen Hines176edba2014-12-01 14:53:08 -0800842 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
843 /*qualified*/ false);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700844
845 TemplateSpecializationType::PrintTemplateArgumentList(
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700846 OS, Ty->template_arguments(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700847 CGM.getContext().getPrintingPolicy());
848
Stephen Hines176edba2014-12-01 14:53:08 -0800849 TypeAliasDecl *AliasDecl = cast<TypeAliasTemplateDecl>(
850 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700851
852 SourceLocation Loc = AliasDecl->getLocation();
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800853 return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc),
854 getLineNumber(Loc),
855 getDeclContextDescriptor(AliasDecl));
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700856}
857
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700858llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
859 llvm::DIFile *Unit) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000860 // We don't set size information, but do specify where the typedef was
861 // declared.
Stephen Hines651f13c2014-04-23 16:59:28 -0700862 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopher6537f082013-05-16 00:45:12 +0000863
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -0700864 // Typedefs are derived from some other type.
865 return DBuilder.createTypedef(
866 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
867 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800868 getDeclContextDescriptor(Ty->getDecl()));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000869}
870
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700871static unsigned getDwarfCC(CallingConv CC) {
872 switch (CC) {
873 case CC_C:
874 // Avoid emitting DW_AT_calling_convention if the C convention was used.
875 return 0;
876
877 case CC_X86StdCall:
878 return llvm::dwarf::DW_CC_BORLAND_stdcall;
879 case CC_X86FastCall:
880 return llvm::dwarf::DW_CC_BORLAND_msfastcall;
881 case CC_X86ThisCall:
882 return llvm::dwarf::DW_CC_BORLAND_thiscall;
883 case CC_X86VectorCall:
884 return llvm::dwarf::DW_CC_LLVM_vectorcall;
885 case CC_X86Pascal:
886 return llvm::dwarf::DW_CC_BORLAND_pascal;
887
888 // FIXME: Create new DW_CC_ codes for these calling conventions.
889 case CC_X86_64Win64:
890 case CC_X86_64SysV:
891 case CC_AAPCS:
892 case CC_AAPCS_VFP:
893 case CC_IntelOclBicc:
894 case CC_SpirFunction:
895 case CC_OpenCLKernel:
896 case CC_Swift:
897 case CC_PreserveMost:
898 case CC_PreserveAll:
899 return 0;
900 }
901 return 0;
902}
903
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700904llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
905 llvm::DIFile *Unit) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700906 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000907
908 // Add the result type at least.
Stephen Hines651f13c2014-04-23 16:59:28 -0700909 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000910
911 // Set up remainder of arguments if there is a prototype.
Stephen Hines651f13c2014-04-23 16:59:28 -0700912 // otherwise emit it as a variadic function.
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000913 if (isa<FunctionNoProtoType>(Ty))
914 EltTys.push_back(DBuilder.createUnspecifiedParameter());
915 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700916 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
917 EltTys.push_back(getOrCreateType(FPT->getParamType(i), Unit));
918 if (FPT->isVariadic())
919 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000920 }
921
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700922 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700923 return DBuilder.createSubroutineType(EltTypeArray, 0,
924 getDwarfCC(Ty->getCallConv()));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000925}
926
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700927/// Convert an AccessSpecifier into the corresponding DINode flag.
Stephen Hines176edba2014-12-01 14:53:08 -0800928/// As an optimization, return 0 if the access specifier equals the
929/// default for the containing type.
930static unsigned getAccessFlag(AccessSpecifier Access, const RecordDecl *RD) {
931 AccessSpecifier Default = clang::AS_none;
932 if (RD && RD->isClass())
933 Default = clang::AS_private;
934 else if (RD && (RD->isStruct() || RD->isUnion()))
935 Default = clang::AS_public;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000936
Stephen Hines176edba2014-12-01 14:53:08 -0800937 if (Access == Default)
938 return 0;
939
940 switch (Access) {
941 case clang::AS_private:
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700942 return llvm::DINode::FlagPrivate;
Stephen Hines176edba2014-12-01 14:53:08 -0800943 case clang::AS_protected:
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700944 return llvm::DINode::FlagProtected;
Stephen Hines176edba2014-12-01 14:53:08 -0800945 case clang::AS_public:
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700946 return llvm::DINode::FlagPublic;
Stephen Hines176edba2014-12-01 14:53:08 -0800947 case clang::AS_none:
948 return 0;
949 }
950 llvm_unreachable("unexpected access enumerator");
951}
952
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700953llvm::DIType *CGDebugInfo::createBitFieldType(const FieldDecl *BitFieldDecl,
954 llvm::DIScope *RecordTy,
955 const RecordDecl *RD) {
956 StringRef Name = BitFieldDecl->getName();
957 QualType Ty = BitFieldDecl->getType();
958 SourceLocation Loc = BitFieldDecl->getLocation();
959 llvm::DIFile *VUnit = getOrCreateFile(Loc);
960 llvm::DIType *DebugType = getOrCreateType(Ty, VUnit);
961
962 // Get the location for the field.
963 llvm::DIFile *File = getOrCreateFile(Loc);
964 unsigned Line = getLineNumber(Loc);
965
966 const CGBitFieldInfo &BitFieldInfo =
967 CGM.getTypes().getCGRecordLayout(RD).getBitFieldInfo(BitFieldDecl);
968 uint64_t SizeInBits = BitFieldInfo.Size;
969 assert(SizeInBits > 0 && "found named 0-width bitfield");
970 unsigned AlignInBits = CGM.getContext().getTypeAlign(Ty);
971 uint64_t StorageOffsetInBits =
972 CGM.getContext().toBits(BitFieldInfo.StorageOffset);
973 uint64_t OffsetInBits = StorageOffsetInBits + BitFieldInfo.Offset;
974 unsigned Flags = getAccessFlag(BitFieldDecl->getAccess(), RD);
975 return DBuilder.createBitFieldMemberType(
976 RecordTy, Name, File, Line, SizeInBits, AlignInBits, OffsetInBits,
977 StorageOffsetInBits, Flags, DebugType);
978}
979
980llvm::DIType *
981CGDebugInfo::createFieldType(StringRef name, QualType type, SourceLocation loc,
982 AccessSpecifier AS, uint64_t offsetInBits,
983 llvm::DIFile *tunit, llvm::DIScope *scope,
984 const RecordDecl *RD) {
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700985 llvm::DIType *debugType = getOrCreateType(type, tunit);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000986
987 // Get the location for the field.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700988 llvm::DIFile *file = getOrCreateFile(loc);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000989 unsigned line = getLineNumber(loc);
990
Stephen Hines176edba2014-12-01 14:53:08 -0800991 uint64_t SizeInBits = 0;
992 unsigned AlignInBits = 0;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000993 if (!type->isIncompleteArrayType()) {
Stephen Hines176edba2014-12-01 14:53:08 -0800994 TypeInfo TI = CGM.getContext().getTypeInfo(type);
995 SizeInBits = TI.Width;
996 AlignInBits = TI.Align;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000997 }
998
Stephen Hines176edba2014-12-01 14:53:08 -0800999 unsigned flags = getAccessFlag(AS, RD);
1000 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
1001 AlignInBits, offsetInBits, flags, debugType);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001002}
1003
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001004void CGDebugInfo::CollectRecordLambdaFields(
1005 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001006 llvm::DIType *RecordTy) {
Eric Christopher0395de32013-01-16 01:22:32 +00001007 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
1008 // has the name and the location of the variable so we should iterate over
1009 // both concurrently.
1010 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
1011 RecordDecl::field_iterator Field = CXXDecl->field_begin();
1012 unsigned fieldno = 0;
1013 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Stephen Hines176edba2014-12-01 14:53:08 -08001014 E = CXXDecl->captures_end();
1015 I != E; ++I, ++Field, ++fieldno) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001016 const LambdaCapture &C = *I;
Eric Christopher0395de32013-01-16 01:22:32 +00001017 if (C.capturesVariable()) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001018 SourceLocation Loc = C.getLocation();
1019 assert(!Field->isBitField() && "lambdas don't have bitfield members!");
Eric Christopher0395de32013-01-16 01:22:32 +00001020 VarDecl *V = C.getCapturedVar();
Eric Christopher0395de32013-01-16 01:22:32 +00001021 StringRef VName = V->getName();
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001022 llvm::DIFile *VUnit = getOrCreateFile(Loc);
1023 llvm::DIType *FieldType = createFieldType(
1024 VName, Field->getType(), Loc, Field->getAccess(),
1025 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
1026 elements.push_back(FieldType);
Stephen Hines176edba2014-12-01 14:53:08 -08001027 } else if (C.capturesThis()) {
Eric Christopher0395de32013-01-16 01:22:32 +00001028 // TODO: Need to handle 'this' in some way by probably renaming the
1029 // this of the lambda class and having a field member of 'this' or
1030 // by using AT_object_pointer for the function and having that be
1031 // used as 'this' for semantic references.
Eric Christopher0395de32013-01-16 01:22:32 +00001032 FieldDecl *f = *Field;
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001033 llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher0395de32013-01-16 01:22:32 +00001034 QualType type = f->getType();
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001035 llvm::DIType *fieldType = createFieldType(
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001036 "this", type, f->getLocation(), f->getAccess(),
Stephen Hines176edba2014-12-01 14:53:08 -08001037 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher0395de32013-01-16 01:22:32 +00001038
1039 elements.push_back(fieldType);
1040 }
1041 }
1042}
1043
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001044llvm::DIDerivedType *
1045CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
1046 const RecordDecl *RD) {
Eric Christopher0395de32013-01-16 01:22:32 +00001047 // Create the descriptor for the static variable, with or without
1048 // constant initializers.
Stephen Hines176edba2014-12-01 14:53:08 -08001049 Var = Var->getCanonicalDecl();
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001050 llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
1051 llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher0395de32013-01-16 01:22:32 +00001052
Eric Christopher0395de32013-01-16 01:22:32 +00001053 unsigned LineNumber = getLineNumber(Var->getLocation());
1054 StringRef VName = Var->getName();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001055 llvm::Constant *C = nullptr;
Eric Christopher0395de32013-01-16 01:22:32 +00001056 if (Var->getInit()) {
1057 const APValue *Value = Var->evaluateValue();
David Blaikiea89701b2013-01-20 01:19:17 +00001058 if (Value) {
1059 if (Value->isInt())
1060 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
1061 if (Value->isFloat())
1062 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
1063 }
Eric Christopher0395de32013-01-16 01:22:32 +00001064 }
1065
Stephen Hines176edba2014-12-01 14:53:08 -08001066 unsigned Flags = getAccessFlag(Var->getAccess(), RD);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001067 llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
David Blaikiecbcb0302013-08-15 22:50:29 +00001068 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001069 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikiecbcb0302013-08-15 22:50:29 +00001070 return GV;
Eric Christopher0395de32013-01-16 01:22:32 +00001071}
1072
Stephen Hines176edba2014-12-01 14:53:08 -08001073void CGDebugInfo::CollectRecordNormalField(
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001074 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
1075 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
Stephen Hines176edba2014-12-01 14:53:08 -08001076 const RecordDecl *RD) {
Eric Christopher0395de32013-01-16 01:22:32 +00001077 StringRef name = field->getName();
1078 QualType type = field->getType();
1079
1080 // Ignore unnamed fields unless they're anonymous structs/unions.
1081 if (name.empty() && !type->isRecordType())
1082 return;
1083
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001084 llvm::DIType *FieldType;
Eric Christopher0395de32013-01-16 01:22:32 +00001085 if (field->isBitField()) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001086 FieldType = createBitFieldType(field, RecordTy, RD);
1087 } else {
1088 FieldType =
1089 createFieldType(name, type, field->getLocation(), field->getAccess(),
1090 OffsetInBits, tunit, RecordTy, RD);
Eric Christopher0395de32013-01-16 01:22:32 +00001091 }
1092
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001093 elements.push_back(FieldType);
Eric Christopher0395de32013-01-16 01:22:32 +00001094}
1095
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001096void CGDebugInfo::CollectRecordFields(
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001097 const RecordDecl *record, llvm::DIFile *tunit,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001098 SmallVectorImpl<llvm::Metadata *> &elements,
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001099 llvm::DICompositeType *RecordTy) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001100 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
1101
Eric Christopher0395de32013-01-16 01:22:32 +00001102 if (CXXDecl && CXXDecl->isLambda())
1103 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
1104 else {
1105 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001106
Eric Christopher0395de32013-01-16 01:22:32 +00001107 // Field number for non-static fields.
Eric Christopherfd5ac0d2013-01-04 17:59:07 +00001108 unsigned fieldNo = 0;
Eric Christopher0395de32013-01-16 01:22:32 +00001109
Eric Christopher0395de32013-01-16 01:22:32 +00001110 // Static and non-static members should appear in the same order as
1111 // the corresponding declarations in the source program.
Stephen Hines651f13c2014-04-23 16:59:28 -07001112 for (const auto *I : record->decls())
1113 if (const auto *V = dyn_cast<VarDecl>(I)) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001114 if (V->hasAttr<NoDebugAttr>())
1115 continue;
David Blaikie5e6937b2013-08-20 21:49:21 +00001116 // Reuse the existing static member declaration if one exists
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001117 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikie5e6937b2013-08-20 21:49:21 +00001118 if (MI != StaticDataMemberCache.end()) {
1119 assert(MI->second &&
1120 "Static data member declaration should still exist");
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001121 elements.push_back(MI->second);
Stephen Hines176edba2014-12-01 14:53:08 -08001122 } else {
1123 auto Field = CreateRecordStaticField(V, RecordTy, record);
1124 elements.push_back(Field);
1125 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001126 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Stephen Hines176edba2014-12-01 14:53:08 -08001127 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
1128 elements, RecordTy, record);
Eric Christopher0395de32013-01-16 01:22:32 +00001129
1130 // Bump field number for next field.
1131 ++fieldNo;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001132 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001133 }
1134}
1135
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001136llvm::DISubroutineType *
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001137CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001138 llvm::DIFile *Unit) {
David Blaikie9c78f9b2013-01-07 23:06:35 +00001139 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie67f8b5e2013-01-07 22:24:59 +00001140 if (Method->isStatic())
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001141 return cast_or_null<llvm::DISubroutineType>(
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07001142 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie9c78f9b2013-01-07 23:06:35 +00001143 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1144 Func, Unit);
1145}
David Blaikie67f8b5e2013-01-07 22:24:59 +00001146
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001147llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1148 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001149 // Add "this" pointer.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001150 llvm::DITypeRefArray Args(
1151 cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07001152 ->getTypeArray());
1153 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001154
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001155 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001156
1157 // First element is always return type. For 'void' functions it is NULL.
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07001158 Elts.push_back(Args[0]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001159
David Blaikie67f8b5e2013-01-07 22:24:59 +00001160 // "this" pointer is always first argument.
David Blaikie9c78f9b2013-01-07 23:06:35 +00001161 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie67f8b5e2013-01-07 22:24:59 +00001162 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1163 // Create pointer type directly in this case.
1164 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1165 QualType PointeeTy = ThisPtrTy->getPointeeType();
1166 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCall64aa4b32013-04-16 22:48:15 +00001167 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie67f8b5e2013-01-07 22:24:59 +00001168 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001169 llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1170 llvm::DIType *ThisPtrType =
Stephen Hines176edba2014-12-01 14:53:08 -08001171 DBuilder.createPointerType(PointeeType, Size, Align);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001172 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie67f8b5e2013-01-07 22:24:59 +00001173 // TODO: This and the artificial type below are misleading, the
1174 // types aren't artificial the argument is, but the current
1175 // metadata doesn't represent that.
1176 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1177 Elts.push_back(ThisPtrType);
1178 } else {
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001179 llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001180 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie67f8b5e2013-01-07 22:24:59 +00001181 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1182 Elts.push_back(ThisPtrType);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001183 }
1184
1185 // Copy rest of the arguments.
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07001186 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1187 Elts.push_back(Args[i]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001188
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001189 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001190
Stephen Hines651f13c2014-04-23 16:59:28 -07001191 unsigned Flags = 0;
1192 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001193 Flags |= llvm::DINode::FlagLValueReference;
Stephen Hines651f13c2014-04-23 16:59:28 -07001194 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001195 Flags |= llvm::DINode::FlagRValueReference;
Stephen Hines651f13c2014-04-23 16:59:28 -07001196
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001197 return DBuilder.createSubroutineType(EltTypeArray, Flags,
1198 getDwarfCC(Func->getCallConv()));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001199}
1200
Eric Christopher6537f082013-05-16 00:45:12 +00001201/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001202/// inside a function.
1203static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1204 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1205 return isFunctionLocalClass(NRD);
1206 if (isa<FunctionDecl>(RD->getDeclContext()))
1207 return true;
1208 return false;
1209}
1210
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001211llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1212 const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
Eric Christopher6537f082013-05-16 00:45:12 +00001213 bool IsCtorOrDtor =
Stephen Hines176edba2014-12-01 14:53:08 -08001214 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopher6537f082013-05-16 00:45:12 +00001215
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001216 StringRef MethodName = getFunctionName(Method);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001217 llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001218
1219 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1220 // make sense to give a single ctor/dtor a linkage name.
1221 StringRef MethodLinkageName;
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001222 // FIXME: 'isFunctionLocalClass' seems like an arbitrary/unintentional
1223 // property to use here. It may've been intended to model "is non-external
1224 // type" but misses cases of non-function-local but non-external classes such
1225 // as those in anonymous namespaces as well as the reverse - external types
1226 // that are function local, such as those in (non-local) inline functions.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001227 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1228 MethodLinkageName = CGM.getMangledName(Method);
1229
1230 // Get the location for the method.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001231 llvm::DIFile *MethodDefUnit = nullptr;
David Blaikiefc946272013-08-19 03:37:48 +00001232 unsigned MethodLine = 0;
1233 if (!Method->isImplicit()) {
1234 MethodDefUnit = getOrCreateFile(Method->getLocation());
1235 MethodLine = getLineNumber(Method->getLocation());
1236 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001237
1238 // Collect virtual method info.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001239 llvm::DIType *ContainingType = nullptr;
Eric Christopher6537f082013-05-16 00:45:12 +00001240 unsigned Virtuality = 0;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001241 unsigned VIndex = 0;
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001242 unsigned Flags = 0;
1243 int ThisAdjustment = 0;
Eric Christopher6537f082013-05-16 00:45:12 +00001244
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001245 if (Method->isVirtual()) {
1246 if (Method->isPure())
1247 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1248 else
1249 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopher6537f082013-05-16 00:45:12 +00001250
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001251 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1252 // It doesn't make sense to give a virtual destructor a vtable index,
1253 // since a single destructor has two entries in the vtable.
1254 if (!isa<CXXDestructorDecl>(Method))
1255 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
1256 } else {
1257 // Emit MS ABI vftable information. There is only one entry for the
1258 // deleting dtor.
1259 const auto *DD = dyn_cast<CXXDestructorDecl>(Method);
1260 GlobalDecl GD = DD ? GlobalDecl(DD, Dtor_Deleting) : GlobalDecl(Method);
1261 MicrosoftVTableContext::MethodVFTableLocation ML =
1262 CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD);
1263 VIndex = ML.Index;
1264
1265 // CodeView only records the vftable offset in the class that introduces
1266 // the virtual method. This is possible because, unlike Itanium, the MS
1267 // C++ ABI does not include all virtual methods from non-primary bases in
1268 // the vtable for the most derived class. For example, if C inherits from
1269 // A and B, C's primary vftable will not include B's virtual methods.
1270 if (Method->begin_overridden_methods() == Method->end_overridden_methods())
1271 Flags |= llvm::DINode::FlagIntroducedVirtual;
1272
1273 // The 'this' adjustment accounts for both the virtual and non-virtual
1274 // portions of the adjustment. Presumably the debugger only uses it when
1275 // it knows the dynamic type of an object.
1276 ThisAdjustment = CGM.getCXXABI()
1277 .getVirtualFunctionPrologueThisAdjustment(GD)
1278 .getQuantity();
1279 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001280 ContainingType = RecordTy;
1281 }
1282
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001283 if (Method->isImplicit())
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001284 Flags |= llvm::DINode::FlagArtificial;
Stephen Hines176edba2014-12-01 14:53:08 -08001285 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001286 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1287 if (CXXC->isExplicit())
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001288 Flags |= llvm::DINode::FlagExplicit;
Eric Christopher6537f082013-05-16 00:45:12 +00001289 } else if (const CXXConversionDecl *CXXC =
Stephen Hines176edba2014-12-01 14:53:08 -08001290 dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001291 if (CXXC->isExplicit())
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001292 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001293 }
1294 if (Method->hasPrototype())
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001295 Flags |= llvm::DINode::FlagPrototyped;
Stephen Hines651f13c2014-04-23 16:59:28 -07001296 if (Method->getRefQualifier() == RQ_LValue)
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001297 Flags |= llvm::DINode::FlagLValueReference;
Stephen Hines651f13c2014-04-23 16:59:28 -07001298 if (Method->getRefQualifier() == RQ_RValue)
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001299 Flags |= llvm::DINode::FlagRValueReference;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001300
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001301 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1302 llvm::DISubprogram *SP = DBuilder.createMethod(
Stephen Hines176edba2014-12-01 14:53:08 -08001303 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001304 MethodTy, /*isLocalToUnit=*/false, /*isDefinition=*/false, Virtuality,
1305 VIndex, ThisAdjustment, ContainingType, Flags, CGM.getLangOpts().Optimize,
1306 TParamsArray.get());
Eric Christopher6537f082013-05-16 00:45:12 +00001307
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001308 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001309
1310 return SP;
1311}
1312
Stephen Hines176edba2014-12-01 14:53:08 -08001313void CGDebugInfo::CollectCXXMemberFunctions(
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001314 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1315 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001316
1317 // Since we want more than just the individual member decls if we
1318 // have templated functions iterate over every declaration to gather
1319 // the functions.
Stephen Hines176edba2014-12-01 14:53:08 -08001320 for (const auto *I : RD->decls()) {
1321 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1322 // If the member is implicit, don't add it to the member list. This avoids
1323 // the member being added to type units by LLVM, while still allowing it
1324 // to be emitted into the type declaration/reference inside the compile
1325 // unit.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001326 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
Stephen Hines176edba2014-12-01 14:53:08 -08001327 // FIXME: Handle Using(Shadow?)Decls here to create
1328 // DW_TAG_imported_declarations inside the class for base decls brought into
1329 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1330 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1331 // referenced)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001332 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
Stephen Hines176edba2014-12-01 14:53:08 -08001333 continue;
1334
1335 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1336 continue;
1337
1338 // Reuse the existing member function declaration if it exists.
1339 // It may be associated with the declaration of the type & should be
1340 // reused as we're building the definition.
1341 //
1342 // This situation can arise in the vtable-based debug info reduction where
1343 // implicit members are emitted in a non-vtable TU.
1344 auto MI = SPCache.find(Method->getCanonicalDecl());
1345 EltTys.push_back(MI == SPCache.end()
1346 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001347 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001348 }
Eric Christopher6537f082013-05-16 00:45:12 +00001349}
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001350
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001351void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001352 SmallVectorImpl<llvm::Metadata *> &EltTys,
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001353 llvm::DIType *RecordTy) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001354 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Stephen Hines651f13c2014-04-23 16:59:28 -07001355 for (const auto &BI : RD->bases()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001356 unsigned BFlags = 0;
1357 uint64_t BaseOffset;
Eric Christopher6537f082013-05-16 00:45:12 +00001358
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001359 const CXXRecordDecl *Base =
Stephen Hines176edba2014-12-01 14:53:08 -08001360 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Eric Christopher6537f082013-05-16 00:45:12 +00001361
Stephen Hines651f13c2014-04-23 16:59:28 -07001362 if (BI.isVirtual()) {
Stephen Hines176edba2014-12-01 14:53:08 -08001363 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1364 // virtual base offset offset is -ve. The code generator emits dwarf
1365 // expression where it expects +ve number.
1366 BaseOffset = 0 - CGM.getItaniumVTableContext()
1367 .getVirtualBaseOffsetOffset(RD, Base)
1368 .getQuantity();
1369 } else {
1370 // In the MS ABI, store the vbtable offset, which is analogous to the
1371 // vbase offset offset in Itanium.
1372 BaseOffset =
1373 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1374 }
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001375 BFlags = llvm::DINode::FlagVirtual;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001376 } else
1377 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1378 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1379 // BI->isVirtual() and bits when not.
Eric Christopher6537f082013-05-16 00:45:12 +00001380
Stephen Hines176edba2014-12-01 14:53:08 -08001381 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001382 llvm::DIType *DTy = DBuilder.createInheritance(
Stephen Hines176edba2014-12-01 14:53:08 -08001383 RecordTy, getOrCreateType(BI.getType(), Unit), BaseOffset, BFlags);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001384 EltTys.push_back(DTy);
1385 }
1386}
1387
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001388llvm::DINodeArray
Stephen Hines176edba2014-12-01 14:53:08 -08001389CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1390 ArrayRef<TemplateArgument> TAList,
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001391 llvm::DIFile *Unit) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001392 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001393 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1394 const TemplateArgument &TA = TAList[i];
David Blaikie35178dc2013-06-22 18:59:18 +00001395 StringRef Name;
1396 if (TPList)
1397 Name = TPList->getParam(i)->getName();
David Blaikie9dfd2432013-05-10 21:53:14 +00001398 switch (TA.getKind()) {
1399 case TemplateArgument::Type: {
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001400 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
1401 TemplateParams.push_back(
1402 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie9dfd2432013-05-10 21:53:14 +00001403 } break;
1404 case TemplateArgument::Integral: {
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001405 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
1406 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1407 TheCU, Name, TTy,
1408 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie9dfd2432013-05-10 21:53:14 +00001409 } break;
1410 case TemplateArgument::Declaration: {
1411 const ValueDecl *D = TA.getAsDecl();
Stephen Hines176edba2014-12-01 14:53:08 -08001412 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001413 llvm::DIType *TTy = getOrCreateType(T, Unit);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001414 llvm::Constant *V = nullptr;
Stephen Hines176edba2014-12-01 14:53:08 -08001415 const CXXMethodDecl *MD;
David Blaikie9dfd2432013-05-10 21:53:14 +00001416 // Variable pointer template parameters have a value that is the address
1417 // of the variable.
Stephen Hines176edba2014-12-01 14:53:08 -08001418 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie9dfd2432013-05-10 21:53:14 +00001419 V = CGM.GetAddrOfGlobalVar(VD);
1420 // Member function pointers have special support for building them, though
1421 // this is currently unsupported in LLVM CodeGen.
Stephen Hines176edba2014-12-01 14:53:08 -08001422 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001423 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
Stephen Hines176edba2014-12-01 14:53:08 -08001424 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikief8aa1552013-05-13 06:57:50 +00001425 V = CGM.GetAddrOfFunction(FD);
David Blaikie9dfd2432013-05-10 21:53:14 +00001426 // Member data pointers have special handling too to compute the fixed
1427 // offset within the object.
Stephen Hines176edba2014-12-01 14:53:08 -08001428 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie9dfd2432013-05-10 21:53:14 +00001429 // These five lines (& possibly the above member function pointer
1430 // handling) might be able to be refactored to use similar code in
1431 // CodeGenModule::getMemberPointerConstant
1432 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1433 CharUnits chars =
Stephen Hines176edba2014-12-01 14:53:08 -08001434 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
1435 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie9dfd2432013-05-10 21:53:14 +00001436 }
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001437 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1438 TheCU, Name, TTy,
1439 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie9dfd2432013-05-10 21:53:14 +00001440 } break;
1441 case TemplateArgument::NullPtr: {
1442 QualType T = TA.getNullPtrType();
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001443 llvm::DIType *TTy = getOrCreateType(T, Unit);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001444 llvm::Constant *V = nullptr;
David Blaikie9dfd2432013-05-10 21:53:14 +00001445 // Special case member data pointer null values since they're actually -1
1446 // instead of zero.
1447 if (const MemberPointerType *MPT =
1448 dyn_cast<MemberPointerType>(T.getTypePtr()))
1449 // But treat member function pointers as simple zero integers because
1450 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1451 // CodeGen grows handling for values of non-null member function
1452 // pointers then perhaps we could remove this special case and rely on
1453 // EmitNullMemberPointer for member function pointers.
1454 if (MPT->isMemberDataPointer())
1455 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1456 if (!V)
1457 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001458 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1459 TheCU, Name, TTy, cast<llvm::Constant>(V)));
David Blaikie9dfd2432013-05-10 21:53:14 +00001460 } break;
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001461 case TemplateArgument::Template:
1462 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
1463 TheCU, Name, nullptr,
1464 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1465 break;
1466 case TemplateArgument::Pack:
1467 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1468 TheCU, Name, nullptr,
1469 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1470 break;
David Majnemer87b1f6d2013-08-24 08:21:10 +00001471 case TemplateArgument::Expression: {
1472 const Expr *E = TA.getAsExpr();
1473 QualType T = E->getType();
Stephen Hines176edba2014-12-01 14:53:08 -08001474 if (E->isGLValue())
1475 T = CGM.getContext().getLValueReferenceType(T);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001476 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer87b1f6d2013-08-24 08:21:10 +00001477 assert(V && "Expression in template argument isn't constant");
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001478 llvm::DIType *TTy = getOrCreateType(T, Unit);
1479 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1480 TheCU, Name, TTy, cast<llvm::Constant>(V->stripPointerCasts())));
David Majnemer87b1f6d2013-08-24 08:21:10 +00001481 } break;
David Blaikiee8065122013-05-10 23:36:06 +00001482 // And the following should never occur:
David Blaikie9dfd2432013-05-10 21:53:14 +00001483 case TemplateArgument::TemplateExpansion:
David Blaikie9dfd2432013-05-10 21:53:14 +00001484 case TemplateArgument::Null:
1485 llvm_unreachable(
1486 "These argument types shouldn't exist in concrete types");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001487 }
1488 }
1489 return DBuilder.getOrCreateArray(TemplateParams);
1490}
1491
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001492llvm::DINodeArray
1493CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
1494 llvm::DIFile *Unit) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001495 if (FD->getTemplatedKind() ==
1496 FunctionDecl::TK_FunctionTemplateSpecialization) {
Stephen Hines176edba2014-12-01 14:53:08 -08001497 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1498 ->getTemplate()
1499 ->getTemplateParameters();
David Blaikie35178dc2013-06-22 18:59:18 +00001500 return CollectTemplateParams(
1501 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001502 }
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001503 return llvm::DINodeArray();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001504}
1505
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001506llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1507 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001508 // Always get the full list of parameters, not just the ones from
1509 // the specialization.
1510 TemplateParameterList *TPList =
Stephen Hines176edba2014-12-01 14:53:08 -08001511 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001512 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie35178dc2013-06-22 18:59:18 +00001513 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001514}
1515
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001516llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07001517 if (VTablePtrType)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001518 return VTablePtrType;
1519
1520 ASTContext &Context = CGM.getContext();
1521
1522 /* Function type */
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001523 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001524 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001525 llvm::DIType *SubTy = DBuilder.createSubroutineType(SElements);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001526 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001527 llvm::DIType *vtbl_ptr_type =
Stephen Hines176edba2014-12-01 14:53:08 -08001528 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001529 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1530 return VTablePtrType;
1531}
1532
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001533StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer84953792013-09-09 16:39:06 +00001534 // Copy the gdb compatible name on the side and use its reference.
1535 return internString("_vptr$", RD->getNameAsString());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001536}
1537
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001538void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001539 SmallVectorImpl<llvm::Metadata *> &EltTys) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001540 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1541
1542 // If there is a primary base then it will hold vtable info.
1543 if (RL.getPrimaryBase())
1544 return;
1545
1546 // If this class is not dynamic then there is not any vtable info to collect.
1547 if (!RD->isDynamicClass())
1548 return;
1549
1550 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001551 llvm::DIType *VPTR = DBuilder.createMemberType(
Stephen Hines176edba2014-12-01 14:53:08 -08001552 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001553 llvm::DINode::FlagArtificial, getOrCreateVTablePtrType(Unit));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001554 EltTys.push_back(VPTR);
1555}
1556
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001557llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
1558 SourceLocation Loc) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001559 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001560 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001561 return T;
1562}
1563
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001564llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
1565 SourceLocation Loc) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001566 return getOrCreateStandaloneType(D, Loc);
1567}
1568
1569llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
1570 SourceLocation Loc) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001571 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001572 assert(!D.isNull() && "null type");
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001573 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001574 assert(T && "could not create debug info for type");
1575
Adrian Prantlebbd7e02013-03-11 18:33:46 +00001576 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001577 return T;
1578}
1579
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001580void CGDebugInfo::completeType(const EnumDecl *ED) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001581 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001582 return;
1583 QualType Ty = CGM.getContext().getEnumType(ED);
Stephen Hines176edba2014-12-01 14:53:08 -08001584 void *TyPtr = Ty.getAsOpaquePtr();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001585 auto I = TypeCache.find(TyPtr);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001586 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001587 return;
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001588 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07001589 assert(!Res->isForwardDecl());
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001590 TypeCache[TyPtr].reset(Res);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001591}
1592
David Blaikie27804892013-08-15 20:49:17 +00001593void CGDebugInfo::completeType(const RecordDecl *RD) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001594 if (DebugKind > codegenoptions::LimitedDebugInfo ||
David Blaikie27804892013-08-15 20:49:17 +00001595 !CGM.getLangOpts().CPlusPlus)
1596 completeRequiredType(RD);
1597}
1598
1599void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001600 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
Stephen Hines651f13c2014-04-23 16:59:28 -07001601 return;
1602
David Blaikie5434fc22013-08-20 01:28:15 +00001603 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1604 if (CXXDecl->isDynamicClass())
1605 return;
1606
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001607 if (DebugTypeExtRefs && RD->isFromASTFile())
1608 return;
1609
David Blaikie27804892013-08-15 20:49:17 +00001610 QualType Ty = CGM.getContext().getRecordType(RD);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001611 llvm::DIType *T = getTypeOrNull(Ty);
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07001612 if (T && T->isForwardDecl())
David Blaikie5434fc22013-08-20 01:28:15 +00001613 completeClassData(RD);
1614}
1615
1616void CGDebugInfo::completeClassData(const RecordDecl *RD) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001617 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
Michael Gottesman90e55232013-08-19 18:46:16 +00001618 return;
David Blaikie5434fc22013-08-20 01:28:15 +00001619 QualType Ty = CGM.getContext().getRecordType(RD);
Stephen Hines176edba2014-12-01 14:53:08 -08001620 void *TyPtr = Ty.getAsOpaquePtr();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001621 auto I = TypeCache.find(TyPtr);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001622 if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie27804892013-08-15 20:49:17 +00001623 return;
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001624 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07001625 assert(!Res->isForwardDecl());
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001626 TypeCache[TyPtr].reset(Res);
David Blaikie27804892013-08-15 20:49:17 +00001627}
1628
Stephen Hines651f13c2014-04-23 16:59:28 -07001629static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1630 CXXRecordDecl::method_iterator End) {
1631 for (; I != End; ++I)
1632 if (FunctionDecl *Tmpl = I->getInstantiatedFromMemberFunction())
1633 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
1634 !I->getMemberSpecializationInfo()->isExplicitSpecialization())
1635 return true;
1636 return false;
1637}
1638
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001639/// Does a type definition exist in an imported clang module?
1640static bool isDefinedInClangModule(const RecordDecl *RD) {
1641 if (!RD || !RD->isFromASTFile())
1642 return false;
1643 if (!RD->isExternallyVisible() && RD->getName().empty())
1644 return false;
1645 if (auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) {
1646 assert(CXXDecl->isCompleteDefinition() && "incomplete record definition");
1647 if (CXXDecl->getTemplateSpecializationKind() != TSK_Undeclared)
1648 // Make sure the instantiation is actually in a module.
1649 if (CXXDecl->field_begin() != CXXDecl->field_end())
1650 return CXXDecl->field_begin()->isFromASTFile();
1651 }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001652
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001653 return true;
1654}
1655
1656static bool shouldOmitDefinition(codegenoptions::DebugInfoKind DebugKind,
1657 bool DebugTypeExtRefs, const RecordDecl *RD,
1658 const LangOptions &LangOpts) {
1659 if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
1660 return true;
1661
1662 if (DebugKind > codegenoptions::LimitedDebugInfo)
Stephen Hines651f13c2014-04-23 16:59:28 -07001663 return false;
1664
1665 if (!LangOpts.CPlusPlus)
1666 return false;
1667
1668 if (!RD->isCompleteDefinitionRequired())
1669 return true;
1670
1671 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1672
1673 if (!CXXDecl)
1674 return false;
1675
1676 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass())
1677 return true;
1678
1679 TemplateSpecializationKind Spec = TSK_Undeclared;
1680 if (const ClassTemplateSpecializationDecl *SD =
1681 dyn_cast<ClassTemplateSpecializationDecl>(RD))
1682 Spec = SD->getSpecializationKind();
1683
1684 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1685 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1686 CXXDecl->method_end()))
1687 return true;
1688
1689 return false;
1690}
1691
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001692llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001693 RecordDecl *RD = Ty->getDecl();
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001694 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001695 if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD,
1696 CGM.getLangOpts())) {
David Blaikie74341d82013-09-06 06:45:04 +00001697 if (!T)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001698 T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD));
David Blaikie74341d82013-09-06 06:45:04 +00001699 return T;
David Blaikie5f6e2f42013-06-05 05:32:23 +00001700 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001701
David Blaikie27804892013-08-15 20:49:17 +00001702 return CreateTypeDefinition(Ty);
1703}
1704
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001705llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikie27804892013-08-15 20:49:17 +00001706 RecordDecl *RD = Ty->getDecl();
1707
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001708 // Get overall information about the record type for the debug info.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001709 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001710
1711 // Records and classes and unions can all be recursive. To handle them, we
1712 // first generate a debug descriptor for the struct as a forward declaration.
1713 // Then (if it is a definition) we go through and get debug info for all of
1714 // its members. Finally, we create a descriptor for the complete type (which
1715 // may refer to the forward decl if the struct is recursive) and replace all
1716 // uses of the forward declaration with the final definition.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001717 llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001718
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001719 const RecordDecl *D = RD->getDefinition();
1720 if (!D || !D->isCompleteDefinition())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001721 return FwdDecl;
1722
David Blaikie498298d2013-08-18 16:55:33 +00001723 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1724 CollectContainingType(CXXDecl, FwdDecl);
1725
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001726 // Push the struct on region stack.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001727 LexicalBlockStack.emplace_back(&*FwdDecl);
1728 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001729
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001730 // Convert all the elements.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001731 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie5434fc22013-08-20 01:28:15 +00001732 // what about nested types?
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001733
1734 // Note: The split of CXXDecl information here is intentional, the
1735 // gdb tests will depend on a certain ordering at printout. The debug
1736 // information offsets are still correct if we merge them all together
1737 // though.
1738 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1739 if (CXXDecl) {
1740 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1741 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1742 }
1743
Eric Christopher0395de32013-01-16 01:22:32 +00001744 // Collect data fields (including static variables and any initializers).
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001745 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopherd6b52972013-10-11 18:16:51 +00001746 if (CXXDecl)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001747 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001748
1749 LexicalBlockStack.pop_back();
1750 RegionMap.erase(Ty->getDecl());
1751
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001752 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001753 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001754
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001755 if (FwdDecl->isTemporary())
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07001756 FwdDecl =
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001757 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001758
1759 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopherf068c922013-04-02 22:59:11 +00001760 return FwdDecl;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001761}
1762
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001763llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1764 llvm::DIFile *Unit) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001765 // Ignore protocols.
1766 return getOrCreateType(Ty->getBaseType(), Unit);
1767}
1768
Adrian Prantl5ae17a12013-06-07 01:10:45 +00001769/// \return true if Getter has the default name for the property PD.
1770static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1771 const ObjCMethodDecl *Getter) {
1772 assert(PD);
1773 if (!Getter)
1774 return true;
1775
1776 assert(Getter->getDeclName().isObjCZeroArgSelector());
1777 return PD->getName() ==
Stephen Hines176edba2014-12-01 14:53:08 -08001778 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantl5ae17a12013-06-07 01:10:45 +00001779}
1780
1781/// \return true if Setter has the default name for the property PD.
1782static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1783 const ObjCMethodDecl *Setter) {
1784 assert(PD);
1785 if (!Setter)
1786 return true;
1787
1788 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantl80e8ea92013-06-07 22:29:12 +00001789 return SelectorTable::constructSetterName(PD->getName()) ==
Stephen Hines176edba2014-12-01 14:53:08 -08001790 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantl5ae17a12013-06-07 01:10:45 +00001791}
1792
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001793llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1794 llvm::DIFile *Unit) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001795 ObjCInterfaceDecl *ID = Ty->getDecl();
1796 if (!ID)
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001797 return nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001798
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001799 // Return a forward declaration if this type was imported from a clang module,
1800 // and this is not the compile unit with the implementation of the type (which
1801 // may contain hidden ivars).
1802 if (DebugTypeExtRefs && ID->isFromASTFile() && ID->getDefinition() &&
1803 !ID->getImplementation())
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001804 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
1805 ID->getName(),
1806 getDeclContextDescriptor(ID), Unit, 0);
1807
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001808 // Get overall information about the record type for the debug info.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001809 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001810 unsigned Line = getLineNumber(ID->getLocation());
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07001811 auto RuntimeLang =
1812 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001813
1814 // If this is just a forward declaration return a special forward-declaration
1815 // debug type since we won't be able to lay out the entire type.
1816 ObjCInterfaceDecl *Def = ID->getDefinition();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001817 if (!Def || !Def->getImplementation()) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001818 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001819 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001820 llvm::dwarf::DW_TAG_structure_type, ID->getName(), Mod ? Mod : TheCU,
1821 DefUnit, Line, RuntimeLang);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001822 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001823 return FwdDecl;
1824 }
1825
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001826 return CreateTypeDefinition(Ty, Unit);
1827}
1828
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001829llvm::DIModule *
1830CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod,
1831 bool CreateSkeletonCU) {
1832 // Use the Module pointer as the key into the cache. This is a
1833 // nullptr if the "Module" is a PCH, which is safe because we don't
1834 // support chained PCH debug info, so there can only be a single PCH.
1835 const Module *M = Mod.getModuleOrNull();
1836 auto ModRef = ModuleCache.find(M);
1837 if (ModRef != ModuleCache.end())
1838 return cast<llvm::DIModule>(ModRef->second);
1839
1840 // Macro definitions that were defined with "-D" on the command line.
1841 SmallString<128> ConfigMacros;
1842 {
1843 llvm::raw_svector_ostream OS(ConfigMacros);
1844 const auto &PPOpts = CGM.getPreprocessorOpts();
1845 unsigned I = 0;
1846 // Translate the macro definitions back into a commmand line.
1847 for (auto &M : PPOpts.Macros) {
1848 if (++I > 1)
1849 OS << " ";
1850 const std::string &Macro = M.first;
1851 bool Undef = M.second;
1852 OS << "\"-" << (Undef ? 'U' : 'D');
1853 for (char c : Macro)
1854 switch (c) {
1855 case '\\' : OS << "\\\\"; break;
1856 case '"' : OS << "\\\""; break;
1857 default: OS << c;
1858 }
1859 OS << '\"';
1860 }
1861 }
1862
1863 bool IsRootModule = M ? !M->Parent : true;
1864 if (CreateSkeletonCU && IsRootModule) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001865 // PCH files don't have a signature field in the control block,
1866 // but LLVM detects skeleton CUs by looking for a non-zero DWO id.
1867 uint64_t Signature = Mod.getSignature() ? Mod.getSignature() : ~1ULL;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001868 llvm::DIBuilder DIB(CGM.getModule());
1869 DIB.createCompileUnit(TheCU->getSourceLanguage(), Mod.getModuleName(),
1870 Mod.getPath(), TheCU->getProducer(), true,
1871 StringRef(), 0, Mod.getASTFile(),
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001872 llvm::DICompileUnit::FullDebug, Signature);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001873 DIB.finalize();
1874 }
1875 llvm::DIModule *Parent =
1876 IsRootModule ? nullptr
1877 : getOrCreateModuleRef(
1878 ExternalASTSource::ASTSourceDescriptor(*M->Parent),
1879 CreateSkeletonCU);
1880 llvm::DIModule *DIMod =
1881 DBuilder.createModule(Parent, Mod.getModuleName(), ConfigMacros,
1882 Mod.getPath(), CGM.getHeaderSearchOpts().Sysroot);
1883 ModuleCache[M].reset(DIMod);
1884 return DIMod;
1885}
1886
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001887llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
1888 llvm::DIFile *Unit) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001889 ObjCInterfaceDecl *ID = Ty->getDecl();
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001890 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001891 unsigned Line = getLineNumber(ID->getLocation());
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07001892 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001893
1894 // Bit size, align and offset of the type.
1895 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1896 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1897
1898 unsigned Flags = 0;
1899 if (ID->getImplementation())
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001900 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001901
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001902 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001903 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001904 Mod ? Mod : Unit, ID->getName(), DefUnit, Line, Size, Align, Flags,
1905 nullptr, llvm::DINodeArray(), RuntimeLang);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001906
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001907 QualType QTy(Ty, 0);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001908 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001909
Eric Christopherd3003dc2013-07-14 21:00:07 +00001910 // Push the struct on region stack.
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07001911 LexicalBlockStack.emplace_back(RealDecl);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001912 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001913
1914 // Convert all the elements.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001915 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001916
1917 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1918 if (SClass) {
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001919 llvm::DIType *SClassTy =
Stephen Hines176edba2014-12-01 14:53:08 -08001920 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07001921 if (!SClassTy)
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001922 return nullptr;
Eric Christopher6537f082013-05-16 00:45:12 +00001923
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001924 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001925 EltTys.push_back(InhTag);
1926 }
1927
Eric Christopherd3003dc2013-07-14 21:00:07 +00001928 // Create entries for all of the properties.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001929 auto AddProperty = [&](const ObjCPropertyDecl *PD) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001930 SourceLocation Loc = PD->getLocation();
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001931 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001932 unsigned PLine = getLineNumber(Loc);
1933 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1934 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Stephen Hines176edba2014-12-01 14:53:08 -08001935 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
1936 PD->getName(), PUnit, PLine,
1937 hasDefaultGetterName(PD, Getter) ? ""
1938 : getSelectorName(PD->getGetterName()),
1939 hasDefaultSetterName(PD, Setter) ? ""
1940 : getSelectorName(PD->getSetterName()),
1941 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001942 EltTys.push_back(PropertyNode);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001943 };
1944 {
1945 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
1946 for (const ObjCCategoryDecl *ClassExt : ID->known_extensions())
1947 for (auto *PD : ClassExt->properties()) {
1948 PropertySet.insert(PD->getIdentifier());
1949 AddProperty(PD);
1950 }
1951 for (const auto *PD : ID->properties()) {
1952 // Don't emit duplicate metadata for properties that were already in a
1953 // class extension.
1954 if (!PropertySet.insert(PD->getIdentifier()).second)
1955 continue;
1956 AddProperty(PD);
1957 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001958 }
1959
1960 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1961 unsigned FieldNo = 0;
1962 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1963 Field = Field->getNextIvar(), ++FieldNo) {
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001964 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07001965 if (!FieldTy)
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001966 return nullptr;
Eric Christopher6537f082013-05-16 00:45:12 +00001967
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001968 StringRef FieldName = Field->getName();
1969
1970 // Ignore unnamed fields.
1971 if (FieldName.empty())
1972 continue;
1973
1974 // Get the location for the field.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001975 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001976 unsigned FieldLine = getLineNumber(Field->getLocation());
1977 QualType FType = Field->getType();
1978 uint64_t FieldSize = 0;
1979 unsigned FieldAlign = 0;
1980
1981 if (!FType->isIncompleteArrayType()) {
1982
1983 // Bit size, align and offset of the type.
1984 FieldSize = Field->isBitField()
Eric Christopherd3003dc2013-07-14 21:00:07 +00001985 ? Field->getBitWidthValue(CGM.getContext())
1986 : CGM.getContext().getTypeSize(FType);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001987 FieldAlign = CGM.getContext().getTypeAlign(FType);
1988 }
1989
1990 uint64_t FieldOffset;
1991 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1992 // We don't know the runtime offset of an ivar if we're using the
1993 // non-fragile ABI. For bitfields, use the bit offset into the first
1994 // byte of storage of the bitfield. For other fields, use zero.
1995 if (Field->isBitField()) {
Stephen Hines176edba2014-12-01 14:53:08 -08001996 FieldOffset =
1997 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001998 FieldOffset %= CGM.getContext().getCharWidth();
1999 } else {
2000 FieldOffset = 0;
2001 }
2002 } else {
2003 FieldOffset = RL.getFieldOffset(FieldNo);
2004 }
2005
2006 unsigned Flags = 0;
2007 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002008 Flags = llvm::DINode::FlagProtected;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002009 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002010 Flags = llvm::DINode::FlagPrivate;
Stephen Hines176edba2014-12-01 14:53:08 -08002011 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002012 Flags = llvm::DINode::FlagPublic;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002013
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002014 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002015 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopher6537f082013-05-16 00:45:12 +00002016 if (ObjCPropertyImplDecl *PImpD =
Stephen Hines176edba2014-12-01 14:53:08 -08002017 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002018 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherbe5f1be2013-02-21 22:35:08 +00002019 SourceLocation Loc = PD->getLocation();
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002020 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherbe5f1be2013-02-21 22:35:08 +00002021 unsigned PLine = getLineNumber(Loc);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002022 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
2023 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Stephen Hines176edba2014-12-01 14:53:08 -08002024 PropertyNode = DBuilder.createObjCProperty(
2025 PD->getName(), PUnit, PLine,
2026 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
2027 PD->getGetterName()),
2028 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
2029 PD->getSetterName()),
2030 PD->getPropertyAttributes(),
2031 getOrCreateType(PD->getType(), PUnit));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002032 }
2033 }
2034 }
Stephen Hines176edba2014-12-01 14:53:08 -08002035 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
2036 FieldSize, FieldAlign, FieldOffset, Flags,
2037 FieldTy, PropertyNode);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002038 EltTys.push_back(FieldTy);
2039 }
2040
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002041 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002042 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantl4919de62013-03-06 22:03:30 +00002043
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002044 LexicalBlockStack.pop_back();
Eric Christopherf068c922013-04-02 22:59:11 +00002045 return RealDecl;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002046}
2047
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002048llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
2049 llvm::DIFile *Unit) {
2050 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002051 int64_t Count = Ty->getNumElements();
2052 if (Count == 0)
2053 // If number of elements are not known then this is an unbounded array.
2054 // Use Count == -1 to express such arrays.
2055 Count = -1;
2056
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002057 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002058 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002059
2060 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2061 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
2062
2063 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
2064}
2065
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002066llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002067 uint64_t Size;
2068 uint64_t Align;
2069
2070 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
2071 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
2072 Size = 0;
2073 Align =
Stephen Hines176edba2014-12-01 14:53:08 -08002074 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002075 } else if (Ty->isIncompleteArrayType()) {
2076 Size = 0;
2077 if (Ty->getElementType()->isIncompleteType())
2078 Align = 0;
2079 else
2080 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikie089db2e2013-05-09 20:48:12 +00002081 } else if (Ty->isIncompleteType()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002082 Size = 0;
2083 Align = 0;
2084 } else {
2085 // Size and align of the whole array, not the element type.
2086 Size = CGM.getContext().getTypeSize(Ty);
2087 Align = CGM.getContext().getTypeAlign(Ty);
2088 }
2089
2090 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
2091 // interior arrays, do we care? Why aren't nested arrays represented the
2092 // obvious/recursive way?
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002093 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002094 QualType EltTy(Ty, 0);
2095 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
2096 // If the number of elements is known, then count is that number. Otherwise,
2097 // it's -1. This allows us to represent a subrange with an array of 0
2098 // elements, like this:
2099 //
2100 // struct foo {
2101 // int x[0];
2102 // };
Stephen Hines176edba2014-12-01 14:53:08 -08002103 int64_t Count = -1; // Count == -1 is an unbounded array.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002104 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
2105 Count = CAT->getSize().getZExtValue();
Eric Christopher6537f082013-05-16 00:45:12 +00002106
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002107 // FIXME: Verify this is right for VLAs.
2108 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
2109 EltTy = Ty->getElementType();
2110 }
2111
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002112 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002113
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002114 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
2115 SubscriptArray);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002116}
2117
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002118llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
2119 llvm::DIFile *Unit) {
Stephen Hines176edba2014-12-01 14:53:08 -08002120 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
2121 Ty->getPointeeType(), Unit);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002122}
2123
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002124llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
2125 llvm::DIFile *Unit) {
Stephen Hines176edba2014-12-01 14:53:08 -08002126 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
2127 Ty->getPointeeType(), Unit);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002128}
2129
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002130llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
2131 llvm::DIFile *U) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002132 unsigned Flags = 0;
2133 uint64_t Size = 0;
2134
2135 if (!Ty->isIncompleteType()) {
2136 Size = CGM.getContext().getTypeSize(Ty);
2137
2138 // Set the MS inheritance model. There is no flag for the unspecified model.
2139 if (CGM.getTarget().getCXXABI().isMicrosoft()) {
2140 switch (Ty->getMostRecentCXXRecordDecl()->getMSInheritanceModel()) {
2141 case MSInheritanceAttr::Keyword_single_inheritance:
2142 Flags |= llvm::DINode::FlagSingleInheritance;
2143 break;
2144 case MSInheritanceAttr::Keyword_multiple_inheritance:
2145 Flags |= llvm::DINode::FlagMultipleInheritance;
2146 break;
2147 case MSInheritanceAttr::Keyword_virtual_inheritance:
2148 Flags |= llvm::DINode::FlagVirtualInheritance;
2149 break;
2150 case MSInheritanceAttr::Keyword_unspecified_inheritance:
2151 break;
2152 }
2153 }
2154 }
2155
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002156 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
2157 if (Ty->isMemberDataPointerType())
David Blaikiee8d75142013-01-19 19:20:56 +00002158 return DBuilder.createMemberPointerType(
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002159 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size, /*Align=*/0,
2160 Flags);
Stephen Hines651f13c2014-04-23 16:59:28 -07002161
2162 const FunctionProtoType *FPT =
Stephen Hines176edba2014-12-01 14:53:08 -08002163 Ty->getPointeeType()->getAs<FunctionProtoType>();
2164 return DBuilder.createMemberPointerType(
2165 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
2166 Ty->getClass(), FPT->getTypeQuals())),
2167 FPT, U),
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002168 ClassType, Size, /*Align=*/0, Flags);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002169}
2170
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002171llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002172 // Ignore the atomic wrapping
2173 // FIXME: What is the correct representation?
2174 return getOrCreateType(Ty->getValueType(), U);
2175}
2176
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002177llvm::DIType* CGDebugInfo::CreateType(const PipeType *Ty,
2178 llvm::DIFile *U) {
2179 return getOrCreateType(Ty->getElementType(), U);
2180}
2181
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002182llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Renf3327332013-08-28 21:20:28 +00002183 const EnumDecl *ED = Ty->getDecl();
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002184
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002185 uint64_t Size = 0;
2186 uint64_t Align = 0;
2187 if (!ED->getTypeForDecl()->isIncompleteType()) {
2188 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
2189 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
2190 }
2191
Manman Ren83369bf2013-08-29 23:19:58 +00002192 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2193
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002194 bool isImportedFromModule =
2195 DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition();
2196
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002197 // If this is just a forward declaration, construct an appropriately
2198 // marked node and just return it.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002199 if (isImportedFromModule || !ED->getDefinition()) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002200 // Note that it is possible for enums to be created as part of
2201 // their own declcontext. In this case a FwdDecl will be created
2202 // twice. This doesn't cause a problem because both FwdDecls are
2203 // entered into the ReplaceMap: finalize() will replace the first
2204 // FwdDecl with the second and then replace the second with
2205 // complete type.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002206 llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002207 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002208 llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType(
2209 llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0));
2210
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002211 unsigned Line = getLineNumber(ED->getLocation());
2212 StringRef EDName = ED->getName();
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002213 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002214 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002215 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002216
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002217 ReplaceMap.emplace_back(
2218 std::piecewise_construct, std::make_tuple(Ty),
2219 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002220 return RetTy;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002221 }
2222
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002223 return CreateTypeDefinition(Ty);
2224}
2225
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002226llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002227 const EnumDecl *ED = Ty->getDecl();
2228 uint64_t Size = 0;
2229 uint64_t Align = 0;
2230 if (!ED->getTypeForDecl()->isIncompleteType()) {
2231 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
2232 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
2233 }
2234
2235 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2236
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002237 // Create elements for each enumerator.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002238 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002239 ED = ED->getDefinition();
Stephen Hines651f13c2014-04-23 16:59:28 -07002240 for (const auto *Enum : ED->enumerators()) {
Stephen Hines176edba2014-12-01 14:53:08 -08002241 Enumerators.push_back(DBuilder.createEnumerator(
2242 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002243 }
2244
2245 // Return a CompositeType for the enum itself.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002246 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002247
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002248 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002249 unsigned Line = getLineNumber(ED->getLocation());
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002250 llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002251 llvm::DIType *ClassTy =
2252 ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
2253 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
2254 Line, Size, Align, EltArray, ClassTy,
2255 FullName);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002256}
2257
David Blaikie4b12be62013-01-21 04:37:12 +00002258static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
2259 Qualifiers Quals;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002260 do {
Adrian Prantl35969ea2013-09-26 21:35:50 +00002261 Qualifiers InnerQuals = T.getLocalQualifiers();
2262 // Qualifiers::operator+() doesn't like it if you add a Qualifier
2263 // that is already there.
2264 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2265 Quals += InnerQuals;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002266 QualType LastT = T;
2267 switch (T->getTypeClass()) {
2268 default:
David Blaikie4b12be62013-01-21 04:37:12 +00002269 return C.getQualifiedType(T.getTypePtr(), Quals);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002270 case Type::TemplateSpecialization: {
2271 const auto *Spec = cast<TemplateSpecializationType>(T);
2272 if (Spec->isTypeAlias())
2273 return C.getQualifiedType(T.getTypePtr(), Quals);
2274 T = Spec->desugar();
Stephen Hines176edba2014-12-01 14:53:08 -08002275 break;
2276 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002277 case Type::TypeOfExpr:
2278 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2279 break;
2280 case Type::TypeOf:
2281 T = cast<TypeOfType>(T)->getUnderlyingType();
2282 break;
2283 case Type::Decltype:
2284 T = cast<DecltypeType>(T)->getUnderlyingType();
2285 break;
2286 case Type::UnaryTransform:
2287 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2288 break;
2289 case Type::Attributed:
2290 T = cast<AttributedType>(T)->getEquivalentType();
2291 break;
2292 case Type::Elaborated:
2293 T = cast<ElaboratedType>(T)->getNamedType();
2294 break;
2295 case Type::Paren:
2296 T = cast<ParenType>(T)->getInnerType();
2297 break;
David Blaikie4b12be62013-01-21 04:37:12 +00002298 case Type::SubstTemplateTypeParm:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002299 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002300 break;
2301 case Type::Auto:
David Blaikie91296482013-05-24 21:24:35 +00002302 QualType DT = cast<AutoType>(T)->getDeducedType();
Stephen Hines176edba2014-12-01 14:53:08 -08002303 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie91296482013-05-24 21:24:35 +00002304 T = DT;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002305 break;
2306 }
Eric Christopher6537f082013-05-16 00:45:12 +00002307
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002308 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumid24c9ab2013-01-21 10:51:28 +00002309 (void)LastT;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002310 } while (true);
2311}
2312
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002313llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002314
2315 // Unwrap the type as needed for debug information.
David Blaikie4b12be62013-01-21 04:37:12 +00002316 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopher6537f082013-05-16 00:45:12 +00002317
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002318 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002319 if (it != TypeCache.end()) {
2320 // Verify that the debug info still exists.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002321 if (llvm::Metadata *V = it->second)
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002322 return cast<llvm::DIType>(V);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002323 }
2324
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002325 return nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002326}
2327
Stephen Hines651f13c2014-04-23 16:59:28 -07002328void CGDebugInfo::completeTemplateDefinition(
2329 const ClassTemplateSpecializationDecl &SD) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002330 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
Stephen Hines651f13c2014-04-23 16:59:28 -07002331 return;
2332
2333 completeClassData(&SD);
2334 // In case this type has no member function definitions being emitted, ensure
2335 // it is retained
2336 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2337}
2338
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002339llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002340 if (Ty.isNull())
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002341 return nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002342
2343 // Unwrap the type as needed for debug information.
David Blaikie4b12be62013-01-21 04:37:12 +00002344 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002345
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002346 if (auto *T = getTypeOrNull(Ty))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002347 return T;
2348
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002349 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002350 void* TyPtr = Ty.getAsOpaquePtr();
Adrian Prantlebbd7e02013-03-11 18:33:46 +00002351
2352 // And update the type cache.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002353 TypeCache[TyPtr].reset(Res);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002354
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002355 return Res;
2356}
2357
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002358llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
2359 // A forward declaration inside a module header does not belong to the module.
2360 if (isa<RecordDecl>(D) && !cast<RecordDecl>(D)->getDefinition())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002361 return nullptr;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002362 if (DebugTypeExtRefs && D->isFromASTFile()) {
2363 // Record a reference to an imported clang module or precompiled header.
2364 auto *Reader = CGM.getContext().getExternalSource();
2365 auto Idx = D->getOwningModuleID();
2366 auto Info = Reader->getSourceDescriptor(Idx);
2367 if (Info)
2368 return getOrCreateModuleRef(*Info, /*SkeletonCU=*/true);
2369 } else if (ClangModuleMap) {
2370 // We are building a clang module or a precompiled header.
2371 //
2372 // TODO: When D is a CXXRecordDecl or a C++ Enum, the ODR applies
2373 // and it wouldn't be necessary to specify the parent scope
2374 // because the type is already unique by definition (it would look
2375 // like the output of -fno-standalone-debug). On the other hand,
2376 // the parent scope helps a consumer to quickly locate the object
2377 // file where the type's definition is located, so it might be
2378 // best to make this behavior a command line or debugger tuning
2379 // option.
2380 FullSourceLoc Loc(D->getLocation(), CGM.getContext().getSourceManager());
2381 if (Module *M = ClangModuleMap->inferModuleFromLocation(Loc)) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002382 // This is a (sub-)module.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002383 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
2384 return getOrCreateModuleRef(Info, /*SkeletonCU=*/false);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002385 } else {
2386 // This the precompiled header being built.
2387 return getOrCreateModuleRef(PCHDescriptor, /*SkeletonCU=*/false);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002388 }
Adrian Prantl4919de62013-03-06 22:03:30 +00002389 }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002390
2391 return nullptr;
Adrian Prantl4919de62013-03-06 22:03:30 +00002392}
2393
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002394llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002395 // Handle qualifiers, which recursively handles what they refer to.
2396 if (Ty.hasLocalQualifiers())
David Blaikie29b8b682013-09-04 22:03:57 +00002397 return CreateQualifiedType(Ty, Unit);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002398
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002399 // Work out details of type.
2400 switch (Ty->getTypeClass()) {
2401#define TYPE(Class, Base)
2402#define ABSTRACT_TYPE(Class, Base)
2403#define NON_CANONICAL_TYPE(Class, Base)
2404#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2405#include "clang/AST/TypeNodes.def"
2406 llvm_unreachable("Dependent types cannot show up in debug information");
2407
2408 case Type::ExtVector:
2409 case Type::Vector:
2410 return CreateType(cast<VectorType>(Ty), Unit);
2411 case Type::ObjCObjectPointer:
2412 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2413 case Type::ObjCObject:
2414 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2415 case Type::ObjCInterface:
2416 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2417 case Type::Builtin:
2418 return CreateType(cast<BuiltinType>(Ty));
2419 case Type::Complex:
2420 return CreateType(cast<ComplexType>(Ty));
2421 case Type::Pointer:
2422 return CreateType(cast<PointerType>(Ty), Unit);
Stephen Hines651f13c2014-04-23 16:59:28 -07002423 case Type::Adjusted:
Reid Kleckner12df2462013-06-24 17:51:48 +00002424 case Type::Decayed:
Stephen Hines651f13c2014-04-23 16:59:28 -07002425 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner12df2462013-06-24 17:51:48 +00002426 return CreateType(
Stephen Hines651f13c2014-04-23 16:59:28 -07002427 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002428 case Type::BlockPointer:
2429 return CreateType(cast<BlockPointerType>(Ty), Unit);
2430 case Type::Typedef:
David Blaikie29b8b682013-09-04 22:03:57 +00002431 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002432 case Type::Record:
David Blaikie29b8b682013-09-04 22:03:57 +00002433 return CreateType(cast<RecordType>(Ty));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002434 case Type::Enum:
Manman Renf3327332013-08-28 21:20:28 +00002435 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002436 case Type::FunctionProto:
2437 case Type::FunctionNoProto:
2438 return CreateType(cast<FunctionType>(Ty), Unit);
2439 case Type::ConstantArray:
2440 case Type::VariableArray:
2441 case Type::IncompleteArray:
2442 return CreateType(cast<ArrayType>(Ty), Unit);
2443
2444 case Type::LValueReference:
2445 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2446 case Type::RValueReference:
2447 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2448
2449 case Type::MemberPointer:
2450 return CreateType(cast<MemberPointerType>(Ty), Unit);
2451
2452 case Type::Atomic:
2453 return CreateType(cast<AtomicType>(Ty), Unit);
2454
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002455 case Type::Pipe:
2456 return CreateType(cast<PipeType>(Ty), Unit);
2457
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002458 case Type::TemplateSpecialization:
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002459 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2460
Stephen Hines176edba2014-12-01 14:53:08 -08002461 case Type::Auto:
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002462 case Type::Attributed:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002463 case Type::Elaborated:
2464 case Type::Paren:
2465 case Type::SubstTemplateTypeParm:
2466 case Type::TypeOfExpr:
2467 case Type::TypeOf:
2468 case Type::Decltype:
2469 case Type::UnaryTransform:
David Blaikie226399c2013-07-13 21:08:08 +00002470 case Type::PackExpansion:
David Blaikie91296482013-05-24 21:24:35 +00002471 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002472 }
Eric Christopher6537f082013-05-16 00:45:12 +00002473
Stephen Hines176edba2014-12-01 14:53:08 -08002474 llvm_unreachable("type should have been unwrapped!");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002475}
2476
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002477llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2478 llvm::DIFile *Unit) {
David Blaikie4a077162013-08-12 22:24:20 +00002479 QualType QTy(Ty, 0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002480
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002481 auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002482
2483 // We may have cached a forward decl when we could have created
2484 // a non-forward decl. Go ahead and create a non-forward decl
2485 // now.
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002486 if (T && !T->isForwardDecl())
Stephen Hines176edba2014-12-01 14:53:08 -08002487 return T;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002488
2489 // Otherwise create the type.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002490 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikieeaacc882013-08-20 21:03:29 +00002491
2492 // Propagate members from the declaration to the definition
2493 // CreateType(const RecordType*) will overwrite this with the members in the
2494 // correct order if the full type is needed.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002495 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002496
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002497 // And update the type cache.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002498 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002499 return Res;
2500}
2501
2502// TODO: Currently used for context chains when limiting debug info.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002503llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002504 RecordDecl *RD = Ty->getDecl();
Eric Christopher6537f082013-05-16 00:45:12 +00002505
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002506 // Get overall information about the record type for the debug info.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002507 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002508 unsigned Line = getLineNumber(RD->getLocation());
2509 StringRef RDName = getClassName(RD);
2510
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002511 llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002512
David Blaikiec138ff52013-08-18 17:36:19 +00002513 // If we ended up creating the type during the context chain construction,
2514 // just return that.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002515 auto *T = cast_or_null<llvm::DICompositeType>(
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002516 getTypeOrNull(CGM.getContext().getRecordType(RD)));
2517 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Stephen Hines176edba2014-12-01 14:53:08 -08002518 return T;
David Blaikiec138ff52013-08-18 17:36:19 +00002519
Stephen Hines651f13c2014-04-23 16:59:28 -07002520 // If this is just a forward or incomplete declaration, construct an
2521 // appropriately marked node and just return it.
2522 const RecordDecl *D = RD->getDefinition();
2523 if (!D || !D->isCompleteDefinition())
Manman Renf3327332013-08-28 21:20:28 +00002524 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002525
2526 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2527 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopher6537f082013-05-16 00:45:12 +00002528
Manman Ren83369bf2013-08-29 23:19:58 +00002529 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2530
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002531 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002532 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align, 0,
2533 FullName);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002534
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002535 // Elements of composite types usually have back to the type, creating
2536 // uniquing cycles. Distinct nodes are more efficient.
2537 switch (RealDecl->getTag()) {
2538 default:
2539 llvm_unreachable("invalid composite type tag");
2540
2541 case llvm::dwarf::DW_TAG_array_type:
2542 case llvm::dwarf::DW_TAG_enumeration_type:
2543 // Array elements and most enumeration elements don't have back references,
2544 // so they don't tend to be involved in uniquing cycles and there is some
2545 // chance of merging them when linking together two modules. Only make
2546 // them distinct if they are ODR-uniqued.
2547 if (FullName.empty())
2548 break;
2549
2550 case llvm::dwarf::DW_TAG_structure_type:
2551 case llvm::dwarf::DW_TAG_union_type:
2552 case llvm::dwarf::DW_TAG_class_type:
2553 // Immediatley resolve to a distinct node.
2554 RealDecl =
2555 llvm::MDNode::replaceWithDistinct(llvm::TempDICompositeType(RealDecl));
2556 break;
2557 }
2558
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002559 RegionMap[Ty->getDecl()].reset(RealDecl);
2560 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002561
David Blaikie498298d2013-08-18 16:55:33 +00002562 if (const ClassTemplateSpecializationDecl *TSpecial =
2563 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002564 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002565 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikiefab829d2013-08-15 22:42:12 +00002566 return RealDecl;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002567}
2568
David Blaikie498298d2013-08-18 16:55:33 +00002569void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002570 llvm::DICompositeType *RealDecl) {
David Blaikie498298d2013-08-18 16:55:33 +00002571 // A class's primary base or the class itself contains the vtable.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002572 llvm::DICompositeType *ContainingType = nullptr;
David Blaikie498298d2013-08-18 16:55:33 +00002573 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2574 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002575 // Seek non-virtual primary base root.
David Blaikie498298d2013-08-18 16:55:33 +00002576 while (1) {
2577 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2578 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2579 if (PBT && !BRL.isPrimaryBaseVirtual())
2580 PBase = PBT;
2581 else
2582 break;
2583 }
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002584 ContainingType = cast<llvm::DICompositeType>(
David Blaikie498298d2013-08-18 16:55:33 +00002585 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2586 getOrCreateFile(RD->getLocation())));
2587 } else if (RD->isDynamicClass())
2588 ContainingType = RealDecl;
2589
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002590 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikie498298d2013-08-18 16:55:33 +00002591}
2592
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002593llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
2594 StringRef Name, uint64_t *Offset) {
2595 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002596 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2597 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002598 llvm::DIType *Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
2599 FieldAlign, *Offset, 0, FieldTy);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002600 *Offset += FieldSize;
2601 return Ty;
2602}
2603
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002604void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
2605 StringRef &Name,
2606 StringRef &LinkageName,
2607 llvm::DIScope *&FDContext,
2608 llvm::DINodeArray &TParamsArray,
2609 unsigned &Flags) {
Stephen Hines176edba2014-12-01 14:53:08 -08002610 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2611 Name = getFunctionName(FD);
2612 // Use mangled name as linkage name for C/C++ functions.
2613 if (FD->hasPrototype()) {
2614 LinkageName = CGM.getMangledName(GD);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002615 Flags |= llvm::DINode::FlagPrototyped;
Stephen Hines176edba2014-12-01 14:53:08 -08002616 }
2617 // No need to replicate the linkage name if it isn't different from the
2618 // subprogram name, no need to have it at all unless coverage is enabled or
2619 // debug is set to more than just line tables.
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002620 if (LinkageName == Name || (!CGM.getCodeGenOpts().EmitGcovArcs &&
2621 !CGM.getCodeGenOpts().EmitGcovNotes &&
2622 DebugKind <= codegenoptions::DebugLineTablesOnly))
Stephen Hines176edba2014-12-01 14:53:08 -08002623 LinkageName = StringRef();
2624
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002625 if (DebugKind >= codegenoptions::LimitedDebugInfo) {
Stephen Hines176edba2014-12-01 14:53:08 -08002626 if (const NamespaceDecl *NSDecl =
2627 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2628 FDContext = getOrCreateNameSpace(NSDecl);
2629 else if (const RecordDecl *RDecl =
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002630 dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) {
2631 llvm::DIScope *Mod = getParentModuleOrNull(RDecl);
2632 FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU);
2633 }
Stephen Hines176edba2014-12-01 14:53:08 -08002634 // Collect template parameters.
2635 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2636 }
2637}
2638
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002639void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Stephen Hines176edba2014-12-01 14:53:08 -08002640 unsigned &LineNo, QualType &T,
2641 StringRef &Name, StringRef &LinkageName,
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002642 llvm::DIScope *&VDContext) {
Stephen Hines176edba2014-12-01 14:53:08 -08002643 Unit = getOrCreateFile(VD->getLocation());
2644 LineNo = getLineNumber(VD->getLocation());
2645
2646 setLocation(VD->getLocation());
2647
2648 T = VD->getType();
2649 if (T->isIncompleteArrayType()) {
2650 // CodeGen turns int[] into int[1] so we'll do the same here.
2651 llvm::APInt ConstVal(32, 1);
2652 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2653
2654 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2655 ArrayType::Normal, 0);
2656 }
2657
2658 Name = VD->getName();
2659 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2660 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2661 LinkageName = CGM.getMangledName(VD);
2662 if (LinkageName == Name)
2663 LinkageName = StringRef();
2664
2665 // Since we emit declarations (DW_AT_members) for static members, place the
2666 // definition of those static members in the namespace they were declared in
2667 // in the source code (the lexical decl context).
2668 // FIXME: Generalize this for even non-member global variables where the
2669 // declaration and definition may have different lexical decl contexts, once
2670 // we have support for emitting declarations of (non-member) global variables.
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07002671 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2672 : VD->getDeclContext();
2673 // When a record type contains an in-line initialization of a static data
2674 // member, and the record type is marked as __declspec(dllexport), an implicit
2675 // definition of the member will be created in the record context. DWARF
2676 // doesn't seem to have a nice way to describe this in a form that consumers
2677 // are likely to understand, so fake the "normal" situation of a definition
2678 // outside the class by putting it in the global scope.
2679 if (DC->isRecord())
2680 DC = CGM.getContext().getTranslationUnitDecl();
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002681
2682 llvm::DIScope *Mod = getParentModuleOrNull(VD);
2683 VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
Stephen Hines176edba2014-12-01 14:53:08 -08002684}
2685
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002686llvm::DISubprogram *
Stephen Hines176edba2014-12-01 14:53:08 -08002687CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002688 llvm::DINodeArray TParamsArray;
Stephen Hines176edba2014-12-01 14:53:08 -08002689 StringRef Name, LinkageName;
2690 unsigned Flags = 0;
2691 SourceLocation Loc = FD->getLocation();
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002692 llvm::DIFile *Unit = getOrCreateFile(Loc);
2693 llvm::DIScope *DContext = Unit;
Stephen Hines176edba2014-12-01 14:53:08 -08002694 unsigned Line = getLineNumber(Loc);
2695
2696 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2697 TParamsArray, Flags);
2698 // Build function type.
2699 SmallVector<QualType, 16> ArgTypes;
2700 for (const ParmVarDecl *Parm: FD->parameters())
2701 ArgTypes.push_back(Parm->getType());
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002702 CallingConv CC = FD->getType()->castAs<FunctionType>()->getCallConv();
2703 QualType FnType = CGM.getContext().getFunctionType(
2704 FD->getReturnType(), ArgTypes, FunctionProtoType::ExtProtoInfo(CC));
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002705 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002706 DContext, Name, LinkageName, Unit, Line,
2707 getOrCreateFunctionType(FD, FnType, Unit), !FD->isExternallyVisible(),
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002708 /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize,
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002709 TParamsArray.get(), getFunctionDeclaration(FD));
Stephen Hines176edba2014-12-01 14:53:08 -08002710 const FunctionDecl *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002711 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2712 std::make_tuple(CanonDecl),
2713 std::make_tuple(SP));
Stephen Hines176edba2014-12-01 14:53:08 -08002714 return SP;
2715}
2716
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002717llvm::DIGlobalVariable *
Stephen Hines176edba2014-12-01 14:53:08 -08002718CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2719 QualType T;
2720 StringRef Name, LinkageName;
2721 SourceLocation Loc = VD->getLocation();
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002722 llvm::DIFile *Unit = getOrCreateFile(Loc);
2723 llvm::DIScope *DContext = Unit;
Stephen Hines176edba2014-12-01 14:53:08 -08002724 unsigned Line = getLineNumber(Loc);
2725
2726 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002727 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
2728 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
2729 !VD->isExternallyVisible(), nullptr, nullptr);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002730 FwdDeclReplaceMap.emplace_back(
2731 std::piecewise_construct,
2732 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2733 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Stephen Hines176edba2014-12-01 14:53:08 -08002734 return GV;
2735}
2736
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002737llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikie9faebd22013-05-20 04:58:53 +00002738 // We only need a declaration (not a definition) of the type - so use whatever
2739 // we would otherwise do to get a type for a pointee. (forward declarations in
2740 // limited debug info, full definitions (if the type definition is available)
2741 // in unlimited debug info)
David Blaikieb3c23772013-08-12 23:14:36 +00002742 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2743 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie29b8b682013-09-04 22:03:57 +00002744 getOrCreateFile(TD->getLocation()));
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002745 auto I = DeclCache.find(D->getCanonicalDecl());
Stephen Hines176edba2014-12-01 14:53:08 -08002746
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002747 if (I != DeclCache.end())
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002748 return dyn_cast_or_null<llvm::DINode>(I->second);
Stephen Hines176edba2014-12-01 14:53:08 -08002749
2750 // No definition for now. Emit a forward definition that might be
2751 // merged with a potential upcoming definition.
2752 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
2753 return getFunctionForwardDeclaration(FD);
2754 else if (const auto *VD = dyn_cast<VarDecl>(D))
2755 return getGlobalVariableForwardDeclaration(VD);
2756
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002757 return nullptr;
David Blaikie9faebd22013-05-20 04:58:53 +00002758}
2759
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002760llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002761 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002762 return nullptr;
David Blaikie23e66db2013-06-22 00:09:36 +00002763
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002764 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Stephen Hines176edba2014-12-01 14:53:08 -08002765 if (!FD)
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002766 return nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002767
2768 // Setup context.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002769 auto *S = getDeclContextDescriptor(D);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002770
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002771 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikied6d5d692013-08-09 17:20:05 +00002772 if (MI == SPCache.end()) {
Eric Christopherac7c25f2013-08-28 23:12:10 +00002773 if (const CXXMethodDecl *MD =
2774 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002775 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
2776 cast<llvm::DICompositeType>(S));
David Blaikied6d5d692013-08-09 17:20:05 +00002777 }
2778 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002779 if (MI != SPCache.end()) {
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002780 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002781 if (SP && !SP->isDefinition())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002782 return SP;
2783 }
2784
Stephen Hines651f13c2014-04-23 16:59:28 -07002785 for (auto NextFD : FD->redecls()) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002786 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002787 if (MI != SPCache.end()) {
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002788 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002789 if (SP && !SP->isDefinition())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002790 return SP;
2791 }
2792 }
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002793 return nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002794}
2795
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002796// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002797// implicit parameter "this".
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002798llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002799 QualType FnType,
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002800 llvm::DIFile *F) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002801 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002802 // Create fake but valid subroutine type. Otherwise -verify would fail, and
2803 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002804 return DBuilder.createSubroutineType(DBuilder.getOrCreateTypeArray(None));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002805
2806 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2807 return getOrCreateMethodType(Method, F);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002808
2809 const auto *FTy = FnType->getAs<FunctionType>();
2810 CallingConv CC = FTy ? FTy->getCallConv() : CallingConv::CC_C;
2811
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002812 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2813 // Add "self" and "_cmd"
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002814 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002815
2816 // First element is always return type. For 'void' functions it is NULL.
Stephen Hines651f13c2014-04-23 16:59:28 -07002817 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl0cb00022013-05-22 21:37:49 +00002818
2819 // Replace the instancetype keyword with the actual type.
2820 if (ResultTy == CGM.getContext().getObjCInstanceType())
2821 ResultTy = CGM.getContext().getPointerType(
Stephen Hines176edba2014-12-01 14:53:08 -08002822 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl0cb00022013-05-22 21:37:49 +00002823
Adrian Prantl566a9c32013-05-10 21:08:31 +00002824 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002825 // "self" pointer is always first argument.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002826 QualType SelfDeclTy;
2827 if (auto *SelfDecl = OMethod->getSelfDecl())
2828 SelfDeclTy = SelfDecl->getType();
2829 else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType))
2830 if (FPT->getNumParams() > 1)
2831 SelfDeclTy = FPT->getParamType(0);
2832 if (!SelfDeclTy.isNull())
2833 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002834 // "_cmd" pointer is always second argument.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002835 Elts.push_back(DBuilder.createArtificialType(
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002836 getOrCreateType(CGM.getContext().getObjCSelType(), F)));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002837 // Get rest of the arguments.
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002838 for (const auto *PI : OMethod->parameters())
Stephen Hines651f13c2014-04-23 16:59:28 -07002839 Elts.push_back(getOrCreateType(PI->getType(), F));
Stephen Hines176edba2014-12-01 14:53:08 -08002840 // Variadic methods need a special marker at the end of the type list.
2841 if (OMethod->isVariadic())
2842 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002843
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002844 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002845 return DBuilder.createSubroutineType(EltTypeArray, 0, getDwarfCC(CC));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002846 }
Stephen Hines651f13c2014-04-23 16:59:28 -07002847
2848 // Handle variadic function types; they need an additional
2849 // unspecified parameter.
2850 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2851 if (FD->isVariadic()) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002852 SmallVector<llvm::Metadata *, 16> EltTys;
Stephen Hines651f13c2014-04-23 16:59:28 -07002853 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2854 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2855 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2856 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2857 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002858 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002859 return DBuilder.createSubroutineType(EltTypeArray, 0, getDwarfCC(CC));
Stephen Hines651f13c2014-04-23 16:59:28 -07002860 }
2861
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002862 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002863}
2864
Stephen Hines176edba2014-12-01 14:53:08 -08002865void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2866 SourceLocation ScopeLoc, QualType FnType,
2867 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002868
2869 StringRef Name;
2870 StringRef LinkageName;
2871
2872 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2873
2874 const Decl *D = GD.getDecl();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002875 bool HasDecl = (D != nullptr);
Stephen Hines651f13c2014-04-23 16:59:28 -07002876
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002877 unsigned Flags = 0;
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002878 llvm::DIFile *Unit = getOrCreateFile(Loc);
2879 llvm::DIScope *FDContext = Unit;
2880 llvm::DINodeArray TParamsArray;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002881 if (!HasDecl) {
2882 // Use llvm function name.
David Blaikiec7971a92013-08-27 23:57:18 +00002883 LinkageName = Fn->getName();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002884 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002885 // If there is a subprogram for this function available then use it.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002886 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002887 if (FI != SPCache.end()) {
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002888 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002889 if (SP && SP->isDefinition()) {
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002890 LexicalBlockStack.emplace_back(SP);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002891 RegionMap[D].reset(SP);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002892 return;
2893 }
2894 }
Stephen Hines176edba2014-12-01 14:53:08 -08002895 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2896 TParamsArray, Flags);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002897 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2898 Name = getObjCMethodName(OMD);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002899 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002900 } else {
2901 // Use llvm function name.
2902 Name = Fn->getName();
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002903 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002904 }
2905 if (!Name.empty() && Name[0] == '\01')
2906 Name = Name.substr(1);
2907
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002908 if (!HasDecl || D->isImplicit()) {
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002909 Flags |= llvm::DINode::FlagArtificial;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002910 // Artificial functions without a location should not silently reuse CurLoc.
2911 if (Loc.isInvalid())
2912 CurLoc = SourceLocation();
2913 }
2914 unsigned LineNo = getLineNumber(Loc);
2915 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002916
Stephen Hines651f13c2014-04-23 16:59:28 -07002917 // FIXME: The function declaration we're constructing here is mostly reusing
2918 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2919 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2920 // all subprograms instead of the actual context since subprogram definitions
2921 // are emitted as CU level entities by the backend.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002922 llvm::DISubprogram *SP = DBuilder.createFunction(
Stephen Hines176edba2014-12-01 14:53:08 -08002923 FDContext, Name, LinkageName, Unit, LineNo,
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002924 getOrCreateFunctionType(D, FnType, Unit), Fn->hasLocalLinkage(),
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002925 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002926 TParamsArray.get(), getFunctionDeclaration(D));
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002927 Fn->setSubprogram(SP);
Stephen Hines176edba2014-12-01 14:53:08 -08002928 // We might get here with a VarDecl in the case we're generating
2929 // code for the initialization of globals. Do not record these decls
2930 // as they will overwrite the actual VarDecl Decl in the cache.
2931 if (HasDecl && isa<FunctionDecl>(D))
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002932 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(SP));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002933
Stephen Hines651f13c2014-04-23 16:59:28 -07002934 // Push the function onto the lexical block stack.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002935 LexicalBlockStack.emplace_back(SP);
Stephen Hines651f13c2014-04-23 16:59:28 -07002936
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002937 if (HasDecl)
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002938 RegionMap[D].reset(SP);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002939}
2940
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002941void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
2942 QualType FnType) {
2943 StringRef Name;
2944 StringRef LinkageName;
2945
2946 const Decl *D = GD.getDecl();
2947 if (!D)
2948 return;
2949
2950 unsigned Flags = 0;
2951 llvm::DIFile *Unit = getOrCreateFile(Loc);
2952 llvm::DIScope *FDContext = getDeclContextDescriptor(D);
2953 llvm::DINodeArray TParamsArray;
2954 if (isa<FunctionDecl>(D)) {
2955 // If there is a DISubprogram for this function available then use it.
2956 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2957 TParamsArray, Flags);
2958 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2959 Name = getObjCMethodName(OMD);
2960 Flags |= llvm::DINode::FlagPrototyped;
2961 } else {
2962 llvm_unreachable("not a function or ObjC method");
2963 }
2964 if (!Name.empty() && Name[0] == '\01')
2965 Name = Name.substr(1);
2966
2967 if (D->isImplicit()) {
2968 Flags |= llvm::DINode::FlagArtificial;
2969 // Artificial functions without a location should not silently reuse CurLoc.
2970 if (Loc.isInvalid())
2971 CurLoc = SourceLocation();
2972 }
2973 unsigned LineNo = getLineNumber(Loc);
2974 unsigned ScopeLine = 0;
2975
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002976 DBuilder.retainType(DBuilder.createFunction(
2977 FDContext, Name, LinkageName, Unit, LineNo,
2978 getOrCreateFunctionType(D, FnType, Unit), false /*internalLinkage*/,
2979 false /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
2980 TParamsArray.get(), getFunctionDeclaration(D)));
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002981}
2982
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002983void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002984 // Update our current location
2985 setLocation(Loc);
2986
Stephen Hines176edba2014-12-01 14:53:08 -08002987 if (CurLoc.isInvalid() || CurLoc.isMacroID())
2988 return;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002989
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002990 llvm::MDNode *Scope = LexicalBlockStack.back();
Stephen Hines176edba2014-12-01 14:53:08 -08002991 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002992 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002993}
2994
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002995void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002996 llvm::MDNode *Back = nullptr;
2997 if (!LexicalBlockStack.empty())
2998 Back = LexicalBlockStack.back().get();
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002999 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
3000 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
3001 getColumnNumber(CurLoc)));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003002}
3003
Eric Christopherf0890c42013-05-16 00:52:20 +00003004void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
3005 SourceLocation Loc) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003006 // Set our current location.
3007 setLocation(Loc);
3008
Stephen Hines176edba2014-12-01 14:53:08 -08003009 // Emit a line table change for the current location inside the new scope.
3010 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
3011 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
3012
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003013 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
Stephen Hines176edba2014-12-01 14:53:08 -08003014 return;
3015
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003016 // Create a new lexical block and push it on the stack.
3017 CreateLexicalBlock(Loc);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003018}
3019
Eric Christopherf0890c42013-05-16 00:52:20 +00003020void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
3021 SourceLocation Loc) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003022 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3023
3024 // Provide an entry in the line table for the end of the block.
3025 EmitLocation(Builder, Loc);
3026
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003027 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
Stephen Hines176edba2014-12-01 14:53:08 -08003028 return;
3029
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003030 LexicalBlockStack.pop_back();
3031}
3032
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003033void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
3034 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3035 unsigned RCount = FnBeginRegionCount.back();
3036 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
3037
3038 // Pop all regions for this function.
Stephen Hines176edba2014-12-01 14:53:08 -08003039 while (LexicalBlockStack.size() != RCount) {
3040 // Provide an entry in the line table for the end of the block.
3041 EmitLocation(Builder, CurLoc);
3042 LexicalBlockStack.pop_back();
3043 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003044 FnBeginRegionCount.pop_back();
3045}
3046
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003047llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
3048 uint64_t *XOffset) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003049
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003050 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003051 QualType FType;
3052 uint64_t FieldSize, FieldOffset;
3053 unsigned FieldAlign;
Eric Christopher6537f082013-05-16 00:45:12 +00003054
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003055 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopher6537f082013-05-16 00:45:12 +00003056 QualType Type = VD->getType();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003057
3058 FieldOffset = 0;
3059 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
3060 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
3061 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
3062 FType = CGM.getContext().IntTy;
3063 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
3064 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
3065
3066 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
3067 if (HasCopyAndDispose) {
3068 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Stephen Hines176edba2014-12-01 14:53:08 -08003069 EltTys.push_back(
3070 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
3071 EltTys.push_back(
3072 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003073 }
3074 bool HasByrefExtendedLayout;
3075 Qualifiers::ObjCLifetime Lifetime;
Stephen Hines176edba2014-12-01 14:53:08 -08003076 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
3077 HasByrefExtendedLayout) &&
3078 HasByrefExtendedLayout) {
Adrian Prantl1f437912013-07-23 00:12:14 +00003079 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Stephen Hines176edba2014-12-01 14:53:08 -08003080 EltTys.push_back(
3081 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantl1f437912013-07-23 00:12:14 +00003082 }
Eric Christopher6537f082013-05-16 00:45:12 +00003083
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003084 CharUnits Align = CGM.getContext().getDeclAlign(VD);
3085 if (Align > CGM.getContext().toCharUnitsFromBits(
Stephen Hines176edba2014-12-01 14:53:08 -08003086 CGM.getTarget().getPointerAlign(0))) {
3087 CharUnits FieldOffsetInBytes =
3088 CGM.getContext().toCharUnitsFromBits(FieldOffset);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003089 CharUnits AlignedOffsetInBytes = FieldOffsetInBytes.alignTo(Align);
Stephen Hines176edba2014-12-01 14:53:08 -08003090 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopher6537f082013-05-16 00:45:12 +00003091
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003092 if (NumPaddingBytes.isPositive()) {
3093 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
3094 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
3095 pad, ArrayType::Normal, 0);
3096 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
3097 }
3098 }
Eric Christopher6537f082013-05-16 00:45:12 +00003099
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003100 FType = Type;
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003101 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003102 FieldSize = CGM.getContext().getTypeSize(FType);
3103 FieldAlign = CGM.getContext().toBits(Align);
3104
Eric Christopher6537f082013-05-16 00:45:12 +00003105 *XOffset = FieldOffset;
Stephen Hines176edba2014-12-01 14:53:08 -08003106 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
3107 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003108 EltTys.push_back(FieldTy);
3109 FieldOffset += FieldSize;
Eric Christopher6537f082013-05-16 00:45:12 +00003110
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003111 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopher6537f082013-05-16 00:45:12 +00003112
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003113 unsigned Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopher6537f082013-05-16 00:45:12 +00003114
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003115 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003116 nullptr, Elements);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003117}
3118
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003119void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::Value *Storage,
3120 llvm::Optional<unsigned> ArgNo,
Stephen Hines176edba2014-12-01 14:53:08 -08003121 CGBuilderTy &Builder) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003122 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003123 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003124 if (VD->hasAttr<NoDebugAttr>())
3125 return;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003126
David Blaikiefc946272013-08-19 03:37:48 +00003127 bool Unwritten =
3128 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
3129 cast<Decl>(VD->getDeclContext())->isImplicit());
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003130 llvm::DIFile *Unit = nullptr;
David Blaikiefc946272013-08-19 03:37:48 +00003131 if (!Unwritten)
3132 Unit = getOrCreateFile(VD->getLocation());
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003133 llvm::DIType *Ty;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003134 uint64_t XOffset = 0;
3135 if (VD->hasAttr<BlocksAttr>())
3136 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopher6537f082013-05-16 00:45:12 +00003137 else
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003138 Ty = getOrCreateType(VD->getType(), Unit);
3139
3140 // If there is no debug info for this type then do not emit debug info
3141 // for this variable.
3142 if (!Ty)
3143 return;
3144
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003145 // Get location information.
David Blaikiefc946272013-08-19 03:37:48 +00003146 unsigned Line = 0;
3147 unsigned Column = 0;
3148 if (!Unwritten) {
3149 Line = getLineNumber(VD->getLocation());
3150 Column = getColumnNumber(VD->getLocation());
3151 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003152 SmallVector<int64_t, 9> Expr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003153 unsigned Flags = 0;
3154 if (VD->isImplicit())
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003155 Flags |= llvm::DINode::FlagArtificial;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003156 // If this is the first argument and it is implicit then
3157 // give it an object pointer flag.
3158 // FIXME: There has to be a better way to do this, but for static
3159 // functions there won't be an implicit param at arg1 and
3160 // otherwise it is 'self' or 'this'.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003161 if (isa<ImplicitParamDecl>(VD) && ArgNo && *ArgNo == 1)
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003162 Flags |= llvm::DINode::FlagObjectPointer;
David Blaikie41c9bae2013-06-19 21:53:53 +00003163 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopher7dab97b2013-07-17 22:52:53 +00003164 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
3165 !VD->getType()->isPointerType())
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003166 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003167
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003168 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003169
3170 StringRef Name = VD->getName();
3171 if (!Name.empty()) {
3172 if (VD->hasAttr<BlocksAttr>()) {
3173 CharUnits offset = CharUnits::fromQuantity(32);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003174 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003175 // offset of __forwarding field
3176 offset = CGM.getContext().toCharUnitsFromBits(
Stephen Hines176edba2014-12-01 14:53:08 -08003177 CGM.getTarget().getPointerWidth(0));
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003178 Expr.push_back(offset.getQuantity());
3179 Expr.push_back(llvm::dwarf::DW_OP_deref);
3180 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003181 // offset of x field
3182 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003183 Expr.push_back(offset.getQuantity());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003184
3185 // Create the descriptor for the variable.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003186 auto *D = ArgNo
3187 ? DBuilder.createParameterVariable(Scope, VD->getName(),
3188 *ArgNo, Unit, Line, Ty)
3189 : DBuilder.createAutoVariable(Scope, VD->getName(), Unit,
3190 Line, Ty);
Eric Christopher6537f082013-05-16 00:45:12 +00003191
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003192 // Insert an llvm.dbg.declare into the current block.
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07003193 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3194 llvm::DebugLoc::get(Line, Column, Scope),
3195 Builder.GetInsertBlock());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003196 return;
Adrian Prantl39232cd2013-09-18 22:18:17 +00003197 } else if (isa<VariableArrayType>(VD->getType()))
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003198 Expr.push_back(llvm::dwarf::DW_OP_deref);
David Blaikie436653b2013-01-05 05:58:35 +00003199 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
3200 // If VD is an anonymous union then Storage represents value for
3201 // all union fields.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003202 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
David Blaikied8180cf2013-01-05 20:03:07 +00003203 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003204 // GDB has trouble finding local variables in anonymous unions, so we emit
3205 // artifical local variables for each of the members.
3206 //
3207 // FIXME: Remove this code as soon as GDB supports this.
3208 // The debug info verifier in LLVM operates based on the assumption that a
3209 // variable has the same size as its storage and we had to disable the check
3210 // for artificial variables.
Stephen Hines651f13c2014-04-23 16:59:28 -07003211 for (const auto *Field : RD->fields()) {
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003212 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003213 StringRef FieldName = Field->getName();
Eric Christopher6537f082013-05-16 00:45:12 +00003214
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003215 // Ignore unnamed fields. Do not ignore unnamed records.
3216 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
3217 continue;
Eric Christopher6537f082013-05-16 00:45:12 +00003218
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003219 // Use VarDecl's Tag, Scope and Line number.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003220 auto *D = DBuilder.createAutoVariable(
3221 Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
3222 Flags | llvm::DINode::FlagArtificial);
Eric Christopher6537f082013-05-16 00:45:12 +00003223
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003224 // Insert an llvm.dbg.declare into the current block.
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07003225 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3226 llvm::DebugLoc::get(Line, Column, Scope),
3227 Builder.GetInsertBlock());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003228 }
3229 }
3230 }
David Blaikie436653b2013-01-05 05:58:35 +00003231
3232 // Create the descriptor for the variable.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003233 auto *D =
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003234 ArgNo
3235 ? DBuilder.createParameterVariable(Scope, Name, *ArgNo, Unit, Line,
3236 Ty, CGM.getLangOpts().Optimize,
3237 Flags)
3238 : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
3239 CGM.getLangOpts().Optimize, Flags);
David Blaikie436653b2013-01-05 05:58:35 +00003240
3241 // Insert an llvm.dbg.declare into the current block.
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07003242 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3243 llvm::DebugLoc::get(Line, Column, Scope),
3244 Builder.GetInsertBlock());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003245}
3246
3247void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
3248 llvm::Value *Storage,
3249 CGBuilderTy &Builder) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003250 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003251 EmitDeclare(VD, Storage, llvm::None, Builder);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003252}
3253
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003254llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
3255 llvm::DIType *Ty) {
3256 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Stephen Hines176edba2014-12-01 14:53:08 -08003257 if (CachedTy)
3258 Ty = CachedTy;
Adrian Prantle86fcc42013-03-29 19:20:29 +00003259 return DBuilder.createObjectPointerType(Ty);
3260}
3261
Stephen Hines176edba2014-12-01 14:53:08 -08003262void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
3263 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
3264 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003265 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003266 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopher6537f082013-05-16 00:45:12 +00003267
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003268 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003269 return;
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003270 if (VD->hasAttr<NoDebugAttr>())
3271 return;
Eric Christopher6537f082013-05-16 00:45:12 +00003272
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003273 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopher6537f082013-05-16 00:45:12 +00003274
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003275 uint64_t XOffset = 0;
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003276 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
3277 llvm::DIType *Ty;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003278 if (isByRef)
3279 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopher6537f082013-05-16 00:45:12 +00003280 else
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003281 Ty = getOrCreateType(VD->getType(), Unit);
3282
3283 // Self is passed along as an implicit non-arg variable in a
3284 // block. Mark it as the object pointer.
3285 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantle86fcc42013-03-29 19:20:29 +00003286 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003287
3288 // Get location information.
3289 unsigned Line = getLineNumber(VD->getLocation());
3290 unsigned Column = getColumnNumber(VD->getLocation());
3291
3292 const llvm::DataLayout &target = CGM.getDataLayout();
3293
3294 CharUnits offset = CharUnits::fromQuantity(
Stephen Hines176edba2014-12-01 14:53:08 -08003295 target.getStructLayout(blockInfo.StructureType)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003296 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
3297
Stephen Hines176edba2014-12-01 14:53:08 -08003298 SmallVector<int64_t, 9> addr;
Adrian Prantl9b97adf2013-03-29 19:20:35 +00003299 if (isa<llvm::AllocaInst>(Storage))
Stephen Hines176edba2014-12-01 14:53:08 -08003300 addr.push_back(llvm::dwarf::DW_OP_deref);
3301 addr.push_back(llvm::dwarf::DW_OP_plus);
3302 addr.push_back(offset.getQuantity());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003303 if (isByRef) {
Stephen Hines176edba2014-12-01 14:53:08 -08003304 addr.push_back(llvm::dwarf::DW_OP_deref);
3305 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003306 // offset of __forwarding field
Stephen Hines176edba2014-12-01 14:53:08 -08003307 offset =
3308 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
3309 addr.push_back(offset.getQuantity());
3310 addr.push_back(llvm::dwarf::DW_OP_deref);
3311 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003312 // offset of x field
3313 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Stephen Hines176edba2014-12-01 14:53:08 -08003314 addr.push_back(offset.getQuantity());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003315 }
3316
3317 // Create the descriptor for the variable.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003318 auto *D = DBuilder.createAutoVariable(
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003319 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07003320 Line, Ty);
Adrian Prantl9b97adf2013-03-29 19:20:35 +00003321
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003322 // Insert an llvm.dbg.declare into the current block.
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07003323 auto DL = llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back());
3324 if (InsertPoint)
3325 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3326 InsertPoint);
3327 else
3328 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3329 Builder.GetInsertBlock());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003330}
3331
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003332void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3333 unsigned ArgNo,
3334 CGBuilderTy &Builder) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003335 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003336 EmitDeclare(VD, AI, ArgNo, Builder);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003337}
3338
3339namespace {
Stephen Hines176edba2014-12-01 14:53:08 -08003340struct BlockLayoutChunk {
3341 uint64_t OffsetInBits;
3342 const BlockDecl::Capture *Capture;
3343};
3344bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3345 return l.OffsetInBits < r.OffsetInBits;
3346}
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003347}
3348
3349void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl836e7c92013-03-14 17:53:33 +00003350 llvm::Value *Arg,
Stephen Hines176edba2014-12-01 14:53:08 -08003351 unsigned ArgNo,
Adrian Prantl836e7c92013-03-14 17:53:33 +00003352 llvm::Value *LocalAddr,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003353 CGBuilderTy &Builder) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003354 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003355 ASTContext &C = CGM.getContext();
3356 const BlockDecl *blockDecl = block.getBlockDecl();
3357
3358 // Collect some general information about the block's location.
3359 SourceLocation loc = blockDecl->getCaretLocation();
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003360 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003361 unsigned line = getLineNumber(loc);
3362 unsigned column = getColumnNumber(loc);
Eric Christopher6537f082013-05-16 00:45:12 +00003363
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003364 // Build the debug-info type for the block literal.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003365 getDeclContextDescriptor(blockDecl);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003366
3367 const llvm::StructLayout *blockLayout =
Stephen Hines176edba2014-12-01 14:53:08 -08003368 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003369
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003370 SmallVector<llvm::Metadata *, 16> fields;
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003371 fields.push_back(createFieldType("__isa", C.VoidPtrTy, loc, AS_public,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003372 blockLayout->getElementOffsetInBits(0),
3373 tunit, tunit));
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003374 fields.push_back(createFieldType("__flags", C.IntTy, loc, AS_public,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003375 blockLayout->getElementOffsetInBits(1),
3376 tunit, tunit));
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003377 fields.push_back(createFieldType("__reserved", C.IntTy, loc, AS_public,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003378 blockLayout->getElementOffsetInBits(2),
3379 tunit, tunit));
Stephen Hines176edba2014-12-01 14:53:08 -08003380 auto *FnTy = block.getBlockExpr()->getFunctionType();
3381 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003382 fields.push_back(createFieldType("__FuncPtr", FnPtrType, loc, AS_public,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003383 blockLayout->getElementOffsetInBits(3),
3384 tunit, tunit));
Stephen Hines176edba2014-12-01 14:53:08 -08003385 fields.push_back(createFieldType(
3386 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3387 ? C.getBlockDescriptorExtendedType()
3388 : C.getBlockDescriptorType()),
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003389 loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003390
3391 // We want to sort the captures by offset, not because DWARF
3392 // requires this, but because we're paranoid about debuggers.
3393 SmallVector<BlockLayoutChunk, 8> chunks;
3394
3395 // 'this' capture.
3396 if (blockDecl->capturesCXXThis()) {
3397 BlockLayoutChunk chunk;
3398 chunk.OffsetInBits =
Stephen Hines176edba2014-12-01 14:53:08 -08003399 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003400 chunk.Capture = nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003401 chunks.push_back(chunk);
3402 }
3403
3404 // Variable captures.
Stephen Hines651f13c2014-04-23 16:59:28 -07003405 for (const auto &capture : blockDecl->captures()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003406 const VarDecl *variable = capture.getVariable();
3407 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3408
3409 // Ignore constant captures.
3410 if (captureInfo.isConstant())
3411 continue;
3412
3413 BlockLayoutChunk chunk;
3414 chunk.OffsetInBits =
Stephen Hines176edba2014-12-01 14:53:08 -08003415 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003416 chunk.Capture = &capture;
3417 chunks.push_back(chunk);
3418 }
3419
3420 // Sort by offset.
3421 llvm::array_pod_sort(chunks.begin(), chunks.end());
3422
Stephen Hines176edba2014-12-01 14:53:08 -08003423 for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
3424 e = chunks.end();
3425 i != e; ++i) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003426 uint64_t offsetInBits = i->OffsetInBits;
3427 const BlockDecl::Capture *capture = i->Capture;
3428
3429 // If we have a null capture, this must be the C++ 'this' capture.
3430 if (!capture) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003431 QualType type;
3432 if (auto *Method =
3433 cast_or_null<CXXMethodDecl>(blockDecl->getNonClosureContext()))
3434 type = Method->getThisType(C);
3435 else if (auto *RDecl = dyn_cast<CXXRecordDecl>(blockDecl->getParent()))
3436 type = QualType(RDecl->getTypeForDecl(), 0);
3437 else
3438 llvm_unreachable("unexpected block declcontext");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003439
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003440 fields.push_back(createFieldType("this", type, loc, AS_public,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003441 offsetInBits, tunit, tunit));
3442 continue;
3443 }
3444
3445 const VarDecl *variable = capture->getVariable();
3446 StringRef name = variable->getName();
3447
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003448 llvm::DIType *fieldType;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003449 if (capture->isByRef()) {
Stephen Hines176edba2014-12-01 14:53:08 -08003450 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003451
3452 // FIXME: this creates a second copy of this type!
3453 uint64_t xoffset;
3454 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
Stephen Hines176edba2014-12-01 14:53:08 -08003455 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3456 fieldType =
3457 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3458 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003459 } else {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003460 fieldType = createFieldType(name, variable->getType(), loc, AS_public,
Stephen Hines176edba2014-12-01 14:53:08 -08003461 offsetInBits, tunit, tunit);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003462 }
3463 fields.push_back(fieldType);
3464 }
3465
3466 SmallString<36> typeName;
Stephen Hines176edba2014-12-01 14:53:08 -08003467 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3468 << CGM.getUniqueBlockCount();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003469
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003470 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003471
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003472 llvm::DIType *type = DBuilder.createStructType(
3473 tunit, typeName.str(), tunit, line,
3474 CGM.getContext().toBits(block.BlockSize),
3475 CGM.getContext().toBits(block.BlockAlign), 0, nullptr, fieldsArray);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003476 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3477
3478 // Get overall information about the block.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003479 unsigned flags = llvm::DINode::FlagArtificial;
3480 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003481
3482 // Create the descriptor for the parameter.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003483 auto *debugVar = DBuilder.createParameterVariable(
3484 scope, Arg->getName(), ArgNo, tunit, line, type,
3485 CGM.getLangOpts().Optimize, flags);
Adrian Prantl836e7c92013-03-14 17:53:33 +00003486
Adrian Prantlbea407c2013-03-14 21:52:59 +00003487 if (LocalAddr) {
Adrian Prantl836e7c92013-03-14 17:53:33 +00003488 // Insert an llvm.dbg.value into the current block.
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07003489 DBuilder.insertDbgValueIntrinsic(
Stephen Hines176edba2014-12-01 14:53:08 -08003490 LocalAddr, 0, debugVar, DBuilder.createExpression(),
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07003491 llvm::DebugLoc::get(line, column, scope), Builder.GetInsertBlock());
Adrian Prantlbea407c2013-03-14 21:52:59 +00003492 }
Adrian Prantl836e7c92013-03-14 17:53:33 +00003493
Adrian Prantlbea407c2013-03-14 21:52:59 +00003494 // Insert an llvm.dbg.declare into the current block.
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07003495 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
3496 llvm::DebugLoc::get(line, column, scope),
3497 Builder.GetInsertBlock());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003498}
3499
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003500llvm::DIDerivedType *
David Blaikie5434fc22013-08-20 01:28:15 +00003501CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3502 if (!D->isStaticDataMember())
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003503 return nullptr;
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07003504
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003505 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie5434fc22013-08-20 01:28:15 +00003506 if (MI != StaticDataMemberCache.end()) {
3507 assert(MI->second && "Static data member declaration should still exist");
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003508 return MI->second;
Evgeniy Stepanov045a9f62013-08-16 10:35:31 +00003509 }
David Blaikie5e6937b2013-08-20 21:49:21 +00003510
3511 // If the member wasn't found in the cache, lazily construct and add it to the
3512 // type (used when a limited form of the type is emitted).
Stephen Hines176edba2014-12-01 14:53:08 -08003513 auto DC = D->getDeclContext();
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003514 auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D));
Stephen Hines176edba2014-12-01 14:53:08 -08003515 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie5434fc22013-08-20 01:28:15 +00003516}
3517
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003518llvm::DIGlobalVariable *CGDebugInfo::CollectAnonRecordDecls(
3519 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3520 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
3521 llvm::DIGlobalVariable *GV = nullptr;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003522
3523 for (const auto *Field : RD->fields()) {
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003524 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003525 StringRef FieldName = Field->getName();
3526
3527 // Ignore unnamed fields, but recurse into anonymous records.
3528 if (FieldName.empty()) {
3529 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3530 if (RT)
3531 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3532 Var, DContext);
3533 continue;
3534 }
3535 // Use VarDecl's Tag, Scope and Line number.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003536 GV = DBuilder.createGlobalVariable(DContext, FieldName, LinkageName, Unit,
3537 LineNo, FieldTy,
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003538 Var->hasLocalLinkage(), Var, nullptr);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003539 }
3540 return GV;
3541}
3542
Yunzhong Gao3b8e0b72013-08-30 08:53:09 +00003543void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003544 const VarDecl *D) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003545 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
3546 if (D->hasAttr<NoDebugAttr>())
3547 return;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003548 // Create global variable debug descriptor.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003549 llvm::DIFile *Unit = nullptr;
3550 llvm::DIScope *DContext = nullptr;
Stephen Hines176edba2014-12-01 14:53:08 -08003551 unsigned LineNo;
3552 StringRef DeclName, LinkageName;
3553 QualType T;
3554 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003555
3556 // Attempt to store one global variable for the declaration - even if we
3557 // emit a lot of fields.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003558 llvm::DIGlobalVariable *GV = nullptr;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003559
3560 // If this is an anonymous union then we'll want to emit a global
3561 // variable for each member of the anonymous union so that it's possible
3562 // to find the name of any field in the union.
3563 if (T->isUnionType() && DeclName.empty()) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003564 const RecordDecl *RD = T->castAs<RecordType>()->getDecl();
Stephen Hines176edba2014-12-01 14:53:08 -08003565 assert(RD->isAnonymousStructOrUnion() &&
3566 "unnamed non-anonymous struct or union?");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003567 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3568 } else {
Stephen Hines176edba2014-12-01 14:53:08 -08003569 GV = DBuilder.createGlobalVariable(
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003570 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003571 Var->hasLocalLinkage(), Var,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003572 getOrCreateStaticDataMemberDeclarationOrNull(D));
3573 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003574 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(GV));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003575}
3576
Yunzhong Gao3b8e0b72013-08-30 08:53:09 +00003577void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3578 llvm::Constant *Init) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003579 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
3580 if (VD->hasAttr<NoDebugAttr>())
3581 return;
Yunzhong Gao3b8e0b72013-08-30 08:53:09 +00003582 // Create the descriptor for the variable.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003583 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao3b8e0b72013-08-30 08:53:09 +00003584 StringRef Name = VD->getName();
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003585 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
Yunzhong Gao3b8e0b72013-08-30 08:53:09 +00003586 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3587 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3588 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3589 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3590 }
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003591 // Do not use global variables for enums.
3592 //
3593 // FIXME: why not?
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07003594 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao3b8e0b72013-08-30 08:53:09 +00003595 return;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003596 // Do not emit separate definitions for function local const/statics.
3597 if (isa<FunctionDecl>(VD->getDeclContext()))
3598 return;
3599 VD = cast<ValueDecl>(VD->getCanonicalDecl());
Stephen Hines176edba2014-12-01 14:53:08 -08003600 auto *VarD = cast<VarDecl>(VD);
3601 if (VarD->isStaticDataMember()) {
3602 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003603 getDeclContextDescriptor(VarD);
Stephen Hines176edba2014-12-01 14:53:08 -08003604 // Ensure that the type is retained even though it's otherwise unreferenced.
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003605 //
3606 // FIXME: This is probably unnecessary, since Ty should reference RD
3607 // through its scope.
Stephen Hines176edba2014-12-01 14:53:08 -08003608 RetainedTypes.push_back(
3609 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
3610 return;
3611 }
3612
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003613 llvm::DIScope *DContext = getDeclContextDescriptor(VD);
Stephen Hines176edba2014-12-01 14:53:08 -08003614
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003615 auto &GV = DeclCache[VD];
3616 if (GV)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003617 return;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003618 GV.reset(DBuilder.createGlobalVariable(
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003619 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003620 true, Init, getOrCreateStaticDataMemberDeclarationOrNull(VarD)));
David Blaikie9faebd22013-05-20 04:58:53 +00003621}
3622
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003623llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikie9faebd22013-05-20 04:58:53 +00003624 if (!LexicalBlockStack.empty())
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003625 return LexicalBlockStack.back();
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003626 llvm::DIScope *Mod = getParentModuleOrNull(D);
3627 return getContextDescriptor(D, Mod ? Mod : TheCU);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003628}
3629
David Blaikie957dac52013-04-22 06:13:21 +00003630void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003631 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikie9faebd22013-05-20 04:58:53 +00003632 return;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003633 const NamespaceDecl *NSDecl = UD.getNominatedNamespace();
3634 if (!NSDecl->isAnonymousNamespace() ||
3635 CGM.getCodeGenOpts().DebugExplicitImport) {
3636 DBuilder.createImportedModule(
3637 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3638 getOrCreateNameSpace(NSDecl),
3639 getLineNumber(UD.getLocation()));
3640 }
David Blaikie957dac52013-04-22 06:13:21 +00003641}
3642
David Blaikie9faebd22013-05-20 04:58:53 +00003643void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003644 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikie9faebd22013-05-20 04:58:53 +00003645 return;
3646 assert(UD.shadow_size() &&
3647 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3648 // Emitting one decl is sufficient - debuggers can detect that this is an
3649 // overloaded name & provide lookup for all the overloads.
3650 const UsingShadowDecl &USD = **UD.shadow_begin();
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003651 if (llvm::DINode *Target =
Eric Christopher56b108a2013-06-07 22:54:39 +00003652 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikie9faebd22013-05-20 04:58:53 +00003653 DBuilder.createImportedDeclaration(
3654 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3655 getLineNumber(USD.getLocation()));
3656}
3657
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003658void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003659 if (CGM.getCodeGenOpts().getDebuggerTuning() != llvm::DebuggerKind::LLDB)
3660 return;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003661 if (Module *M = ID.getImportedModule()) {
3662 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
3663 DBuilder.createImportedDeclaration(
3664 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
3665 getOrCreateModuleRef(Info, DebugTypeExtRefs),
3666 getLineNumber(ID.getLocation()));
3667 }
3668}
3669
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003670llvm::DIImportedEntity *
David Blaikiefc46ebc2013-05-20 22:50:41 +00003671CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003672 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003673 return nullptr;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003674 auto &VH = NamespaceAliasCache[&NA];
David Blaikiefc46ebc2013-05-20 22:50:41 +00003675 if (VH)
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003676 return cast<llvm::DIImportedEntity>(VH);
3677 llvm::DIImportedEntity *R;
David Blaikiefc46ebc2013-05-20 22:50:41 +00003678 if (const NamespaceAliasDecl *Underlying =
3679 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3680 // This could cache & dedup here rather than relying on metadata deduping.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003681 R = DBuilder.createImportedDeclaration(
David Blaikiefc46ebc2013-05-20 22:50:41 +00003682 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3683 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3684 NA.getName());
3685 else
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003686 R = DBuilder.createImportedDeclaration(
David Blaikiefc46ebc2013-05-20 22:50:41 +00003687 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3688 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3689 getLineNumber(NA.getLocation()), NA.getName());
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003690 VH.reset(R);
David Blaikiefc46ebc2013-05-20 22:50:41 +00003691 return R;
3692}
3693
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003694llvm::DINamespace *
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003695CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie8863e6b2013-08-16 22:52:07 +00003696 NSDecl = NSDecl->getCanonicalDecl();
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003697 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003698 if (I != NameSpaceCache.end())
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003699 return cast<llvm::DINamespace>(I->second);
Eric Christopher6537f082013-05-16 00:45:12 +00003700
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003701 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003702 llvm::DIFile *FileD = getOrCreateFile(NSDecl->getLocation());
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003703 llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003704 llvm::DINamespace *NS =
3705 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003706 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003707 return NS;
3708}
3709
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003710void CGDebugInfo::setDwoId(uint64_t Signature) {
3711 assert(TheCU && "no main compile unit");
3712 TheCU->setDWOId(Signature);
3713}
3714
3715
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003716void CGDebugInfo::finalize() {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003717 // Creating types might create further types - invalidating the current
3718 // element and the size(), so don't cache/reference them.
3719 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3720 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003721 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07003722 ? CreateTypeDefinition(E.Type, E.Unit)
3723 : E.Decl;
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003724 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003725 }
Eric Christopher6537f082013-05-16 00:45:12 +00003726
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003727 for (auto p : ReplaceMap) {
3728 assert(p.second);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003729 auto *Ty = cast<llvm::DIType>(p.second);
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07003730 assert(Ty->isForwardDecl());
Adrian Prantlebbd7e02013-03-11 18:33:46 +00003731
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003732 auto it = TypeCache.find(p.first);
3733 assert(it != TypeCache.end());
3734 assert(it->second);
3735
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003736 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
3737 cast<llvm::DIType>(it->second));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003738 }
Adrian Prantlebbd7e02013-03-11 18:33:46 +00003739
Stephen Hines176edba2014-12-01 14:53:08 -08003740 for (const auto &p : FwdDeclReplaceMap) {
3741 assert(p.second);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003742 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003743 llvm::Metadata *Repl;
Stephen Hines176edba2014-12-01 14:53:08 -08003744
3745 auto it = DeclCache.find(p.first);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003746 // If there has been no definition for the declaration, call RAUW
Stephen Hines176edba2014-12-01 14:53:08 -08003747 // with ourselves, that will destroy the temporary MDNode and
3748 // replace it with a standard one, avoiding leaking memory.
3749 if (it == DeclCache.end())
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003750 Repl = p.second;
Stephen Hines176edba2014-12-01 14:53:08 -08003751 else
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003752 Repl = it->second;
Stephen Hines176edba2014-12-01 14:53:08 -08003753
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003754 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Stephen Hines176edba2014-12-01 14:53:08 -08003755 }
3756
Adrian Prantlebbd7e02013-03-11 18:33:46 +00003757 // We keep our own list of retained types, because we need to look
3758 // up the final type in the type cache.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003759 for (auto &RT : RetainedTypes)
3760 if (auto MD = TypeCache[RT])
3761 DBuilder.retainType(cast<llvm::DIType>(MD));
Adrian Prantlebbd7e02013-03-11 18:33:46 +00003762
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003763 DBuilder.finalize();
3764}
Stephen Hines176edba2014-12-01 14:53:08 -08003765
3766void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003767 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
Stephen Hines176edba2014-12-01 14:53:08 -08003768 return;
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07003769
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003770 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07003771 // Don't ignore in case of explicit cast where it is referenced indirectly.
3772 DBuilder.retainType(DieTy);
Stephen Hines176edba2014-12-01 14:53:08 -08003773}