blob: 38438896357d41c66421dc42dbdd1361ecaf4a14 [file] [log] [blame]
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +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"
Mike Stumpb1a6e682009-09-30 02:43:10 +000015#include "CodeGenFunction.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000016#include "CodeGenModule.h"
John McCalld16c2cf2011-02-08 08:22:06 +000017#include "CGBlocks.h"
Eli Friedman77457862012-11-06 23:40:48 +000018#include "CGObjCRuntime.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000019#include "clang/AST/ASTContext.h"
Devang Patel2ed8f002010-08-27 17:47:47 +000020#include "clang/AST/DeclFriend.h"
Devang Patel9ca36b62009-02-26 21:10:26 +000021#include "clang/AST/DeclObjC.h"
Devang Patel700a1cb2010-07-20 20:24:18 +000022#include "clang/AST/DeclTemplate.h"
Chris Lattner3cc5c402008-11-11 07:01:36 +000023#include "clang/AST/Expr.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Benjamin Kramer00bd44d2012-02-04 12:31:12 +000025#include "clang/Basic/SourceManager.h"
Benjamin Kramerd7a3e2c2012-02-07 22:29:24 +000026#include "clang/Basic/FileManager.h"
Mike Stump5a862172009-09-15 21:48:34 +000027#include "clang/Basic/Version.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000028#include "clang/Frontend/CodeGenOptions.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000029#include "llvm/Constants.h"
30#include "llvm/DerivedTypes.h"
31#include "llvm/Instructions.h"
32#include "llvm/Intrinsics.h"
33#include "llvm/Module.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000034#include "llvm/ADT/StringExtras.h"
35#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000036#include "llvm/Support/Dwarf.h"
Benjamin Kramerbcbca752011-10-14 18:45:11 +000037#include "llvm/Support/FileSystem.h"
Micah Villmow25a6a842012-10-08 16:25:52 +000038#include "llvm/DataLayout.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000039using namespace clang;
40using namespace clang::CodeGen;
41
Anders Carlsson20f12a22009-12-06 18:00:51 +000042CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Devang Patel823d8e92010-12-08 22:42:58 +000043 : CGM(CGM), DBuilder(CGM.getModule()),
Dan Gohman4cac5b42010-08-20 22:02:57 +000044 BlockLiteralGenericSet(false) {
Devang Patel17800552010-03-09 00:44:50 +000045 CreateCompileUnit();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000046}
47
Chris Lattner9c85ba32008-11-10 06:08:34 +000048CGDebugInfo::~CGDebugInfo() {
Eric Christopherab5278e2011-10-11 23:00:51 +000049 assert(LexicalBlockStack.empty() &&
50 "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000051}
52
Chris Lattner9c85ba32008-11-10 06:08:34 +000053void CGDebugInfo::setLocation(SourceLocation Loc) {
Eric Christopher944542e2011-10-11 23:00:45 +000054 // If the new location isn't valid return.
55 if (!Loc.isValid()) return;
56
57 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
Eric Christopher73fb3502011-10-13 21:45:18 +000058
59 // If we've changed files in the middle of a lexical scope go ahead
60 // and create a new lexical scope with file node if it's different
61 // from the one in the scope.
62 if (LexicalBlockStack.empty()) return;
63
64 SourceManager &SM = CGM.getContext().getSourceManager();
65 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
66 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc);
67
68 if (PCLoc.isInvalid() || PPLoc.isInvalid() ||
69 !strcmp(PPLoc.getFilename(), PCLoc.getFilename()))
70 return;
71
72 llvm::MDNode *LB = LexicalBlockStack.back();
73 llvm::DIScope Scope = llvm::DIScope(LB);
74 if (Scope.isLexicalBlockFile()) {
75 llvm::DILexicalBlockFile LBF = llvm::DILexicalBlockFile(LB);
76 llvm::DIDescriptor D
77 = DBuilder.createLexicalBlockFile(LBF.getScope(),
Devang Patel53bc5182012-02-08 00:10:20 +000078 getOrCreateFile(CurLoc));
Eric Christopher73fb3502011-10-13 21:45:18 +000079 llvm::MDNode *N = D;
80 LexicalBlockStack.pop_back();
81 LexicalBlockStack.push_back(N);
82 } else if (Scope.isLexicalBlock()) {
83 llvm::DIDescriptor D
84 = DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc));
85 llvm::MDNode *N = D;
86 LexicalBlockStack.pop_back();
87 LexicalBlockStack.push_back(N);
88 }
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000089}
90
Devang Patel33583052010-01-28 23:15:27 +000091/// getContextDescriptor - Get context info for the decl.
Devang Patel170cef32010-12-09 00:33:05 +000092llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context) {
Devang Pateleb6d79b2010-02-01 21:34:11 +000093 if (!Context)
Devang Patel170cef32010-12-09 00:33:05 +000094 return TheCU;
Devang Pateleb6d79b2010-02-01 21:34:11 +000095
96 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
97 I = RegionMap.find(Context);
Richard Smithe7259aa2012-08-17 04:17:54 +000098 if (I != RegionMap.end()) {
99 llvm::Value *V = I->second;
100 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(V));
101 }
Devang Patel411894b2010-02-01 22:40:08 +0000102
Devang Pateleb6d79b2010-02-01 21:34:11 +0000103 // Check namespace.
104 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
Devang Patel170cef32010-12-09 00:33:05 +0000105 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl));
Devang Patel8b90a782010-05-13 23:52:37 +0000106
107 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context)) {
108 if (!RDecl->isDependentType()) {
Devang Patela2e57692010-10-28 17:27:32 +0000109 llvm::DIType Ty = getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Devang Patel170cef32010-12-09 00:33:05 +0000110 getOrCreateMainFile());
Devang Patel8b90a782010-05-13 23:52:37 +0000111 return llvm::DIDescriptor(Ty);
112 }
113 }
Devang Patel170cef32010-12-09 00:33:05 +0000114 return TheCU;
Devang Patel979ec2e2009-10-06 00:35:31 +0000115}
116
Devang Patel9c6c3a02010-01-14 00:36:21 +0000117/// getFunctionName - Get function name for the given FunctionDecl. If the
118/// name is constructred on demand (e.g. C++ destructor) then the name
119/// is stored on the side.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000120StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Devang Patel9c6c3a02010-01-14 00:36:21 +0000121 assert (FD && "Invalid FunctionDecl!");
122 IdentifierInfo *FII = FD->getIdentifier();
Eric Christopher16717452012-03-14 00:25:46 +0000123 FunctionTemplateSpecializationInfo *Info
124 = FD->getTemplateSpecializationInfo();
125 if (!Info && FII)
Devang Patel9c6c3a02010-01-14 00:36:21 +0000126 return FII->getName();
127
128 // Otherwise construct human readable name for debug info.
129 std::string NS = FD->getNameAsString();
130
Eric Christopher16717452012-03-14 00:25:46 +0000131 // Add any template specialization args.
132 if (Info) {
133 const TemplateArgumentList *TArgs = Info->TemplateArguments;
134 const TemplateArgument *Args = TArgs->data();
135 unsigned NumArgs = TArgs->size();
136 PrintingPolicy Policy(CGM.getLangOpts());
137 NS += TemplateSpecializationType::PrintTemplateArgumentList(Args,
138 NumArgs,
139 Policy);
140 }
141
Devang Patel9c6c3a02010-01-14 00:36:21 +0000142 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000143 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer1b627dc2010-01-23 18:16:07 +0000144 memcpy(StrPtr, NS.data(), NS.length());
Chris Lattner5f9e2722011-07-23 10:55:15 +0000145 return StringRef(StrPtr, NS.length());
Devang Patel9c6c3a02010-01-14 00:36:21 +0000146}
147
Chris Lattner5f9e2722011-07-23 10:55:15 +0000148StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000149 SmallString<256> MethodName;
David Chisnall52044a22010-09-02 18:01:51 +0000150 llvm::raw_svector_ostream OS(MethodName);
151 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
152 const DeclContext *DC = OMD->getDeclContext();
Devang Patela2e57692010-10-28 17:27:32 +0000153 if (const ObjCImplementationDecl *OID =
154 dyn_cast<const ObjCImplementationDecl>(DC)) {
David Chisnall52044a22010-09-02 18:01:51 +0000155 OS << OID->getName();
Devang Patela2e57692010-10-28 17:27:32 +0000156 } else if (const ObjCInterfaceDecl *OID =
157 dyn_cast<const ObjCInterfaceDecl>(DC)) {
Fariborz Jahanian1a4c9372010-10-18 17:51:06 +0000158 OS << OID->getName();
Devang Patela2e57692010-10-28 17:27:32 +0000159 } else if (const ObjCCategoryImplDecl *OCD =
160 dyn_cast<const ObjCCategoryImplDecl>(DC)){
Roman Divacky31ba6132012-09-06 15:59:27 +0000161 OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '(' <<
David Chisnall52044a22010-09-02 18:01:51 +0000162 OCD->getIdentifier()->getNameStart() << ')';
163 }
164 OS << ' ' << OMD->getSelector().getAsString() << ']';
165
166 char *StrPtr = DebugInfoNames.Allocate<char>(OS.tell());
167 memcpy(StrPtr, MethodName.begin(), OS.tell());
Chris Lattner5f9e2722011-07-23 10:55:15 +0000168 return StringRef(StrPtr, OS.tell());
David Chisnall52044a22010-09-02 18:01:51 +0000169}
170
Eric Christopherecae5962012-03-29 17:31:33 +0000171/// getSelectorName - Return selector name. This is used for debugging
172/// info.
173StringRef CGDebugInfo::getSelectorName(Selector S) {
174 const std::string &SName = S.getAsString();
175 char *StrPtr = DebugInfoNames.Allocate<char>(SName.size());
176 memcpy(StrPtr, SName.data(), SName.size());
177 return StringRef(StrPtr, SName.size());
178}
179
Devang Patel700a1cb2010-07-20 20:24:18 +0000180/// getClassName - Get class name including template argument list.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000181StringRef
Eric Christopher9caf4402012-02-08 01:53:14 +0000182CGDebugInfo::getClassName(const RecordDecl *RD) {
183 const ClassTemplateSpecializationDecl *Spec
Devang Patel700a1cb2010-07-20 20:24:18 +0000184 = dyn_cast<ClassTemplateSpecializationDecl>(RD);
185 if (!Spec)
186 return RD->getName();
187
188 const TemplateArgument *Args;
189 unsigned NumArgs;
Devang Patel700a1cb2010-07-20 20:24:18 +0000190 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
191 const TemplateSpecializationType *TST =
192 cast<TemplateSpecializationType>(TAW->getType());
193 Args = TST->getArgs();
194 NumArgs = TST->getNumArgs();
195 } else {
196 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Douglas Gregor910f8002010-11-07 23:05:16 +0000197 Args = TemplateArgs.data();
198 NumArgs = TemplateArgs.size();
Devang Patel700a1cb2010-07-20 20:24:18 +0000199 }
Benjamin Kramerc6b468e2012-04-13 18:00:37 +0000200 StringRef Name = RD->getIdentifier()->getName();
David Blaikie4e4d0842012-03-11 07:00:24 +0000201 PrintingPolicy Policy(CGM.getLangOpts());
Benjamin Kramerc6b468e2012-04-13 18:00:37 +0000202 std::string TemplateArgList =
203 TemplateSpecializationType::PrintTemplateArgumentList(Args, NumArgs, Policy);
Devang Patel700a1cb2010-07-20 20:24:18 +0000204
205 // Copy this name on the side and use its reference.
Benjamin Kramerc6b468e2012-04-13 18:00:37 +0000206 size_t Length = Name.size() + TemplateArgList.size();
207 char *StrPtr = DebugInfoNames.Allocate<char>(Length);
208 memcpy(StrPtr, Name.data(), Name.size());
209 memcpy(StrPtr + Name.size(), TemplateArgList.data(), TemplateArgList.size());
210 return StringRef(StrPtr, Length);
Devang Patel700a1cb2010-07-20 20:24:18 +0000211}
212
Devang Patel17800552010-03-09 00:44:50 +0000213/// getOrCreateFile - Get the file debug info descriptor for the input location.
214llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Devang Patel823d8e92010-12-08 22:42:58 +0000215 if (!Loc.isValid())
216 // If Location is not valid then use main input file.
Devang Patel16674e82011-02-22 18:56:36 +0000217 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
Devang Patel823d8e92010-12-08 22:42:58 +0000218
Anders Carlsson20f12a22009-12-06 18:00:51 +0000219 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel17800552010-03-09 00:44:50 +0000220 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Ted Kremenek9c250392010-03-30 00:27:51 +0000221
Chris Lattner5f9e2722011-07-23 10:55:15 +0000222 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
Douglas Gregor8c457a82010-11-11 20:45:16 +0000223 // If the location is not valid then use main input file.
Devang Patel16674e82011-02-22 18:56:36 +0000224 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
Douglas Gregor8c457a82010-11-11 20:45:16 +0000225
Ted Kremenek9c250392010-03-30 00:27:51 +0000226 // Cache the results.
227 const char *fname = PLoc.getFilename();
228 llvm::DenseMap<const char *, llvm::WeakVH>::iterator it =
229 DIFileCache.find(fname);
230
231 if (it != DIFileCache.end()) {
232 // Verify that the information still exists.
Richard Smithe7259aa2012-08-17 04:17:54 +0000233 if (llvm::Value *V = it->second)
234 return llvm::DIFile(cast<llvm::MDNode>(V));
Ted Kremenek9c250392010-03-30 00:27:51 +0000235 }
236
Devang Patel16674e82011-02-22 18:56:36 +0000237 llvm::DIFile F = DBuilder.createFile(PLoc.getFilename(), getCurrentDirname());
Ted Kremenek9c250392010-03-30 00:27:51 +0000238
Devang Patelab699792010-05-07 18:12:35 +0000239 DIFileCache[fname] = F;
Ted Kremenek9c250392010-03-30 00:27:51 +0000240 return F;
Devang Patel17800552010-03-09 00:44:50 +0000241}
Devang Patel8ab870d2010-05-12 23:46:38 +0000242
Devang Patel532105f2010-10-28 22:03:20 +0000243/// getOrCreateMainFile - Get the file info for main compile unit.
244llvm::DIFile CGDebugInfo::getOrCreateMainFile() {
Devang Patel16674e82011-02-22 18:56:36 +0000245 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
Devang Patel532105f2010-10-28 22:03:20 +0000246}
247
Devang Patel8ab870d2010-05-12 23:46:38 +0000248/// getLineNumber - Get line number for the location. If location is invalid
249/// then use current location.
250unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
Devang Patel362ed2a2012-02-06 23:24:13 +0000251 if (Loc.isInvalid() && CurLoc.isInvalid())
252 return 0;
Devang Patel8ab870d2010-05-12 23:46:38 +0000253 SourceManager &SM = CGM.getContext().getSourceManager();
254 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Douglas Gregor8c457a82010-11-11 20:45:16 +0000255 return PLoc.isValid()? PLoc.getLine() : 0;
Devang Patel8ab870d2010-05-12 23:46:38 +0000256}
257
Eric Christopher25dfaac2012-10-18 22:08:02 +0000258/// getColumnNumber - Get column number for the location.
Devang Patel8ab870d2010-05-12 23:46:38 +0000259unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) {
Eric Christopher25dfaac2012-10-18 22:08:02 +0000260 // We may not want column information at all.
Eric Christopherda3301e2012-10-18 21:52:18 +0000261 if (!CGM.getCodeGenOpts().DebugColumnInfo)
262 return 0;
Eric Christopher25dfaac2012-10-18 22:08:02 +0000263
264 // If the location is invalid then use the current column.
265 if (Loc.isInvalid() && CurLoc.isInvalid())
266 return 0;
Devang Patel8ab870d2010-05-12 23:46:38 +0000267 SourceManager &SM = CGM.getContext().getSourceManager();
268 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Douglas Gregor8c457a82010-11-11 20:45:16 +0000269 return PLoc.isValid()? PLoc.getColumn() : 0;
Devang Patel8ab870d2010-05-12 23:46:38 +0000270}
271
Chris Lattner5f9e2722011-07-23 10:55:15 +0000272StringRef CGDebugInfo::getCurrentDirname() {
Nick Lewycky7c4fd912011-10-21 02:32:14 +0000273 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
274 return CGM.getCodeGenOpts().DebugCompilationDir;
275
Devang Patelac4d13c2010-07-27 15:17:16 +0000276 if (!CWDName.empty())
277 return CWDName;
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000278 SmallString<256> CWD;
Benjamin Kramerbcbca752011-10-14 18:45:11 +0000279 llvm::sys::fs::current_path(CWD);
280 char *CompDirnamePtr = DebugInfoNames.Allocate<char>(CWD.size());
281 memcpy(CompDirnamePtr, CWD.data(), CWD.size());
Chris Lattner5f9e2722011-07-23 10:55:15 +0000282 return CWDName = StringRef(CompDirnamePtr, CWD.size());
Devang Patelac4d13c2010-07-27 15:17:16 +0000283}
284
Devang Patel17800552010-03-09 00:44:50 +0000285/// CreateCompileUnit - Create new compile unit.
286void CGDebugInfo::CreateCompileUnit() {
287
288 // Get absolute path name.
Douglas Gregorac91b4c2010-03-18 23:46:43 +0000289 SourceManager &SM = CGM.getContext().getSourceManager();
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000290 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
291 if (MainFileName.empty())
Devang Patel22fe5852010-03-12 21:04:27 +0000292 MainFileName = "<unknown>";
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000293
Douglas Gregorf6728fc2010-03-22 21:28:29 +0000294 // The main file name provided via the "-main-file-name" option contains just
295 // the file name itself with no path information. This file name may have had
296 // a relative path, so we look into the actual file entry for the main
297 // file to determine the real absolute path for the file.
Devang Patel6e6bc392010-07-23 23:04:28 +0000298 std::string MainFileDir;
Devang Patelac4d13c2010-07-27 15:17:16 +0000299 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000300 MainFileDir = MainFile->getDir()->getName();
Devang Patelac4d13c2010-07-27 15:17:16 +0000301 if (MainFileDir != ".")
302 MainFileName = MainFileDir + "/" + MainFileName;
303 }
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000304
Devang Patelac4d13c2010-07-27 15:17:16 +0000305 // Save filename string.
306 char *FilenamePtr = DebugInfoNames.Allocate<char>(MainFileName.length());
307 memcpy(FilenamePtr, MainFileName.c_str(), MainFileName.length());
Chris Lattner5f9e2722011-07-23 10:55:15 +0000308 StringRef Filename(FilenamePtr, MainFileName.length());
Devang Patelac4d13c2010-07-27 15:17:16 +0000309
Chris Lattner515455a2009-03-25 03:28:08 +0000310 unsigned LangTag;
David Blaikie4e4d0842012-03-11 07:00:24 +0000311 const LangOptions &LO = CGM.getLangOpts();
Chris Lattner515455a2009-03-25 03:28:08 +0000312 if (LO.CPlusPlus) {
313 if (LO.ObjC1)
314 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
315 else
316 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
317 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000318 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000319 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000320 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000321 } else {
322 LangTag = llvm::dwarf::DW_LANG_C89;
323 }
Devang Patel446c6192009-04-17 21:06:59 +0000324
Daniel Dunbar19f19832010-08-24 17:41:09 +0000325 std::string Producer = getClangFullVersion();
Chris Lattner4c2577a2009-05-02 01:00:04 +0000326
327 // Figure out which version of the ObjC runtime we have.
328 unsigned RuntimeVers = 0;
329 if (LO.ObjC1)
John McCall260611a2012-06-20 06:18:46 +0000330 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000332 // Create new compile unit.
Devang Patel16674e82011-02-22 18:56:36 +0000333 DBuilder.createCompileUnit(
Devang Patel58115002010-07-27 20:49:59 +0000334 LangTag, Filename, getCurrentDirname(),
Devang Patel823d8e92010-12-08 22:42:58 +0000335 Producer,
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000336 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Devang Patel823d8e92010-12-08 22:42:58 +0000337 // FIXME - Eliminate TheCU.
338 TheCU = llvm::DICompileUnit(DBuilder.getCU());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000339}
340
Devang Patel65e99f22009-02-25 01:36:11 +0000341/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000342/// one if necessary.
Devang Patelf1d1d9a2010-11-01 16:52:40 +0000343llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000344 unsigned Encoding = 0;
Argyrios Kyrtzidis27a00972012-05-05 04:20:28 +0000345 StringRef BTName;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000346 switch (BT->getKind()) {
John McCalle0a22d02011-10-18 21:02:43 +0000347#define BUILTIN_TYPE(Id, SingletonId)
348#define PLACEHOLDER_TYPE(Id, SingletonId) \
349 case BuiltinType::Id:
350#include "clang/AST/BuiltinTypes.def"
Devang Patele7566cf2011-09-12 18:50:21 +0000351 case BuiltinType::Dependent:
John McCalle0a22d02011-10-18 21:02:43 +0000352 llvm_unreachable("Unexpected builtin type");
Devang Patele7566cf2011-09-12 18:50:21 +0000353 case BuiltinType::NullPtr:
Devang Patelf60dca32011-09-14 23:14:14 +0000354 return DBuilder.
Richard Smith7edf9e32012-11-01 22:30:59 +0000355 createNullPtrType(BT->getName(CGM.getLangOpts()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000356 case BuiltinType::Void:
357 return llvm::DIType();
Devang Patelc8972c62010-07-28 01:33:15 +0000358 case BuiltinType::ObjCClass:
Eric Christopherbf3a9662012-08-20 23:32:17 +0000359 if (ClassTy.Verify())
360 return ClassTy;
361 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
362 "objc_class", TheCU,
363 getOrCreateMainFile(), 0);
364 return ClassTy;
Devang Patelc8972c62010-07-28 01:33:15 +0000365 case BuiltinType::ObjCId: {
366 // typedef struct objc_class *Class;
367 // typedef struct objc_object {
368 // Class isa;
369 // } *id;
370
Eric Christopherbf3a9662012-08-20 23:32:17 +0000371 if (ObjTy.Verify())
372 return ObjTy;
373
374 if (!ClassTy.Verify())
375 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
376 "objc_class", TheCU,
377 getOrCreateMainFile(), 0);
378
Devang Patelc8972c62010-07-28 01:33:15 +0000379 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
380
Eric Christopherbf3a9662012-08-20 23:32:17 +0000381 llvm::DIType ISATy = DBuilder.createPointerType(ClassTy, Size);
Devang Patelc8972c62010-07-28 01:33:15 +0000382
Eric Christopherbf3a9662012-08-20 23:32:17 +0000383 llvm::DIType FwdTy = DBuilder.createStructType(TheCU, "objc_object",
Eric Christopher003e7562012-08-17 22:54:57 +0000384 getOrCreateMainFile(),
Eric Christopherbf3a9662012-08-20 23:32:17 +0000385 0, 0, 0, 0,
386 llvm::DIArray());
387
388 llvm::TrackingVH<llvm::MDNode> ObjNode(FwdTy);
Eric Christopher003e7562012-08-17 22:54:57 +0000389 SmallVector<llvm::Value *, 1> EltTys;
Devang Patelc8972c62010-07-28 01:33:15 +0000390 llvm::DIType FieldTy =
Eric Christopherbf3a9662012-08-20 23:32:17 +0000391 DBuilder.createMemberType(llvm::DIDescriptor(ObjNode), "isa",
Devang Patel1d323e02011-06-24 22:00:59 +0000392 getOrCreateMainFile(), 0, Size,
393 0, 0, 0, ISATy);
Devang Patelc8972c62010-07-28 01:33:15 +0000394 EltTys.push_back(FieldTy);
Jay Foadc556ef22011-04-24 10:11:03 +0000395 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopher003e7562012-08-17 22:54:57 +0000396
Eric Christopherbf3a9662012-08-20 23:32:17 +0000397 ObjNode->replaceOperandWith(10, Elements);
398 ObjTy = llvm::DIType(ObjNode);
399 return ObjTy;
Devang Patelc8972c62010-07-28 01:33:15 +0000400 }
Devang Patel6e108ce2011-02-09 03:15:05 +0000401 case BuiltinType::ObjCSel: {
Eric Christopherbf3a9662012-08-20 23:32:17 +0000402 if (SelTy.Verify())
403 return SelTy;
404 SelTy =
Eric Christopher917bc8d2012-02-20 18:05:04 +0000405 DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
Eric Christopher87380aa2012-04-23 19:00:24 +0000406 "objc_selector", TheCU, getOrCreateMainFile(),
Eric Christophere86b9ea2012-02-20 23:02:36 +0000407 0);
Eric Christopherbf3a9662012-08-20 23:32:17 +0000408 return SelTy;
Devang Patel6e108ce2011-02-09 03:15:05 +0000409 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000410 case BuiltinType::UChar:
411 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
412 case BuiltinType::Char_S:
413 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
Devang Patele8ee3f22011-09-12 17:11:58 +0000414 case BuiltinType::Char16:
415 case BuiltinType::Char32: Encoding = llvm::dwarf::DW_ATE_UTF; break;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000416 case BuiltinType::UShort:
417 case BuiltinType::UInt:
Devang Patel31c79b42011-05-05 17:06:30 +0000418 case BuiltinType::UInt128:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000419 case BuiltinType::ULong:
Devang Patel68f76b12011-09-10 00:44:49 +0000420 case BuiltinType::WChar_U:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000421 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
422 case BuiltinType::Short:
423 case BuiltinType::Int:
Devang Patel31c79b42011-05-05 17:06:30 +0000424 case BuiltinType::Int128:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000425 case BuiltinType::Long:
Devang Patel68f76b12011-09-10 00:44:49 +0000426 case BuiltinType::WChar_S:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000427 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
428 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +0000429 case BuiltinType::Half:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000430 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000431 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000432 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000433 }
Devang Patel05127ca2010-07-28 23:23:29 +0000434
435 switch (BT->getKind()) {
436 case BuiltinType::Long: BTName = "long int"; break;
437 case BuiltinType::LongLong: BTName = "long long int"; break;
438 case BuiltinType::ULong: BTName = "long unsigned int"; break;
439 case BuiltinType::ULongLong: BTName = "long long unsigned int"; break;
440 default:
Richard Smith7edf9e32012-11-01 22:30:59 +0000441 BTName = BT->getName(CGM.getLangOpts());
Devang Patel05127ca2010-07-28 23:23:29 +0000442 break;
443 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000444 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000445 uint64_t Size = CGM.getContext().getTypeSize(BT);
446 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Devang Patelca80a5f2009-10-20 19:55:01 +0000447 llvm::DIType DbgTy =
Devang Patel16674e82011-02-22 18:56:36 +0000448 DBuilder.createBasicType(BTName, Size, Align, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000449 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000450}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000451
Devang Patel344ff5d2010-12-09 00:25:29 +0000452llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty) {
Chris Lattnerb7003772009-04-23 06:13:01 +0000453 // Bit size, align and offset of the type.
454 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
455 if (Ty->isComplexIntegerType())
456 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Anders Carlsson20f12a22009-12-06 18:00:51 +0000458 uint64_t Size = CGM.getContext().getTypeSize(Ty);
459 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Devang Patelca80a5f2009-10-20 19:55:01 +0000460 llvm::DIType DbgTy =
Devang Patel16674e82011-02-22 18:56:36 +0000461 DBuilder.createBasicType("complex", Size, Align, Encoding);
Devang Patel823d8e92010-12-08 22:42:58 +0000462
Devang Patelca80a5f2009-10-20 19:55:01 +0000463 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000464}
465
John McCalla1805292009-09-25 01:40:47 +0000466/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000467/// a new one if necessary.
Devang Patel17800552010-03-09 00:44:50 +0000468llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +0000469 QualifierCollector Qc;
470 const Type *T = Qc.strip(Ty);
471
472 // Ignore these qualifiers for now.
473 Qc.removeObjCGCAttr();
474 Qc.removeAddressSpace();
John McCallf85e1932011-06-15 23:02:42 +0000475 Qc.removeObjCLifetime();
John McCalla1805292009-09-25 01:40:47 +0000476
Chris Lattner9c85ba32008-11-10 06:08:34 +0000477 // We will create one Derived type for one qualifier and recurse to handle any
478 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000479 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000480 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000481 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000482 Qc.removeConst();
483 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000484 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000485 Qc.removeVolatile();
486 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000487 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000488 Qc.removeRestrict();
489 } else {
490 assert(Qc.empty() && "Unknown type qualifier for debug info");
491 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000492 }
Mike Stump1eb44332009-09-09 15:08:12 +0000493
John McCall49f4e1c2010-12-10 11:01:00 +0000494 llvm::DIType FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
John McCalla1805292009-09-25 01:40:47 +0000495
Daniel Dunbar3845f862008-10-31 03:54:29 +0000496 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
497 // CVR derived types.
Devang Patel16674e82011-02-22 18:56:36 +0000498 llvm::DIType DbgTy = DBuilder.createQualifiedType(Tag, FromTy);
Devang Patel823d8e92010-12-08 22:42:58 +0000499
Devang Patelca80a5f2009-10-20 19:55:01 +0000500 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000501}
502
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000503llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000504 llvm::DIFile Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000505 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000506 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
507 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000508 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000509}
510
Chris Lattner9c85ba32008-11-10 06:08:34 +0000511llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000512 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000513 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
514 Ty->getPointeeType(), Unit);
515}
516
Eric Christopher5d613b52012-01-25 02:06:59 +0000517// Creates a forward declaration for a RecordDecl in the given context.
518llvm::DIType CGDebugInfo::createRecordFwdDecl(const RecordDecl *RD,
Devang Patel53bc5182012-02-08 00:10:20 +0000519 llvm::DIDescriptor Ctx) {
Eric Christopher5d613b52012-01-25 02:06:59 +0000520 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
521 unsigned Line = getLineNumber(RD->getLocation());
David Blaikie9417b052012-11-02 20:49:01 +0000522 StringRef RDName = getClassName(RD);
Eric Christophere88a71f2012-02-13 15:08:45 +0000523
Eric Christophere88a71f2012-02-13 15:08:45 +0000524 unsigned Tag = 0;
David Blaikie9417b052012-11-02 20:49:01 +0000525 if (RD->isStruct() || RD->isInterface())
Joao Matos17d35c32012-08-31 22:18:20 +0000526 Tag = llvm::dwarf::DW_TAG_structure_type;
527 else if (RD->isUnion())
528 Tag = llvm::dwarf::DW_TAG_union_type;
David Blaikie9417b052012-11-02 20:49:01 +0000529 else {
530 assert(RD->isClass());
531 Tag = llvm::dwarf::DW_TAG_class_type;
532 }
Eric Christophere88a71f2012-02-13 15:08:45 +0000533
534 // Create the type.
Eric Christopher87380aa2012-04-23 19:00:24 +0000535 return DBuilder.createForwardDecl(Tag, RDName, Ctx, DefUnit, Line);
Eric Christopher5d613b52012-01-25 02:06:59 +0000536}
537
Eric Christopher4ddca8a2012-01-20 22:10:15 +0000538// Walk up the context chain and create forward decls for record decls,
539// and normal descriptors for namespaces.
540llvm::DIDescriptor CGDebugInfo::createContextChain(const Decl *Context) {
541 if (!Context)
542 return TheCU;
543
544 // See if we already have the parent.
545 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
546 I = RegionMap.find(Context);
Richard Smithe7259aa2012-08-17 04:17:54 +0000547 if (I != RegionMap.end()) {
548 llvm::Value *V = I->second;
549 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(V));
550 }
Eric Christopher4ddca8a2012-01-20 22:10:15 +0000551
552 // Check namespace.
553 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
554 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl));
555
556 if (const RecordDecl *RD = dyn_cast<RecordDecl>(Context)) {
557 if (!RD->isDependentType()) {
Eric Christopher9965dea2012-02-16 22:54:45 +0000558 llvm::DIType Ty = getOrCreateLimitedType(CGM.getContext().getTypeDeclType(RD),
559 getOrCreateMainFile());
Eric Christopher4ddca8a2012-01-20 22:10:15 +0000560 return llvm::DIDescriptor(Ty);
561 }
562 }
563 return TheCU;
564}
565
Eric Christopheredc95922011-09-13 23:45:09 +0000566/// CreatePointeeType - Create Pointee type. If Pointee is a record
Devang Patelc69e1cf2010-09-30 19:05:55 +0000567/// then emit record's fwd if debug info size reduction is enabled.
568llvm::DIType CGDebugInfo::CreatePointeeType(QualType PointeeTy,
569 llvm::DIFile Unit) {
Douglas Gregor4cdad312012-10-23 20:05:01 +0000570 if (CGM.getCodeGenOpts().getDebugInfo() != CodeGenOptions::LimitedDebugInfo)
Devang Patelc69e1cf2010-09-30 19:05:55 +0000571 return getOrCreateType(PointeeTy, Unit);
Devang Patel41422512011-10-24 23:15:17 +0000572
573 // Limit debug info for the pointee type.
574
Eric Christopher973bbb62011-12-16 23:40:18 +0000575 // If we have an existing type, use that, it's still smaller than creating
576 // a new type.
577 llvm::DIType Ty = getTypeOrNull(PointeeTy);
578 if (Ty.Verify()) return Ty;
579
Devang Patel41422512011-10-24 23:15:17 +0000580 // Handle qualifiers.
581 if (PointeeTy.hasLocalQualifiers())
Eric Christopherd0a97c42012-08-07 00:18:40 +0000582 return CreateQualifiedType(PointeeTy, Unit);
Devang Patel41422512011-10-24 23:15:17 +0000583
Devang Patelc69e1cf2010-09-30 19:05:55 +0000584 if (const RecordType *RTy = dyn_cast<RecordType>(PointeeTy)) {
585 RecordDecl *RD = RTy->getDecl();
Devang Patelc69e1cf2010-09-30 19:05:55 +0000586 llvm::DIDescriptor FDContext =
John McCall8178df32011-02-22 22:38:33 +0000587 getContextDescriptor(cast<Decl>(RD->getDeclContext()));
Eric Christopher86211df2012-02-20 18:05:24 +0000588 llvm::DIType RetTy = createRecordFwdDecl(RD, FDContext);
589 TypeCache[QualType(RTy, 0).getAsOpaquePtr()] = RetTy;
590 return RetTy;
Devang Patelc69e1cf2010-09-30 19:05:55 +0000591 }
592 return getOrCreateType(PointeeTy, Unit);
Eric Christopher42e75da2012-02-13 14:56:11 +0000593
Devang Patelc69e1cf2010-09-30 19:05:55 +0000594}
595
Anders Carlssona031b352009-11-06 19:19:55 +0000596llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
597 const Type *Ty,
598 QualType PointeeTy,
Devang Patel17800552010-03-09 00:44:50 +0000599 llvm::DIFile Unit) {
Eric Christopher37e4cea2012-05-19 01:36:50 +0000600 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
601 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
602 return DBuilder.createReferenceType(Tag,
603 CreatePointeeType(PointeeTy, Unit));
Devang Patel823d8e92010-12-08 22:42:58 +0000604
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000605 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000606 // Size is always the size of a pointer. We can't use getTypeSize here
607 // because that does not return the correct value for references.
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000608 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000609 uint64_t Size = CGM.getContext().getTargetInfo().getPointerWidth(AS);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000610 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Nick Lewycky7480d962011-11-10 00:34:02 +0000612 return DBuilder.createPointerType(CreatePointeeType(PointeeTy, Unit),
613 Size, Align);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000614}
615
Mike Stump9bc093c2009-05-14 02:03:51 +0000616llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000617 llvm::DIFile Unit) {
Mike Stump9bc093c2009-05-14 02:03:51 +0000618 if (BlockLiteralGenericSet)
619 return BlockLiteralGeneric;
620
Chris Lattner5f9e2722011-07-23 10:55:15 +0000621 SmallVector<llvm::Value *, 8> EltTys;
Mike Stump9bc093c2009-05-14 02:03:51 +0000622 llvm::DIType FieldTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000623 QualType FType;
624 uint64_t FieldSize, FieldOffset;
625 unsigned FieldAlign;
Mike Stump9bc093c2009-05-14 02:03:51 +0000626 llvm::DIArray Elements;
627 llvm::DIType EltTy, DescTy;
628
629 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000630 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000631 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
632 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000633
Jay Foadc556ef22011-04-24 10:11:03 +0000634 Elements = DBuilder.getOrCreateArray(EltTys);
Mike Stump9bc093c2009-05-14 02:03:51 +0000635 EltTys.clear();
636
Devang Patele2472482010-09-29 21:05:52 +0000637 unsigned Flags = llvm::DIDescriptor::FlagAppleBlock;
Devang Patel8ab870d2010-05-12 23:46:38 +0000638 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump3d363c52009-10-02 02:30:50 +0000639
Devang Patel16674e82011-02-22 18:56:36 +0000640 EltTy = DBuilder.createStructType(Unit, "__block_descriptor",
Devang Patel823d8e92010-12-08 22:42:58 +0000641 Unit, LineNo, FieldOffset, 0,
642 Flags, Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000643
Mike Stump9bc093c2009-05-14 02:03:51 +0000644 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000645 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000646
Devang Patel16674e82011-02-22 18:56:36 +0000647 DescTy = DBuilder.createPointerType(EltTy, Size);
Mike Stump9bc093c2009-05-14 02:03:51 +0000648
649 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000650 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000651 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson20f12a22009-12-06 18:00:51 +0000652 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000653 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
654 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramerd3651cc2010-04-24 20:26:20 +0000655 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000656 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000657
Anders Carlsson20f12a22009-12-06 18:00:51 +0000658 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000659 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000660 FieldSize = CGM.getContext().getTypeSize(Ty);
661 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Devang Patel1d323e02011-06-24 22:00:59 +0000662 FieldTy = DBuilder.createMemberType(Unit, "__descriptor", Unit,
Devang Patel823d8e92010-12-08 22:42:58 +0000663 LineNo, FieldSize, FieldAlign,
664 FieldOffset, 0, FieldTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000665 EltTys.push_back(FieldTy);
666
667 FieldOffset += FieldSize;
Jay Foadc556ef22011-04-24 10:11:03 +0000668 Elements = DBuilder.getOrCreateArray(EltTys);
Mike Stump9bc093c2009-05-14 02:03:51 +0000669
Devang Patel16674e82011-02-22 18:56:36 +0000670 EltTy = DBuilder.createStructType(Unit, "__block_literal_generic",
Devang Patel823d8e92010-12-08 22:42:58 +0000671 Unit, LineNo, FieldOffset, 0,
672 Flags, Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Mike Stump9bc093c2009-05-14 02:03:51 +0000674 BlockLiteralGenericSet = true;
Devang Patel16674e82011-02-22 18:56:36 +0000675 BlockLiteralGeneric = DBuilder.createPointerType(EltTy, Size);
Mike Stump9bc093c2009-05-14 02:03:51 +0000676 return BlockLiteralGeneric;
677}
678
Nick Lewycky7480d962011-11-10 00:34:02 +0000679llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty, llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000680 // Typedefs are derived from some other type. If we have a typedef of a
681 // typedef, make sure to emit the whole chain.
682 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Devang Patel823d8e92010-12-08 22:42:58 +0000683 if (!Src.Verify())
684 return llvm::DIType();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000685 // We don't set size information, but do specify where the typedef was
686 // declared.
Devang Patel8ab870d2010-05-12 23:46:38 +0000687 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Devang Patelc4903122011-06-03 17:23:47 +0000688 const TypedefNameDecl *TyDecl = Ty->getDecl();
Eric Christopher9965dea2012-02-16 22:54:45 +0000689
Nick Lewycky7480d962011-11-10 00:34:02 +0000690 llvm::DIDescriptor TypedefContext =
Devang Patelc4903122011-06-03 17:23:47 +0000691 getContextDescriptor(cast<Decl>(Ty->getDecl()->getDeclContext()));
Eric Christopher9965dea2012-02-16 22:54:45 +0000692
693 return
Nick Lewycky7480d962011-11-10 00:34:02 +0000694 DBuilder.createTypedef(Src, TyDecl->getName(), Unit, Line, TypedefContext);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000695}
696
Chris Lattner9c85ba32008-11-10 06:08:34 +0000697llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000698 llvm::DIFile Unit) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000699 SmallVector<llvm::Value *, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000700
Chris Lattner9c85ba32008-11-10 06:08:34 +0000701 // Add the result type at least.
702 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000703
Chris Lattner9c85ba32008-11-10 06:08:34 +0000704 // Set up remainder of arguments if there is a prototype.
705 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Devang Patelaf164bb2010-10-06 20:51:45 +0000706 if (isa<FunctionNoProtoType>(Ty))
Devang Patel16674e82011-02-22 18:56:36 +0000707 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Eric Christopheraa6eccc2012-08-04 00:11:22 +0000708 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
Eric Christopherd0a97c42012-08-07 00:18:40 +0000709 for (unsigned i = 0, e = FPT->getNumArgs(); i != e; ++i)
Eric Christopheraa6eccc2012-08-04 00:11:22 +0000710 EltTys.push_back(getOrCreateType(FPT->getArgType(i), Unit));
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000711 }
712
Jay Foadc556ef22011-04-24 10:11:03 +0000713 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(EltTys);
Eric Christopherd9f07d42012-05-16 22:02:36 +0000714 return DBuilder.createSubroutineType(Unit, EltTypeArray);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000715}
716
Eric Christopher42e75da2012-02-13 14:56:11 +0000717
Eric Christopher6faa5542012-01-26 01:57:13 +0000718void CGDebugInfo::
719CollectRecordStaticVars(const RecordDecl *RD, llvm::DIType FwdDecl) {
720
721 for (RecordDecl::decl_iterator I = RD->decls_begin(), E = RD->decls_end();
722 I != E; ++I)
723 if (const VarDecl *V = dyn_cast<VarDecl>(*I)) {
724 if (V->getInit()) {
725 const APValue *Value = V->evaluateValue();
726 if (Value && Value->isInt()) {
727 llvm::ConstantInt *CI
728 = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
729
730 // Create the descriptor for static variable.
731 llvm::DIFile VUnit = getOrCreateFile(V->getLocation());
732 StringRef VName = V->getName();
733 llvm::DIType VTy = getOrCreateType(V->getType(), VUnit);
734 // Do not use DIGlobalVariable for enums.
735 if (VTy.getTag() != llvm::dwarf::DW_TAG_enumeration_type) {
736 DBuilder.createStaticVariable(FwdDecl, VName, VName, VUnit,
737 getLineNumber(V->getLocation()),
738 VTy, true, CI);
739 }
740 }
741 }
742 }
743}
744
Chris Lattner5f9e2722011-07-23 10:55:15 +0000745llvm::DIType CGDebugInfo::createFieldType(StringRef name,
John McCall8178df32011-02-22 22:38:33 +0000746 QualType type,
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000747 uint64_t sizeInBitsOverride,
John McCall8178df32011-02-22 22:38:33 +0000748 SourceLocation loc,
749 AccessSpecifier AS,
750 uint64_t offsetInBits,
Devang Patel1d323e02011-06-24 22:00:59 +0000751 llvm::DIFile tunit,
752 llvm::DIDescriptor scope) {
John McCall8178df32011-02-22 22:38:33 +0000753 llvm::DIType debugType = getOrCreateType(type, tunit);
754
755 // Get the location for the field.
756 llvm::DIFile file = getOrCreateFile(loc);
757 unsigned line = getLineNumber(loc);
758
759 uint64_t sizeInBits = 0;
760 unsigned alignInBits = 0;
761 if (!type->isIncompleteArrayType()) {
762 llvm::tie(sizeInBits, alignInBits) = CGM.getContext().getTypeInfo(type);
763
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000764 if (sizeInBitsOverride)
765 sizeInBits = sizeInBitsOverride;
John McCall8178df32011-02-22 22:38:33 +0000766 }
767
768 unsigned flags = 0;
769 if (AS == clang::AS_private)
770 flags |= llvm::DIDescriptor::FlagPrivate;
771 else if (AS == clang::AS_protected)
772 flags |= llvm::DIDescriptor::FlagProtected;
773
Devang Patel1d323e02011-06-24 22:00:59 +0000774 return DBuilder.createMemberType(scope, name, file, line, sizeInBits,
775 alignInBits, offsetInBits, flags, debugType);
John McCall8178df32011-02-22 22:38:33 +0000776}
777
Devang Patel428deb52010-01-19 00:00:59 +0000778/// CollectRecordFields - A helper function to collect debug info for
779/// record fields. This is used while creating debug info entry for a Record.
780void CGDebugInfo::
John McCall8178df32011-02-22 22:38:33 +0000781CollectRecordFields(const RecordDecl *record, llvm::DIFile tunit,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000782 SmallVectorImpl<llvm::Value *> &elements,
Devang Patel1d323e02011-06-24 22:00:59 +0000783 llvm::DIType RecordTy) {
John McCall8178df32011-02-22 22:38:33 +0000784 unsigned fieldNo = 0;
785 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Eric Christopherad8de512012-03-01 21:36:52 +0000786 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
787
Eric Christopherda970d22012-06-28 01:20:05 +0000788 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
Eric Christopherad8de512012-03-01 21:36:52 +0000789 // has the name and the location of the variable so we should iterate over
790 // both concurrently.
791 if (CXXDecl && CXXDecl->isLambda()) {
792 RecordDecl::field_iterator Field = CXXDecl->field_begin();
793 unsigned fieldno = 0;
794 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
795 E = CXXDecl->captures_end(); I != E; ++I, ++Field, ++fieldno) {
796 const LambdaExpr::Capture C = *I;
Eric Christopherad8de512012-03-01 21:36:52 +0000797 if (C.capturesVariable()) {
798 VarDecl *V = C.getCapturedVar();
799 llvm::DIFile VUnit = getOrCreateFile(C.getLocation());
800 StringRef VName = V->getName();
801 uint64_t SizeInBitsOverride = 0;
802 if (Field->isBitField()) {
803 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
804 assert(SizeInBitsOverride && "found named 0-width bitfield");
805 }
806 llvm::DIType fieldType
807 = createFieldType(VName, Field->getType(), SizeInBitsOverride, C.getLocation(),
808 Field->getAccess(), layout.getFieldOffset(fieldno),
809 VUnit, RecordTy);
810 elements.push_back(fieldType);
Eric Christopher28e3c992012-09-19 21:47:34 +0000811 } else {
Eric Christopher20ec2c42012-09-19 22:01:42 +0000812 // TODO: Need to handle 'this' in some way by probably renaming the
813 // this of the lambda class and having a field member of 'this' or
Eric Christopher847665d2012-09-19 22:40:44 +0000814 // by using AT_object_pointer for the function and having that be
Eric Christopher20ec2c42012-09-19 22:01:42 +0000815 // used as 'this' for semantic references.
Eric Christopher28e3c992012-09-19 21:47:34 +0000816 assert(C.capturesThis() && "Field that isn't captured and isn't this?");
817 FieldDecl *f = *Field;
818 llvm::DIFile VUnit = getOrCreateFile(f->getLocation());
819 QualType type = f->getType();
820 llvm::DIType fieldType
821 = createFieldType("this", type, 0, f->getLocation(), f->getAccess(),
822 layout.getFieldOffset(fieldNo), VUnit, RecordTy);
823
824 elements.push_back(fieldType);
Eric Christopherad8de512012-03-01 21:36:52 +0000825 }
826 }
827 } else {
Eli Friedman5f608ae2012-10-12 23:29:20 +0000828 bool IsMsStruct = record->isMsStruct(CGM.getContext());
Eric Christopherad8de512012-03-01 21:36:52 +0000829 const FieldDecl *LastFD = 0;
830 for (RecordDecl::field_iterator I = record->field_begin(),
831 E = record->field_end();
832 I != E; ++I, ++fieldNo) {
David Blaikie581deb32012-06-06 20:45:41 +0000833 FieldDecl *field = *I;
Eric Christopherad8de512012-03-01 21:36:52 +0000834
835 if (IsMsStruct) {
836 // Zero-length bitfields following non-bitfield members are ignored
837 if (CGM.getContext().ZeroBitfieldFollowsNonBitfield((field), LastFD)) {
838 --fieldNo;
839 continue;
840 }
841 LastFD = field;
842 }
843
844 StringRef name = field->getName();
845 QualType type = field->getType();
846
847 // Ignore unnamed fields unless they're anonymous structs/unions.
848 if (name.empty() && !type->isRecordType()) {
849 LastFD = field;
Fariborz Jahanianfbc3cc62011-04-28 23:43:23 +0000850 continue;
851 }
Eric Christopherad8de512012-03-01 21:36:52 +0000852
853 uint64_t SizeInBitsOverride = 0;
854 if (field->isBitField()) {
855 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
856 assert(SizeInBitsOverride && "found named 0-width bitfield");
857 }
858
859 llvm::DIType fieldType
860 = createFieldType(name, type, SizeInBitsOverride,
861 field->getLocation(), field->getAccess(),
862 layout.getFieldOffset(fieldNo), tunit, RecordTy);
863
864 elements.push_back(fieldType);
Fariborz Jahanianfbc3cc62011-04-28 23:43:23 +0000865 }
Devang Patel428deb52010-01-19 00:00:59 +0000866 }
867}
868
Devang Patela6da1922010-01-28 00:28:01 +0000869/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
870/// function type is not updated to include implicit "this" pointer. Use this
871/// routine to get a method type which includes "this" pointer.
872llvm::DIType
873CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000874 llvm::DIFile Unit) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +0000875 llvm::DIType FnTy
876 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
877 0),
878 Unit);
Eric Christopher3b10cfe2012-03-13 23:40:48 +0000879
Devang Patela6da1922010-01-28 00:28:01 +0000880 // Add "this" pointer.
Devang Patelab699792010-05-07 18:12:35 +0000881 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patela6da1922010-01-28 00:28:01 +0000882 assert (Args.getNumElements() && "Invalid number of arguments!");
883
Chris Lattner5f9e2722011-07-23 10:55:15 +0000884 SmallVector<llvm::Value *, 16> Elts;
Devang Patela6da1922010-01-28 00:28:01 +0000885
886 // First element is always return type. For 'void' functions it is NULL.
887 Elts.push_back(Args.getElement(0));
888
Eric Christopher2121cda2011-09-14 01:10:50 +0000889 if (!Method->isStatic()) {
890 // "this" pointer is always first argument.
891 QualType ThisPtr = Method->getThisType(CGM.getContext());
Devang Patelef8857d2011-10-28 21:12:13 +0000892
893 const CXXRecordDecl *RD = Method->getParent();
894 if (isa<ClassTemplateSpecializationDecl>(RD)) {
895 // Create pointer type directly in this case.
896 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
897 QualType PointeeTy = ThisPtrTy->getPointeeType();
898 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
899 uint64_t Size = CGM.getContext().getTargetInfo().getPointerWidth(AS);
900 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
Nick Lewyckyd4c100e2011-11-09 04:25:21 +0000901 llvm::DIType PointeeType = getOrCreateType(PointeeTy, Unit);
Eric Christopher3b8e1972012-02-09 07:26:21 +0000902 llvm::DIType ThisPtrType = DBuilder.createPointerType(PointeeType, Size, Align);
Devang Patelef8857d2011-10-28 21:12:13 +0000903 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
Eric Christopher3b8e1972012-02-09 07:26:21 +0000904 // TODO: This and the artificial type below are misleading, the
905 // types aren't artificial the argument is, but the current
906 // metadata doesn't represent that.
Eric Christopherd5a73dc2012-09-12 23:36:49 +0000907 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
Devang Patelef8857d2011-10-28 21:12:13 +0000908 Elts.push_back(ThisPtrType);
909 } else {
Eric Christopher3b8e1972012-02-09 07:26:21 +0000910 llvm::DIType ThisPtrType = getOrCreateType(ThisPtr, Unit);
Devang Patelef8857d2011-10-28 21:12:13 +0000911 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
Eric Christopherd5a73dc2012-09-12 23:36:49 +0000912 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
Devang Patelef8857d2011-10-28 21:12:13 +0000913 Elts.push_back(ThisPtrType);
914 }
Eric Christopher2121cda2011-09-14 01:10:50 +0000915 }
Devang Patela6da1922010-01-28 00:28:01 +0000916
917 // Copy rest of the arguments.
918 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
919 Elts.push_back(Args.getElement(i));
920
Jay Foadc556ef22011-04-24 10:11:03 +0000921 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(Elts);
Devang Patela6da1922010-01-28 00:28:01 +0000922
Devang Patel16674e82011-02-22 18:56:36 +0000923 return DBuilder.createSubroutineType(Unit, EltTypeArray);
Devang Patela6da1922010-01-28 00:28:01 +0000924}
925
Devang Patel58faf202010-10-22 17:11:50 +0000926/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
927/// inside a function.
928static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
Nick Lewycky7480d962011-11-10 00:34:02 +0000929 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
Devang Patel58faf202010-10-22 17:11:50 +0000930 return isFunctionLocalClass(NRD);
Nick Lewycky7480d962011-11-10 00:34:02 +0000931 if (isa<FunctionDecl>(RD->getDeclContext()))
Devang Patel58faf202010-10-22 17:11:50 +0000932 return true;
933 return false;
Devang Patel58faf202010-10-22 17:11:50 +0000934}
Nick Lewyckyd4c100e2011-11-09 04:25:21 +0000935
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000936/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
937/// a single member function GlobalDecl.
938llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000939CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000940 llvm::DIFile Unit,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000941 llvm::DIType RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000942 bool IsCtorOrDtor =
943 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
944
Chris Lattner5f9e2722011-07-23 10:55:15 +0000945 StringRef MethodName = getFunctionName(Method);
Devang Patela6da1922010-01-28 00:28:01 +0000946 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Eric Christopheraa6eccc2012-08-04 00:11:22 +0000947
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000948 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
949 // make sense to give a single ctor/dtor a linkage name.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000950 StringRef MethodLinkageName;
Devang Patel58faf202010-10-22 17:11:50 +0000951 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
Anders Carlsson9a20d552010-06-22 16:16:50 +0000952 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000953
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000954 // Get the location for the method.
Devang Patel8ab870d2010-05-12 23:46:38 +0000955 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
956 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000957
958 // Collect virtual method info.
959 llvm::DIType ContainingType;
960 unsigned Virtuality = 0;
961 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000962
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000963 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000964 if (Method->isPure())
965 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
966 else
967 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
968
969 // It doesn't make sense to give a virtual destructor a vtable index,
970 // since a single destructor has two entries in the vtable.
971 if (!isa<CXXDestructorDecl>(Method))
Peter Collingbourne1d2b3172011-09-26 01:56:30 +0000972 VIndex = CGM.getVTableContext().getMethodVTableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000973 ContainingType = RecordTy;
974 }
975
Devang Patele2472482010-09-29 21:05:52 +0000976 unsigned Flags = 0;
977 if (Method->isImplicit())
978 Flags |= llvm::DIDescriptor::FlagArtificial;
Devang Patel10a7a6a2010-09-29 21:46:16 +0000979 AccessSpecifier Access = Method->getAccess();
980 if (Access == clang::AS_private)
981 Flags |= llvm::DIDescriptor::FlagPrivate;
982 else if (Access == clang::AS_protected)
983 Flags |= llvm::DIDescriptor::FlagProtected;
Devang Pateld78a0192010-10-01 23:32:17 +0000984 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
985 if (CXXC->isExplicit())
986 Flags |= llvm::DIDescriptor::FlagExplicit;
987 } else if (const CXXConversionDecl *CXXC =
988 dyn_cast<CXXConversionDecl>(Method)) {
989 if (CXXC->isExplicit())
990 Flags |= llvm::DIDescriptor::FlagExplicit;
991 }
Devang Patel3951e712010-10-07 22:03:49 +0000992 if (Method->hasPrototype())
993 Flags |= llvm::DIDescriptor::FlagPrototyped;
Eric Christopher3b10cfe2012-03-13 23:40:48 +0000994
995 llvm::DIArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000996 llvm::DISubprogram SP =
Nick Lewycky7803ec82011-09-01 21:49:51 +0000997 DBuilder.createMethod(RecordTy, MethodName, MethodLinkageName,
Devang Patel823d8e92010-12-08 22:42:58 +0000998 MethodDefUnit, MethodLine,
999 MethodTy, /*isLocalToUnit=*/false,
1000 /* isDefinition=*/ false,
1001 Virtuality, VIndex, ContainingType,
Eric Christopher3b10cfe2012-03-13 23:40:48 +00001002 Flags, CGM.getLangOpts().Optimize, NULL,
1003 TParamsArray);
Anders Carlsson4433f1c2010-01-26 05:19:50 +00001004
Eric Christopherdeae6a82011-11-17 23:45:00 +00001005 SPCache[Method->getCanonicalDecl()] = llvm::WeakVH(SP);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +00001006
1007 return SP;
1008}
1009
Devang Patel4125fd22010-01-19 01:54:44 +00001010/// CollectCXXMemberFunctions - A helper function to collect debug info for
Eric Christopher7c9b2fd2012-01-12 01:26:51 +00001011/// C++ member functions. This is used while creating debug info entry for
Devang Patel4125fd22010-01-19 01:54:44 +00001012/// a Record.
1013void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +00001014CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001015 SmallVectorImpl<llvm::Value *> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +00001016 llvm::DIType RecordTy) {
Eric Christopher3b10cfe2012-03-13 23:40:48 +00001017
1018 // Since we want more than just the individual member decls if we
1019 // have templated functions iterate over every declaration to gather
1020 // the functions.
1021 for(DeclContext::decl_iterator I = RD->decls_begin(),
1022 E = RD->decls_end(); I != E; ++I) {
1023 Decl *D = *I;
1024 if (D->isImplicit() && !D->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +00001025 continue;
Devang Patel4125fd22010-01-19 01:54:44 +00001026
Eric Christopher9556b392012-10-17 17:37:17 +00001027 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
1028 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Eric Christopher3b10cfe2012-03-13 23:40:48 +00001029 else if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D))
1030 for (FunctionTemplateDecl::spec_iterator SI = FTD->spec_begin(),
Eric Christopher860de6b2012-08-13 02:07:42 +00001031 SE = FTD->spec_end(); SI != SE; ++SI)
1032 EltTys.push_back(CreateCXXMemberFunction(cast<CXXMethodDecl>(*SI), Unit,
1033 RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +00001034 }
1035}
1036
Devang Patel2ed8f002010-08-27 17:47:47 +00001037/// CollectCXXFriends - A helper function to collect debug info for
1038/// C++ base classes. This is used while creating debug info entry for
1039/// a Record.
1040void CGDebugInfo::
1041CollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001042 SmallVectorImpl<llvm::Value *> &EltTys,
Devang Patel2ed8f002010-08-27 17:47:47 +00001043 llvm::DIType RecordTy) {
Eric Christopher121c67d2012-01-12 01:26:58 +00001044 for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(),
Devang Patel2ed8f002010-08-27 17:47:47 +00001045 BE = RD->friend_end(); BI != BE; ++BI) {
Nick Lewycky7803ec82011-09-01 21:49:51 +00001046 if ((*BI)->isUnsupportedFriend())
1047 continue;
Devang Patel823d8e92010-12-08 22:42:58 +00001048 if (TypeSourceInfo *TInfo = (*BI)->getFriendType())
Devang Patel16674e82011-02-22 18:56:36 +00001049 EltTys.push_back(DBuilder.createFriend(RecordTy,
Devang Patel823d8e92010-12-08 22:42:58 +00001050 getOrCreateType(TInfo->getType(),
1051 Unit)));
Devang Patel2ed8f002010-08-27 17:47:47 +00001052 }
1053}
1054
Devang Patela245c5b2010-01-25 23:32:18 +00001055/// CollectCXXBases - A helper function to collect debug info for
1056/// C++ base classes. This is used while creating debug info entry for
1057/// a Record.
1058void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +00001059CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001060 SmallVectorImpl<llvm::Value *> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +00001061 llvm::DIType RecordTy) {
Devang Patela245c5b2010-01-25 23:32:18 +00001062
Devang Patel239cec62010-02-01 21:39:52 +00001063 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1064 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
1065 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +00001066 unsigned BFlags = 0;
Devang Patel62c117d2011-04-04 20:36:06 +00001067 uint64_t BaseOffset;
Devang Patelca7daed2010-01-28 21:54:15 +00001068
1069 const CXXRecordDecl *Base =
1070 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
1071
1072 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +00001073 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +00001074 // expression where it expects +ve number.
Ken Dyck14c65ca2011-04-07 12:37:09 +00001075 BaseOffset =
Peter Collingbourne1d2b3172011-09-26 01:56:30 +00001076 0 - CGM.getVTableContext()
1077 .getVirtualBaseOffsetOffset(RD, Base).getQuantity();
Devang Patele2472482010-09-29 21:05:52 +00001078 BFlags = llvm::DIDescriptor::FlagVirtual;
Devang Patelca7daed2010-01-28 21:54:15 +00001079 } else
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001080 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
Ken Dyck14c65ca2011-04-07 12:37:09 +00001081 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1082 // BI->isVirtual() and bits when not.
Devang Patelca7daed2010-01-28 21:54:15 +00001083
1084 AccessSpecifier Access = BI->getAccessSpecifier();
1085 if (Access == clang::AS_private)
Devang Patele2472482010-09-29 21:05:52 +00001086 BFlags |= llvm::DIDescriptor::FlagPrivate;
Devang Patelca7daed2010-01-28 21:54:15 +00001087 else if (Access == clang::AS_protected)
Devang Patele2472482010-09-29 21:05:52 +00001088 BFlags |= llvm::DIDescriptor::FlagProtected;
Devang Patelca7daed2010-01-28 21:54:15 +00001089
Devang Patel823d8e92010-12-08 22:42:58 +00001090 llvm::DIType DTy =
Devang Patel16674e82011-02-22 18:56:36 +00001091 DBuilder.createInheritance(RecordTy,
Devang Patel823d8e92010-12-08 22:42:58 +00001092 getOrCreateType(BI->getType(), Unit),
Devang Patel62c117d2011-04-04 20:36:06 +00001093 BaseOffset, BFlags);
Devang Patelca7daed2010-01-28 21:54:15 +00001094 EltTys.push_back(DTy);
1095 }
Devang Patela245c5b2010-01-25 23:32:18 +00001096}
1097
Devang Patel5ecb1df2011-04-05 22:54:11 +00001098/// CollectTemplateParams - A helper function to collect template parameters.
Devang Patel9c1714b2011-04-05 17:30:54 +00001099llvm::DIArray CGDebugInfo::
Devang Patel5ecb1df2011-04-05 22:54:11 +00001100CollectTemplateParams(const TemplateParameterList *TPList,
1101 const TemplateArgumentList &TAList,
1102 llvm::DIFile Unit) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001103 SmallVector<llvm::Value *, 16> TemplateParams;
Devang Patelc5ce2972011-04-05 20:15:06 +00001104 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1105 const TemplateArgument &TA = TAList[i];
Devang Patel5ecb1df2011-04-05 22:54:11 +00001106 const NamedDecl *ND = TPList->getParam(i);
Devang Patel9c1714b2011-04-05 17:30:54 +00001107 if (TA.getKind() == TemplateArgument::Type) {
1108 llvm::DIType TTy = getOrCreateType(TA.getAsType(), Unit);
1109 llvm::DITemplateTypeParameter TTP =
Devang Patelc5ce2972011-04-05 20:15:06 +00001110 DBuilder.createTemplateTypeParameter(TheCU, ND->getName(), TTy);
Devang Patel9c1714b2011-04-05 17:30:54 +00001111 TemplateParams.push_back(TTP);
1112 } else if (TA.getKind() == TemplateArgument::Integral) {
1113 llvm::DIType TTy = getOrCreateType(TA.getIntegralType(), Unit);
Devang Patel9c1714b2011-04-05 17:30:54 +00001114 llvm::DITemplateValueParameter TVP =
Devang Patelc5ce2972011-04-05 20:15:06 +00001115 DBuilder.createTemplateValueParameter(TheCU, ND->getName(), TTy,
Benjamin Kramer85524372012-06-07 15:09:51 +00001116 TA.getAsIntegral().getZExtValue());
Devang Patel9c1714b2011-04-05 17:30:54 +00001117 TemplateParams.push_back(TVP);
1118 }
1119 }
Jay Foadc556ef22011-04-24 10:11:03 +00001120 return DBuilder.getOrCreateArray(TemplateParams);
Devang Patel9c1714b2011-04-05 17:30:54 +00001121}
1122
Devang Patel5ecb1df2011-04-05 22:54:11 +00001123/// CollectFunctionTemplateParams - A helper function to collect debug
1124/// info for function template parameters.
1125llvm::DIArray CGDebugInfo::
1126CollectFunctionTemplateParams(const FunctionDecl *FD, llvm::DIFile Unit) {
Eric Christopherab5278e2011-10-11 23:00:51 +00001127 if (FD->getTemplatedKind() ==
1128 FunctionDecl::TK_FunctionTemplateSpecialization) {
Devang Patel5ecb1df2011-04-05 22:54:11 +00001129 const TemplateParameterList *TList =
Eric Christopherab5278e2011-10-11 23:00:51 +00001130 FD->getTemplateSpecializationInfo()->getTemplate()
1131 ->getTemplateParameters();
Devang Patel5ecb1df2011-04-05 22:54:11 +00001132 return
1133 CollectTemplateParams(TList, *FD->getTemplateSpecializationArgs(), Unit);
1134 }
1135 return llvm::DIArray();
1136}
1137
1138/// CollectCXXTemplateParams - A helper function to collect debug info for
1139/// template parameters.
1140llvm::DIArray CGDebugInfo::
1141CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TSpecial,
1142 llvm::DIFile Unit) {
1143 llvm::PointerUnion<ClassTemplateDecl *,
1144 ClassTemplatePartialSpecializationDecl *>
1145 PU = TSpecial->getSpecializedTemplateOrPartial();
1146
1147 TemplateParameterList *TPList = PU.is<ClassTemplateDecl *>() ?
1148 PU.get<ClassTemplateDecl *>()->getTemplateParameters() :
1149 PU.get<ClassTemplatePartialSpecializationDecl *>()->getTemplateParameters();
1150 const TemplateArgumentList &TAList = TSpecial->getTemplateInstantiationArgs();
1151 return CollectTemplateParams(TPList, TAList, Unit);
1152}
1153
Devang Patel4ce3f202010-01-28 18:11:52 +00001154/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +00001155llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +00001156 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +00001157 return VTablePtrType;
1158
1159 ASTContext &Context = CGM.getContext();
1160
1161 /* Function type */
Devang Patel823d8e92010-12-08 22:42:58 +00001162 llvm::Value *STy = getOrCreateType(Context.IntTy, Unit);
Jay Foadc556ef22011-04-24 10:11:03 +00001163 llvm::DIArray SElements = DBuilder.getOrCreateArray(STy);
Devang Patel16674e82011-02-22 18:56:36 +00001164 llvm::DIType SubTy = DBuilder.createSubroutineType(Unit, SElements);
Devang Patel4ce3f202010-01-28 18:11:52 +00001165 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Devang Patel16674e82011-02-22 18:56:36 +00001166 llvm::DIType vtbl_ptr_type = DBuilder.createPointerType(SubTy, Size, 0,
Devang Patel823d8e92010-12-08 22:42:58 +00001167 "__vtbl_ptr_type");
Devang Patel16674e82011-02-22 18:56:36 +00001168 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
Devang Patel4ce3f202010-01-28 18:11:52 +00001169 return VTablePtrType;
1170}
1171
Anders Carlsson046c2942010-04-17 20:15:18 +00001172/// getVTableName - Get vtable name for the given Class.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001173StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Eric Christopher51cb75a2012-01-25 21:47:09 +00001174 // Construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +00001175 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +00001176
1177 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +00001178 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +00001179 memcpy(StrPtr, Name.data(), Name.length());
Chris Lattner5f9e2722011-07-23 10:55:15 +00001180 return StringRef(StrPtr, Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +00001181}
1182
1183
Anders Carlsson046c2942010-04-17 20:15:18 +00001184/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel4ce3f202010-01-28 18:11:52 +00001185/// debug info entry in EltTys vector.
1186void CGDebugInfo::
Anders Carlsson046c2942010-04-17 20:15:18 +00001187CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001188 SmallVectorImpl<llvm::Value *> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +00001189 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +00001190
1191 // If there is a primary base then it will hold vtable info.
1192 if (RL.getPrimaryBase())
1193 return;
1194
1195 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +00001196 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +00001197 return;
1198
1199 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
1200 llvm::DIType VPTR
Devang Patel1d323e02011-06-24 22:00:59 +00001201 = DBuilder.createMemberType(Unit, getVTableName(RD), Unit,
Devang Patel823d8e92010-12-08 22:42:58 +00001202 0, Size, 0, 0, 0,
1203 getOrCreateVTablePtrType(Unit));
Devang Patel4ce3f202010-01-28 18:11:52 +00001204 EltTys.push_back(VPTR);
1205}
1206
Devang Patelc69e1cf2010-09-30 19:05:55 +00001207/// getOrCreateRecordType - Emit record type's standalone debug info.
1208llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
1209 SourceLocation Loc) {
Douglas Gregor4cdad312012-10-23 20:05:01 +00001210 assert(CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo);
Nick Lewyckyd4c100e2011-11-09 04:25:21 +00001211 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
Devang Patelc69e1cf2010-09-30 19:05:55 +00001212 return T;
1213}
1214
Eric Christopherbe6c6862012-04-11 05:56:05 +00001215/// getOrCreateInterfaceType - Emit an objective c interface type standalone
1216/// debug info.
1217llvm::DIType CGDebugInfo::getOrCreateInterfaceType(QualType D,
1218 SourceLocation Loc) {
Douglas Gregor4cdad312012-10-23 20:05:01 +00001219 assert(CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo);
Eric Christopherbe6c6862012-04-11 05:56:05 +00001220 llvm::DIType T = getOrCreateType(D, getOrCreateFile(Loc));
1221 DBuilder.retainType(T);
1222 return T;
1223}
1224
Devang Patel65e99f22009-02-25 01:36:11 +00001225/// CreateType - get structure or union type.
Devang Patel31f7d022011-01-17 22:23:07 +00001226llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001227 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001228
Chris Lattner9c85ba32008-11-10 06:08:34 +00001229 // Get overall information about the record type for the debug info.
Devang Patel8ab870d2010-05-12 23:46:38 +00001230 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001231
Chris Lattner9c85ba32008-11-10 06:08:34 +00001232 // Records and classes and unions can all be recursive. To handle them, we
1233 // first generate a debug descriptor for the struct as a forward declaration.
1234 // Then (if it is a definition) we go through and get debug info for all of
1235 // its members. Finally, we create a descriptor for the complete type (which
1236 // may refer to the forward decl if the struct is recursive) and replace all
1237 // uses of the forward declaration with the final definition.
Eric Christopher4ddca8a2012-01-20 22:10:15 +00001238
Eric Christopher9965dea2012-02-16 22:54:45 +00001239 llvm::DIType FwdDecl = getOrCreateLimitedType(QualType(Ty, 0), DefUnit);
Devang Patel0b897992010-07-08 19:56:29 +00001240
Eric Christopher9965dea2012-02-16 22:54:45 +00001241 if (FwdDecl.isForwardDecl())
1242 return FwdDecl;
Benjamin Kramer6181e562012-03-20 19:49:14 +00001243
1244 llvm::TrackingVH<llvm::MDNode> FwdDeclNode(FwdDecl);
1245
Devang Patele4c1ea02010-03-11 20:01:48 +00001246 // Push the struct on region stack.
Eric Christopheraa2164c2011-09-29 00:00:45 +00001247 LexicalBlockStack.push_back(FwdDeclNode);
Devang Patelab699792010-05-07 18:12:35 +00001248 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001249
Eric Christopher9965dea2012-02-16 22:54:45 +00001250 // Add this to the completed types cache since we're completing it.
1251 CompletedTypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
1252
Chris Lattner9c85ba32008-11-10 06:08:34 +00001253 // Convert all the elements.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001254 SmallVector<llvm::Value *, 16> EltTys;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001255
Eric Christopher1c081d92012-01-26 07:01:04 +00001256 // Note: The split of CXXDecl information here is intentional, the
1257 // gdb tests will depend on a certain ordering at printout. The debug
1258 // information offsets are still correct if we merge them all together
1259 // though.
Devang Pateld6c5a262010-02-01 21:52:22 +00001260 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +00001261 if (CXXDecl) {
Eric Christopher3ee8c912012-01-26 06:20:57 +00001262 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1263 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
Eric Christopher1c081d92012-01-26 07:01:04 +00001264 }
1265
1266 // Collect static variables with initializers and other fields.
1267 CollectRecordStaticVars(RD, FwdDecl);
1268 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
1269 llvm::DIArray TParamsArray;
1270 if (CXXDecl) {
Eric Christopher3ee8c912012-01-26 06:20:57 +00001271 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
1272 CollectCXXFriends(CXXDecl, DefUnit, EltTys, FwdDecl);
Devang Patel9c1714b2011-04-05 17:30:54 +00001273 if (const ClassTemplateSpecializationDecl *TSpecial
1274 = dyn_cast<ClassTemplateSpecializationDecl>(RD))
Eric Christopher3ee8c912012-01-26 06:20:57 +00001275 TParamsArray = CollectCXXTemplateParams(TSpecial, DefUnit);
Devang Patel823d8e92010-12-08 22:42:58 +00001276 }
Devang Patel0ac8f312010-01-28 00:54:21 +00001277
Eric Christopheraa2164c2011-09-29 00:00:45 +00001278 LexicalBlockStack.pop_back();
Benjamin Kramer7e423922012-03-24 18:22:12 +00001279 RegionMap.erase(Ty->getDecl());
Devang Patel823d8e92010-12-08 22:42:58 +00001280
Jay Foadc556ef22011-04-24 10:11:03 +00001281 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopher9965dea2012-02-16 22:54:45 +00001282 // FIXME: Magic numbers ahoy! These should be changed when we
1283 // get some enums in llvm/Analysis/DebugInfo.h to refer to
1284 // them.
Eric Christopher64a04302012-02-15 23:51:20 +00001285 if (RD->isUnion())
Benjamin Kramer6181e562012-03-20 19:49:14 +00001286 FwdDeclNode->replaceOperandWith(10, Elements);
Eric Christopher64a04302012-02-15 23:51:20 +00001287 else if (CXXDecl) {
Benjamin Kramer6181e562012-03-20 19:49:14 +00001288 FwdDeclNode->replaceOperandWith(10, Elements);
1289 FwdDeclNode->replaceOperandWith(13, TParamsArray);
Eric Christopher64a04302012-02-15 23:51:20 +00001290 } else
Benjamin Kramer6181e562012-03-20 19:49:14 +00001291 FwdDeclNode->replaceOperandWith(10, Elements);
Eric Christopher64a04302012-02-15 23:51:20 +00001292
Benjamin Kramer6181e562012-03-20 19:49:14 +00001293 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDeclNode);
1294 return llvm::DIType(FwdDeclNode);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001295}
1296
John McCallc12c5bb2010-05-15 11:32:37 +00001297/// CreateType - get objective-c object type.
1298llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1299 llvm::DIFile Unit) {
1300 // Ignore protocols.
1301 return getOrCreateType(Ty->getBaseType(), Unit);
1302}
1303
Devang Patel9ca36b62009-02-26 21:10:26 +00001304/// CreateType - get objective-c interface type.
1305llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001306 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001307 ObjCInterfaceDecl *ID = Ty->getDecl();
Douglas Gregora6a28972010-11-30 06:38:09 +00001308 if (!ID)
1309 return llvm::DIType();
Devang Patel9ca36b62009-02-26 21:10:26 +00001310
1311 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +00001312 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001313 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel17800552010-03-09 00:44:50 +00001314 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +00001315
Eric Christopherd1ab1a22011-10-06 00:31:18 +00001316 // If this is just a forward declaration return a special forward-declaration
1317 // debug type since we won't be able to lay out the entire type.
Douglas Gregor7c1f1f12011-12-15 23:32:29 +00001318 ObjCInterfaceDecl *Def = ID->getDefinition();
1319 if (!Def) {
Devang Patel823d8e92010-12-08 22:42:58 +00001320 llvm::DIType FwdDecl =
Eric Christopher917bc8d2012-02-20 18:05:04 +00001321 DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
Eric Christopher87380aa2012-04-23 19:00:24 +00001322 ID->getName(), TheCU, DefUnit, Line,
Eric Christopher917bc8d2012-02-20 18:05:04 +00001323 RuntimeLang);
Dan Gohman45f7c782010-08-23 21:15:56 +00001324 return FwdDecl;
1325 }
Eric Christopherbe6c6862012-04-11 05:56:05 +00001326
Douglas Gregor7c1f1f12011-12-15 23:32:29 +00001327 ID = Def;
Dan Gohman45f7c782010-08-23 21:15:56 +00001328
Eric Christopher9965dea2012-02-16 22:54:45 +00001329 // Bit size, align and offset of the type.
1330 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1331 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001332
Eric Christopher9965dea2012-02-16 22:54:45 +00001333 unsigned Flags = 0;
1334 if (ID->getImplementation())
1335 Flags |= llvm::DIDescriptor::FlagObjcClassComplete;
1336
1337 llvm::DIType RealDecl =
1338 DBuilder.createStructType(Unit, ID->getName(), DefUnit,
1339 Line, Size, Align, Flags,
1340 llvm::DIArray(), RuntimeLang);
Eric Christopherbe6c6862012-04-11 05:56:05 +00001341
Eric Christopheraf3db7d2012-02-27 08:23:23 +00001342 // Otherwise, insert it into the CompletedTypeCache so that recursive uses
1343 // will find it and we're emitting the complete type.
1344 CompletedTypeCache[QualType(Ty, 0).getAsOpaquePtr()] = RealDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +00001345 // Push the struct on region stack.
Benjamin Kramer6181e562012-03-20 19:49:14 +00001346 llvm::TrackingVH<llvm::MDNode> FwdDeclNode(RealDecl);
Eric Christopher9965dea2012-02-16 22:54:45 +00001347
Eric Christopheraa2164c2011-09-29 00:00:45 +00001348 LexicalBlockStack.push_back(FwdDeclNode);
Eric Christopher9965dea2012-02-16 22:54:45 +00001349 RegionMap[Ty->getDecl()] = llvm::WeakVH(RealDecl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001350
1351 // Convert all the elements.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001352 SmallVector<llvm::Value *, 16> EltTys;
Devang Patel9ca36b62009-02-26 21:10:26 +00001353
Devang Pateld6c5a262010-02-01 21:52:22 +00001354 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +00001355 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +00001356 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001357 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Douglas Gregora6a28972010-11-30 06:38:09 +00001358 if (!SClassTy.isValid())
1359 return llvm::DIType();
1360
Mike Stump1eb44332009-09-09 15:08:12 +00001361 llvm::DIType InhTag =
Eric Christopher9965dea2012-02-16 22:54:45 +00001362 DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Devang Patelfbe899f2009-03-10 21:30:26 +00001363 EltTys.push_back(InhTag);
1364 }
1365
Devang Patel693fcaa2012-02-07 18:40:30 +00001366 for (ObjCContainerDecl::prop_iterator I = ID->prop_begin(),
1367 E = ID->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001368 const ObjCPropertyDecl *PD = *I;
Eric Christopher51c03712012-03-29 08:43:37 +00001369 SourceLocation Loc = PD->getLocation();
1370 llvm::DIFile PUnit = getOrCreateFile(Loc);
1371 unsigned PLine = getLineNumber(Loc);
Eric Christopher78af8fd2012-04-05 22:03:32 +00001372 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1373 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Devang Patel693fcaa2012-02-07 18:40:30 +00001374 llvm::MDNode *PropertyNode =
1375 DBuilder.createObjCProperty(PD->getName(),
Eric Christopher51c03712012-03-29 08:43:37 +00001376 PUnit, PLine,
Eric Christopher78af8fd2012-04-05 22:03:32 +00001377 (Getter && Getter->isImplicit()) ? "" :
Eric Christopherecae5962012-03-29 17:31:33 +00001378 getSelectorName(PD->getGetterName()),
Eric Christopher78af8fd2012-04-05 22:03:32 +00001379 (Setter && Setter->isImplicit()) ? "" :
Eric Christopherecae5962012-03-29 17:31:33 +00001380 getSelectorName(PD->getSetterName()),
Eric Christopher51c03712012-03-29 08:43:37 +00001381 PD->getPropertyAttributes(),
1382 getOrCreateType(PD->getType(), PUnit));
Devang Patel693fcaa2012-02-07 18:40:30 +00001383 EltTys.push_back(PropertyNode);
1384 }
1385
Devang Pateld6c5a262010-02-01 21:52:22 +00001386 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001387 unsigned FieldNo = 0;
Fariborz Jahanian97477392010-10-01 00:01:53 +00001388 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
Fariborz Jahanianfe8fdba2010-10-11 23:55:47 +00001389 Field = Field->getNextIvar(), ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001390 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Douglas Gregora6a28972010-11-30 06:38:09 +00001391 if (!FieldTy.isValid())
1392 return llvm::DIType();
1393
Chris Lattner5f9e2722011-07-23 10:55:15 +00001394 StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001395
Devang Patelde135022009-04-27 22:40:36 +00001396 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +00001397 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +00001398 continue;
1399
Devang Patel9ca36b62009-02-26 21:10:26 +00001400 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +00001401 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1402 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel99c20eb2009-03-20 18:24:39 +00001403 QualType FType = Field->getType();
1404 uint64_t FieldSize = 0;
1405 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +00001406
Devang Patel99c20eb2009-03-20 18:24:39 +00001407 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001408
Devang Patel99c20eb2009-03-20 18:24:39 +00001409 // Bit size, align and offset of the type.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001410 FieldSize = Field->isBitField()
1411 ? Field->getBitWidthValue(CGM.getContext())
1412 : CGM.getContext().getTypeSize(FType);
1413 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001414 }
1415
Eli Friedman77457862012-11-06 23:40:48 +00001416 uint64_t FieldOffset;
1417 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1418 // We don't know the runtime offset of an ivar if we're using the
1419 // non-fragile ABI. For bitfields, use the bit offset into the first
1420 // byte of storage of the bitfield. For other fields, use zero.
1421 if (Field->isBitField()) {
1422 FieldOffset = CGM.getObjCRuntime().ComputeBitfieldBitOffset(
1423 CGM, ID, Field);
1424 FieldOffset %= CGM.getContext().getCharWidth();
1425 } else {
1426 FieldOffset = 0;
1427 }
1428 } else {
1429 FieldOffset = RL.getFieldOffset(FieldNo);
1430 }
Mike Stump1eb44332009-09-09 15:08:12 +00001431
Devang Patelc20482b2009-03-19 00:23:53 +00001432 unsigned Flags = 0;
1433 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Devang Patele2472482010-09-29 21:05:52 +00001434 Flags = llvm::DIDescriptor::FlagProtected;
Devang Patelc20482b2009-03-19 00:23:53 +00001435 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Devang Patele2472482010-09-29 21:05:52 +00001436 Flags = llvm::DIDescriptor::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +00001437
Devang Patel693a70d2012-02-04 01:15:04 +00001438 llvm::MDNode *PropertyNode = NULL;
Devang Patel693fcaa2012-02-07 18:40:30 +00001439 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Devang Patel8c6f9c42011-09-19 18:54:16 +00001440 if (ObjCPropertyImplDecl *PImpD =
Devang Patel693fcaa2012-02-07 18:40:30 +00001441 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
1442 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopher51c03712012-03-29 08:43:37 +00001443 SourceLocation Loc = PD->getLocation();
1444 llvm::DIFile PUnit = getOrCreateFile(Loc);
1445 unsigned PLine = getLineNumber(Loc);
Eric Christopher78af8fd2012-04-05 22:03:32 +00001446 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1447 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
1448 PropertyNode =
1449 DBuilder.createObjCProperty(PD->getName(),
1450 PUnit, PLine,
1451 (Getter && Getter->isImplicit()) ? "" :
Eric Christopherecae5962012-03-29 17:31:33 +00001452 getSelectorName(PD->getGetterName()),
Eric Christopher78af8fd2012-04-05 22:03:32 +00001453 (Setter && Setter->isImplicit()) ? "" :
Eric Christopherecae5962012-03-29 17:31:33 +00001454 getSelectorName(PD->getSetterName()),
Eric Christopher78af8fd2012-04-05 22:03:32 +00001455 PD->getPropertyAttributes(),
1456 getOrCreateType(PD->getType(), PUnit));
Devang Patel53bc5182012-02-08 00:10:20 +00001457 }
Devang Patel693fcaa2012-02-07 18:40:30 +00001458 }
Devang Patel693a70d2012-02-04 01:15:04 +00001459 }
Devang Patelfa936d82011-04-16 00:12:55 +00001460 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit,
1461 FieldLine, FieldSize, FieldAlign,
1462 FieldOffset, Flags, FieldTy,
Devang Patel5f3c7fa2012-02-06 18:20:02 +00001463 PropertyNode);
Devang Patel9ca36b62009-02-26 21:10:26 +00001464 EltTys.push_back(FieldTy);
1465 }
Mike Stump1eb44332009-09-09 15:08:12 +00001466
Jay Foadc556ef22011-04-24 10:11:03 +00001467 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Benjamin Kramer6181e562012-03-20 19:49:14 +00001468 FwdDeclNode->replaceOperandWith(10, Elements);
Eric Christopher9965dea2012-02-16 22:54:45 +00001469
Eric Christopheraa2164c2011-09-29 00:00:45 +00001470 LexicalBlockStack.pop_back();
Benjamin Kramer6181e562012-03-20 19:49:14 +00001471 return llvm::DIType(FwdDeclNode);
Devang Patel9ca36b62009-02-26 21:10:26 +00001472}
1473
Nick Lewyckyd4c100e2011-11-09 04:25:21 +00001474llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty, llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001475 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Bill Wendling30305be2012-11-13 02:31:58 +00001476 int64_t UpperBound = Ty->getNumElements();
Devang Patel6cf37dd2011-04-08 21:56:52 +00001477 int64_t LowerBound = 0;
Bill Wendling30305be2012-11-13 02:31:58 +00001478 if (UpperBound == 0)
Devang Patel6cf37dd2011-04-08 21:56:52 +00001479 // If number of elements are not known then this is an unbounded array.
1480 // Use Low = 1, Hi = 0 to express such arrays.
1481 LowerBound = 1;
1482 else
Bill Wendling30305be2012-11-13 02:31:58 +00001483 --UpperBound;
Devang Patel70c23cd2010-02-23 22:59:39 +00001484
Bill Wendling30305be2012-11-13 02:31:58 +00001485 llvm::Value *Subscript = DBuilder.getOrCreateSubrange(LowerBound, UpperBound,
1486 Ty->getNumElements());
Jay Foadc556ef22011-04-24 10:11:03 +00001487 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Devang Patel70c23cd2010-02-23 22:59:39 +00001488
1489 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1490 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1491
Bill Wendling30305be2012-11-13 02:31:58 +00001492 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
Devang Patel70c23cd2010-02-23 22:59:39 +00001493}
1494
Chris Lattner9c85ba32008-11-10 06:08:34 +00001495llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001496 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001497 uint64_t Size;
1498 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001499
Nuno Lopes010d5142009-01-28 00:35:17 +00001500 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001501 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001502 Size = 0;
1503 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001504 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001505 } else if (Ty->isIncompleteArrayType()) {
1506 Size = 0;
Eric Christopherc7fb7482012-08-07 00:48:43 +00001507 if (Ty->getElementType()->isIncompleteType())
1508 Align = 0;
1509 else
1510 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Devang Patelba690a42011-04-04 23:18:38 +00001511 } else if (Ty->isDependentSizedArrayType() || Ty->isIncompleteType()) {
Devang Patelae503df2011-04-01 19:02:33 +00001512 Size = 0;
1513 Align = 0;
Anders Carlsson835c9092009-01-05 01:23:29 +00001514 } else {
1515 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001516 Size = CGM.getContext().getTypeSize(Ty);
1517 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001518 }
Mike Stump1eb44332009-09-09 15:08:12 +00001519
Chris Lattner9c85ba32008-11-10 06:08:34 +00001520 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1521 // interior arrays, do we care? Why aren't nested arrays represented the
1522 // obvious/recursive way?
Chris Lattner5f9e2722011-07-23 10:55:15 +00001523 SmallVector<llvm::Value *, 8> Subscripts;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001524 QualType EltTy(Ty, 0);
Eric Christophere6d11972012-05-21 22:13:23 +00001525 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1526 int64_t UpperBound = 0;
1527 int64_t LowerBound = 0;
Bill Wendling30305be2012-11-13 02:31:58 +00001528 uint64_t Count = 0;
Eric Christophere6d11972012-05-21 22:13:23 +00001529 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty)) {
Bill Wendling30305be2012-11-13 02:31:58 +00001530 Count = CAT->getSize().getZExtValue();
1531 if (Count)
1532 UpperBound = Count - 1;
Eric Christophere6d11972012-05-21 22:13:23 +00001533 } else
1534 // This is an unbounded array. Use Low = 1, Hi = 0 to express such
1535 // arrays.
1536 LowerBound = 1;
1537
1538 // FIXME: Verify this is right for VLAs.
1539 Subscripts.push_back(DBuilder.getOrCreateSubrange(LowerBound,
Bill Wendling30305be2012-11-13 02:31:58 +00001540 UpperBound,
1541 Count));
Chris Lattner9c85ba32008-11-10 06:08:34 +00001542 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +00001543 }
Mike Stump1eb44332009-09-09 15:08:12 +00001544
Jay Foadc556ef22011-04-24 10:11:03 +00001545 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001546
Devang Patelca80a5f2009-10-20 19:55:01 +00001547 llvm::DIType DbgTy =
Devang Patel16674e82011-02-22 18:56:36 +00001548 DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
Devang Patel823d8e92010-12-08 22:42:58 +00001549 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001550 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001551}
1552
Anders Carlssona031b352009-11-06 19:19:55 +00001553llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001554 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001555 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1556 Ty, Ty->getPointeeType(), Unit);
1557}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001558
Douglas Gregor36b8ee62011-01-22 01:58:15 +00001559llvm::DIType CGDebugInfo::CreateType(const RValueReferenceType *Ty,
1560 llvm::DIFile Unit) {
1561 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type,
1562 Ty, Ty->getPointeeType(), Unit);
1563}
1564
Anders Carlsson20f12a22009-12-06 18:00:51 +00001565llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001566 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001567 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1568 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1569
1570 if (!Ty->getPointeeType()->isFunctionType()) {
1571 // We have a data member pointer type.
1572 return PointerDiffDITy;
1573 }
1574
1575 // We have a member function pointer type. Treat it as a struct with two
1576 // ptrdiff_t members.
1577 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1578
1579 uint64_t FieldOffset = 0;
Devang Patel823d8e92010-12-08 22:42:58 +00001580 llvm::Value *ElementTypes[2];
Anders Carlsson20f12a22009-12-06 18:00:51 +00001581
Eric Christopher3e078812012-08-04 00:11:20 +00001582 // FIXME: This should be a DW_TAG_pointer_to_member type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001583 ElementTypes[0] =
Devang Patel1d323e02011-06-24 22:00:59 +00001584 DBuilder.createMemberType(U, "ptr", U, 0,
Devang Patel823d8e92010-12-08 22:42:58 +00001585 Info.first, Info.second, FieldOffset, 0,
1586 PointerDiffDITy);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001587 FieldOffset += Info.first;
1588
1589 ElementTypes[1] =
Devang Patel1d323e02011-06-24 22:00:59 +00001590 DBuilder.createMemberType(U, "ptr", U, 0,
Devang Patel823d8e92010-12-08 22:42:58 +00001591 Info.first, Info.second, FieldOffset, 0,
1592 PointerDiffDITy);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001593
Jay Foadc556ef22011-04-24 10:11:03 +00001594 llvm::DIArray Elements = DBuilder.getOrCreateArray(ElementTypes);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001595
Chris Lattner5f9e2722011-07-23 10:55:15 +00001596 return DBuilder.createStructType(U, StringRef("test"),
Devang Patel823d8e92010-12-08 22:42:58 +00001597 U, 0, FieldOffset,
1598 0, 0, Elements);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001599}
1600
Eli Friedmanb001de72011-10-06 23:00:33 +00001601llvm::DIType CGDebugInfo::CreateType(const AtomicType *Ty,
1602 llvm::DIFile U) {
1603 // Ignore the atomic wrapping
1604 // FIXME: What is the correct representation?
1605 return getOrCreateType(Ty->getValueType(), U);
1606}
1607
Devang Patel6237cea2010-08-23 22:07:25 +00001608/// CreateEnumType - get enumeration type.
Devang Patel31f7d022011-01-17 22:23:07 +00001609llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED) {
Eli Friedmane6b39bc2012-10-05 01:49:33 +00001610 uint64_t Size = 0;
1611 uint64_t Align = 0;
1612 if (!ED->getTypeForDecl()->isIncompleteType()) {
1613 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1614 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1615 }
1616
1617 // If this is just a forward declaration, construct an appropriately
1618 // marked node and just return it.
1619 if (!ED->getDefinition()) {
1620 llvm::DIDescriptor EDContext;
1621 EDContext = getContextDescriptor(cast<Decl>(ED->getDeclContext()));
1622 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1623 unsigned Line = getLineNumber(ED->getLocation());
1624 StringRef EDName = ED->getName();
1625 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_enumeration_type,
1626 EDName, EDContext, DefUnit, Line, 0,
1627 Size, Align);
1628 }
Devang Patel6237cea2010-08-23 22:07:25 +00001629
1630 // Create DIEnumerator elements for each enumerator.
Eli Friedmane6b39bc2012-10-05 01:49:33 +00001631 SmallVector<llvm::Value *, 16> Enumerators;
1632 ED = ED->getDefinition();
Devang Patel6237cea2010-08-23 22:07:25 +00001633 for (EnumDecl::enumerator_iterator
1634 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
1635 Enum != EnumEnd; ++Enum) {
Devang Patel823d8e92010-12-08 22:42:58 +00001636 Enumerators.push_back(
Devang Patel16674e82011-02-22 18:56:36 +00001637 DBuilder.createEnumerator(Enum->getName(),
Devang Patel823d8e92010-12-08 22:42:58 +00001638 Enum->getInitVal().getZExtValue()));
Devang Patel6237cea2010-08-23 22:07:25 +00001639 }
1640
1641 // Return a CompositeType for the enum itself.
Jay Foadc556ef22011-04-24 10:11:03 +00001642 llvm::DIArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Devang Patel6237cea2010-08-23 22:07:25 +00001643
1644 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1645 unsigned Line = getLineNumber(ED->getLocation());
Devang Patel4bc48872010-10-27 23:23:58 +00001646 llvm::DIDescriptor EnumContext =
John McCall8178df32011-02-22 22:38:33 +00001647 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
Eric Christopher9ee5f462012-05-23 00:09:47 +00001648 llvm::DIType ClassTy = ED->isScopedUsingClassTag() ?
1649 getOrCreateType(ED->getIntegerType(), DefUnit) : llvm::DIType();
Devang Patel6237cea2010-08-23 22:07:25 +00001650 llvm::DIType DbgTy =
Devang Patel16674e82011-02-22 18:56:36 +00001651 DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit, Line,
Eric Christopher9ee5f462012-05-23 00:09:47 +00001652 Size, Align, EltArray,
Eli Friedmane6b39bc2012-10-05 01:49:33 +00001653 ClassTy);
Devang Patel6237cea2010-08-23 22:07:25 +00001654 return DbgTy;
1655}
1656
Douglas Gregor840943d2009-12-21 20:18:30 +00001657static QualType UnwrapTypeForDebugInfo(QualType T) {
1658 do {
1659 QualType LastT = T;
1660 switch (T->getTypeClass()) {
1661 default:
1662 return T;
1663 case Type::TemplateSpecialization:
1664 T = cast<TemplateSpecializationType>(T)->desugar();
1665 break;
John McCallf4c73712011-01-19 06:33:43 +00001666 case Type::TypeOfExpr:
1667 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001668 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001669 case Type::TypeOf:
1670 T = cast<TypeOfType>(T)->getUnderlyingType();
1671 break;
1672 case Type::Decltype:
1673 T = cast<DecltypeType>(T)->getUnderlyingType();
1674 break;
Sean Huntca63c202011-05-24 22:41:36 +00001675 case Type::UnaryTransform:
1676 T = cast<UnaryTransformType>(T)->getUnderlyingType();
1677 break;
John McCall9d156a72011-01-06 01:58:22 +00001678 case Type::Attributed:
1679 T = cast<AttributedType>(T)->getEquivalentType();
John McCall14aa2172011-03-04 04:00:19 +00001680 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001681 case Type::Elaborated:
1682 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001683 break;
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001684 case Type::Paren:
1685 T = cast<ParenType>(T)->getInnerType();
1686 break;
Eric Christopher363e5ac2012-08-07 00:14:25 +00001687 case Type::SubstTemplateTypeParm: {
1688 // We need to keep the qualifiers handy since getReplacementType()
1689 // will strip them away.
1690 unsigned Quals = T.getLocalFastQualifiers();
Douglas Gregor840943d2009-12-21 20:18:30 +00001691 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Eric Christopher363e5ac2012-08-07 00:14:25 +00001692 T.addFastQualifiers(Quals);
1693 }
Douglas Gregor840943d2009-12-21 20:18:30 +00001694 break;
Anders Carlssonebc32792011-03-06 16:43:04 +00001695 case Type::Auto:
1696 T = cast<AutoType>(T)->getDeducedType();
1697 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001698 }
1699
1700 assert(T != LastT && "Type unwrapping failed to unwrap!");
1701 if (T == LastT)
1702 return T;
1703 } while (true);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001704}
1705
Eric Christopher973bbb62011-12-16 23:40:18 +00001706/// getType - Get the type from the cache or return null type if it doesn't exist.
1707llvm::DIType CGDebugInfo::getTypeOrNull(QualType Ty) {
Mike Stump1eb44332009-09-09 15:08:12 +00001708
Douglas Gregor840943d2009-12-21 20:18:30 +00001709 // Unwrap the type as needed for debug information.
1710 Ty = UnwrapTypeForDebugInfo(Ty);
Eric Christopher42e75da2012-02-13 14:56:11 +00001711
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001712 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001713 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001714 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001715 if (it != TypeCache.end()) {
1716 // Verify that the debug info still exists.
Richard Smithe7259aa2012-08-17 04:17:54 +00001717 if (llvm::Value *V = it->second)
1718 return llvm::DIType(cast<llvm::MDNode>(V));
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001719 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001720
Eric Christopher973bbb62011-12-16 23:40:18 +00001721 return llvm::DIType();
1722}
1723
Eric Christopher9965dea2012-02-16 22:54:45 +00001724/// getCompletedTypeOrNull - Get the type from the cache or return null if it
1725/// doesn't exist.
1726llvm::DIType CGDebugInfo::getCompletedTypeOrNull(QualType Ty) {
1727
1728 // Unwrap the type as needed for debug information.
1729 Ty = UnwrapTypeForDebugInfo(Ty);
1730
1731 // Check for existing entry.
1732 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
1733 CompletedTypeCache.find(Ty.getAsOpaquePtr());
1734 if (it != CompletedTypeCache.end()) {
1735 // Verify that the debug info still exists.
Richard Smithe7259aa2012-08-17 04:17:54 +00001736 if (llvm::Value *V = it->second)
1737 return llvm::DIType(cast<llvm::MDNode>(V));
Eric Christopher9965dea2012-02-16 22:54:45 +00001738 }
1739
1740 return llvm::DIType();
1741}
1742
1743
Eric Christopher973bbb62011-12-16 23:40:18 +00001744/// getOrCreateType - Get the type from the cache or create a new
1745/// one if necessary.
1746llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile Unit) {
1747 if (Ty.isNull())
1748 return llvm::DIType();
1749
1750 // Unwrap the type as needed for debug information.
1751 Ty = UnwrapTypeForDebugInfo(Ty);
Eric Christopher363e5ac2012-08-07 00:14:25 +00001752
Eric Christopher9965dea2012-02-16 22:54:45 +00001753 llvm::DIType T = getCompletedTypeOrNull(Ty);
1754
Eric Christopher363e5ac2012-08-07 00:14:25 +00001755 if (T.Verify())
1756 return T;
Eric Christopher973bbb62011-12-16 23:40:18 +00001757
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001758 // Otherwise create the type.
1759 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Eric Christopher7ff0c5d2012-02-18 00:50:17 +00001760
1761 llvm::DIType TC = getTypeOrNull(Ty);
1762 if (TC.Verify() && TC.isForwardDecl())
Michael J. Spencer50e3faa2012-06-08 23:47:12 +00001763 ReplaceMap.push_back(std::make_pair(Ty.getAsOpaquePtr(),
1764 static_cast<llvm::Value*>(TC)));
Eric Christopher9965dea2012-02-16 22:54:45 +00001765
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001766 // And update the type cache.
Eric Christopher9965dea2012-02-16 22:54:45 +00001767 TypeCache[Ty.getAsOpaquePtr()] = Res;
1768
1769 if (!Res.isForwardDecl())
1770 CompletedTypeCache[Ty.getAsOpaquePtr()] = Res;
Eric Christopher363e5ac2012-08-07 00:14:25 +00001771
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001772 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001773}
1774
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001775/// CreateTypeNode - Create a new debug type node.
Nick Lewycky7b3819d2011-11-09 04:27:23 +00001776llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001777 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001778 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001779 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001780
Douglas Gregor2101a822009-12-21 19:57:21 +00001781 const char *Diag = 0;
1782
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001783 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001784 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001785#define TYPE(Class, Base)
1786#define ABSTRACT_TYPE(Class, Base)
1787#define NON_CANONICAL_TYPE(Class, Base)
1788#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1789#include "clang/AST/TypeNodes.def"
David Blaikieb219cfc2011-09-23 05:06:16 +00001790 llvm_unreachable("Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001791
Anders Carlssonbfe69952009-11-06 18:24:04 +00001792 case Type::ExtVector:
Devang Patel70c23cd2010-02-23 22:59:39 +00001793 case Type::Vector:
1794 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001795 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001796 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCallc12c5bb2010-05-15 11:32:37 +00001797 case Type::ObjCObject:
1798 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001799 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001800 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
Nick Lewyckyd4c100e2011-11-09 04:25:21 +00001801 case Type::Builtin:
1802 return CreateType(cast<BuiltinType>(Ty));
1803 case Type::Complex:
1804 return CreateType(cast<ComplexType>(Ty));
1805 case Type::Pointer:
1806 return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001807 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001808 return CreateType(cast<BlockPointerType>(Ty), Unit);
Nick Lewyckyd4c100e2011-11-09 04:25:21 +00001809 case Type::Typedef:
1810 return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001811 case Type::Record:
Nick Lewyckyd4c100e2011-11-09 04:25:21 +00001812 return CreateType(cast<RecordType>(Ty));
Douglas Gregor72564e72009-02-26 23:50:07 +00001813 case Type::Enum:
Nick Lewyckyd4c100e2011-11-09 04:25:21 +00001814 return CreateEnumType(cast<EnumType>(Ty)->getDecl());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001815 case Type::FunctionProto:
1816 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001817 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001818 case Type::ConstantArray:
1819 case Type::VariableArray:
1820 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001821 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001822
1823 case Type::LValueReference:
1824 return CreateType(cast<LValueReferenceType>(Ty), Unit);
Douglas Gregor36b8ee62011-01-22 01:58:15 +00001825 case Type::RValueReference:
1826 return CreateType(cast<RValueReferenceType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001827
Anders Carlsson20f12a22009-12-06 18:00:51 +00001828 case Type::MemberPointer:
1829 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001830
Eli Friedmanb001de72011-10-06 23:00:33 +00001831 case Type::Atomic:
1832 return CreateType(cast<AtomicType>(Ty), Unit);
1833
John McCall9d156a72011-01-06 01:58:22 +00001834 case Type::Attributed:
Douglas Gregor2101a822009-12-21 19:57:21 +00001835 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001836 case Type::Elaborated:
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001837 case Type::Paren:
Douglas Gregor2101a822009-12-21 19:57:21 +00001838 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001839 case Type::TypeOfExpr:
1840 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001841 case Type::Decltype:
Sean Huntca63c202011-05-24 22:41:36 +00001842 case Type::UnaryTransform:
Richard Smith34b41d92011-02-20 03:19:35 +00001843 case Type::Auto:
Douglas Gregor840943d2009-12-21 20:18:30 +00001844 llvm_unreachable("type should have been unwrapped!");
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001845 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001846
1847 assert(Diag && "Fall through without a diagnostic?");
David Blaikied6471f72011-09-25 23:23:43 +00001848 unsigned DiagID = CGM.getDiags().getCustomDiagID(DiagnosticsEngine::Error,
Douglas Gregor2101a822009-12-21 19:57:21 +00001849 "debug information for %0 is not yet supported");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00001850 CGM.getDiags().Report(DiagID)
Douglas Gregor2101a822009-12-21 19:57:21 +00001851 << Diag;
1852 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001853}
1854
Eric Christopher9965dea2012-02-16 22:54:45 +00001855/// getOrCreateLimitedType - Get the type from the cache or create a new
1856/// limited type if necessary.
1857llvm::DIType CGDebugInfo::getOrCreateLimitedType(QualType Ty,
1858 llvm::DIFile Unit) {
1859 if (Ty.isNull())
1860 return llvm::DIType();
1861
1862 // Unwrap the type as needed for debug information.
1863 Ty = UnwrapTypeForDebugInfo(Ty);
1864
1865 llvm::DIType T = getTypeOrNull(Ty);
1866
1867 // We may have cached a forward decl when we could have created
1868 // a non-forward decl. Go ahead and create a non-forward decl
1869 // now.
1870 if (T.Verify() && !T.isForwardDecl()) return T;
1871
1872 // Otherwise create the type.
1873 llvm::DIType Res = CreateLimitedTypeNode(Ty, Unit);
1874
Eric Christopher7ff0c5d2012-02-18 00:50:17 +00001875 if (T.Verify() && T.isForwardDecl())
Michael J. Spencer50e3faa2012-06-08 23:47:12 +00001876 ReplaceMap.push_back(std::make_pair(Ty.getAsOpaquePtr(),
1877 static_cast<llvm::Value*>(T)));
Eric Christopher7ff0c5d2012-02-18 00:50:17 +00001878
Eric Christopher9965dea2012-02-16 22:54:45 +00001879 // And update the type cache.
1880 TypeCache[Ty.getAsOpaquePtr()] = Res;
1881 return Res;
1882}
1883
1884// TODO: Currently used for context chains when limiting debug info.
1885llvm::DIType CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
1886 RecordDecl *RD = Ty->getDecl();
1887
1888 // Get overall information about the record type for the debug info.
1889 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
1890 unsigned Line = getLineNumber(RD->getLocation());
David Blaikie70ae1222012-11-02 23:40:00 +00001891 StringRef RDName = getClassName(RD);
Eric Christopher9965dea2012-02-16 22:54:45 +00001892
1893 llvm::DIDescriptor RDContext;
Douglas Gregor4cdad312012-10-23 20:05:01 +00001894 if (CGM.getCodeGenOpts().getDebugInfo() == CodeGenOptions::LimitedDebugInfo)
Eric Christopher9965dea2012-02-16 22:54:45 +00001895 RDContext = createContextChain(cast<Decl>(RD->getDeclContext()));
1896 else
1897 RDContext = getContextDescriptor(cast<Decl>(RD->getDeclContext()));
1898
1899 // If this is just a forward declaration, construct an appropriately
1900 // marked node and just return it.
Eric Christopher7ff0c5d2012-02-18 00:50:17 +00001901 if (!RD->getDefinition())
1902 return createRecordFwdDecl(RD, RDContext);
Eric Christopher9965dea2012-02-16 22:54:45 +00001903
1904 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1905 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1906 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Benjamin Kramer6181e562012-03-20 19:49:14 +00001907 llvm::TrackingVH<llvm::MDNode> RealDecl;
Eric Christopher9965dea2012-02-16 22:54:45 +00001908
1909 if (RD->isUnion())
1910 RealDecl = DBuilder.createUnionType(RDContext, RDName, DefUnit, Line,
1911 Size, Align, 0, llvm::DIArray());
David Blaikie70ae1222012-11-02 23:40:00 +00001912 else if (RD->isClass()) {
Eric Christopher9965dea2012-02-16 22:54:45 +00001913 // FIXME: This could be a struct type giving a default visibility different
1914 // than C++ class type, but needs llvm metadata changes first.
1915 RealDecl = DBuilder.createClassType(RDContext, RDName, DefUnit, Line,
1916 Size, Align, 0, 0, llvm::DIType(),
Eric Christopher86211df2012-02-20 18:05:24 +00001917 llvm::DIArray(), llvm::DIType(),
Eric Christopher9965dea2012-02-16 22:54:45 +00001918 llvm::DIArray());
1919 } else
1920 RealDecl = DBuilder.createStructType(RDContext, RDName, DefUnit, Line,
1921 Size, Align, 0, llvm::DIArray());
1922
1923 RegionMap[Ty->getDecl()] = llvm::WeakVH(RealDecl);
1924 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = llvm::DIType(RealDecl);
1925
1926 if (CXXDecl) {
1927 // A class's primary base or the class itself contains the vtable.
1928 llvm::MDNode *ContainingType = NULL;
1929 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1930 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
1931 // Seek non virtual primary base root.
1932 while (1) {
1933 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
1934 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
1935 if (PBT && !BRL.isPrimaryBaseVirtual())
1936 PBase = PBT;
1937 else
1938 break;
1939 }
1940 ContainingType =
1941 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), DefUnit);
1942 }
1943 else if (CXXDecl->isDynamicClass())
1944 ContainingType = RealDecl;
1945
Eric Christopher1e009d52012-02-17 07:09:48 +00001946 RealDecl->replaceOperandWith(12, ContainingType);
Eric Christopher9965dea2012-02-16 22:54:45 +00001947 }
1948 return llvm::DIType(RealDecl);
1949}
1950
1951/// CreateLimitedTypeNode - Create a new debug type node, but only forward
1952/// declare composite types that haven't been processed yet.
1953llvm::DIType CGDebugInfo::CreateLimitedTypeNode(QualType Ty,llvm::DIFile Unit) {
1954
1955 // Work out details of type.
1956 switch (Ty->getTypeClass()) {
1957#define TYPE(Class, Base)
1958#define ABSTRACT_TYPE(Class, Base)
1959#define NON_CANONICAL_TYPE(Class, Base)
1960#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1961 #include "clang/AST/TypeNodes.def"
1962 llvm_unreachable("Dependent types cannot show up in debug information");
1963
1964 case Type::Record:
1965 return CreateLimitedType(cast<RecordType>(Ty));
1966 default:
1967 return CreateTypeNode(Ty, Unit);
1968 }
1969}
1970
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001971/// CreateMemberType - Create new member and increase Offset by FType's size.
1972llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001973 StringRef Name,
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001974 uint64_t *Offset) {
1975 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1976 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1977 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel1d323e02011-06-24 22:00:59 +00001978 llvm::DIType Ty = DBuilder.createMemberType(Unit, Name, Unit, 0,
Devang Patel823d8e92010-12-08 22:42:58 +00001979 FieldSize, FieldAlign,
1980 *Offset, 0, FieldTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001981 *Offset += FieldSize;
1982 return Ty;
1983}
1984
Devang Patel120bf322011-04-23 00:08:01 +00001985/// getFunctionDeclaration - Return debug info descriptor to describe method
1986/// declaration for the given method definition.
1987llvm::DISubprogram CGDebugInfo::getFunctionDeclaration(const Decl *D) {
1988 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1989 if (!FD) return llvm::DISubprogram();
1990
1991 // Setup context.
1992 getContextDescriptor(cast<Decl>(D->getDeclContext()));
1993
Devang Patel22a5cdf2011-04-29 23:42:32 +00001994 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
Eric Christopherdeae6a82011-11-17 23:45:00 +00001995 MI = SPCache.find(FD->getCanonicalDecl());
Devang Patel22a5cdf2011-04-29 23:42:32 +00001996 if (MI != SPCache.end()) {
Richard Smithe7259aa2012-08-17 04:17:54 +00001997 llvm::Value *V = MI->second;
1998 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(V));
Devang Patel22a5cdf2011-04-29 23:42:32 +00001999 if (SP.isSubprogram() && !llvm::DISubprogram(SP).isDefinition())
2000 return SP;
2001 }
2002
Devang Patel120bf322011-04-23 00:08:01 +00002003 for (FunctionDecl::redecl_iterator I = FD->redecls_begin(),
2004 E = FD->redecls_end(); I != E; ++I) {
2005 const FunctionDecl *NextFD = *I;
2006 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
Eric Christopherdeae6a82011-11-17 23:45:00 +00002007 MI = SPCache.find(NextFD->getCanonicalDecl());
Devang Patel120bf322011-04-23 00:08:01 +00002008 if (MI != SPCache.end()) {
Richard Smithe7259aa2012-08-17 04:17:54 +00002009 llvm::Value *V = MI->second;
2010 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(V));
Devang Patel120bf322011-04-23 00:08:01 +00002011 if (SP.isSubprogram() && !llvm::DISubprogram(SP).isDefinition())
2012 return SP;
2013 }
2014 }
2015 return llvm::DISubprogram();
2016}
2017
Devang Patel1c296522011-05-31 20:46:46 +00002018// getOrCreateFunctionType - Construct DIType. If it is a c++ method, include
2019// implicit parameter "this".
Eric Christopher85f90bd2012-09-11 01:36:56 +00002020llvm::DIType CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Eric Christopherab5278e2011-10-11 23:00:51 +00002021 QualType FnType,
Devang Patel1c296522011-05-31 20:46:46 +00002022 llvm::DIFile F) {
Eric Christopher363e5ac2012-08-07 00:14:25 +00002023
Devang Patel1c296522011-05-31 20:46:46 +00002024 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2025 return getOrCreateMethodType(Method, F);
Nick Lewycky7480d962011-11-10 00:34:02 +00002026 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
Devang Patelc478f212011-05-31 21:18:50 +00002027 // Add "self" and "_cmd"
Chris Lattner5f9e2722011-07-23 10:55:15 +00002028 SmallVector<llvm::Value *, 16> Elts;
Devang Patelc478f212011-05-31 21:18:50 +00002029
2030 // First element is always return type. For 'void' functions it is NULL.
Devang Pateld127bcb2011-05-31 22:21:11 +00002031 Elts.push_back(getOrCreateType(OMethod->getResultType(), F));
Devang Patelc478f212011-05-31 21:18:50 +00002032 // "self" pointer is always first argument.
Eric Christopher3ed6b912012-09-11 01:36:54 +00002033 llvm::DIType SelfTy = getOrCreateType(OMethod->getSelfDecl()->getType(), F);
Eric Christopherd5a73dc2012-09-12 23:36:49 +00002034 Elts.push_back(DBuilder.createObjectPointerType(SelfTy));
Eric Christopher85f90bd2012-09-11 01:36:56 +00002035 // "_cmd" pointer is always second argument.
Eric Christopher3ed6b912012-09-11 01:36:54 +00002036 llvm::DIType CmdTy = getOrCreateType(OMethod->getCmdDecl()->getType(), F);
2037 Elts.push_back(DBuilder.createArtificialType(CmdTy));
Devang Pateld127bcb2011-05-31 22:21:11 +00002038 // Get rest of the arguments.
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002039 for (ObjCMethodDecl::param_const_iterator PI = OMethod->param_begin(),
Devang Pateld127bcb2011-05-31 22:21:11 +00002040 PE = OMethod->param_end(); PI != PE; ++PI)
2041 Elts.push_back(getOrCreateType((*PI)->getType(), F));
2042
Devang Patelc478f212011-05-31 21:18:50 +00002043 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(Elts);
2044 return DBuilder.createSubroutineType(F, EltTypeArray);
2045 }
Devang Patel1c296522011-05-31 20:46:46 +00002046 return getOrCreateType(FnType, F);
2047}
2048
Eric Christopher451b4412012-03-20 23:28:32 +00002049/// EmitFunctionStart - Constructs the debug code for entering a function.
Devang Patel9c6c3a02010-01-14 00:36:21 +00002050void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00002051 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00002052 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00002053
Chris Lattner5f9e2722011-07-23 10:55:15 +00002054 StringRef Name;
2055 StringRef LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00002056
Eric Christopheraa2164c2011-09-29 00:00:45 +00002057 FnBeginRegionCount.push_back(LexicalBlockStack.size());
Devang Patel5a6fbcf2010-07-22 22:29:16 +00002058
Devang Patel9c6c3a02010-01-14 00:36:21 +00002059 const Decl *D = GD.getDecl();
Alexey Samsonov34b41f82012-10-25 10:18:50 +00002060 // Function may lack declaration in source code if it is created by Clang
2061 // CodeGen (examples: _GLOBAL__I_a, __cxx_global_array_dtor, thunk).
2062 bool HasDecl = (D != 0);
Eric Christopherea320472012-04-03 00:44:15 +00002063 // Use the location of the declaration.
Alexey Samsonov34b41f82012-10-25 10:18:50 +00002064 SourceLocation Loc;
2065 if (HasDecl)
2066 Loc = D->getLocation();
2067
Devang Patel3951e712010-10-07 22:03:49 +00002068 unsigned Flags = 0;
Eric Christopherea320472012-04-03 00:44:15 +00002069 llvm::DIFile Unit = getOrCreateFile(Loc);
Devang Patel0692f832010-10-11 21:58:41 +00002070 llvm::DIDescriptor FDContext(Unit);
Devang Patel5ecb1df2011-04-05 22:54:11 +00002071 llvm::DIArray TParamsArray;
Alexey Samsonov34b41f82012-10-25 10:18:50 +00002072 if (!HasDecl) {
2073 // Use llvm function name.
2074 Name = Fn->getName();
2075 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Eric Christopherbf979472011-11-14 18:55:02 +00002076 // If there is a DISubprogram for this function available then use it.
Devang Patel4125fd22010-01-19 01:54:44 +00002077 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
Eric Christopherdeae6a82011-11-17 23:45:00 +00002078 FI = SPCache.find(FD->getCanonicalDecl());
Devang Patel4125fd22010-01-19 01:54:44 +00002079 if (FI != SPCache.end()) {
Richard Smithe7259aa2012-08-17 04:17:54 +00002080 llvm::Value *V = FI->second;
2081 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(V));
Devang Patelab699792010-05-07 18:12:35 +00002082 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
2083 llvm::MDNode *SPN = SP;
Eric Christopheraa2164c2011-09-29 00:00:45 +00002084 LexicalBlockStack.push_back(SPN);
Devang Patelab699792010-05-07 18:12:35 +00002085 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00002086 return;
2087 }
2088 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00002089 Name = getFunctionName(FD);
2090 // Use mangled name as linkage name for c/c++ functions.
Eric Christopher43443de2012-04-12 00:35:06 +00002091 if (FD->hasPrototype()) {
Devang Patel2df74c02011-05-02 22:37:48 +00002092 LinkageName = CGM.getMangledName(GD);
Eric Christopher43443de2012-04-12 00:35:06 +00002093 Flags |= llvm::DIDescriptor::FlagPrototyped;
2094 }
Alexey Samsonovfd00eec2012-05-04 07:39:27 +00002095 if (LinkageName == Name ||
Douglas Gregor4cdad312012-10-23 20:05:01 +00002096 CGM.getCodeGenOpts().getDebugInfo() <= CodeGenOptions::DebugLineTablesOnly)
Chris Lattner5f9e2722011-07-23 10:55:15 +00002097 LinkageName = StringRef();
Eric Christopher43443de2012-04-12 00:35:06 +00002098
Douglas Gregor4cdad312012-10-23 20:05:01 +00002099 if (CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo) {
Alexey Samsonovfd00eec2012-05-04 07:39:27 +00002100 if (const NamespaceDecl *NSDecl =
2101 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2102 FDContext = getOrCreateNameSpace(NSDecl);
2103 else if (const RecordDecl *RDecl =
2104 dyn_cast_or_null<RecordDecl>(FD->getDeclContext()))
2105 FDContext = getContextDescriptor(cast<Decl>(RDecl->getDeclContext()));
Devang Patel5ecb1df2011-04-05 22:54:11 +00002106
Alexey Samsonovfd00eec2012-05-04 07:39:27 +00002107 // Collect template parameters.
2108 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2109 }
David Chisnall70b9b442010-09-02 17:16:32 +00002110 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
David Chisnall52044a22010-09-02 18:01:51 +00002111 Name = getObjCMethodName(OMD);
Devang Patel3951e712010-10-07 22:03:49 +00002112 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00002113 } else {
Devang Patel58faf202010-10-22 17:11:50 +00002114 // Use llvm function name.
Devang Patel9c6c3a02010-01-14 00:36:21 +00002115 Name = Fn->getName();
Devang Patel3951e712010-10-07 22:03:49 +00002116 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00002117 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00002118 if (!Name.empty() && Name[0] == '\01')
2119 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002120
Eric Christopherea320472012-04-03 00:44:15 +00002121 unsigned LineNo = getLineNumber(Loc);
Alexey Samsonov34b41f82012-10-25 10:18:50 +00002122 if (!HasDecl || D->isImplicit())
Devang Patele2472482010-09-29 21:05:52 +00002123 Flags |= llvm::DIDescriptor::FlagArtificial;
Eric Christopherea320472012-04-03 00:44:15 +00002124
Alexey Samsonovfd00eec2012-05-04 07:39:27 +00002125 llvm::DIType DIFnType;
2126 llvm::DISubprogram SPDecl;
Alexey Samsonov34b41f82012-10-25 10:18:50 +00002127 if (HasDecl &&
2128 CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo) {
Alexey Samsonovfd00eec2012-05-04 07:39:27 +00002129 DIFnType = getOrCreateFunctionType(D, FnType, Unit);
2130 SPDecl = getFunctionDeclaration(D);
2131 } else {
2132 // Create fake but valid subroutine type. Otherwise
2133 // llvm::DISubprogram::Verify() would return false, and
2134 // subprogram DIE will miss DW_AT_decl_file and
2135 // DW_AT_decl_line fields.
2136 SmallVector<llvm::Value*, 16> Elts;
2137 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(Elts);
2138 DIFnType = DBuilder.createSubroutineType(Unit, EltTypeArray);
2139 }
2140 llvm::DISubprogram SP;
2141 SP = DBuilder.createFunction(FDContext, Name, LinkageName, Unit,
2142 LineNo, DIFnType,
2143 Fn->hasInternalLinkage(), true/*definition*/,
2144 getLineNumber(CurLoc), Flags,
2145 CGM.getLangOpts().Optimize,
2146 Fn, TParamsArray, SPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002147
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00002148 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00002149 llvm::MDNode *SPN = SP;
Eric Christopheraa2164c2011-09-29 00:00:45 +00002150 LexicalBlockStack.push_back(SPN);
Alexey Samsonov34b41f82012-10-25 10:18:50 +00002151 if (HasDecl)
2152 RegionMap[D] = llvm::WeakVH(SP);
Eric Christopher69a1b742011-09-29 00:00:37 +00002153}
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00002154
Eric Christopher5321bc42011-09-29 00:00:41 +00002155/// EmitLocation - Emit metadata to indicate a change in line/column
2156/// information in the source file.
Eric Christopher73fb3502011-10-13 21:45:18 +00002157void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
2158
2159 // Update our current location
2160 setLocation(Loc);
2161
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00002162 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00002163
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00002164 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00002165 SourceManager &SM = CGM.getContext().getSourceManager();
Eric Christopher73fb3502011-10-13 21:45:18 +00002166 if (CurLoc == PrevLoc ||
Chandler Carruth40278532011-07-25 16:49:02 +00002167 SM.getExpansionLoc(CurLoc) == SM.getExpansionLoc(PrevLoc))
Devang Patel4800ea62010-04-05 21:09:15 +00002168 // New Builder may not be in sync with CGDebugInfo.
2169 if (!Builder.getCurrentDebugLocation().isUnknown())
2170 return;
Eric Christopher414ee4b2011-09-29 00:00:35 +00002171
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00002172 // Update last state.
2173 PrevLoc = CurLoc;
2174
Eric Christopheraa2164c2011-09-29 00:00:45 +00002175 llvm::MDNode *Scope = LexicalBlockStack.back();
Devang Patel8ab870d2010-05-12 23:46:38 +00002176 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
2177 getColumnNumber(CurLoc),
Chris Lattnere541d012010-04-02 20:21:43 +00002178 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00002179}
2180
Eric Christopher73fb3502011-10-13 21:45:18 +00002181/// CreateLexicalBlock - Creates a new lexical block node and pushes it on
2182/// the stack.
2183void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Devang Patel8fae0602009-11-13 19:10:24 +00002184 llvm::DIDescriptor D =
Eric Christopher73fb3502011-10-13 21:45:18 +00002185 DBuilder.createLexicalBlock(LexicalBlockStack.empty() ?
Devang Patel53bc5182012-02-08 00:10:20 +00002186 llvm::DIDescriptor() :
2187 llvm::DIDescriptor(LexicalBlockStack.back()),
2188 getOrCreateFile(CurLoc),
2189 getLineNumber(CurLoc),
2190 getColumnNumber(CurLoc));
Devang Patelab699792010-05-07 18:12:35 +00002191 llvm::MDNode *DN = D;
Eric Christopheraa2164c2011-09-29 00:00:45 +00002192 LexicalBlockStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00002193}
2194
Eric Christopher73fb3502011-10-13 21:45:18 +00002195/// EmitLexicalBlockStart - Constructs the debug code for entering a declarative
2196/// region - beginning of a DW_TAG_lexical_block.
2197void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc) {
2198 // Set our current location.
2199 setLocation(Loc);
2200
2201 // Create a new lexical block and push it on the stack.
2202 CreateLexicalBlock(Loc);
2203
2204 // Emit a line table change for the current location inside the new scope.
2205 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(Loc),
Devang Patel53bc5182012-02-08 00:10:20 +00002206 getColumnNumber(Loc),
2207 LexicalBlockStack.back()));
Eric Christopher73fb3502011-10-13 21:45:18 +00002208}
2209
Eric Christopheraa2164c2011-09-29 00:00:45 +00002210/// EmitLexicalBlockEnd - Constructs the debug code for exiting a declarative
Eric Christopher43202ae2011-09-26 15:03:22 +00002211/// region - end of a DW_TAG_lexical_block.
Eric Christopher73fb3502011-10-13 21:45:18 +00002212void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc) {
Eric Christopheraa2164c2011-09-29 00:00:45 +00002213 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherc852e9f2012-07-11 15:32:13 +00002214
2215 // Provide an entry in the line table for the end of the block.
2216 EmitLocation(Builder, Loc);
2217
Eric Christopheraa2164c2011-09-29 00:00:45 +00002218 LexicalBlockStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00002219}
2220
Devang Patel5a6fbcf2010-07-22 22:29:16 +00002221/// EmitFunctionEnd - Constructs the debug code for exiting a function.
2222void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
Eric Christopheraa2164c2011-09-29 00:00:45 +00002223 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Devang Patel5a6fbcf2010-07-22 22:29:16 +00002224 unsigned RCount = FnBeginRegionCount.back();
Eric Christopheraa2164c2011-09-29 00:00:45 +00002225 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
Devang Patel5a6fbcf2010-07-22 22:29:16 +00002226
2227 // Pop all regions for this function.
Eric Christopheraa2164c2011-09-29 00:00:45 +00002228 while (LexicalBlockStack.size() != RCount)
Eric Christopher73fb3502011-10-13 21:45:18 +00002229 EmitLexicalBlockEnd(Builder, CurLoc);
Devang Patel5a6fbcf2010-07-22 22:29:16 +00002230 FnBeginRegionCount.pop_back();
2231}
2232
Devang Patel809b9bb2010-02-10 18:49:08 +00002233// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
2234// See BuildByRefType.
2235llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
2236 uint64_t *XOffset) {
2237
Chris Lattner5f9e2722011-07-23 10:55:15 +00002238 SmallVector<llvm::Value *, 5> EltTys;
Devang Patel809b9bb2010-02-10 18:49:08 +00002239 QualType FType;
2240 uint64_t FieldSize, FieldOffset;
2241 unsigned FieldAlign;
2242
Devang Patel17800552010-03-09 00:44:50 +00002243 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00002244 QualType Type = VD->getType();
2245
2246 FieldOffset = 0;
2247 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00002248 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2249 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00002250 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00002251 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2252 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2253
John McCall6b5a61b2011-02-07 10:33:21 +00002254 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type);
Devang Patel809b9bb2010-02-10 18:49:08 +00002255 if (HasCopyAndDispose) {
2256 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00002257 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
2258 &FieldOffset));
2259 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
2260 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00002261 }
2262
2263 CharUnits Align = CGM.getContext().getDeclAlign(VD);
Ken Dyck573be632011-04-22 17:34:18 +00002264 if (Align > CGM.getContext().toCharUnitsFromBits(
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002265 CGM.getContext().getTargetInfo().getPointerAlign(0))) {
Ken Dyck573be632011-04-22 17:34:18 +00002266 CharUnits FieldOffsetInBytes
2267 = CGM.getContext().toCharUnitsFromBits(FieldOffset);
2268 CharUnits AlignedOffsetInBytes
2269 = FieldOffsetInBytes.RoundUpToAlignment(Align);
2270 CharUnits NumPaddingBytes
2271 = AlignedOffsetInBytes - FieldOffsetInBytes;
Devang Patel809b9bb2010-02-10 18:49:08 +00002272
Ken Dyck573be632011-04-22 17:34:18 +00002273 if (NumPaddingBytes.isPositive()) {
2274 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
Devang Patel809b9bb2010-02-10 18:49:08 +00002275 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2276 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00002277 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00002278 }
2279 }
2280
2281 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00002282 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00002283 FieldSize = CGM.getContext().getTypeSize(FType);
Ken Dyck573be632011-04-22 17:34:18 +00002284 FieldAlign = CGM.getContext().toBits(Align);
Devang Patel809b9bb2010-02-10 18:49:08 +00002285
2286 *XOffset = FieldOffset;
Devang Patel1d323e02011-06-24 22:00:59 +00002287 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit,
Devang Patel823d8e92010-12-08 22:42:58 +00002288 0, FieldSize, FieldAlign,
2289 FieldOffset, 0, FieldTy);
Devang Patel809b9bb2010-02-10 18:49:08 +00002290 EltTys.push_back(FieldTy);
2291 FieldOffset += FieldSize;
2292
Jay Foadc556ef22011-04-24 10:11:03 +00002293 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Devang Patel809b9bb2010-02-10 18:49:08 +00002294
Devang Patele2472482010-09-29 21:05:52 +00002295 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Devang Patel809b9bb2010-02-10 18:49:08 +00002296
Devang Patel16674e82011-02-22 18:56:36 +00002297 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Devang Patel823d8e92010-12-08 22:42:58 +00002298 Elements);
Devang Patel809b9bb2010-02-10 18:49:08 +00002299}
Devang Patel823d8e92010-12-08 22:42:58 +00002300
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00002301/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00002302void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Devang Patel093ac462011-03-03 20:13:15 +00002303 llvm::Value *Storage,
2304 unsigned ArgNo, CGBuilderTy &Builder) {
Douglas Gregor4cdad312012-10-23 20:05:01 +00002305 assert(CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo);
Eric Christopheraa2164c2011-09-29 00:00:45 +00002306 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Daniel Dunbar5273f512008-10-17 01:07:56 +00002307
Devang Patel17800552010-03-09 00:44:50 +00002308 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00002309 llvm::DIType Ty;
2310 uint64_t XOffset = 0;
2311 if (VD->hasAttr<BlocksAttr>())
2312 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
2313 else
2314 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00002315
Eric Christopher195ff582012-09-19 21:47:29 +00002316 // If there is no debug info for this type then do not emit debug info
Devang Patelf4e54a22010-05-07 23:05:55 +00002317 // for this variable.
2318 if (!Ty)
2319 return;
2320
Devang Patel34753802011-02-16 01:11:51 +00002321 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage)) {
2322 // If Storage is an aggregate returned as 'sret' then let debugger know
2323 // about this.
Devang Patel0691f932011-02-10 00:40:52 +00002324 if (Arg->hasStructRetAttr())
Eric Christopher37e4cea2012-05-19 01:36:50 +00002325 Ty = DBuilder.createReferenceType(llvm::dwarf::DW_TAG_reference_type, Ty);
Devang Patel34753802011-02-16 01:11:51 +00002326 else if (CXXRecordDecl *Record = VD->getType()->getAsCXXRecordDecl()) {
2327 // If an aggregate variable has non trivial destructor or non trivial copy
2328 // constructor than it is pass indirectly. Let debug info know about this
2329 // by using reference of the aggregate type as a argument type.
Eric Christopherab5278e2011-10-11 23:00:51 +00002330 if (!Record->hasTrivialCopyConstructor() ||
2331 !Record->hasTrivialDestructor())
Eric Christopher37e4cea2012-05-19 01:36:50 +00002332 Ty = DBuilder.createReferenceType(llvm::dwarf::DW_TAG_reference_type, Ty);
Devang Patel34753802011-02-16 01:11:51 +00002333 }
2334 }
Devang Patel0691f932011-02-10 00:40:52 +00002335
Chris Lattner9c85ba32008-11-10 06:08:34 +00002336 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00002337 unsigned Line = getLineNumber(VD->getLocation());
2338 unsigned Column = getColumnNumber(VD->getLocation());
Devang Patelaca745b2010-09-29 23:09:21 +00002339 unsigned Flags = 0;
2340 if (VD->isImplicit())
2341 Flags |= llvm::DIDescriptor::FlagArtificial;
Eric Christopherd5a73dc2012-09-12 23:36:49 +00002342 // If this is the first argument and it is implicit then
2343 // give it an object pointer flag.
2344 // FIXME: There has to be a better way to do this, but for static
2345 // functions there won't be an implicit param at arg1 and
2346 // otherwise it is 'self' or 'this'.
2347 if (isa<ImplicitParamDecl>(VD) && ArgNo == 1)
2348 Flags |= llvm::DIDescriptor::FlagObjectPointer;
2349
Eric Christopheraa2164c2011-09-29 00:00:45 +00002350 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christopherd5a73dc2012-09-12 23:36:49 +00002351
Chris Lattner5f9e2722011-07-23 10:55:15 +00002352 StringRef Name = VD->getName();
Devang Patelcebbedd2010-10-12 23:24:54 +00002353 if (!Name.empty()) {
Devang Patelb1fd0eb2011-01-11 00:30:27 +00002354 if (VD->hasAttr<BlocksAttr>()) {
2355 CharUnits offset = CharUnits::fromQuantity(32);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002356 SmallVector<llvm::Value *, 9> addr;
Chris Lattner8b418682012-02-07 00:39:47 +00002357 llvm::Type *Int64Ty = CGM.Int64Ty;
Devang Patel4a4e2ef2011-02-18 23:29:22 +00002358 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Devang Patelb1fd0eb2011-01-11 00:30:27 +00002359 // offset of __forwarding field
Ken Dyck0ebce0e2011-04-22 17:41:34 +00002360 offset = CGM.getContext().toCharUnitsFromBits(
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002361 CGM.getContext().getTargetInfo().getPointerWidth(0));
Devang Patelb1fd0eb2011-01-11 00:30:27 +00002362 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Devang Patel4a4e2ef2011-02-18 23:29:22 +00002363 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
2364 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Devang Patelb1fd0eb2011-01-11 00:30:27 +00002365 // offset of x field
Ken Dyck0ebce0e2011-04-22 17:41:34 +00002366 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Devang Patelb1fd0eb2011-01-11 00:30:27 +00002367 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
2368
2369 // Create the descriptor for the variable.
2370 llvm::DIVariable D =
Devang Patel16674e82011-02-22 18:56:36 +00002371 DBuilder.createComplexVariable(Tag,
Eric Christopherab5278e2011-10-11 23:00:51 +00002372 llvm::DIDescriptor(Scope),
Devang Patelb1fd0eb2011-01-11 00:30:27 +00002373 VD->getName(), Unit, Line, Ty,
Jay Foadc556ef22011-04-24 10:11:03 +00002374 addr, ArgNo);
Devang Patelb1fd0eb2011-01-11 00:30:27 +00002375
2376 // Insert an llvm.dbg.declare into the current block.
2377 llvm::Instruction *Call =
Devang Patel16674e82011-02-22 18:56:36 +00002378 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patelb1fd0eb2011-01-11 00:30:27 +00002379 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
2380 return;
Eric Christophera135f2c2012-05-08 18:56:47 +00002381 } else if (isa<VariableArrayType>(VD->getType())) {
2382 // These are "complex" variables in that they need an op_deref.
Devang Patelb1fd0eb2011-01-11 00:30:27 +00002383 // Create the descriptor for the variable.
Eric Christophera135f2c2012-05-08 18:56:47 +00002384 llvm::Value *Addr = llvm::ConstantInt::get(CGM.Int64Ty,
2385 llvm::DIBuilder::OpDeref);
2386 llvm::DIVariable D =
2387 DBuilder.createComplexVariable(Tag,
2388 llvm::DIDescriptor(Scope),
2389 Name, Unit, Line, Ty,
2390 Addr, ArgNo);
2391
2392 // Insert an llvm.dbg.declare into the current block.
2393 llvm::Instruction *Call =
2394 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
2395 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
2396 return;
2397 }
2398
2399 // Create the descriptor for the variable.
Devang Patelcebbedd2010-10-12 23:24:54 +00002400 llvm::DIVariable D =
Devang Patel16674e82011-02-22 18:56:36 +00002401 DBuilder.createLocalVariable(Tag, llvm::DIDescriptor(Scope),
Devang Patel823d8e92010-12-08 22:42:58 +00002402 Name, Unit, Line, Ty,
David Blaikie4e4d0842012-03-11 07:00:24 +00002403 CGM.getLangOpts().Optimize, Flags, ArgNo);
Devang Patelcebbedd2010-10-12 23:24:54 +00002404
2405 // Insert an llvm.dbg.declare into the current block.
2406 llvm::Instruction *Call =
Devang Patel16674e82011-02-22 18:56:36 +00002407 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patelcebbedd2010-10-12 23:24:54 +00002408 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Devang Patelf4dd9622010-10-29 16:21:19 +00002409 return;
Devang Patelcebbedd2010-10-12 23:24:54 +00002410 }
2411
2412 // If VD is an anonymous union then Storage represents value for
2413 // all union fields.
John McCall8178df32011-02-22 22:38:33 +00002414 if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2415 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
2416 if (RD->isUnion()) {
2417 for (RecordDecl::field_iterator I = RD->field_begin(),
2418 E = RD->field_end();
2419 I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002420 FieldDecl *Field = *I;
John McCall8178df32011-02-22 22:38:33 +00002421 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002422 StringRef FieldName = Field->getName();
Devang Patelcebbedd2010-10-12 23:24:54 +00002423
John McCall8178df32011-02-22 22:38:33 +00002424 // Ignore unnamed fields. Do not ignore unnamed records.
2425 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2426 continue;
Devang Patelcebbedd2010-10-12 23:24:54 +00002427
John McCall8178df32011-02-22 22:38:33 +00002428 // Use VarDecl's Tag, Scope and Line number.
2429 llvm::DIVariable D =
2430 DBuilder.createLocalVariable(Tag, llvm::DIDescriptor(Scope),
2431 FieldName, Unit, Line, FieldTy,
David Blaikie4e4d0842012-03-11 07:00:24 +00002432 CGM.getLangOpts().Optimize, Flags,
Devang Patel093ac462011-03-03 20:13:15 +00002433 ArgNo);
Devang Patelcebbedd2010-10-12 23:24:54 +00002434
John McCall8178df32011-02-22 22:38:33 +00002435 // Insert an llvm.dbg.declare into the current block.
2436 llvm::Instruction *Call =
2437 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
John McCall8178df32011-02-22 22:38:33 +00002438 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Devang Patelcebbedd2010-10-12 23:24:54 +00002439 }
John McCall8178df32011-02-22 22:38:33 +00002440 }
2441 }
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00002442}
2443
Devang Patele2d01912011-04-25 23:43:36 +00002444void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
2445 llvm::Value *Storage,
2446 CGBuilderTy &Builder) {
Douglas Gregor4cdad312012-10-23 20:05:01 +00002447 assert(CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo);
Devang Patele2d01912011-04-25 23:43:36 +00002448 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, 0, Builder);
2449}
Mike Stumpb1a6e682009-09-30 02:43:10 +00002450
Eric Christopher245d5a32012-09-21 22:18:42 +00002451void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(const VarDecl *VD,
2452 llvm::Value *Storage,
2453 CGBuilderTy &Builder,
2454 const CGBlockInfo &blockInfo) {
Douglas Gregor4cdad312012-10-23 20:05:01 +00002455 assert(CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo);
Eric Christopheraa2164c2011-09-29 00:00:45 +00002456 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Devang Patele2d01912011-04-25 23:43:36 +00002457
Devang Patel2b594b92010-04-26 23:28:46 +00002458 if (Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00002459 return;
Devang Patele2d01912011-04-25 23:43:36 +00002460
John McCall6b5a61b2011-02-07 10:33:21 +00002461 bool isByRef = VD->hasAttr<BlocksAttr>();
Devang Patele2d01912011-04-25 23:43:36 +00002462
Mike Stumpb1a6e682009-09-30 02:43:10 +00002463 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00002464 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00002465 llvm::DIType Ty;
John McCall6b5a61b2011-02-07 10:33:21 +00002466 if (isByRef)
Devang Patel809b9bb2010-02-10 18:49:08 +00002467 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
2468 else
2469 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00002470
Eric Christopher245d5a32012-09-21 22:18:42 +00002471 // Self is passed along as an implicit non-arg variable in a
2472 // block. Mark it as the object pointer.
2473 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
2474 Ty = DBuilder.createObjectPointerType(Ty);
2475
Mike Stumpb1a6e682009-09-30 02:43:10 +00002476 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00002477 unsigned Line = getLineNumber(VD->getLocation());
2478 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00002479
Micah Villmow25a6a842012-10-08 16:25:52 +00002480 const llvm::DataLayout &target = CGM.getDataLayout();
John McCall6b5a61b2011-02-07 10:33:21 +00002481
2482 CharUnits offset = CharUnits::fromQuantity(
2483 target.getStructLayout(blockInfo.StructureType)
2484 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
2485
Chris Lattner5f9e2722011-07-23 10:55:15 +00002486 SmallVector<llvm::Value *, 9> addr;
Chris Lattner8b418682012-02-07 00:39:47 +00002487 llvm::Type *Int64Ty = CGM.Int64Ty;
Devang Patel4a4e2ef2011-02-18 23:29:22 +00002488 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Chris Lattner14b1a362010-01-25 03:29:35 +00002489 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
John McCall6b5a61b2011-02-07 10:33:21 +00002490 if (isByRef) {
Devang Patel4a4e2ef2011-02-18 23:29:22 +00002491 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
2492 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00002493 // offset of __forwarding field
Eric Christopherab5278e2011-10-11 23:00:51 +00002494 offset = CGM.getContext()
Micah Villmowcadaf4b2012-10-11 17:21:41 +00002495 .toCharUnitsFromBits(target.getPointerSizeInBits(0));
Chris Lattner14b1a362010-01-25 03:29:35 +00002496 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Devang Patel4a4e2ef2011-02-18 23:29:22 +00002497 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
2498 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00002499 // offset of x field
Ken Dyck0ebce0e2011-04-22 17:41:34 +00002500 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Chris Lattner14b1a362010-01-25 03:29:35 +00002501 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00002502 }
2503
2504 // Create the descriptor for the variable.
2505 llvm::DIVariable D =
Devang Patele2d01912011-04-25 23:43:36 +00002506 DBuilder.createComplexVariable(llvm::dwarf::DW_TAG_auto_variable,
Eric Christopheraa2164c2011-09-29 00:00:45 +00002507 llvm::DIDescriptor(LexicalBlockStack.back()),
Jay Foadc556ef22011-04-24 10:11:03 +00002508 VD->getName(), Unit, Line, Ty, addr);
Mike Stumpb1a6e682009-09-30 02:43:10 +00002509 // Insert an llvm.dbg.declare into the current block.
Eric Christopher73fb3502011-10-13 21:45:18 +00002510 llvm::Instruction *Call =
Devang Patel50811d22011-04-25 23:52:27 +00002511 DBuilder.insertDeclare(Storage, D, Builder.GetInsertPoint());
Eric Christopher73fb3502011-10-13 21:45:18 +00002512 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column,
2513 LexicalBlockStack.back()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00002514}
2515
Chris Lattner9c85ba32008-11-10 06:08:34 +00002516/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
2517/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00002518void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Devang Patel093ac462011-03-03 20:13:15 +00002519 unsigned ArgNo,
Devang Patel34753802011-02-16 01:11:51 +00002520 CGBuilderTy &Builder) {
Douglas Gregor4cdad312012-10-23 20:05:01 +00002521 assert(CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo);
Devang Patel093ac462011-03-03 20:13:15 +00002522 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, ArgNo, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00002523}
2524
John McCall8178df32011-02-22 22:38:33 +00002525namespace {
2526 struct BlockLayoutChunk {
2527 uint64_t OffsetInBits;
2528 const BlockDecl::Capture *Capture;
2529 };
2530 bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
2531 return l.OffsetInBits < r.OffsetInBits;
2532 }
2533}
Chris Lattner9c85ba32008-11-10 06:08:34 +00002534
John McCall8178df32011-02-22 22:38:33 +00002535void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
2536 llvm::Value *addr,
2537 CGBuilderTy &Builder) {
Douglas Gregor4cdad312012-10-23 20:05:01 +00002538 assert(CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo);
John McCall8178df32011-02-22 22:38:33 +00002539 ASTContext &C = CGM.getContext();
2540 const BlockDecl *blockDecl = block.getBlockDecl();
2541
2542 // Collect some general information about the block's location.
2543 SourceLocation loc = blockDecl->getCaretLocation();
2544 llvm::DIFile tunit = getOrCreateFile(loc);
2545 unsigned line = getLineNumber(loc);
2546 unsigned column = getColumnNumber(loc);
2547
2548 // Build the debug-info type for the block literal.
Nick Lewycky7d4b1592011-05-02 01:41:48 +00002549 getContextDescriptor(cast<Decl>(blockDecl->getDeclContext()));
John McCall8178df32011-02-22 22:38:33 +00002550
2551 const llvm::StructLayout *blockLayout =
Micah Villmow25a6a842012-10-08 16:25:52 +00002552 CGM.getDataLayout().getStructLayout(block.StructureType);
John McCall8178df32011-02-22 22:38:33 +00002553
Chris Lattner5f9e2722011-07-23 10:55:15 +00002554 SmallVector<llvm::Value*, 16> fields;
John McCall8178df32011-02-22 22:38:33 +00002555 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
2556 blockLayout->getElementOffsetInBits(0),
Devang Patel1d323e02011-06-24 22:00:59 +00002557 tunit, tunit));
John McCall8178df32011-02-22 22:38:33 +00002558 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
2559 blockLayout->getElementOffsetInBits(1),
Devang Patel1d323e02011-06-24 22:00:59 +00002560 tunit, tunit));
John McCall8178df32011-02-22 22:38:33 +00002561 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
2562 blockLayout->getElementOffsetInBits(2),
Devang Patel1d323e02011-06-24 22:00:59 +00002563 tunit, tunit));
John McCall8178df32011-02-22 22:38:33 +00002564 fields.push_back(createFieldType("__FuncPtr", C.VoidPtrTy, 0, loc, AS_public,
2565 blockLayout->getElementOffsetInBits(3),
Devang Patel1d323e02011-06-24 22:00:59 +00002566 tunit, tunit));
John McCall8178df32011-02-22 22:38:33 +00002567 fields.push_back(createFieldType("__descriptor",
2568 C.getPointerType(block.NeedsCopyDispose ?
2569 C.getBlockDescriptorExtendedType() :
2570 C.getBlockDescriptorType()),
2571 0, loc, AS_public,
2572 blockLayout->getElementOffsetInBits(4),
Devang Patel1d323e02011-06-24 22:00:59 +00002573 tunit, tunit));
John McCall8178df32011-02-22 22:38:33 +00002574
2575 // We want to sort the captures by offset, not because DWARF
2576 // requires this, but because we're paranoid about debuggers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002577 SmallVector<BlockLayoutChunk, 8> chunks;
John McCall8178df32011-02-22 22:38:33 +00002578
2579 // 'this' capture.
2580 if (blockDecl->capturesCXXThis()) {
2581 BlockLayoutChunk chunk;
2582 chunk.OffsetInBits =
2583 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
2584 chunk.Capture = 0;
2585 chunks.push_back(chunk);
2586 }
2587
2588 // Variable captures.
2589 for (BlockDecl::capture_const_iterator
2590 i = blockDecl->capture_begin(), e = blockDecl->capture_end();
2591 i != e; ++i) {
2592 const BlockDecl::Capture &capture = *i;
2593 const VarDecl *variable = capture.getVariable();
2594 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
2595
2596 // Ignore constant captures.
2597 if (captureInfo.isConstant())
2598 continue;
2599
2600 BlockLayoutChunk chunk;
2601 chunk.OffsetInBits =
2602 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
2603 chunk.Capture = &capture;
2604 chunks.push_back(chunk);
2605 }
2606
2607 // Sort by offset.
2608 llvm::array_pod_sort(chunks.begin(), chunks.end());
2609
Chris Lattner5f9e2722011-07-23 10:55:15 +00002610 for (SmallVectorImpl<BlockLayoutChunk>::iterator
John McCall8178df32011-02-22 22:38:33 +00002611 i = chunks.begin(), e = chunks.end(); i != e; ++i) {
2612 uint64_t offsetInBits = i->OffsetInBits;
2613 const BlockDecl::Capture *capture = i->Capture;
2614
2615 // If we have a null capture, this must be the C++ 'this' capture.
2616 if (!capture) {
2617 const CXXMethodDecl *method =
2618 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
2619 QualType type = method->getThisType(C);
2620
2621 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
Devang Patel1d323e02011-06-24 22:00:59 +00002622 offsetInBits, tunit, tunit));
John McCall8178df32011-02-22 22:38:33 +00002623 continue;
2624 }
2625
2626 const VarDecl *variable = capture->getVariable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002627 StringRef name = variable->getName();
John McCalld113a6f2011-03-02 06:57:14 +00002628
2629 llvm::DIType fieldType;
2630 if (capture->isByRef()) {
2631 std::pair<uint64_t,unsigned> ptrInfo = C.getTypeInfo(C.VoidPtrTy);
2632
2633 // FIXME: this creates a second copy of this type!
2634 uint64_t xoffset;
2635 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
2636 fieldType = DBuilder.createPointerType(fieldType, ptrInfo.first);
Devang Patel1d323e02011-06-24 22:00:59 +00002637 fieldType = DBuilder.createMemberType(tunit, name, tunit, line,
John McCalld113a6f2011-03-02 06:57:14 +00002638 ptrInfo.first, ptrInfo.second,
2639 offsetInBits, 0, fieldType);
2640 } else {
2641 fieldType = createFieldType(name, variable->getType(), 0,
Devang Patel1d323e02011-06-24 22:00:59 +00002642 loc, AS_public, offsetInBits, tunit, tunit);
John McCalld113a6f2011-03-02 06:57:14 +00002643 }
2644 fields.push_back(fieldType);
John McCall8178df32011-02-22 22:38:33 +00002645 }
2646
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002647 SmallString<36> typeName;
John McCall8178df32011-02-22 22:38:33 +00002648 llvm::raw_svector_ostream(typeName)
2649 << "__block_literal_" << CGM.getUniqueBlockCount();
2650
Jay Foadc556ef22011-04-24 10:11:03 +00002651 llvm::DIArray fieldsArray = DBuilder.getOrCreateArray(fields);
John McCall8178df32011-02-22 22:38:33 +00002652
2653 llvm::DIType type =
2654 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
2655 CGM.getContext().toBits(block.BlockSize),
2656 CGM.getContext().toBits(block.BlockAlign),
2657 0, fieldsArray);
2658 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
2659
2660 // Get overall information about the block.
2661 unsigned flags = llvm::DIDescriptor::FlagArtificial;
Eric Christopheraa2164c2011-09-29 00:00:45 +00002662 llvm::MDNode *scope = LexicalBlockStack.back();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002663 StringRef name = ".block_descriptor";
John McCall8178df32011-02-22 22:38:33 +00002664
2665 // Create the descriptor for the parameter.
2666 llvm::DIVariable debugVar =
2667 DBuilder.createLocalVariable(llvm::dwarf::DW_TAG_arg_variable,
2668 llvm::DIDescriptor(scope),
2669 name, tunit, line, type,
David Blaikie4e4d0842012-03-11 07:00:24 +00002670 CGM.getLangOpts().Optimize, flags,
Devang Patel093ac462011-03-03 20:13:15 +00002671 cast<llvm::Argument>(addr)->getArgNo() + 1);
John McCall8178df32011-02-22 22:38:33 +00002672
2673 // Insert an llvm.dbg.value into the current block.
2674 llvm::Instruction *declare =
2675 DBuilder.insertDbgValueIntrinsic(addr, 0, debugVar,
2676 Builder.GetInsertBlock());
2677 declare->setDebugLoc(llvm::DebugLoc::get(line, column, scope));
2678}
Chris Lattner9c85ba32008-11-10 06:08:34 +00002679
Sanjiv Gupta686226b2008-06-05 08:59:10 +00002680/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00002681void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00002682 const VarDecl *D) {
Douglas Gregor4cdad312012-10-23 20:05:01 +00002683 assert(CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00002684 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00002685 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00002686 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +00002687
Eric Christopher73fb3502011-10-13 21:45:18 +00002688 setLocation(D->getLocation());
2689
Devang Pateleb6d79b2010-02-01 21:34:11 +00002690 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00002691 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002692
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00002693 // CodeGen turns int[] into int[1] so we'll do the same here.
Benjamin Kramer65263b42012-08-04 17:00:46 +00002694 llvm::APInt ConstVal(32, 1);
Anders Carlsson20f12a22009-12-06 18:00:51 +00002695 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00002696
Anders Carlsson20f12a22009-12-06 18:00:51 +00002697 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Nick Lewyckyd4c100e2011-11-09 04:25:21 +00002698 ArrayType::Normal, 0);
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00002699 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002700 StringRef DeclName = D->getName();
2701 StringRef LinkageName;
Devang Pateleb4c45b2011-02-09 19:16:38 +00002702 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext())
2703 && !isa<ObjCMethodDecl>(D->getDeclContext()))
Devang Patel8b90a782010-05-13 23:52:37 +00002704 LinkageName = Var->getName();
Devang Patel58faf202010-10-22 17:11:50 +00002705 if (LinkageName == DeclName)
Chris Lattner5f9e2722011-07-23 10:55:15 +00002706 LinkageName = StringRef();
Devang Pateleb6d79b2010-02-01 21:34:11 +00002707 llvm::DIDescriptor DContext =
Devang Patel170cef32010-12-09 00:33:05 +00002708 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()));
Devang Patel16674e82011-02-22 18:56:36 +00002709 DBuilder.createStaticVariable(DContext, DeclName, LinkageName,
Devang Patel823d8e92010-12-08 22:42:58 +00002710 Unit, LineNo, getOrCreateType(T, Unit),
2711 Var->hasInternalLinkage(), Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00002712}
2713
Devang Patel9ca36b62009-02-26 21:10:26 +00002714/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00002715void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00002716 ObjCInterfaceDecl *ID) {
Douglas Gregor4cdad312012-10-23 20:05:01 +00002717 assert(CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo);
Devang Patel9ca36b62009-02-26 21:10:26 +00002718 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00002719 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00002720 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patel9ca36b62009-02-26 21:10:26 +00002721
Chris Lattner5f9e2722011-07-23 10:55:15 +00002722 StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00002723
Devang Pateld6c5a262010-02-01 21:52:22 +00002724 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00002725 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002726
Devang Patel9ca36b62009-02-26 21:10:26 +00002727 // CodeGen turns int[] into int[1] so we'll do the same here.
Benjamin Kramer65263b42012-08-04 17:00:46 +00002728 llvm::APInt ConstVal(32, 1);
Anders Carlsson20f12a22009-12-06 18:00:51 +00002729 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00002730
Anders Carlsson20f12a22009-12-06 18:00:51 +00002731 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00002732 ArrayType::Normal, 0);
2733 }
2734
Devang Patel16674e82011-02-22 18:56:36 +00002735 DBuilder.createGlobalVariable(Name, Unit, LineNo,
Devang Patel823d8e92010-12-08 22:42:58 +00002736 getOrCreateType(T, Unit),
2737 Var->hasInternalLinkage(), Var);
Devang Patel9ca36b62009-02-26 21:10:26 +00002738}
Devang Patelabb485f2010-02-01 19:16:32 +00002739
Devang Patel25c2c8f2010-08-10 17:53:33 +00002740/// EmitGlobalVariable - Emit global variable's debug info.
2741void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
John McCall189d6ef2010-10-09 01:34:31 +00002742 llvm::Constant *Init) {
Douglas Gregor4cdad312012-10-23 20:05:01 +00002743 assert(CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo);
Devang Patel8d308382010-08-10 07:24:25 +00002744 // Create the descriptor for the variable.
2745 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Chris Lattner5f9e2722011-07-23 10:55:15 +00002746 StringRef Name = VD->getName();
Devang Patel0317ab02010-08-10 18:27:15 +00002747 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
Devang Patel6237cea2010-08-23 22:07:25 +00002748 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
Benjamin Kramer527e6162012-06-20 18:11:18 +00002749 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
2750 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
2751 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
Devang Patel6237cea2010-08-23 22:07:25 +00002752 }
Devang Patel0317ab02010-08-10 18:27:15 +00002753 // Do not use DIGlobalVariable for enums.
2754 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
2755 return;
Devang Patel16674e82011-02-22 18:56:36 +00002756 DBuilder.createStaticVariable(Unit, Name, Name, Unit,
Devang Patel823d8e92010-12-08 22:42:58 +00002757 getLineNumber(VD->getLocation()),
2758 Ty, true, Init);
Devang Patel8d308382010-08-10 07:24:25 +00002759}
2760
Devang Patelabb485f2010-02-01 19:16:32 +00002761/// getOrCreateNamesSpace - Return namespace descriptor for the given
2762/// namespace decl.
2763llvm::DINameSpace
Devang Patel170cef32010-12-09 00:33:05 +00002764CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
Devang Patelabb485f2010-02-01 19:16:32 +00002765 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
2766 NameSpaceCache.find(NSDecl);
2767 if (I != NameSpaceCache.end())
2768 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
2769
Devang Patel8ab870d2010-05-12 23:46:38 +00002770 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patel8c376682010-10-28 19:12:46 +00002771 llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation());
Devang Patelabb485f2010-02-01 19:16:32 +00002772 llvm::DIDescriptor Context =
Devang Patel170cef32010-12-09 00:33:05 +00002773 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()));
Devang Patelabb485f2010-02-01 19:16:32 +00002774 llvm::DINameSpace NS =
Devang Patel16674e82011-02-22 18:56:36 +00002775 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Devang Patelab699792010-05-07 18:12:35 +00002776 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00002777 return NS;
2778}
Eric Christopher7ff0c5d2012-02-18 00:50:17 +00002779
2780void CGDebugInfo::finalize(void) {
2781 for (std::vector<std::pair<void *, llvm::WeakVH> >::const_iterator VI
2782 = ReplaceMap.begin(), VE = ReplaceMap.end(); VI != VE; ++VI) {
2783 llvm::DIType Ty, RepTy;
2784 // Verify that the debug info still exists.
Richard Smithe7259aa2012-08-17 04:17:54 +00002785 if (llvm::Value *V = VI->second)
2786 Ty = llvm::DIType(cast<llvm::MDNode>(V));
Eric Christopher7ff0c5d2012-02-18 00:50:17 +00002787
2788 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
2789 TypeCache.find(VI->first);
2790 if (it != TypeCache.end()) {
2791 // Verify that the debug info still exists.
Richard Smithe7259aa2012-08-17 04:17:54 +00002792 if (llvm::Value *V = it->second)
2793 RepTy = llvm::DIType(cast<llvm::MDNode>(V));
Eric Christopher7ff0c5d2012-02-18 00:50:17 +00002794 }
2795
Eric Christopher86211df2012-02-20 18:05:24 +00002796 if (Ty.Verify() && Ty.isForwardDecl() && RepTy.Verify()) {
Eric Christopher7ff0c5d2012-02-18 00:50:17 +00002797 Ty.replaceAllUsesWith(RepTy);
Eric Christopher86211df2012-02-20 18:05:24 +00002798 }
Eric Christopher7ff0c5d2012-02-18 00:50:17 +00002799 }
2800 DBuilder.finalize();
2801}