blob: 80fa09be747370f7bdaf8805688e663f108745a9 [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);
Devang Patel6cf37dd2011-04-08 21:56:52 +00001476 int64_t NumElems = Ty->getNumElements();
1477 int64_t LowerBound = 0;
1478 if (NumElems == 0)
1479 // 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
Devang Patel70c23cd2010-02-23 22:59:39 +00001483 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001484
Devang Patel6cf37dd2011-04-08 21:56:52 +00001485 llvm::Value *Subscript = DBuilder.getOrCreateSubrange(LowerBound, NumElems);
Jay Foadc556ef22011-04-24 10:11:03 +00001486 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Devang Patel70c23cd2010-02-23 22:59:39 +00001487
1488 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1489 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1490
1491 return
Devang Patel16674e82011-02-22 18:56:36 +00001492 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;
1528 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty)) {
1529 if (CAT->getSize().getZExtValue())
1530 UpperBound = CAT->getSize().getZExtValue() - 1;
1531 } else
1532 // This is an unbounded array. Use Low = 1, Hi = 0 to express such
1533 // arrays.
1534 LowerBound = 1;
1535
1536 // FIXME: Verify this is right for VLAs.
1537 Subscripts.push_back(DBuilder.getOrCreateSubrange(LowerBound,
1538 UpperBound));
Chris Lattner9c85ba32008-11-10 06:08:34 +00001539 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +00001540 }
Mike Stump1eb44332009-09-09 15:08:12 +00001541
Jay Foadc556ef22011-04-24 10:11:03 +00001542 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001543
Devang Patelca80a5f2009-10-20 19:55:01 +00001544 llvm::DIType DbgTy =
Devang Patel16674e82011-02-22 18:56:36 +00001545 DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
Devang Patel823d8e92010-12-08 22:42:58 +00001546 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001547 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001548}
1549
Anders Carlssona031b352009-11-06 19:19:55 +00001550llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001551 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001552 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1553 Ty, Ty->getPointeeType(), Unit);
1554}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001555
Douglas Gregor36b8ee62011-01-22 01:58:15 +00001556llvm::DIType CGDebugInfo::CreateType(const RValueReferenceType *Ty,
1557 llvm::DIFile Unit) {
1558 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type,
1559 Ty, Ty->getPointeeType(), Unit);
1560}
1561
Anders Carlsson20f12a22009-12-06 18:00:51 +00001562llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001563 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001564 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1565 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1566
1567 if (!Ty->getPointeeType()->isFunctionType()) {
1568 // We have a data member pointer type.
1569 return PointerDiffDITy;
1570 }
1571
1572 // We have a member function pointer type. Treat it as a struct with two
1573 // ptrdiff_t members.
1574 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1575
1576 uint64_t FieldOffset = 0;
Devang Patel823d8e92010-12-08 22:42:58 +00001577 llvm::Value *ElementTypes[2];
Anders Carlsson20f12a22009-12-06 18:00:51 +00001578
Eric Christopher3e078812012-08-04 00:11:20 +00001579 // FIXME: This should be a DW_TAG_pointer_to_member type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001580 ElementTypes[0] =
Devang Patel1d323e02011-06-24 22:00:59 +00001581 DBuilder.createMemberType(U, "ptr", U, 0,
Devang Patel823d8e92010-12-08 22:42:58 +00001582 Info.first, Info.second, FieldOffset, 0,
1583 PointerDiffDITy);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001584 FieldOffset += Info.first;
1585
1586 ElementTypes[1] =
Devang Patel1d323e02011-06-24 22:00:59 +00001587 DBuilder.createMemberType(U, "ptr", U, 0,
Devang Patel823d8e92010-12-08 22:42:58 +00001588 Info.first, Info.second, FieldOffset, 0,
1589 PointerDiffDITy);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001590
Jay Foadc556ef22011-04-24 10:11:03 +00001591 llvm::DIArray Elements = DBuilder.getOrCreateArray(ElementTypes);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001592
Chris Lattner5f9e2722011-07-23 10:55:15 +00001593 return DBuilder.createStructType(U, StringRef("test"),
Devang Patel823d8e92010-12-08 22:42:58 +00001594 U, 0, FieldOffset,
1595 0, 0, Elements);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001596}
1597
Eli Friedmanb001de72011-10-06 23:00:33 +00001598llvm::DIType CGDebugInfo::CreateType(const AtomicType *Ty,
1599 llvm::DIFile U) {
1600 // Ignore the atomic wrapping
1601 // FIXME: What is the correct representation?
1602 return getOrCreateType(Ty->getValueType(), U);
1603}
1604
Devang Patel6237cea2010-08-23 22:07:25 +00001605/// CreateEnumType - get enumeration type.
Devang Patel31f7d022011-01-17 22:23:07 +00001606llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED) {
Eli Friedmane6b39bc2012-10-05 01:49:33 +00001607 uint64_t Size = 0;
1608 uint64_t Align = 0;
1609 if (!ED->getTypeForDecl()->isIncompleteType()) {
1610 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1611 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1612 }
1613
1614 // If this is just a forward declaration, construct an appropriately
1615 // marked node and just return it.
1616 if (!ED->getDefinition()) {
1617 llvm::DIDescriptor EDContext;
1618 EDContext = getContextDescriptor(cast<Decl>(ED->getDeclContext()));
1619 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1620 unsigned Line = getLineNumber(ED->getLocation());
1621 StringRef EDName = ED->getName();
1622 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_enumeration_type,
1623 EDName, EDContext, DefUnit, Line, 0,
1624 Size, Align);
1625 }
Devang Patel6237cea2010-08-23 22:07:25 +00001626
1627 // Create DIEnumerator elements for each enumerator.
Eli Friedmane6b39bc2012-10-05 01:49:33 +00001628 SmallVector<llvm::Value *, 16> Enumerators;
1629 ED = ED->getDefinition();
Devang Patel6237cea2010-08-23 22:07:25 +00001630 for (EnumDecl::enumerator_iterator
1631 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
1632 Enum != EnumEnd; ++Enum) {
Devang Patel823d8e92010-12-08 22:42:58 +00001633 Enumerators.push_back(
Devang Patel16674e82011-02-22 18:56:36 +00001634 DBuilder.createEnumerator(Enum->getName(),
Devang Patel823d8e92010-12-08 22:42:58 +00001635 Enum->getInitVal().getZExtValue()));
Devang Patel6237cea2010-08-23 22:07:25 +00001636 }
1637
1638 // Return a CompositeType for the enum itself.
Jay Foadc556ef22011-04-24 10:11:03 +00001639 llvm::DIArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Devang Patel6237cea2010-08-23 22:07:25 +00001640
1641 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1642 unsigned Line = getLineNumber(ED->getLocation());
Devang Patel4bc48872010-10-27 23:23:58 +00001643 llvm::DIDescriptor EnumContext =
John McCall8178df32011-02-22 22:38:33 +00001644 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
Eric Christopher9ee5f462012-05-23 00:09:47 +00001645 llvm::DIType ClassTy = ED->isScopedUsingClassTag() ?
1646 getOrCreateType(ED->getIntegerType(), DefUnit) : llvm::DIType();
Devang Patel6237cea2010-08-23 22:07:25 +00001647 llvm::DIType DbgTy =
Devang Patel16674e82011-02-22 18:56:36 +00001648 DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit, Line,
Eric Christopher9ee5f462012-05-23 00:09:47 +00001649 Size, Align, EltArray,
Eli Friedmane6b39bc2012-10-05 01:49:33 +00001650 ClassTy);
Devang Patel6237cea2010-08-23 22:07:25 +00001651 return DbgTy;
1652}
1653
Douglas Gregor840943d2009-12-21 20:18:30 +00001654static QualType UnwrapTypeForDebugInfo(QualType T) {
1655 do {
1656 QualType LastT = T;
1657 switch (T->getTypeClass()) {
1658 default:
1659 return T;
1660 case Type::TemplateSpecialization:
1661 T = cast<TemplateSpecializationType>(T)->desugar();
1662 break;
John McCallf4c73712011-01-19 06:33:43 +00001663 case Type::TypeOfExpr:
1664 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001665 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001666 case Type::TypeOf:
1667 T = cast<TypeOfType>(T)->getUnderlyingType();
1668 break;
1669 case Type::Decltype:
1670 T = cast<DecltypeType>(T)->getUnderlyingType();
1671 break;
Sean Huntca63c202011-05-24 22:41:36 +00001672 case Type::UnaryTransform:
1673 T = cast<UnaryTransformType>(T)->getUnderlyingType();
1674 break;
John McCall9d156a72011-01-06 01:58:22 +00001675 case Type::Attributed:
1676 T = cast<AttributedType>(T)->getEquivalentType();
John McCall14aa2172011-03-04 04:00:19 +00001677 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001678 case Type::Elaborated:
1679 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001680 break;
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001681 case Type::Paren:
1682 T = cast<ParenType>(T)->getInnerType();
1683 break;
Eric Christopher363e5ac2012-08-07 00:14:25 +00001684 case Type::SubstTemplateTypeParm: {
1685 // We need to keep the qualifiers handy since getReplacementType()
1686 // will strip them away.
1687 unsigned Quals = T.getLocalFastQualifiers();
Douglas Gregor840943d2009-12-21 20:18:30 +00001688 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Eric Christopher363e5ac2012-08-07 00:14:25 +00001689 T.addFastQualifiers(Quals);
1690 }
Douglas Gregor840943d2009-12-21 20:18:30 +00001691 break;
Anders Carlssonebc32792011-03-06 16:43:04 +00001692 case Type::Auto:
1693 T = cast<AutoType>(T)->getDeducedType();
1694 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001695 }
1696
1697 assert(T != LastT && "Type unwrapping failed to unwrap!");
1698 if (T == LastT)
1699 return T;
1700 } while (true);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001701}
1702
Eric Christopher973bbb62011-12-16 23:40:18 +00001703/// getType - Get the type from the cache or return null type if it doesn't exist.
1704llvm::DIType CGDebugInfo::getTypeOrNull(QualType Ty) {
Mike Stump1eb44332009-09-09 15:08:12 +00001705
Douglas Gregor840943d2009-12-21 20:18:30 +00001706 // Unwrap the type as needed for debug information.
1707 Ty = UnwrapTypeForDebugInfo(Ty);
Eric Christopher42e75da2012-02-13 14:56:11 +00001708
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001709 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001710 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001711 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001712 if (it != TypeCache.end()) {
1713 // Verify that the debug info still exists.
Richard Smithe7259aa2012-08-17 04:17:54 +00001714 if (llvm::Value *V = it->second)
1715 return llvm::DIType(cast<llvm::MDNode>(V));
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001716 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001717
Eric Christopher973bbb62011-12-16 23:40:18 +00001718 return llvm::DIType();
1719}
1720
Eric Christopher9965dea2012-02-16 22:54:45 +00001721/// getCompletedTypeOrNull - Get the type from the cache or return null if it
1722/// doesn't exist.
1723llvm::DIType CGDebugInfo::getCompletedTypeOrNull(QualType Ty) {
1724
1725 // Unwrap the type as needed for debug information.
1726 Ty = UnwrapTypeForDebugInfo(Ty);
1727
1728 // Check for existing entry.
1729 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
1730 CompletedTypeCache.find(Ty.getAsOpaquePtr());
1731 if (it != CompletedTypeCache.end()) {
1732 // Verify that the debug info still exists.
Richard Smithe7259aa2012-08-17 04:17:54 +00001733 if (llvm::Value *V = it->second)
1734 return llvm::DIType(cast<llvm::MDNode>(V));
Eric Christopher9965dea2012-02-16 22:54:45 +00001735 }
1736
1737 return llvm::DIType();
1738}
1739
1740
Eric Christopher973bbb62011-12-16 23:40:18 +00001741/// getOrCreateType - Get the type from the cache or create a new
1742/// one if necessary.
1743llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile Unit) {
1744 if (Ty.isNull())
1745 return llvm::DIType();
1746
1747 // Unwrap the type as needed for debug information.
1748 Ty = UnwrapTypeForDebugInfo(Ty);
Eric Christopher363e5ac2012-08-07 00:14:25 +00001749
Eric Christopher9965dea2012-02-16 22:54:45 +00001750 llvm::DIType T = getCompletedTypeOrNull(Ty);
1751
Eric Christopher363e5ac2012-08-07 00:14:25 +00001752 if (T.Verify())
1753 return T;
Eric Christopher973bbb62011-12-16 23:40:18 +00001754
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001755 // Otherwise create the type.
1756 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Eric Christopher7ff0c5d2012-02-18 00:50:17 +00001757
1758 llvm::DIType TC = getTypeOrNull(Ty);
1759 if (TC.Verify() && TC.isForwardDecl())
Michael J. Spencer50e3faa2012-06-08 23:47:12 +00001760 ReplaceMap.push_back(std::make_pair(Ty.getAsOpaquePtr(),
1761 static_cast<llvm::Value*>(TC)));
Eric Christopher9965dea2012-02-16 22:54:45 +00001762
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001763 // And update the type cache.
Eric Christopher9965dea2012-02-16 22:54:45 +00001764 TypeCache[Ty.getAsOpaquePtr()] = Res;
1765
1766 if (!Res.isForwardDecl())
1767 CompletedTypeCache[Ty.getAsOpaquePtr()] = Res;
Eric Christopher363e5ac2012-08-07 00:14:25 +00001768
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001769 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001770}
1771
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001772/// CreateTypeNode - Create a new debug type node.
Nick Lewycky7b3819d2011-11-09 04:27:23 +00001773llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001774 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001775 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001776 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001777
Douglas Gregor2101a822009-12-21 19:57:21 +00001778 const char *Diag = 0;
1779
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001780 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001781 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001782#define TYPE(Class, Base)
1783#define ABSTRACT_TYPE(Class, Base)
1784#define NON_CANONICAL_TYPE(Class, Base)
1785#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1786#include "clang/AST/TypeNodes.def"
David Blaikieb219cfc2011-09-23 05:06:16 +00001787 llvm_unreachable("Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001788
Anders Carlssonbfe69952009-11-06 18:24:04 +00001789 case Type::ExtVector:
Devang Patel70c23cd2010-02-23 22:59:39 +00001790 case Type::Vector:
1791 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001792 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001793 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCallc12c5bb2010-05-15 11:32:37 +00001794 case Type::ObjCObject:
1795 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001796 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001797 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
Nick Lewyckyd4c100e2011-11-09 04:25:21 +00001798 case Type::Builtin:
1799 return CreateType(cast<BuiltinType>(Ty));
1800 case Type::Complex:
1801 return CreateType(cast<ComplexType>(Ty));
1802 case Type::Pointer:
1803 return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001804 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001805 return CreateType(cast<BlockPointerType>(Ty), Unit);
Nick Lewyckyd4c100e2011-11-09 04:25:21 +00001806 case Type::Typedef:
1807 return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001808 case Type::Record:
Nick Lewyckyd4c100e2011-11-09 04:25:21 +00001809 return CreateType(cast<RecordType>(Ty));
Douglas Gregor72564e72009-02-26 23:50:07 +00001810 case Type::Enum:
Nick Lewyckyd4c100e2011-11-09 04:25:21 +00001811 return CreateEnumType(cast<EnumType>(Ty)->getDecl());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001812 case Type::FunctionProto:
1813 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001814 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001815 case Type::ConstantArray:
1816 case Type::VariableArray:
1817 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001818 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001819
1820 case Type::LValueReference:
1821 return CreateType(cast<LValueReferenceType>(Ty), Unit);
Douglas Gregor36b8ee62011-01-22 01:58:15 +00001822 case Type::RValueReference:
1823 return CreateType(cast<RValueReferenceType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001824
Anders Carlsson20f12a22009-12-06 18:00:51 +00001825 case Type::MemberPointer:
1826 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001827
Eli Friedmanb001de72011-10-06 23:00:33 +00001828 case Type::Atomic:
1829 return CreateType(cast<AtomicType>(Ty), Unit);
1830
John McCall9d156a72011-01-06 01:58:22 +00001831 case Type::Attributed:
Douglas Gregor2101a822009-12-21 19:57:21 +00001832 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001833 case Type::Elaborated:
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001834 case Type::Paren:
Douglas Gregor2101a822009-12-21 19:57:21 +00001835 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001836 case Type::TypeOfExpr:
1837 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001838 case Type::Decltype:
Sean Huntca63c202011-05-24 22:41:36 +00001839 case Type::UnaryTransform:
Richard Smith34b41d92011-02-20 03:19:35 +00001840 case Type::Auto:
Douglas Gregor840943d2009-12-21 20:18:30 +00001841 llvm_unreachable("type should have been unwrapped!");
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001842 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001843
1844 assert(Diag && "Fall through without a diagnostic?");
David Blaikied6471f72011-09-25 23:23:43 +00001845 unsigned DiagID = CGM.getDiags().getCustomDiagID(DiagnosticsEngine::Error,
Douglas Gregor2101a822009-12-21 19:57:21 +00001846 "debug information for %0 is not yet supported");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00001847 CGM.getDiags().Report(DiagID)
Douglas Gregor2101a822009-12-21 19:57:21 +00001848 << Diag;
1849 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001850}
1851
Eric Christopher9965dea2012-02-16 22:54:45 +00001852/// getOrCreateLimitedType - Get the type from the cache or create a new
1853/// limited type if necessary.
1854llvm::DIType CGDebugInfo::getOrCreateLimitedType(QualType Ty,
1855 llvm::DIFile Unit) {
1856 if (Ty.isNull())
1857 return llvm::DIType();
1858
1859 // Unwrap the type as needed for debug information.
1860 Ty = UnwrapTypeForDebugInfo(Ty);
1861
1862 llvm::DIType T = getTypeOrNull(Ty);
1863
1864 // We may have cached a forward decl when we could have created
1865 // a non-forward decl. Go ahead and create a non-forward decl
1866 // now.
1867 if (T.Verify() && !T.isForwardDecl()) return T;
1868
1869 // Otherwise create the type.
1870 llvm::DIType Res = CreateLimitedTypeNode(Ty, Unit);
1871
Eric Christopher7ff0c5d2012-02-18 00:50:17 +00001872 if (T.Verify() && T.isForwardDecl())
Michael J. Spencer50e3faa2012-06-08 23:47:12 +00001873 ReplaceMap.push_back(std::make_pair(Ty.getAsOpaquePtr(),
1874 static_cast<llvm::Value*>(T)));
Eric Christopher7ff0c5d2012-02-18 00:50:17 +00001875
Eric Christopher9965dea2012-02-16 22:54:45 +00001876 // And update the type cache.
1877 TypeCache[Ty.getAsOpaquePtr()] = Res;
1878 return Res;
1879}
1880
1881// TODO: Currently used for context chains when limiting debug info.
1882llvm::DIType CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
1883 RecordDecl *RD = Ty->getDecl();
1884
1885 // Get overall information about the record type for the debug info.
1886 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
1887 unsigned Line = getLineNumber(RD->getLocation());
David Blaikie70ae1222012-11-02 23:40:00 +00001888 StringRef RDName = getClassName(RD);
Eric Christopher9965dea2012-02-16 22:54:45 +00001889
1890 llvm::DIDescriptor RDContext;
Douglas Gregor4cdad312012-10-23 20:05:01 +00001891 if (CGM.getCodeGenOpts().getDebugInfo() == CodeGenOptions::LimitedDebugInfo)
Eric Christopher9965dea2012-02-16 22:54:45 +00001892 RDContext = createContextChain(cast<Decl>(RD->getDeclContext()));
1893 else
1894 RDContext = getContextDescriptor(cast<Decl>(RD->getDeclContext()));
1895
1896 // If this is just a forward declaration, construct an appropriately
1897 // marked node and just return it.
Eric Christopher7ff0c5d2012-02-18 00:50:17 +00001898 if (!RD->getDefinition())
1899 return createRecordFwdDecl(RD, RDContext);
Eric Christopher9965dea2012-02-16 22:54:45 +00001900
1901 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1902 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1903 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Benjamin Kramer6181e562012-03-20 19:49:14 +00001904 llvm::TrackingVH<llvm::MDNode> RealDecl;
Eric Christopher9965dea2012-02-16 22:54:45 +00001905
1906 if (RD->isUnion())
1907 RealDecl = DBuilder.createUnionType(RDContext, RDName, DefUnit, Line,
1908 Size, Align, 0, llvm::DIArray());
David Blaikie70ae1222012-11-02 23:40:00 +00001909 else if (RD->isClass()) {
Eric Christopher9965dea2012-02-16 22:54:45 +00001910 // FIXME: This could be a struct type giving a default visibility different
1911 // than C++ class type, but needs llvm metadata changes first.
1912 RealDecl = DBuilder.createClassType(RDContext, RDName, DefUnit, Line,
1913 Size, Align, 0, 0, llvm::DIType(),
Eric Christopher86211df2012-02-20 18:05:24 +00001914 llvm::DIArray(), llvm::DIType(),
Eric Christopher9965dea2012-02-16 22:54:45 +00001915 llvm::DIArray());
1916 } else
1917 RealDecl = DBuilder.createStructType(RDContext, RDName, DefUnit, Line,
1918 Size, Align, 0, llvm::DIArray());
1919
1920 RegionMap[Ty->getDecl()] = llvm::WeakVH(RealDecl);
1921 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = llvm::DIType(RealDecl);
1922
1923 if (CXXDecl) {
1924 // A class's primary base or the class itself contains the vtable.
1925 llvm::MDNode *ContainingType = NULL;
1926 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1927 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
1928 // Seek non virtual primary base root.
1929 while (1) {
1930 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
1931 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
1932 if (PBT && !BRL.isPrimaryBaseVirtual())
1933 PBase = PBT;
1934 else
1935 break;
1936 }
1937 ContainingType =
1938 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), DefUnit);
1939 }
1940 else if (CXXDecl->isDynamicClass())
1941 ContainingType = RealDecl;
1942
Eric Christopher1e009d52012-02-17 07:09:48 +00001943 RealDecl->replaceOperandWith(12, ContainingType);
Eric Christopher9965dea2012-02-16 22:54:45 +00001944 }
1945 return llvm::DIType(RealDecl);
1946}
1947
1948/// CreateLimitedTypeNode - Create a new debug type node, but only forward
1949/// declare composite types that haven't been processed yet.
1950llvm::DIType CGDebugInfo::CreateLimitedTypeNode(QualType Ty,llvm::DIFile Unit) {
1951
1952 // Work out details of type.
1953 switch (Ty->getTypeClass()) {
1954#define TYPE(Class, Base)
1955#define ABSTRACT_TYPE(Class, Base)
1956#define NON_CANONICAL_TYPE(Class, Base)
1957#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1958 #include "clang/AST/TypeNodes.def"
1959 llvm_unreachable("Dependent types cannot show up in debug information");
1960
1961 case Type::Record:
1962 return CreateLimitedType(cast<RecordType>(Ty));
1963 default:
1964 return CreateTypeNode(Ty, Unit);
1965 }
1966}
1967
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001968/// CreateMemberType - Create new member and increase Offset by FType's size.
1969llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001970 StringRef Name,
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001971 uint64_t *Offset) {
1972 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1973 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1974 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel1d323e02011-06-24 22:00:59 +00001975 llvm::DIType Ty = DBuilder.createMemberType(Unit, Name, Unit, 0,
Devang Patel823d8e92010-12-08 22:42:58 +00001976 FieldSize, FieldAlign,
1977 *Offset, 0, FieldTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001978 *Offset += FieldSize;
1979 return Ty;
1980}
1981
Devang Patel120bf322011-04-23 00:08:01 +00001982/// getFunctionDeclaration - Return debug info descriptor to describe method
1983/// declaration for the given method definition.
1984llvm::DISubprogram CGDebugInfo::getFunctionDeclaration(const Decl *D) {
1985 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1986 if (!FD) return llvm::DISubprogram();
1987
1988 // Setup context.
1989 getContextDescriptor(cast<Decl>(D->getDeclContext()));
1990
Devang Patel22a5cdf2011-04-29 23:42:32 +00001991 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
Eric Christopherdeae6a82011-11-17 23:45:00 +00001992 MI = SPCache.find(FD->getCanonicalDecl());
Devang Patel22a5cdf2011-04-29 23:42:32 +00001993 if (MI != SPCache.end()) {
Richard Smithe7259aa2012-08-17 04:17:54 +00001994 llvm::Value *V = MI->second;
1995 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(V));
Devang Patel22a5cdf2011-04-29 23:42:32 +00001996 if (SP.isSubprogram() && !llvm::DISubprogram(SP).isDefinition())
1997 return SP;
1998 }
1999
Devang Patel120bf322011-04-23 00:08:01 +00002000 for (FunctionDecl::redecl_iterator I = FD->redecls_begin(),
2001 E = FD->redecls_end(); I != E; ++I) {
2002 const FunctionDecl *NextFD = *I;
2003 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
Eric Christopherdeae6a82011-11-17 23:45:00 +00002004 MI = SPCache.find(NextFD->getCanonicalDecl());
Devang Patel120bf322011-04-23 00:08:01 +00002005 if (MI != SPCache.end()) {
Richard Smithe7259aa2012-08-17 04:17:54 +00002006 llvm::Value *V = MI->second;
2007 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(V));
Devang Patel120bf322011-04-23 00:08:01 +00002008 if (SP.isSubprogram() && !llvm::DISubprogram(SP).isDefinition())
2009 return SP;
2010 }
2011 }
2012 return llvm::DISubprogram();
2013}
2014
Devang Patel1c296522011-05-31 20:46:46 +00002015// getOrCreateFunctionType - Construct DIType. If it is a c++ method, include
2016// implicit parameter "this".
Eric Christopher85f90bd2012-09-11 01:36:56 +00002017llvm::DIType CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Eric Christopherab5278e2011-10-11 23:00:51 +00002018 QualType FnType,
Devang Patel1c296522011-05-31 20:46:46 +00002019 llvm::DIFile F) {
Eric Christopher363e5ac2012-08-07 00:14:25 +00002020
Devang Patel1c296522011-05-31 20:46:46 +00002021 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2022 return getOrCreateMethodType(Method, F);
Nick Lewycky7480d962011-11-10 00:34:02 +00002023 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
Devang Patelc478f212011-05-31 21:18:50 +00002024 // Add "self" and "_cmd"
Chris Lattner5f9e2722011-07-23 10:55:15 +00002025 SmallVector<llvm::Value *, 16> Elts;
Devang Patelc478f212011-05-31 21:18:50 +00002026
2027 // First element is always return type. For 'void' functions it is NULL.
Devang Pateld127bcb2011-05-31 22:21:11 +00002028 Elts.push_back(getOrCreateType(OMethod->getResultType(), F));
Devang Patelc478f212011-05-31 21:18:50 +00002029 // "self" pointer is always first argument.
Eric Christopher3ed6b912012-09-11 01:36:54 +00002030 llvm::DIType SelfTy = getOrCreateType(OMethod->getSelfDecl()->getType(), F);
Eric Christopherd5a73dc2012-09-12 23:36:49 +00002031 Elts.push_back(DBuilder.createObjectPointerType(SelfTy));
Eric Christopher85f90bd2012-09-11 01:36:56 +00002032 // "_cmd" pointer is always second argument.
Eric Christopher3ed6b912012-09-11 01:36:54 +00002033 llvm::DIType CmdTy = getOrCreateType(OMethod->getCmdDecl()->getType(), F);
2034 Elts.push_back(DBuilder.createArtificialType(CmdTy));
Devang Pateld127bcb2011-05-31 22:21:11 +00002035 // Get rest of the arguments.
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002036 for (ObjCMethodDecl::param_const_iterator PI = OMethod->param_begin(),
Devang Pateld127bcb2011-05-31 22:21:11 +00002037 PE = OMethod->param_end(); PI != PE; ++PI)
2038 Elts.push_back(getOrCreateType((*PI)->getType(), F));
2039
Devang Patelc478f212011-05-31 21:18:50 +00002040 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(Elts);
2041 return DBuilder.createSubroutineType(F, EltTypeArray);
2042 }
Devang Patel1c296522011-05-31 20:46:46 +00002043 return getOrCreateType(FnType, F);
2044}
2045
Eric Christopher451b4412012-03-20 23:28:32 +00002046/// EmitFunctionStart - Constructs the debug code for entering a function.
Devang Patel9c6c3a02010-01-14 00:36:21 +00002047void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00002048 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00002049 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00002050
Chris Lattner5f9e2722011-07-23 10:55:15 +00002051 StringRef Name;
2052 StringRef LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00002053
Eric Christopheraa2164c2011-09-29 00:00:45 +00002054 FnBeginRegionCount.push_back(LexicalBlockStack.size());
Devang Patel5a6fbcf2010-07-22 22:29:16 +00002055
Devang Patel9c6c3a02010-01-14 00:36:21 +00002056 const Decl *D = GD.getDecl();
Alexey Samsonov34b41f82012-10-25 10:18:50 +00002057 // Function may lack declaration in source code if it is created by Clang
2058 // CodeGen (examples: _GLOBAL__I_a, __cxx_global_array_dtor, thunk).
2059 bool HasDecl = (D != 0);
Eric Christopherea320472012-04-03 00:44:15 +00002060 // Use the location of the declaration.
Alexey Samsonov34b41f82012-10-25 10:18:50 +00002061 SourceLocation Loc;
2062 if (HasDecl)
2063 Loc = D->getLocation();
2064
Devang Patel3951e712010-10-07 22:03:49 +00002065 unsigned Flags = 0;
Eric Christopherea320472012-04-03 00:44:15 +00002066 llvm::DIFile Unit = getOrCreateFile(Loc);
Devang Patel0692f832010-10-11 21:58:41 +00002067 llvm::DIDescriptor FDContext(Unit);
Devang Patel5ecb1df2011-04-05 22:54:11 +00002068 llvm::DIArray TParamsArray;
Alexey Samsonov34b41f82012-10-25 10:18:50 +00002069 if (!HasDecl) {
2070 // Use llvm function name.
2071 Name = Fn->getName();
2072 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Eric Christopherbf979472011-11-14 18:55:02 +00002073 // If there is a DISubprogram for this function available then use it.
Devang Patel4125fd22010-01-19 01:54:44 +00002074 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
Eric Christopherdeae6a82011-11-17 23:45:00 +00002075 FI = SPCache.find(FD->getCanonicalDecl());
Devang Patel4125fd22010-01-19 01:54:44 +00002076 if (FI != SPCache.end()) {
Richard Smithe7259aa2012-08-17 04:17:54 +00002077 llvm::Value *V = FI->second;
2078 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(V));
Devang Patelab699792010-05-07 18:12:35 +00002079 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
2080 llvm::MDNode *SPN = SP;
Eric Christopheraa2164c2011-09-29 00:00:45 +00002081 LexicalBlockStack.push_back(SPN);
Devang Patelab699792010-05-07 18:12:35 +00002082 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00002083 return;
2084 }
2085 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00002086 Name = getFunctionName(FD);
2087 // Use mangled name as linkage name for c/c++ functions.
Eric Christopher43443de2012-04-12 00:35:06 +00002088 if (FD->hasPrototype()) {
Devang Patel2df74c02011-05-02 22:37:48 +00002089 LinkageName = CGM.getMangledName(GD);
Eric Christopher43443de2012-04-12 00:35:06 +00002090 Flags |= llvm::DIDescriptor::FlagPrototyped;
2091 }
Alexey Samsonovfd00eec2012-05-04 07:39:27 +00002092 if (LinkageName == Name ||
Douglas Gregor4cdad312012-10-23 20:05:01 +00002093 CGM.getCodeGenOpts().getDebugInfo() <= CodeGenOptions::DebugLineTablesOnly)
Chris Lattner5f9e2722011-07-23 10:55:15 +00002094 LinkageName = StringRef();
Eric Christopher43443de2012-04-12 00:35:06 +00002095
Douglas Gregor4cdad312012-10-23 20:05:01 +00002096 if (CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo) {
Alexey Samsonovfd00eec2012-05-04 07:39:27 +00002097 if (const NamespaceDecl *NSDecl =
2098 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2099 FDContext = getOrCreateNameSpace(NSDecl);
2100 else if (const RecordDecl *RDecl =
2101 dyn_cast_or_null<RecordDecl>(FD->getDeclContext()))
2102 FDContext = getContextDescriptor(cast<Decl>(RDecl->getDeclContext()));
Devang Patel5ecb1df2011-04-05 22:54:11 +00002103
Alexey Samsonovfd00eec2012-05-04 07:39:27 +00002104 // Collect template parameters.
2105 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2106 }
David Chisnall70b9b442010-09-02 17:16:32 +00002107 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
David Chisnall52044a22010-09-02 18:01:51 +00002108 Name = getObjCMethodName(OMD);
Devang Patel3951e712010-10-07 22:03:49 +00002109 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00002110 } else {
Devang Patel58faf202010-10-22 17:11:50 +00002111 // Use llvm function name.
Devang Patel9c6c3a02010-01-14 00:36:21 +00002112 Name = Fn->getName();
Devang Patel3951e712010-10-07 22:03:49 +00002113 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00002114 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00002115 if (!Name.empty() && Name[0] == '\01')
2116 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002117
Eric Christopherea320472012-04-03 00:44:15 +00002118 unsigned LineNo = getLineNumber(Loc);
Alexey Samsonov34b41f82012-10-25 10:18:50 +00002119 if (!HasDecl || D->isImplicit())
Devang Patele2472482010-09-29 21:05:52 +00002120 Flags |= llvm::DIDescriptor::FlagArtificial;
Eric Christopherea320472012-04-03 00:44:15 +00002121
Alexey Samsonovfd00eec2012-05-04 07:39:27 +00002122 llvm::DIType DIFnType;
2123 llvm::DISubprogram SPDecl;
Alexey Samsonov34b41f82012-10-25 10:18:50 +00002124 if (HasDecl &&
2125 CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo) {
Alexey Samsonovfd00eec2012-05-04 07:39:27 +00002126 DIFnType = getOrCreateFunctionType(D, FnType, Unit);
2127 SPDecl = getFunctionDeclaration(D);
2128 } else {
2129 // Create fake but valid subroutine type. Otherwise
2130 // llvm::DISubprogram::Verify() would return false, and
2131 // subprogram DIE will miss DW_AT_decl_file and
2132 // DW_AT_decl_line fields.
2133 SmallVector<llvm::Value*, 16> Elts;
2134 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(Elts);
2135 DIFnType = DBuilder.createSubroutineType(Unit, EltTypeArray);
2136 }
2137 llvm::DISubprogram SP;
2138 SP = DBuilder.createFunction(FDContext, Name, LinkageName, Unit,
2139 LineNo, DIFnType,
2140 Fn->hasInternalLinkage(), true/*definition*/,
2141 getLineNumber(CurLoc), Flags,
2142 CGM.getLangOpts().Optimize,
2143 Fn, TParamsArray, SPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002144
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00002145 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00002146 llvm::MDNode *SPN = SP;
Eric Christopheraa2164c2011-09-29 00:00:45 +00002147 LexicalBlockStack.push_back(SPN);
Alexey Samsonov34b41f82012-10-25 10:18:50 +00002148 if (HasDecl)
2149 RegionMap[D] = llvm::WeakVH(SP);
Eric Christopher69a1b742011-09-29 00:00:37 +00002150}
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00002151
Eric Christopher5321bc42011-09-29 00:00:41 +00002152/// EmitLocation - Emit metadata to indicate a change in line/column
2153/// information in the source file.
Eric Christopher73fb3502011-10-13 21:45:18 +00002154void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
2155
2156 // Update our current location
2157 setLocation(Loc);
2158
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00002159 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00002160
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00002161 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00002162 SourceManager &SM = CGM.getContext().getSourceManager();
Eric Christopher73fb3502011-10-13 21:45:18 +00002163 if (CurLoc == PrevLoc ||
Chandler Carruth40278532011-07-25 16:49:02 +00002164 SM.getExpansionLoc(CurLoc) == SM.getExpansionLoc(PrevLoc))
Devang Patel4800ea62010-04-05 21:09:15 +00002165 // New Builder may not be in sync with CGDebugInfo.
2166 if (!Builder.getCurrentDebugLocation().isUnknown())
2167 return;
Eric Christopher414ee4b2011-09-29 00:00:35 +00002168
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00002169 // Update last state.
2170 PrevLoc = CurLoc;
2171
Eric Christopheraa2164c2011-09-29 00:00:45 +00002172 llvm::MDNode *Scope = LexicalBlockStack.back();
Devang Patel8ab870d2010-05-12 23:46:38 +00002173 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
2174 getColumnNumber(CurLoc),
Chris Lattnere541d012010-04-02 20:21:43 +00002175 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00002176}
2177
Eric Christopher73fb3502011-10-13 21:45:18 +00002178/// CreateLexicalBlock - Creates a new lexical block node and pushes it on
2179/// the stack.
2180void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Devang Patel8fae0602009-11-13 19:10:24 +00002181 llvm::DIDescriptor D =
Eric Christopher73fb3502011-10-13 21:45:18 +00002182 DBuilder.createLexicalBlock(LexicalBlockStack.empty() ?
Devang Patel53bc5182012-02-08 00:10:20 +00002183 llvm::DIDescriptor() :
2184 llvm::DIDescriptor(LexicalBlockStack.back()),
2185 getOrCreateFile(CurLoc),
2186 getLineNumber(CurLoc),
2187 getColumnNumber(CurLoc));
Devang Patelab699792010-05-07 18:12:35 +00002188 llvm::MDNode *DN = D;
Eric Christopheraa2164c2011-09-29 00:00:45 +00002189 LexicalBlockStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00002190}
2191
Eric Christopher73fb3502011-10-13 21:45:18 +00002192/// EmitLexicalBlockStart - Constructs the debug code for entering a declarative
2193/// region - beginning of a DW_TAG_lexical_block.
2194void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc) {
2195 // Set our current location.
2196 setLocation(Loc);
2197
2198 // Create a new lexical block and push it on the stack.
2199 CreateLexicalBlock(Loc);
2200
2201 // Emit a line table change for the current location inside the new scope.
2202 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(Loc),
Devang Patel53bc5182012-02-08 00:10:20 +00002203 getColumnNumber(Loc),
2204 LexicalBlockStack.back()));
Eric Christopher73fb3502011-10-13 21:45:18 +00002205}
2206
Eric Christopheraa2164c2011-09-29 00:00:45 +00002207/// EmitLexicalBlockEnd - Constructs the debug code for exiting a declarative
Eric Christopher43202ae2011-09-26 15:03:22 +00002208/// region - end of a DW_TAG_lexical_block.
Eric Christopher73fb3502011-10-13 21:45:18 +00002209void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc) {
Eric Christopheraa2164c2011-09-29 00:00:45 +00002210 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherc852e9f2012-07-11 15:32:13 +00002211
2212 // Provide an entry in the line table for the end of the block.
2213 EmitLocation(Builder, Loc);
2214
Eric Christopheraa2164c2011-09-29 00:00:45 +00002215 LexicalBlockStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00002216}
2217
Devang Patel5a6fbcf2010-07-22 22:29:16 +00002218/// EmitFunctionEnd - Constructs the debug code for exiting a function.
2219void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
Eric Christopheraa2164c2011-09-29 00:00:45 +00002220 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Devang Patel5a6fbcf2010-07-22 22:29:16 +00002221 unsigned RCount = FnBeginRegionCount.back();
Eric Christopheraa2164c2011-09-29 00:00:45 +00002222 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
Devang Patel5a6fbcf2010-07-22 22:29:16 +00002223
2224 // Pop all regions for this function.
Eric Christopheraa2164c2011-09-29 00:00:45 +00002225 while (LexicalBlockStack.size() != RCount)
Eric Christopher73fb3502011-10-13 21:45:18 +00002226 EmitLexicalBlockEnd(Builder, CurLoc);
Devang Patel5a6fbcf2010-07-22 22:29:16 +00002227 FnBeginRegionCount.pop_back();
2228}
2229
Devang Patel809b9bb2010-02-10 18:49:08 +00002230// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
2231// See BuildByRefType.
2232llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
2233 uint64_t *XOffset) {
2234
Chris Lattner5f9e2722011-07-23 10:55:15 +00002235 SmallVector<llvm::Value *, 5> EltTys;
Devang Patel809b9bb2010-02-10 18:49:08 +00002236 QualType FType;
2237 uint64_t FieldSize, FieldOffset;
2238 unsigned FieldAlign;
2239
Devang Patel17800552010-03-09 00:44:50 +00002240 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00002241 QualType Type = VD->getType();
2242
2243 FieldOffset = 0;
2244 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00002245 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2246 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00002247 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00002248 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2249 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2250
John McCall6b5a61b2011-02-07 10:33:21 +00002251 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type);
Devang Patel809b9bb2010-02-10 18:49:08 +00002252 if (HasCopyAndDispose) {
2253 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00002254 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
2255 &FieldOffset));
2256 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
2257 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00002258 }
2259
2260 CharUnits Align = CGM.getContext().getDeclAlign(VD);
Ken Dyck573be632011-04-22 17:34:18 +00002261 if (Align > CGM.getContext().toCharUnitsFromBits(
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002262 CGM.getContext().getTargetInfo().getPointerAlign(0))) {
Ken Dyck573be632011-04-22 17:34:18 +00002263 CharUnits FieldOffsetInBytes
2264 = CGM.getContext().toCharUnitsFromBits(FieldOffset);
2265 CharUnits AlignedOffsetInBytes
2266 = FieldOffsetInBytes.RoundUpToAlignment(Align);
2267 CharUnits NumPaddingBytes
2268 = AlignedOffsetInBytes - FieldOffsetInBytes;
Devang Patel809b9bb2010-02-10 18:49:08 +00002269
Ken Dyck573be632011-04-22 17:34:18 +00002270 if (NumPaddingBytes.isPositive()) {
2271 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
Devang Patel809b9bb2010-02-10 18:49:08 +00002272 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2273 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00002274 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00002275 }
2276 }
2277
2278 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00002279 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00002280 FieldSize = CGM.getContext().getTypeSize(FType);
Ken Dyck573be632011-04-22 17:34:18 +00002281 FieldAlign = CGM.getContext().toBits(Align);
Devang Patel809b9bb2010-02-10 18:49:08 +00002282
2283 *XOffset = FieldOffset;
Devang Patel1d323e02011-06-24 22:00:59 +00002284 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit,
Devang Patel823d8e92010-12-08 22:42:58 +00002285 0, FieldSize, FieldAlign,
2286 FieldOffset, 0, FieldTy);
Devang Patel809b9bb2010-02-10 18:49:08 +00002287 EltTys.push_back(FieldTy);
2288 FieldOffset += FieldSize;
2289
Jay Foadc556ef22011-04-24 10:11:03 +00002290 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Devang Patel809b9bb2010-02-10 18:49:08 +00002291
Devang Patele2472482010-09-29 21:05:52 +00002292 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Devang Patel809b9bb2010-02-10 18:49:08 +00002293
Devang Patel16674e82011-02-22 18:56:36 +00002294 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Devang Patel823d8e92010-12-08 22:42:58 +00002295 Elements);
Devang Patel809b9bb2010-02-10 18:49:08 +00002296}
Devang Patel823d8e92010-12-08 22:42:58 +00002297
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00002298/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00002299void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Devang Patel093ac462011-03-03 20:13:15 +00002300 llvm::Value *Storage,
2301 unsigned ArgNo, CGBuilderTy &Builder) {
Douglas Gregor4cdad312012-10-23 20:05:01 +00002302 assert(CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo);
Eric Christopheraa2164c2011-09-29 00:00:45 +00002303 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Daniel Dunbar5273f512008-10-17 01:07:56 +00002304
Devang Patel17800552010-03-09 00:44:50 +00002305 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00002306 llvm::DIType Ty;
2307 uint64_t XOffset = 0;
2308 if (VD->hasAttr<BlocksAttr>())
2309 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
2310 else
2311 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00002312
Eric Christopher195ff582012-09-19 21:47:29 +00002313 // If there is no debug info for this type then do not emit debug info
Devang Patelf4e54a22010-05-07 23:05:55 +00002314 // for this variable.
2315 if (!Ty)
2316 return;
2317
Devang Patel34753802011-02-16 01:11:51 +00002318 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage)) {
2319 // If Storage is an aggregate returned as 'sret' then let debugger know
2320 // about this.
Devang Patel0691f932011-02-10 00:40:52 +00002321 if (Arg->hasStructRetAttr())
Eric Christopher37e4cea2012-05-19 01:36:50 +00002322 Ty = DBuilder.createReferenceType(llvm::dwarf::DW_TAG_reference_type, Ty);
Devang Patel34753802011-02-16 01:11:51 +00002323 else if (CXXRecordDecl *Record = VD->getType()->getAsCXXRecordDecl()) {
2324 // If an aggregate variable has non trivial destructor or non trivial copy
2325 // constructor than it is pass indirectly. Let debug info know about this
2326 // by using reference of the aggregate type as a argument type.
Eric Christopherab5278e2011-10-11 23:00:51 +00002327 if (!Record->hasTrivialCopyConstructor() ||
2328 !Record->hasTrivialDestructor())
Eric Christopher37e4cea2012-05-19 01:36:50 +00002329 Ty = DBuilder.createReferenceType(llvm::dwarf::DW_TAG_reference_type, Ty);
Devang Patel34753802011-02-16 01:11:51 +00002330 }
2331 }
Devang Patel0691f932011-02-10 00:40:52 +00002332
Chris Lattner9c85ba32008-11-10 06:08:34 +00002333 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00002334 unsigned Line = getLineNumber(VD->getLocation());
2335 unsigned Column = getColumnNumber(VD->getLocation());
Devang Patelaca745b2010-09-29 23:09:21 +00002336 unsigned Flags = 0;
2337 if (VD->isImplicit())
2338 Flags |= llvm::DIDescriptor::FlagArtificial;
Eric Christopherd5a73dc2012-09-12 23:36:49 +00002339 // If this is the first argument and it is implicit then
2340 // give it an object pointer flag.
2341 // FIXME: There has to be a better way to do this, but for static
2342 // functions there won't be an implicit param at arg1 and
2343 // otherwise it is 'self' or 'this'.
2344 if (isa<ImplicitParamDecl>(VD) && ArgNo == 1)
2345 Flags |= llvm::DIDescriptor::FlagObjectPointer;
2346
Eric Christopheraa2164c2011-09-29 00:00:45 +00002347 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christopherd5a73dc2012-09-12 23:36:49 +00002348
Chris Lattner5f9e2722011-07-23 10:55:15 +00002349 StringRef Name = VD->getName();
Devang Patelcebbedd2010-10-12 23:24:54 +00002350 if (!Name.empty()) {
Devang Patelb1fd0eb2011-01-11 00:30:27 +00002351 if (VD->hasAttr<BlocksAttr>()) {
2352 CharUnits offset = CharUnits::fromQuantity(32);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002353 SmallVector<llvm::Value *, 9> addr;
Chris Lattner8b418682012-02-07 00:39:47 +00002354 llvm::Type *Int64Ty = CGM.Int64Ty;
Devang Patel4a4e2ef2011-02-18 23:29:22 +00002355 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Devang Patelb1fd0eb2011-01-11 00:30:27 +00002356 // offset of __forwarding field
Ken Dyck0ebce0e2011-04-22 17:41:34 +00002357 offset = CGM.getContext().toCharUnitsFromBits(
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002358 CGM.getContext().getTargetInfo().getPointerWidth(0));
Devang Patelb1fd0eb2011-01-11 00:30:27 +00002359 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Devang Patel4a4e2ef2011-02-18 23:29:22 +00002360 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
2361 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Devang Patelb1fd0eb2011-01-11 00:30:27 +00002362 // offset of x field
Ken Dyck0ebce0e2011-04-22 17:41:34 +00002363 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Devang Patelb1fd0eb2011-01-11 00:30:27 +00002364 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
2365
2366 // Create the descriptor for the variable.
2367 llvm::DIVariable D =
Devang Patel16674e82011-02-22 18:56:36 +00002368 DBuilder.createComplexVariable(Tag,
Eric Christopherab5278e2011-10-11 23:00:51 +00002369 llvm::DIDescriptor(Scope),
Devang Patelb1fd0eb2011-01-11 00:30:27 +00002370 VD->getName(), Unit, Line, Ty,
Jay Foadc556ef22011-04-24 10:11:03 +00002371 addr, ArgNo);
Devang Patelb1fd0eb2011-01-11 00:30:27 +00002372
2373 // Insert an llvm.dbg.declare into the current block.
2374 llvm::Instruction *Call =
Devang Patel16674e82011-02-22 18:56:36 +00002375 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patelb1fd0eb2011-01-11 00:30:27 +00002376 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
2377 return;
Eric Christophera135f2c2012-05-08 18:56:47 +00002378 } else if (isa<VariableArrayType>(VD->getType())) {
2379 // These are "complex" variables in that they need an op_deref.
Devang Patelb1fd0eb2011-01-11 00:30:27 +00002380 // Create the descriptor for the variable.
Eric Christophera135f2c2012-05-08 18:56:47 +00002381 llvm::Value *Addr = llvm::ConstantInt::get(CGM.Int64Ty,
2382 llvm::DIBuilder::OpDeref);
2383 llvm::DIVariable D =
2384 DBuilder.createComplexVariable(Tag,
2385 llvm::DIDescriptor(Scope),
2386 Name, Unit, Line, Ty,
2387 Addr, ArgNo);
2388
2389 // Insert an llvm.dbg.declare into the current block.
2390 llvm::Instruction *Call =
2391 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
2392 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
2393 return;
2394 }
2395
2396 // Create the descriptor for the variable.
Devang Patelcebbedd2010-10-12 23:24:54 +00002397 llvm::DIVariable D =
Devang Patel16674e82011-02-22 18:56:36 +00002398 DBuilder.createLocalVariable(Tag, llvm::DIDescriptor(Scope),
Devang Patel823d8e92010-12-08 22:42:58 +00002399 Name, Unit, Line, Ty,
David Blaikie4e4d0842012-03-11 07:00:24 +00002400 CGM.getLangOpts().Optimize, Flags, ArgNo);
Devang Patelcebbedd2010-10-12 23:24:54 +00002401
2402 // Insert an llvm.dbg.declare into the current block.
2403 llvm::Instruction *Call =
Devang Patel16674e82011-02-22 18:56:36 +00002404 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patelcebbedd2010-10-12 23:24:54 +00002405 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Devang Patelf4dd9622010-10-29 16:21:19 +00002406 return;
Devang Patelcebbedd2010-10-12 23:24:54 +00002407 }
2408
2409 // If VD is an anonymous union then Storage represents value for
2410 // all union fields.
John McCall8178df32011-02-22 22:38:33 +00002411 if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2412 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
2413 if (RD->isUnion()) {
2414 for (RecordDecl::field_iterator I = RD->field_begin(),
2415 E = RD->field_end();
2416 I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002417 FieldDecl *Field = *I;
John McCall8178df32011-02-22 22:38:33 +00002418 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002419 StringRef FieldName = Field->getName();
Devang Patelcebbedd2010-10-12 23:24:54 +00002420
John McCall8178df32011-02-22 22:38:33 +00002421 // Ignore unnamed fields. Do not ignore unnamed records.
2422 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2423 continue;
Devang Patelcebbedd2010-10-12 23:24:54 +00002424
John McCall8178df32011-02-22 22:38:33 +00002425 // Use VarDecl's Tag, Scope and Line number.
2426 llvm::DIVariable D =
2427 DBuilder.createLocalVariable(Tag, llvm::DIDescriptor(Scope),
2428 FieldName, Unit, Line, FieldTy,
David Blaikie4e4d0842012-03-11 07:00:24 +00002429 CGM.getLangOpts().Optimize, Flags,
Devang Patel093ac462011-03-03 20:13:15 +00002430 ArgNo);
Devang Patelcebbedd2010-10-12 23:24:54 +00002431
John McCall8178df32011-02-22 22:38:33 +00002432 // Insert an llvm.dbg.declare into the current block.
2433 llvm::Instruction *Call =
2434 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
John McCall8178df32011-02-22 22:38:33 +00002435 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Devang Patelcebbedd2010-10-12 23:24:54 +00002436 }
John McCall8178df32011-02-22 22:38:33 +00002437 }
2438 }
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00002439}
2440
Devang Patele2d01912011-04-25 23:43:36 +00002441void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
2442 llvm::Value *Storage,
2443 CGBuilderTy &Builder) {
Douglas Gregor4cdad312012-10-23 20:05:01 +00002444 assert(CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo);
Devang Patele2d01912011-04-25 23:43:36 +00002445 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, 0, Builder);
2446}
Mike Stumpb1a6e682009-09-30 02:43:10 +00002447
Eric Christopher245d5a32012-09-21 22:18:42 +00002448void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(const VarDecl *VD,
2449 llvm::Value *Storage,
2450 CGBuilderTy &Builder,
2451 const CGBlockInfo &blockInfo) {
Douglas Gregor4cdad312012-10-23 20:05:01 +00002452 assert(CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo);
Eric Christopheraa2164c2011-09-29 00:00:45 +00002453 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Devang Patele2d01912011-04-25 23:43:36 +00002454
Devang Patel2b594b92010-04-26 23:28:46 +00002455 if (Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00002456 return;
Devang Patele2d01912011-04-25 23:43:36 +00002457
John McCall6b5a61b2011-02-07 10:33:21 +00002458 bool isByRef = VD->hasAttr<BlocksAttr>();
Devang Patele2d01912011-04-25 23:43:36 +00002459
Mike Stumpb1a6e682009-09-30 02:43:10 +00002460 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00002461 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00002462 llvm::DIType Ty;
John McCall6b5a61b2011-02-07 10:33:21 +00002463 if (isByRef)
Devang Patel809b9bb2010-02-10 18:49:08 +00002464 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
2465 else
2466 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00002467
Eric Christopher245d5a32012-09-21 22:18:42 +00002468 // Self is passed along as an implicit non-arg variable in a
2469 // block. Mark it as the object pointer.
2470 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
2471 Ty = DBuilder.createObjectPointerType(Ty);
2472
Mike Stumpb1a6e682009-09-30 02:43:10 +00002473 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00002474 unsigned Line = getLineNumber(VD->getLocation());
2475 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00002476
Micah Villmow25a6a842012-10-08 16:25:52 +00002477 const llvm::DataLayout &target = CGM.getDataLayout();
John McCall6b5a61b2011-02-07 10:33:21 +00002478
2479 CharUnits offset = CharUnits::fromQuantity(
2480 target.getStructLayout(blockInfo.StructureType)
2481 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
2482
Chris Lattner5f9e2722011-07-23 10:55:15 +00002483 SmallVector<llvm::Value *, 9> addr;
Chris Lattner8b418682012-02-07 00:39:47 +00002484 llvm::Type *Int64Ty = CGM.Int64Ty;
Devang Patel4a4e2ef2011-02-18 23:29:22 +00002485 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Chris Lattner14b1a362010-01-25 03:29:35 +00002486 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
John McCall6b5a61b2011-02-07 10:33:21 +00002487 if (isByRef) {
Devang Patel4a4e2ef2011-02-18 23:29:22 +00002488 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
2489 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00002490 // offset of __forwarding field
Eric Christopherab5278e2011-10-11 23:00:51 +00002491 offset = CGM.getContext()
Micah Villmowcadaf4b2012-10-11 17:21:41 +00002492 .toCharUnitsFromBits(target.getPointerSizeInBits(0));
Chris Lattner14b1a362010-01-25 03:29:35 +00002493 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Devang Patel4a4e2ef2011-02-18 23:29:22 +00002494 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
2495 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00002496 // offset of x field
Ken Dyck0ebce0e2011-04-22 17:41:34 +00002497 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Chris Lattner14b1a362010-01-25 03:29:35 +00002498 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00002499 }
2500
2501 // Create the descriptor for the variable.
2502 llvm::DIVariable D =
Devang Patele2d01912011-04-25 23:43:36 +00002503 DBuilder.createComplexVariable(llvm::dwarf::DW_TAG_auto_variable,
Eric Christopheraa2164c2011-09-29 00:00:45 +00002504 llvm::DIDescriptor(LexicalBlockStack.back()),
Jay Foadc556ef22011-04-24 10:11:03 +00002505 VD->getName(), Unit, Line, Ty, addr);
Mike Stumpb1a6e682009-09-30 02:43:10 +00002506 // Insert an llvm.dbg.declare into the current block.
Eric Christopher73fb3502011-10-13 21:45:18 +00002507 llvm::Instruction *Call =
Devang Patel50811d22011-04-25 23:52:27 +00002508 DBuilder.insertDeclare(Storage, D, Builder.GetInsertPoint());
Eric Christopher73fb3502011-10-13 21:45:18 +00002509 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column,
2510 LexicalBlockStack.back()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00002511}
2512
Chris Lattner9c85ba32008-11-10 06:08:34 +00002513/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
2514/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00002515void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Devang Patel093ac462011-03-03 20:13:15 +00002516 unsigned ArgNo,
Devang Patel34753802011-02-16 01:11:51 +00002517 CGBuilderTy &Builder) {
Douglas Gregor4cdad312012-10-23 20:05:01 +00002518 assert(CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo);
Devang Patel093ac462011-03-03 20:13:15 +00002519 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, ArgNo, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00002520}
2521
John McCall8178df32011-02-22 22:38:33 +00002522namespace {
2523 struct BlockLayoutChunk {
2524 uint64_t OffsetInBits;
2525 const BlockDecl::Capture *Capture;
2526 };
2527 bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
2528 return l.OffsetInBits < r.OffsetInBits;
2529 }
2530}
Chris Lattner9c85ba32008-11-10 06:08:34 +00002531
John McCall8178df32011-02-22 22:38:33 +00002532void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
2533 llvm::Value *addr,
2534 CGBuilderTy &Builder) {
Douglas Gregor4cdad312012-10-23 20:05:01 +00002535 assert(CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo);
John McCall8178df32011-02-22 22:38:33 +00002536 ASTContext &C = CGM.getContext();
2537 const BlockDecl *blockDecl = block.getBlockDecl();
2538
2539 // Collect some general information about the block's location.
2540 SourceLocation loc = blockDecl->getCaretLocation();
2541 llvm::DIFile tunit = getOrCreateFile(loc);
2542 unsigned line = getLineNumber(loc);
2543 unsigned column = getColumnNumber(loc);
2544
2545 // Build the debug-info type for the block literal.
Nick Lewycky7d4b1592011-05-02 01:41:48 +00002546 getContextDescriptor(cast<Decl>(blockDecl->getDeclContext()));
John McCall8178df32011-02-22 22:38:33 +00002547
2548 const llvm::StructLayout *blockLayout =
Micah Villmow25a6a842012-10-08 16:25:52 +00002549 CGM.getDataLayout().getStructLayout(block.StructureType);
John McCall8178df32011-02-22 22:38:33 +00002550
Chris Lattner5f9e2722011-07-23 10:55:15 +00002551 SmallVector<llvm::Value*, 16> fields;
John McCall8178df32011-02-22 22:38:33 +00002552 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
2553 blockLayout->getElementOffsetInBits(0),
Devang Patel1d323e02011-06-24 22:00:59 +00002554 tunit, tunit));
John McCall8178df32011-02-22 22:38:33 +00002555 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
2556 blockLayout->getElementOffsetInBits(1),
Devang Patel1d323e02011-06-24 22:00:59 +00002557 tunit, tunit));
John McCall8178df32011-02-22 22:38:33 +00002558 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
2559 blockLayout->getElementOffsetInBits(2),
Devang Patel1d323e02011-06-24 22:00:59 +00002560 tunit, tunit));
John McCall8178df32011-02-22 22:38:33 +00002561 fields.push_back(createFieldType("__FuncPtr", C.VoidPtrTy, 0, loc, AS_public,
2562 blockLayout->getElementOffsetInBits(3),
Devang Patel1d323e02011-06-24 22:00:59 +00002563 tunit, tunit));
John McCall8178df32011-02-22 22:38:33 +00002564 fields.push_back(createFieldType("__descriptor",
2565 C.getPointerType(block.NeedsCopyDispose ?
2566 C.getBlockDescriptorExtendedType() :
2567 C.getBlockDescriptorType()),
2568 0, loc, AS_public,
2569 blockLayout->getElementOffsetInBits(4),
Devang Patel1d323e02011-06-24 22:00:59 +00002570 tunit, tunit));
John McCall8178df32011-02-22 22:38:33 +00002571
2572 // We want to sort the captures by offset, not because DWARF
2573 // requires this, but because we're paranoid about debuggers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002574 SmallVector<BlockLayoutChunk, 8> chunks;
John McCall8178df32011-02-22 22:38:33 +00002575
2576 // 'this' capture.
2577 if (blockDecl->capturesCXXThis()) {
2578 BlockLayoutChunk chunk;
2579 chunk.OffsetInBits =
2580 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
2581 chunk.Capture = 0;
2582 chunks.push_back(chunk);
2583 }
2584
2585 // Variable captures.
2586 for (BlockDecl::capture_const_iterator
2587 i = blockDecl->capture_begin(), e = blockDecl->capture_end();
2588 i != e; ++i) {
2589 const BlockDecl::Capture &capture = *i;
2590 const VarDecl *variable = capture.getVariable();
2591 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
2592
2593 // Ignore constant captures.
2594 if (captureInfo.isConstant())
2595 continue;
2596
2597 BlockLayoutChunk chunk;
2598 chunk.OffsetInBits =
2599 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
2600 chunk.Capture = &capture;
2601 chunks.push_back(chunk);
2602 }
2603
2604 // Sort by offset.
2605 llvm::array_pod_sort(chunks.begin(), chunks.end());
2606
Chris Lattner5f9e2722011-07-23 10:55:15 +00002607 for (SmallVectorImpl<BlockLayoutChunk>::iterator
John McCall8178df32011-02-22 22:38:33 +00002608 i = chunks.begin(), e = chunks.end(); i != e; ++i) {
2609 uint64_t offsetInBits = i->OffsetInBits;
2610 const BlockDecl::Capture *capture = i->Capture;
2611
2612 // If we have a null capture, this must be the C++ 'this' capture.
2613 if (!capture) {
2614 const CXXMethodDecl *method =
2615 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
2616 QualType type = method->getThisType(C);
2617
2618 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
Devang Patel1d323e02011-06-24 22:00:59 +00002619 offsetInBits, tunit, tunit));
John McCall8178df32011-02-22 22:38:33 +00002620 continue;
2621 }
2622
2623 const VarDecl *variable = capture->getVariable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002624 StringRef name = variable->getName();
John McCalld113a6f2011-03-02 06:57:14 +00002625
2626 llvm::DIType fieldType;
2627 if (capture->isByRef()) {
2628 std::pair<uint64_t,unsigned> ptrInfo = C.getTypeInfo(C.VoidPtrTy);
2629
2630 // FIXME: this creates a second copy of this type!
2631 uint64_t xoffset;
2632 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
2633 fieldType = DBuilder.createPointerType(fieldType, ptrInfo.first);
Devang Patel1d323e02011-06-24 22:00:59 +00002634 fieldType = DBuilder.createMemberType(tunit, name, tunit, line,
John McCalld113a6f2011-03-02 06:57:14 +00002635 ptrInfo.first, ptrInfo.second,
2636 offsetInBits, 0, fieldType);
2637 } else {
2638 fieldType = createFieldType(name, variable->getType(), 0,
Devang Patel1d323e02011-06-24 22:00:59 +00002639 loc, AS_public, offsetInBits, tunit, tunit);
John McCalld113a6f2011-03-02 06:57:14 +00002640 }
2641 fields.push_back(fieldType);
John McCall8178df32011-02-22 22:38:33 +00002642 }
2643
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002644 SmallString<36> typeName;
John McCall8178df32011-02-22 22:38:33 +00002645 llvm::raw_svector_ostream(typeName)
2646 << "__block_literal_" << CGM.getUniqueBlockCount();
2647
Jay Foadc556ef22011-04-24 10:11:03 +00002648 llvm::DIArray fieldsArray = DBuilder.getOrCreateArray(fields);
John McCall8178df32011-02-22 22:38:33 +00002649
2650 llvm::DIType type =
2651 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
2652 CGM.getContext().toBits(block.BlockSize),
2653 CGM.getContext().toBits(block.BlockAlign),
2654 0, fieldsArray);
2655 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
2656
2657 // Get overall information about the block.
2658 unsigned flags = llvm::DIDescriptor::FlagArtificial;
Eric Christopheraa2164c2011-09-29 00:00:45 +00002659 llvm::MDNode *scope = LexicalBlockStack.back();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002660 StringRef name = ".block_descriptor";
John McCall8178df32011-02-22 22:38:33 +00002661
2662 // Create the descriptor for the parameter.
2663 llvm::DIVariable debugVar =
2664 DBuilder.createLocalVariable(llvm::dwarf::DW_TAG_arg_variable,
2665 llvm::DIDescriptor(scope),
2666 name, tunit, line, type,
David Blaikie4e4d0842012-03-11 07:00:24 +00002667 CGM.getLangOpts().Optimize, flags,
Devang Patel093ac462011-03-03 20:13:15 +00002668 cast<llvm::Argument>(addr)->getArgNo() + 1);
John McCall8178df32011-02-22 22:38:33 +00002669
2670 // Insert an llvm.dbg.value into the current block.
2671 llvm::Instruction *declare =
2672 DBuilder.insertDbgValueIntrinsic(addr, 0, debugVar,
2673 Builder.GetInsertBlock());
2674 declare->setDebugLoc(llvm::DebugLoc::get(line, column, scope));
2675}
Chris Lattner9c85ba32008-11-10 06:08:34 +00002676
Sanjiv Gupta686226b2008-06-05 08:59:10 +00002677/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00002678void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00002679 const VarDecl *D) {
Douglas Gregor4cdad312012-10-23 20:05:01 +00002680 assert(CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00002681 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00002682 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00002683 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +00002684
Eric Christopher73fb3502011-10-13 21:45:18 +00002685 setLocation(D->getLocation());
2686
Devang Pateleb6d79b2010-02-01 21:34:11 +00002687 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00002688 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002689
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00002690 // CodeGen turns int[] into int[1] so we'll do the same here.
Benjamin Kramer65263b42012-08-04 17:00:46 +00002691 llvm::APInt ConstVal(32, 1);
Anders Carlsson20f12a22009-12-06 18:00:51 +00002692 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00002693
Anders Carlsson20f12a22009-12-06 18:00:51 +00002694 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Nick Lewyckyd4c100e2011-11-09 04:25:21 +00002695 ArrayType::Normal, 0);
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00002696 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002697 StringRef DeclName = D->getName();
2698 StringRef LinkageName;
Devang Pateleb4c45b2011-02-09 19:16:38 +00002699 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext())
2700 && !isa<ObjCMethodDecl>(D->getDeclContext()))
Devang Patel8b90a782010-05-13 23:52:37 +00002701 LinkageName = Var->getName();
Devang Patel58faf202010-10-22 17:11:50 +00002702 if (LinkageName == DeclName)
Chris Lattner5f9e2722011-07-23 10:55:15 +00002703 LinkageName = StringRef();
Devang Pateleb6d79b2010-02-01 21:34:11 +00002704 llvm::DIDescriptor DContext =
Devang Patel170cef32010-12-09 00:33:05 +00002705 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()));
Devang Patel16674e82011-02-22 18:56:36 +00002706 DBuilder.createStaticVariable(DContext, DeclName, LinkageName,
Devang Patel823d8e92010-12-08 22:42:58 +00002707 Unit, LineNo, getOrCreateType(T, Unit),
2708 Var->hasInternalLinkage(), Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00002709}
2710
Devang Patel9ca36b62009-02-26 21:10:26 +00002711/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00002712void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00002713 ObjCInterfaceDecl *ID) {
Douglas Gregor4cdad312012-10-23 20:05:01 +00002714 assert(CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo);
Devang Patel9ca36b62009-02-26 21:10:26 +00002715 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00002716 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00002717 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patel9ca36b62009-02-26 21:10:26 +00002718
Chris Lattner5f9e2722011-07-23 10:55:15 +00002719 StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00002720
Devang Pateld6c5a262010-02-01 21:52:22 +00002721 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00002722 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002723
Devang Patel9ca36b62009-02-26 21:10:26 +00002724 // CodeGen turns int[] into int[1] so we'll do the same here.
Benjamin Kramer65263b42012-08-04 17:00:46 +00002725 llvm::APInt ConstVal(32, 1);
Anders Carlsson20f12a22009-12-06 18:00:51 +00002726 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00002727
Anders Carlsson20f12a22009-12-06 18:00:51 +00002728 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00002729 ArrayType::Normal, 0);
2730 }
2731
Devang Patel16674e82011-02-22 18:56:36 +00002732 DBuilder.createGlobalVariable(Name, Unit, LineNo,
Devang Patel823d8e92010-12-08 22:42:58 +00002733 getOrCreateType(T, Unit),
2734 Var->hasInternalLinkage(), Var);
Devang Patel9ca36b62009-02-26 21:10:26 +00002735}
Devang Patelabb485f2010-02-01 19:16:32 +00002736
Devang Patel25c2c8f2010-08-10 17:53:33 +00002737/// EmitGlobalVariable - Emit global variable's debug info.
2738void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
John McCall189d6ef2010-10-09 01:34:31 +00002739 llvm::Constant *Init) {
Douglas Gregor4cdad312012-10-23 20:05:01 +00002740 assert(CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo);
Devang Patel8d308382010-08-10 07:24:25 +00002741 // Create the descriptor for the variable.
2742 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Chris Lattner5f9e2722011-07-23 10:55:15 +00002743 StringRef Name = VD->getName();
Devang Patel0317ab02010-08-10 18:27:15 +00002744 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
Devang Patel6237cea2010-08-23 22:07:25 +00002745 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
Benjamin Kramer527e6162012-06-20 18:11:18 +00002746 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
2747 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
2748 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
Devang Patel6237cea2010-08-23 22:07:25 +00002749 }
Devang Patel0317ab02010-08-10 18:27:15 +00002750 // Do not use DIGlobalVariable for enums.
2751 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
2752 return;
Devang Patel16674e82011-02-22 18:56:36 +00002753 DBuilder.createStaticVariable(Unit, Name, Name, Unit,
Devang Patel823d8e92010-12-08 22:42:58 +00002754 getLineNumber(VD->getLocation()),
2755 Ty, true, Init);
Devang Patel8d308382010-08-10 07:24:25 +00002756}
2757
Devang Patelabb485f2010-02-01 19:16:32 +00002758/// getOrCreateNamesSpace - Return namespace descriptor for the given
2759/// namespace decl.
2760llvm::DINameSpace
Devang Patel170cef32010-12-09 00:33:05 +00002761CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
Devang Patelabb485f2010-02-01 19:16:32 +00002762 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
2763 NameSpaceCache.find(NSDecl);
2764 if (I != NameSpaceCache.end())
2765 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
2766
Devang Patel8ab870d2010-05-12 23:46:38 +00002767 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patel8c376682010-10-28 19:12:46 +00002768 llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation());
Devang Patelabb485f2010-02-01 19:16:32 +00002769 llvm::DIDescriptor Context =
Devang Patel170cef32010-12-09 00:33:05 +00002770 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()));
Devang Patelabb485f2010-02-01 19:16:32 +00002771 llvm::DINameSpace NS =
Devang Patel16674e82011-02-22 18:56:36 +00002772 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Devang Patelab699792010-05-07 18:12:35 +00002773 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00002774 return NS;
2775}
Eric Christopher7ff0c5d2012-02-18 00:50:17 +00002776
2777void CGDebugInfo::finalize(void) {
2778 for (std::vector<std::pair<void *, llvm::WeakVH> >::const_iterator VI
2779 = ReplaceMap.begin(), VE = ReplaceMap.end(); VI != VE; ++VI) {
2780 llvm::DIType Ty, RepTy;
2781 // Verify that the debug info still exists.
Richard Smithe7259aa2012-08-17 04:17:54 +00002782 if (llvm::Value *V = VI->second)
2783 Ty = llvm::DIType(cast<llvm::MDNode>(V));
Eric Christopher7ff0c5d2012-02-18 00:50:17 +00002784
2785 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
2786 TypeCache.find(VI->first);
2787 if (it != TypeCache.end()) {
2788 // Verify that the debug info still exists.
Richard Smithe7259aa2012-08-17 04:17:54 +00002789 if (llvm::Value *V = it->second)
2790 RepTy = llvm::DIType(cast<llvm::MDNode>(V));
Eric Christopher7ff0c5d2012-02-18 00:50:17 +00002791 }
2792
Eric Christopher86211df2012-02-20 18:05:24 +00002793 if (Ty.Verify() && Ty.isForwardDecl() && RepTy.Verify()) {
Eric Christopher7ff0c5d2012-02-18 00:50:17 +00002794 Ty.replaceAllUsesWith(RepTy);
Eric Christopher86211df2012-02-20 18:05:24 +00002795 }
Eric Christopher7ff0c5d2012-02-18 00:50:17 +00002796 }
2797 DBuilder.finalize();
2798}