blob: e6e7ecf5711028d927794211a8e5d0f5aac0afbb [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"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000018#include "clang/AST/ASTContext.h"
Devang Patel2ed8f002010-08-27 17:47:47 +000019#include "clang/AST/DeclFriend.h"
Devang Patel9ca36b62009-02-26 21:10:26 +000020#include "clang/AST/DeclObjC.h"
Devang Patel700a1cb2010-07-20 20:24:18 +000021#include "clang/AST/DeclTemplate.h"
Chris Lattner3cc5c402008-11-11 07:01:36 +000022#include "clang/AST/Expr.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000023#include "clang/AST/RecordLayout.h"
Benjamin Kramer00bd44d2012-02-04 12:31:12 +000024#include "clang/Basic/SourceManager.h"
Benjamin Kramerd7a3e2c2012-02-07 22:29:24 +000025#include "clang/Basic/FileManager.h"
Mike Stump5a862172009-09-15 21:48:34 +000026#include "clang/Basic/Version.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000027#include "clang/Frontend/CodeGenOptions.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000028#include "llvm/Constants.h"
29#include "llvm/DerivedTypes.h"
30#include "llvm/Instructions.h"
31#include "llvm/Intrinsics.h"
32#include "llvm/Module.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000033#include "llvm/ADT/StringExtras.h"
34#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000035#include "llvm/Support/Dwarf.h"
Benjamin Kramerbcbca752011-10-14 18:45:11 +000036#include "llvm/Support/FileSystem.h"
Micah Villmow25a6a842012-10-08 16:25:52 +000037#include "llvm/DataLayout.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000038using namespace clang;
39using namespace clang::CodeGen;
40
Anders Carlsson20f12a22009-12-06 18:00:51 +000041CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Devang Patel823d8e92010-12-08 22:42:58 +000042 : CGM(CGM), DBuilder(CGM.getModule()),
Dan Gohman4cac5b42010-08-20 22:02:57 +000043 BlockLiteralGenericSet(false) {
Devang Patel17800552010-03-09 00:44:50 +000044 CreateCompileUnit();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000045}
46
Chris Lattner9c85ba32008-11-10 06:08:34 +000047CGDebugInfo::~CGDebugInfo() {
Eric Christopherab5278e2011-10-11 23:00:51 +000048 assert(LexicalBlockStack.empty() &&
49 "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000050}
51
Chris Lattner9c85ba32008-11-10 06:08:34 +000052void CGDebugInfo::setLocation(SourceLocation Loc) {
Eric Christopher944542e2011-10-11 23:00:45 +000053 // If the new location isn't valid return.
54 if (!Loc.isValid()) return;
55
56 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
Eric Christopher73fb3502011-10-13 21:45:18 +000057
58 // If we've changed files in the middle of a lexical scope go ahead
59 // and create a new lexical scope with file node if it's different
60 // from the one in the scope.
61 if (LexicalBlockStack.empty()) return;
62
63 SourceManager &SM = CGM.getContext().getSourceManager();
64 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
65 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc);
66
67 if (PCLoc.isInvalid() || PPLoc.isInvalid() ||
68 !strcmp(PPLoc.getFilename(), PCLoc.getFilename()))
69 return;
70
71 llvm::MDNode *LB = LexicalBlockStack.back();
72 llvm::DIScope Scope = llvm::DIScope(LB);
73 if (Scope.isLexicalBlockFile()) {
74 llvm::DILexicalBlockFile LBF = llvm::DILexicalBlockFile(LB);
75 llvm::DIDescriptor D
76 = DBuilder.createLexicalBlockFile(LBF.getScope(),
Devang Patel53bc5182012-02-08 00:10:20 +000077 getOrCreateFile(CurLoc));
Eric Christopher73fb3502011-10-13 21:45:18 +000078 llvm::MDNode *N = D;
79 LexicalBlockStack.pop_back();
80 LexicalBlockStack.push_back(N);
81 } else if (Scope.isLexicalBlock()) {
82 llvm::DIDescriptor D
83 = DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc));
84 llvm::MDNode *N = D;
85 LexicalBlockStack.pop_back();
86 LexicalBlockStack.push_back(N);
87 }
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000088}
89
Devang Patel33583052010-01-28 23:15:27 +000090/// getContextDescriptor - Get context info for the decl.
Devang Patel170cef32010-12-09 00:33:05 +000091llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context) {
Devang Pateleb6d79b2010-02-01 21:34:11 +000092 if (!Context)
Devang Patel170cef32010-12-09 00:33:05 +000093 return TheCU;
Devang Pateleb6d79b2010-02-01 21:34:11 +000094
95 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
96 I = RegionMap.find(Context);
Richard Smithe7259aa2012-08-17 04:17:54 +000097 if (I != RegionMap.end()) {
98 llvm::Value *V = I->second;
99 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(V));
100 }
Devang Patel411894b2010-02-01 22:40:08 +0000101
Devang Pateleb6d79b2010-02-01 21:34:11 +0000102 // Check namespace.
103 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
Devang Patel170cef32010-12-09 00:33:05 +0000104 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl));
Devang Patel8b90a782010-05-13 23:52:37 +0000105
106 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context)) {
107 if (!RDecl->isDependentType()) {
Devang Patela2e57692010-10-28 17:27:32 +0000108 llvm::DIType Ty = getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Devang Patel170cef32010-12-09 00:33:05 +0000109 getOrCreateMainFile());
Devang Patel8b90a782010-05-13 23:52:37 +0000110 return llvm::DIDescriptor(Ty);
111 }
112 }
Devang Patel170cef32010-12-09 00:33:05 +0000113 return TheCU;
Devang Patel979ec2e2009-10-06 00:35:31 +0000114}
115
Devang Patel9c6c3a02010-01-14 00:36:21 +0000116/// getFunctionName - Get function name for the given FunctionDecl. If the
117/// name is constructred on demand (e.g. C++ destructor) then the name
118/// is stored on the side.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000119StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Devang Patel9c6c3a02010-01-14 00:36:21 +0000120 assert (FD && "Invalid FunctionDecl!");
121 IdentifierInfo *FII = FD->getIdentifier();
Eric Christopher16717452012-03-14 00:25:46 +0000122 FunctionTemplateSpecializationInfo *Info
123 = FD->getTemplateSpecializationInfo();
124 if (!Info && FII)
Devang Patel9c6c3a02010-01-14 00:36:21 +0000125 return FII->getName();
126
127 // Otherwise construct human readable name for debug info.
128 std::string NS = FD->getNameAsString();
129
Eric Christopher16717452012-03-14 00:25:46 +0000130 // Add any template specialization args.
131 if (Info) {
132 const TemplateArgumentList *TArgs = Info->TemplateArguments;
133 const TemplateArgument *Args = TArgs->data();
134 unsigned NumArgs = TArgs->size();
135 PrintingPolicy Policy(CGM.getLangOpts());
136 NS += TemplateSpecializationType::PrintTemplateArgumentList(Args,
137 NumArgs,
138 Policy);
139 }
140
Devang Patel9c6c3a02010-01-14 00:36:21 +0000141 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000142 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer1b627dc2010-01-23 18:16:07 +0000143 memcpy(StrPtr, NS.data(), NS.length());
Chris Lattner5f9e2722011-07-23 10:55:15 +0000144 return StringRef(StrPtr, NS.length());
Devang Patel9c6c3a02010-01-14 00:36:21 +0000145}
146
Chris Lattner5f9e2722011-07-23 10:55:15 +0000147StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000148 SmallString<256> MethodName;
David Chisnall52044a22010-09-02 18:01:51 +0000149 llvm::raw_svector_ostream OS(MethodName);
150 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
151 const DeclContext *DC = OMD->getDeclContext();
Devang Patela2e57692010-10-28 17:27:32 +0000152 if (const ObjCImplementationDecl *OID =
153 dyn_cast<const ObjCImplementationDecl>(DC)) {
David Chisnall52044a22010-09-02 18:01:51 +0000154 OS << OID->getName();
Devang Patela2e57692010-10-28 17:27:32 +0000155 } else if (const ObjCInterfaceDecl *OID =
156 dyn_cast<const ObjCInterfaceDecl>(DC)) {
Fariborz Jahanian1a4c9372010-10-18 17:51:06 +0000157 OS << OID->getName();
Devang Patela2e57692010-10-28 17:27:32 +0000158 } else if (const ObjCCategoryImplDecl *OCD =
159 dyn_cast<const ObjCCategoryImplDecl>(DC)){
Roman Divacky31ba6132012-09-06 15:59:27 +0000160 OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '(' <<
David Chisnall52044a22010-09-02 18:01:51 +0000161 OCD->getIdentifier()->getNameStart() << ')';
162 }
163 OS << ' ' << OMD->getSelector().getAsString() << ']';
164
165 char *StrPtr = DebugInfoNames.Allocate<char>(OS.tell());
166 memcpy(StrPtr, MethodName.begin(), OS.tell());
Chris Lattner5f9e2722011-07-23 10:55:15 +0000167 return StringRef(StrPtr, OS.tell());
David Chisnall52044a22010-09-02 18:01:51 +0000168}
169
Eric Christopherecae5962012-03-29 17:31:33 +0000170/// getSelectorName - Return selector name. This is used for debugging
171/// info.
172StringRef CGDebugInfo::getSelectorName(Selector S) {
173 const std::string &SName = S.getAsString();
174 char *StrPtr = DebugInfoNames.Allocate<char>(SName.size());
175 memcpy(StrPtr, SName.data(), SName.size());
176 return StringRef(StrPtr, SName.size());
177}
178
Devang Patel700a1cb2010-07-20 20:24:18 +0000179/// getClassName - Get class name including template argument list.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000180StringRef
Eric Christopher9caf4402012-02-08 01:53:14 +0000181CGDebugInfo::getClassName(const RecordDecl *RD) {
182 const ClassTemplateSpecializationDecl *Spec
Devang Patel700a1cb2010-07-20 20:24:18 +0000183 = dyn_cast<ClassTemplateSpecializationDecl>(RD);
184 if (!Spec)
185 return RD->getName();
186
187 const TemplateArgument *Args;
188 unsigned NumArgs;
Devang Patel700a1cb2010-07-20 20:24:18 +0000189 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
190 const TemplateSpecializationType *TST =
191 cast<TemplateSpecializationType>(TAW->getType());
192 Args = TST->getArgs();
193 NumArgs = TST->getNumArgs();
194 } else {
195 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Douglas Gregor910f8002010-11-07 23:05:16 +0000196 Args = TemplateArgs.data();
197 NumArgs = TemplateArgs.size();
Devang Patel700a1cb2010-07-20 20:24:18 +0000198 }
Benjamin Kramerc6b468e2012-04-13 18:00:37 +0000199 StringRef Name = RD->getIdentifier()->getName();
David Blaikie4e4d0842012-03-11 07:00:24 +0000200 PrintingPolicy Policy(CGM.getLangOpts());
Benjamin Kramerc6b468e2012-04-13 18:00:37 +0000201 std::string TemplateArgList =
202 TemplateSpecializationType::PrintTemplateArgumentList(Args, NumArgs, Policy);
Devang Patel700a1cb2010-07-20 20:24:18 +0000203
204 // Copy this name on the side and use its reference.
Benjamin Kramerc6b468e2012-04-13 18:00:37 +0000205 size_t Length = Name.size() + TemplateArgList.size();
206 char *StrPtr = DebugInfoNames.Allocate<char>(Length);
207 memcpy(StrPtr, Name.data(), Name.size());
208 memcpy(StrPtr + Name.size(), TemplateArgList.data(), TemplateArgList.size());
209 return StringRef(StrPtr, Length);
Devang Patel700a1cb2010-07-20 20:24:18 +0000210}
211
Devang Patel17800552010-03-09 00:44:50 +0000212/// getOrCreateFile - Get the file debug info descriptor for the input location.
213llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Devang Patel823d8e92010-12-08 22:42:58 +0000214 if (!Loc.isValid())
215 // If Location is not valid then use main input file.
Devang Patel16674e82011-02-22 18:56:36 +0000216 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
Devang Patel823d8e92010-12-08 22:42:58 +0000217
Anders Carlsson20f12a22009-12-06 18:00:51 +0000218 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel17800552010-03-09 00:44:50 +0000219 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Ted Kremenek9c250392010-03-30 00:27:51 +0000220
Chris Lattner5f9e2722011-07-23 10:55:15 +0000221 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
Douglas Gregor8c457a82010-11-11 20:45:16 +0000222 // If the location is not valid then use main input file.
Devang Patel16674e82011-02-22 18:56:36 +0000223 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
Douglas Gregor8c457a82010-11-11 20:45:16 +0000224
Ted Kremenek9c250392010-03-30 00:27:51 +0000225 // Cache the results.
226 const char *fname = PLoc.getFilename();
227 llvm::DenseMap<const char *, llvm::WeakVH>::iterator it =
228 DIFileCache.find(fname);
229
230 if (it != DIFileCache.end()) {
231 // Verify that the information still exists.
Richard Smithe7259aa2012-08-17 04:17:54 +0000232 if (llvm::Value *V = it->second)
233 return llvm::DIFile(cast<llvm::MDNode>(V));
Ted Kremenek9c250392010-03-30 00:27:51 +0000234 }
235
Devang Patel16674e82011-02-22 18:56:36 +0000236 llvm::DIFile F = DBuilder.createFile(PLoc.getFilename(), getCurrentDirname());
Ted Kremenek9c250392010-03-30 00:27:51 +0000237
Devang Patelab699792010-05-07 18:12:35 +0000238 DIFileCache[fname] = F;
Ted Kremenek9c250392010-03-30 00:27:51 +0000239 return F;
Devang Patel17800552010-03-09 00:44:50 +0000240}
Devang Patel8ab870d2010-05-12 23:46:38 +0000241
Devang Patel532105f2010-10-28 22:03:20 +0000242/// getOrCreateMainFile - Get the file info for main compile unit.
243llvm::DIFile CGDebugInfo::getOrCreateMainFile() {
Devang Patel16674e82011-02-22 18:56:36 +0000244 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
Devang Patel532105f2010-10-28 22:03:20 +0000245}
246
Devang Patel8ab870d2010-05-12 23:46:38 +0000247/// getLineNumber - Get line number for the location. If location is invalid
248/// then use current location.
249unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
Devang Patel362ed2a2012-02-06 23:24:13 +0000250 if (Loc.isInvalid() && CurLoc.isInvalid())
251 return 0;
Devang Patel8ab870d2010-05-12 23:46:38 +0000252 SourceManager &SM = CGM.getContext().getSourceManager();
253 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Douglas Gregor8c457a82010-11-11 20:45:16 +0000254 return PLoc.isValid()? PLoc.getLine() : 0;
Devang Patel8ab870d2010-05-12 23:46:38 +0000255}
256
257/// getColumnNumber - Get column number for the location. If location is
258/// invalid then use current location.
259unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) {
Devang Patel362ed2a2012-02-06 23:24:13 +0000260 if (Loc.isInvalid() && CurLoc.isInvalid())
261 return 0;
Eric Christopherda3301e2012-10-18 21:52:18 +0000262 if (!CGM.getCodeGenOpts().DebugColumnInfo)
263 return 0;
Devang Patel8ab870d2010-05-12 23:46:38 +0000264 SourceManager &SM = CGM.getContext().getSourceManager();
265 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Douglas Gregor8c457a82010-11-11 20:45:16 +0000266 return PLoc.isValid()? PLoc.getColumn() : 0;
Devang Patel8ab870d2010-05-12 23:46:38 +0000267}
268
Chris Lattner5f9e2722011-07-23 10:55:15 +0000269StringRef CGDebugInfo::getCurrentDirname() {
Nick Lewycky7c4fd912011-10-21 02:32:14 +0000270 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
271 return CGM.getCodeGenOpts().DebugCompilationDir;
272
Devang Patelac4d13c2010-07-27 15:17:16 +0000273 if (!CWDName.empty())
274 return CWDName;
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000275 SmallString<256> CWD;
Benjamin Kramerbcbca752011-10-14 18:45:11 +0000276 llvm::sys::fs::current_path(CWD);
277 char *CompDirnamePtr = DebugInfoNames.Allocate<char>(CWD.size());
278 memcpy(CompDirnamePtr, CWD.data(), CWD.size());
Chris Lattner5f9e2722011-07-23 10:55:15 +0000279 return CWDName = StringRef(CompDirnamePtr, CWD.size());
Devang Patelac4d13c2010-07-27 15:17:16 +0000280}
281
Devang Patel17800552010-03-09 00:44:50 +0000282/// CreateCompileUnit - Create new compile unit.
283void CGDebugInfo::CreateCompileUnit() {
284
285 // Get absolute path name.
Douglas Gregorac91b4c2010-03-18 23:46:43 +0000286 SourceManager &SM = CGM.getContext().getSourceManager();
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000287 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
288 if (MainFileName.empty())
Devang Patel22fe5852010-03-12 21:04:27 +0000289 MainFileName = "<unknown>";
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000290
Douglas Gregorf6728fc2010-03-22 21:28:29 +0000291 // The main file name provided via the "-main-file-name" option contains just
292 // the file name itself with no path information. This file name may have had
293 // a relative path, so we look into the actual file entry for the main
294 // file to determine the real absolute path for the file.
Devang Patel6e6bc392010-07-23 23:04:28 +0000295 std::string MainFileDir;
Devang Patelac4d13c2010-07-27 15:17:16 +0000296 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000297 MainFileDir = MainFile->getDir()->getName();
Devang Patelac4d13c2010-07-27 15:17:16 +0000298 if (MainFileDir != ".")
299 MainFileName = MainFileDir + "/" + MainFileName;
300 }
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000301
Devang Patelac4d13c2010-07-27 15:17:16 +0000302 // Save filename string.
303 char *FilenamePtr = DebugInfoNames.Allocate<char>(MainFileName.length());
304 memcpy(FilenamePtr, MainFileName.c_str(), MainFileName.length());
Chris Lattner5f9e2722011-07-23 10:55:15 +0000305 StringRef Filename(FilenamePtr, MainFileName.length());
Devang Patelac4d13c2010-07-27 15:17:16 +0000306
Chris Lattner515455a2009-03-25 03:28:08 +0000307 unsigned LangTag;
David Blaikie4e4d0842012-03-11 07:00:24 +0000308 const LangOptions &LO = CGM.getLangOpts();
Chris Lattner515455a2009-03-25 03:28:08 +0000309 if (LO.CPlusPlus) {
310 if (LO.ObjC1)
311 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
312 else
313 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
314 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000315 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000316 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000317 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000318 } else {
319 LangTag = llvm::dwarf::DW_LANG_C89;
320 }
Devang Patel446c6192009-04-17 21:06:59 +0000321
Daniel Dunbar19f19832010-08-24 17:41:09 +0000322 std::string Producer = getClangFullVersion();
Chris Lattner4c2577a2009-05-02 01:00:04 +0000323
324 // Figure out which version of the ObjC runtime we have.
325 unsigned RuntimeVers = 0;
326 if (LO.ObjC1)
John McCall260611a2012-06-20 06:18:46 +0000327 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000329 // Create new compile unit.
Devang Patel16674e82011-02-22 18:56:36 +0000330 DBuilder.createCompileUnit(
Devang Patel58115002010-07-27 20:49:59 +0000331 LangTag, Filename, getCurrentDirname(),
Devang Patel823d8e92010-12-08 22:42:58 +0000332 Producer,
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000333 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Devang Patel823d8e92010-12-08 22:42:58 +0000334 // FIXME - Eliminate TheCU.
335 TheCU = llvm::DICompileUnit(DBuilder.getCU());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000336}
337
Devang Patel65e99f22009-02-25 01:36:11 +0000338/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000339/// one if necessary.
Devang Patelf1d1d9a2010-11-01 16:52:40 +0000340llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000341 unsigned Encoding = 0;
Argyrios Kyrtzidis27a00972012-05-05 04:20:28 +0000342 StringRef BTName;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000343 switch (BT->getKind()) {
John McCalle0a22d02011-10-18 21:02:43 +0000344#define BUILTIN_TYPE(Id, SingletonId)
345#define PLACEHOLDER_TYPE(Id, SingletonId) \
346 case BuiltinType::Id:
347#include "clang/AST/BuiltinTypes.def"
Devang Patele7566cf2011-09-12 18:50:21 +0000348 case BuiltinType::Dependent:
John McCalle0a22d02011-10-18 21:02:43 +0000349 llvm_unreachable("Unexpected builtin type");
Devang Patele7566cf2011-09-12 18:50:21 +0000350 case BuiltinType::NullPtr:
Devang Patelf60dca32011-09-14 23:14:14 +0000351 return DBuilder.
David Blaikie4e4d0842012-03-11 07:00:24 +0000352 createNullPtrType(BT->getName(CGM.getContext().getLangOpts()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000353 case BuiltinType::Void:
354 return llvm::DIType();
Devang Patelc8972c62010-07-28 01:33:15 +0000355 case BuiltinType::ObjCClass:
Eric Christopherbf3a9662012-08-20 23:32:17 +0000356 if (ClassTy.Verify())
357 return ClassTy;
358 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
359 "objc_class", TheCU,
360 getOrCreateMainFile(), 0);
361 return ClassTy;
Devang Patelc8972c62010-07-28 01:33:15 +0000362 case BuiltinType::ObjCId: {
363 // typedef struct objc_class *Class;
364 // typedef struct objc_object {
365 // Class isa;
366 // } *id;
367
Eric Christopherbf3a9662012-08-20 23:32:17 +0000368 if (ObjTy.Verify())
369 return ObjTy;
370
371 if (!ClassTy.Verify())
372 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
373 "objc_class", TheCU,
374 getOrCreateMainFile(), 0);
375
Devang Patelc8972c62010-07-28 01:33:15 +0000376 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
377
Eric Christopherbf3a9662012-08-20 23:32:17 +0000378 llvm::DIType ISATy = DBuilder.createPointerType(ClassTy, Size);
Devang Patelc8972c62010-07-28 01:33:15 +0000379
Eric Christopherbf3a9662012-08-20 23:32:17 +0000380 llvm::DIType FwdTy = DBuilder.createStructType(TheCU, "objc_object",
Eric Christopher003e7562012-08-17 22:54:57 +0000381 getOrCreateMainFile(),
Eric Christopherbf3a9662012-08-20 23:32:17 +0000382 0, 0, 0, 0,
383 llvm::DIArray());
384
385 llvm::TrackingVH<llvm::MDNode> ObjNode(FwdTy);
Eric Christopher003e7562012-08-17 22:54:57 +0000386 SmallVector<llvm::Value *, 1> EltTys;
Devang Patelc8972c62010-07-28 01:33:15 +0000387 llvm::DIType FieldTy =
Eric Christopherbf3a9662012-08-20 23:32:17 +0000388 DBuilder.createMemberType(llvm::DIDescriptor(ObjNode), "isa",
Devang Patel1d323e02011-06-24 22:00:59 +0000389 getOrCreateMainFile(), 0, Size,
390 0, 0, 0, ISATy);
Devang Patelc8972c62010-07-28 01:33:15 +0000391 EltTys.push_back(FieldTy);
Jay Foadc556ef22011-04-24 10:11:03 +0000392 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopher003e7562012-08-17 22:54:57 +0000393
Eric Christopherbf3a9662012-08-20 23:32:17 +0000394 ObjNode->replaceOperandWith(10, Elements);
395 ObjTy = llvm::DIType(ObjNode);
396 return ObjTy;
Devang Patelc8972c62010-07-28 01:33:15 +0000397 }
Devang Patel6e108ce2011-02-09 03:15:05 +0000398 case BuiltinType::ObjCSel: {
Eric Christopherbf3a9662012-08-20 23:32:17 +0000399 if (SelTy.Verify())
400 return SelTy;
401 SelTy =
Eric Christopher917bc8d2012-02-20 18:05:04 +0000402 DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
Eric Christopher87380aa2012-04-23 19:00:24 +0000403 "objc_selector", TheCU, getOrCreateMainFile(),
Eric Christophere86b9ea2012-02-20 23:02:36 +0000404 0);
Eric Christopherbf3a9662012-08-20 23:32:17 +0000405 return SelTy;
Devang Patel6e108ce2011-02-09 03:15:05 +0000406 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000407 case BuiltinType::UChar:
408 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
409 case BuiltinType::Char_S:
410 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
Devang Patele8ee3f22011-09-12 17:11:58 +0000411 case BuiltinType::Char16:
412 case BuiltinType::Char32: Encoding = llvm::dwarf::DW_ATE_UTF; break;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000413 case BuiltinType::UShort:
414 case BuiltinType::UInt:
Devang Patel31c79b42011-05-05 17:06:30 +0000415 case BuiltinType::UInt128:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000416 case BuiltinType::ULong:
Devang Patel68f76b12011-09-10 00:44:49 +0000417 case BuiltinType::WChar_U:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000418 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
419 case BuiltinType::Short:
420 case BuiltinType::Int:
Devang Patel31c79b42011-05-05 17:06:30 +0000421 case BuiltinType::Int128:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000422 case BuiltinType::Long:
Devang Patel68f76b12011-09-10 00:44:49 +0000423 case BuiltinType::WChar_S:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000424 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
425 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +0000426 case BuiltinType::Half:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000427 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000428 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000429 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000430 }
Devang Patel05127ca2010-07-28 23:23:29 +0000431
432 switch (BT->getKind()) {
433 case BuiltinType::Long: BTName = "long int"; break;
434 case BuiltinType::LongLong: BTName = "long long int"; break;
435 case BuiltinType::ULong: BTName = "long unsigned int"; break;
436 case BuiltinType::ULongLong: BTName = "long long unsigned int"; break;
437 default:
David Blaikie4e4d0842012-03-11 07:00:24 +0000438 BTName = BT->getName(CGM.getContext().getLangOpts());
Devang Patel05127ca2010-07-28 23:23:29 +0000439 break;
440 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000441 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000442 uint64_t Size = CGM.getContext().getTypeSize(BT);
443 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Devang Patelca80a5f2009-10-20 19:55:01 +0000444 llvm::DIType DbgTy =
Devang Patel16674e82011-02-22 18:56:36 +0000445 DBuilder.createBasicType(BTName, Size, Align, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000446 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000447}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000448
Devang Patel344ff5d2010-12-09 00:25:29 +0000449llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty) {
Chris Lattnerb7003772009-04-23 06:13:01 +0000450 // Bit size, align and offset of the type.
451 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
452 if (Ty->isComplexIntegerType())
453 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Anders Carlsson20f12a22009-12-06 18:00:51 +0000455 uint64_t Size = CGM.getContext().getTypeSize(Ty);
456 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Devang Patelca80a5f2009-10-20 19:55:01 +0000457 llvm::DIType DbgTy =
Devang Patel16674e82011-02-22 18:56:36 +0000458 DBuilder.createBasicType("complex", Size, Align, Encoding);
Devang Patel823d8e92010-12-08 22:42:58 +0000459
Devang Patelca80a5f2009-10-20 19:55:01 +0000460 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000461}
462
John McCalla1805292009-09-25 01:40:47 +0000463/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000464/// a new one if necessary.
Devang Patel17800552010-03-09 00:44:50 +0000465llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +0000466 QualifierCollector Qc;
467 const Type *T = Qc.strip(Ty);
468
469 // Ignore these qualifiers for now.
470 Qc.removeObjCGCAttr();
471 Qc.removeAddressSpace();
John McCallf85e1932011-06-15 23:02:42 +0000472 Qc.removeObjCLifetime();
John McCalla1805292009-09-25 01:40:47 +0000473
Chris Lattner9c85ba32008-11-10 06:08:34 +0000474 // We will create one Derived type for one qualifier and recurse to handle any
475 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000476 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000477 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000478 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000479 Qc.removeConst();
480 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000481 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000482 Qc.removeVolatile();
483 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000484 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000485 Qc.removeRestrict();
486 } else {
487 assert(Qc.empty() && "Unknown type qualifier for debug info");
488 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000489 }
Mike Stump1eb44332009-09-09 15:08:12 +0000490
John McCall49f4e1c2010-12-10 11:01:00 +0000491 llvm::DIType FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
John McCalla1805292009-09-25 01:40:47 +0000492
Daniel Dunbar3845f862008-10-31 03:54:29 +0000493 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
494 // CVR derived types.
Devang Patel16674e82011-02-22 18:56:36 +0000495 llvm::DIType DbgTy = DBuilder.createQualifiedType(Tag, FromTy);
Devang Patel823d8e92010-12-08 22:42:58 +0000496
Devang Patelca80a5f2009-10-20 19:55:01 +0000497 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000498}
499
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000500llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000501 llvm::DIFile Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000502 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000503 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
504 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000505 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000506}
507
Chris Lattner9c85ba32008-11-10 06:08:34 +0000508llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000509 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000510 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
511 Ty->getPointeeType(), Unit);
512}
513
Eric Christopher5d613b52012-01-25 02:06:59 +0000514// Creates a forward declaration for a RecordDecl in the given context.
515llvm::DIType CGDebugInfo::createRecordFwdDecl(const RecordDecl *RD,
Devang Patel53bc5182012-02-08 00:10:20 +0000516 llvm::DIDescriptor Ctx) {
Eric Christopher5d613b52012-01-25 02:06:59 +0000517 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
518 unsigned Line = getLineNumber(RD->getLocation());
Eric Christophere88a71f2012-02-13 15:08:45 +0000519 StringRef RDName = RD->getName();
520
521 // Get the tag.
Eric Christopher5d613b52012-01-25 02:06:59 +0000522 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Eric Christophere88a71f2012-02-13 15:08:45 +0000523 unsigned Tag = 0;
524 if (CXXDecl) {
Joao Matos17d35c32012-08-31 22:18:20 +0000525 RDName = getClassName(RD);
526 Tag = llvm::dwarf::DW_TAG_class_type;
527 }
528 else if (RD->isStruct() || RD->isInterface())
529 Tag = llvm::dwarf::DW_TAG_structure_type;
530 else if (RD->isUnion())
531 Tag = llvm::dwarf::DW_TAG_union_type;
Eric Christopher5d613b52012-01-25 02:06:59 +0000532 else
533 llvm_unreachable("Unknown RecordDecl type!");
Eric Christophere88a71f2012-02-13 15:08:45 +0000534
535 // Create the type.
Eric Christopher87380aa2012-04-23 19:00:24 +0000536 return DBuilder.createForwardDecl(Tag, RDName, Ctx, DefUnit, Line);
Eric Christopher5d613b52012-01-25 02:06:59 +0000537}
538
Eric Christopher4ddca8a2012-01-20 22:10:15 +0000539// Walk up the context chain and create forward decls for record decls,
540// and normal descriptors for namespaces.
541llvm::DIDescriptor CGDebugInfo::createContextChain(const Decl *Context) {
542 if (!Context)
543 return TheCU;
544
545 // See if we already have the parent.
546 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
547 I = RegionMap.find(Context);
Richard Smithe7259aa2012-08-17 04:17:54 +0000548 if (I != RegionMap.end()) {
549 llvm::Value *V = I->second;
550 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(V));
551 }
Eric Christopher4ddca8a2012-01-20 22:10:15 +0000552
553 // Check namespace.
554 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
555 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl));
556
557 if (const RecordDecl *RD = dyn_cast<RecordDecl>(Context)) {
558 if (!RD->isDependentType()) {
Eric Christopher9965dea2012-02-16 22:54:45 +0000559 llvm::DIType Ty = getOrCreateLimitedType(CGM.getContext().getTypeDeclType(RD),
560 getOrCreateMainFile());
Eric Christopher4ddca8a2012-01-20 22:10:15 +0000561 return llvm::DIDescriptor(Ty);
562 }
563 }
564 return TheCU;
565}
566
Eric Christopheredc95922011-09-13 23:45:09 +0000567/// CreatePointeeType - Create Pointee type. If Pointee is a record
Devang Patelc69e1cf2010-09-30 19:05:55 +0000568/// then emit record's fwd if debug info size reduction is enabled.
569llvm::DIType CGDebugInfo::CreatePointeeType(QualType PointeeTy,
570 llvm::DIFile Unit) {
Alexey Samsonov3a70cd62012-04-27 07:24:20 +0000571 if (CGM.getCodeGenOpts().DebugInfo != CodeGenOptions::LimitedDebugInfo)
Devang Patelc69e1cf2010-09-30 19:05:55 +0000572 return getOrCreateType(PointeeTy, Unit);
Devang Patel41422512011-10-24 23:15:17 +0000573
574 // Limit debug info for the pointee type.
575
Eric Christopher973bbb62011-12-16 23:40:18 +0000576 // If we have an existing type, use that, it's still smaller than creating
577 // a new type.
578 llvm::DIType Ty = getTypeOrNull(PointeeTy);
579 if (Ty.Verify()) return Ty;
580
Devang Patel41422512011-10-24 23:15:17 +0000581 // Handle qualifiers.
582 if (PointeeTy.hasLocalQualifiers())
Eric Christopherd0a97c42012-08-07 00:18:40 +0000583 return CreateQualifiedType(PointeeTy, Unit);
Devang Patel41422512011-10-24 23:15:17 +0000584
Devang Patelc69e1cf2010-09-30 19:05:55 +0000585 if (const RecordType *RTy = dyn_cast<RecordType>(PointeeTy)) {
586 RecordDecl *RD = RTy->getDecl();
Devang Patelc69e1cf2010-09-30 19:05:55 +0000587 llvm::DIDescriptor FDContext =
John McCall8178df32011-02-22 22:38:33 +0000588 getContextDescriptor(cast<Decl>(RD->getDeclContext()));
Eric Christopher86211df2012-02-20 18:05:24 +0000589 llvm::DIType RetTy = createRecordFwdDecl(RD, FDContext);
590 TypeCache[QualType(RTy, 0).getAsOpaquePtr()] = RetTy;
591 return RetTy;
Devang Patelc69e1cf2010-09-30 19:05:55 +0000592 }
593 return getOrCreateType(PointeeTy, Unit);
Eric Christopher42e75da2012-02-13 14:56:11 +0000594
Devang Patelc69e1cf2010-09-30 19:05:55 +0000595}
596
Anders Carlssona031b352009-11-06 19:19:55 +0000597llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
598 const Type *Ty,
599 QualType PointeeTy,
Devang Patel17800552010-03-09 00:44:50 +0000600 llvm::DIFile Unit) {
Eric Christopher37e4cea2012-05-19 01:36:50 +0000601 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
602 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
603 return DBuilder.createReferenceType(Tag,
604 CreatePointeeType(PointeeTy, Unit));
Devang Patel823d8e92010-12-08 22:42:58 +0000605
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000606 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000607 // Size is always the size of a pointer. We can't use getTypeSize here
608 // because that does not return the correct value for references.
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000609 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000610 uint64_t Size = CGM.getContext().getTargetInfo().getPointerWidth(AS);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000611 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000612
Nick Lewycky7480d962011-11-10 00:34:02 +0000613 return DBuilder.createPointerType(CreatePointeeType(PointeeTy, Unit),
614 Size, Align);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000615}
616
Mike Stump9bc093c2009-05-14 02:03:51 +0000617llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000618 llvm::DIFile Unit) {
Mike Stump9bc093c2009-05-14 02:03:51 +0000619 if (BlockLiteralGenericSet)
620 return BlockLiteralGeneric;
621
Chris Lattner5f9e2722011-07-23 10:55:15 +0000622 SmallVector<llvm::Value *, 8> EltTys;
Mike Stump9bc093c2009-05-14 02:03:51 +0000623 llvm::DIType FieldTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000624 QualType FType;
625 uint64_t FieldSize, FieldOffset;
626 unsigned FieldAlign;
Mike Stump9bc093c2009-05-14 02:03:51 +0000627 llvm::DIArray Elements;
628 llvm::DIType EltTy, DescTy;
629
630 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000631 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000632 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
633 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000634
Jay Foadc556ef22011-04-24 10:11:03 +0000635 Elements = DBuilder.getOrCreateArray(EltTys);
Mike Stump9bc093c2009-05-14 02:03:51 +0000636 EltTys.clear();
637
Devang Patele2472482010-09-29 21:05:52 +0000638 unsigned Flags = llvm::DIDescriptor::FlagAppleBlock;
Devang Patel8ab870d2010-05-12 23:46:38 +0000639 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump3d363c52009-10-02 02:30:50 +0000640
Devang Patel16674e82011-02-22 18:56:36 +0000641 EltTy = DBuilder.createStructType(Unit, "__block_descriptor",
Devang Patel823d8e92010-12-08 22:42:58 +0000642 Unit, LineNo, FieldOffset, 0,
643 Flags, Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000644
Mike Stump9bc093c2009-05-14 02:03:51 +0000645 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000646 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000647
Devang Patel16674e82011-02-22 18:56:36 +0000648 DescTy = DBuilder.createPointerType(EltTy, Size);
Mike Stump9bc093c2009-05-14 02:03:51 +0000649
650 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000651 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000652 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson20f12a22009-12-06 18:00:51 +0000653 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000654 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
655 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramerd3651cc2010-04-24 20:26:20 +0000656 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000657 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000658
Anders Carlsson20f12a22009-12-06 18:00:51 +0000659 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000660 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000661 FieldSize = CGM.getContext().getTypeSize(Ty);
662 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Devang Patel1d323e02011-06-24 22:00:59 +0000663 FieldTy = DBuilder.createMemberType(Unit, "__descriptor", Unit,
Devang Patel823d8e92010-12-08 22:42:58 +0000664 LineNo, FieldSize, FieldAlign,
665 FieldOffset, 0, FieldTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000666 EltTys.push_back(FieldTy);
667
668 FieldOffset += FieldSize;
Jay Foadc556ef22011-04-24 10:11:03 +0000669 Elements = DBuilder.getOrCreateArray(EltTys);
Mike Stump9bc093c2009-05-14 02:03:51 +0000670
Devang Patel16674e82011-02-22 18:56:36 +0000671 EltTy = DBuilder.createStructType(Unit, "__block_literal_generic",
Devang Patel823d8e92010-12-08 22:42:58 +0000672 Unit, LineNo, FieldOffset, 0,
673 Flags, Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Mike Stump9bc093c2009-05-14 02:03:51 +0000675 BlockLiteralGenericSet = true;
Devang Patel16674e82011-02-22 18:56:36 +0000676 BlockLiteralGeneric = DBuilder.createPointerType(EltTy, Size);
Mike Stump9bc093c2009-05-14 02:03:51 +0000677 return BlockLiteralGeneric;
678}
679
Nick Lewycky7480d962011-11-10 00:34:02 +0000680llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty, llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000681 // Typedefs are derived from some other type. If we have a typedef of a
682 // typedef, make sure to emit the whole chain.
683 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Devang Patel823d8e92010-12-08 22:42:58 +0000684 if (!Src.Verify())
685 return llvm::DIType();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000686 // We don't set size information, but do specify where the typedef was
687 // declared.
Devang Patel8ab870d2010-05-12 23:46:38 +0000688 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Devang Patelc4903122011-06-03 17:23:47 +0000689 const TypedefNameDecl *TyDecl = Ty->getDecl();
Eric Christopher9965dea2012-02-16 22:54:45 +0000690
Nick Lewycky7480d962011-11-10 00:34:02 +0000691 llvm::DIDescriptor TypedefContext =
Devang Patelc4903122011-06-03 17:23:47 +0000692 getContextDescriptor(cast<Decl>(Ty->getDecl()->getDeclContext()));
Eric Christopher9965dea2012-02-16 22:54:45 +0000693
694 return
Nick Lewycky7480d962011-11-10 00:34:02 +0000695 DBuilder.createTypedef(Src, TyDecl->getName(), Unit, Line, TypedefContext);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000696}
697
Chris Lattner9c85ba32008-11-10 06:08:34 +0000698llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000699 llvm::DIFile Unit) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000700 SmallVector<llvm::Value *, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000701
Chris Lattner9c85ba32008-11-10 06:08:34 +0000702 // Add the result type at least.
703 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000704
Chris Lattner9c85ba32008-11-10 06:08:34 +0000705 // Set up remainder of arguments if there is a prototype.
706 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Devang Patelaf164bb2010-10-06 20:51:45 +0000707 if (isa<FunctionNoProtoType>(Ty))
Devang Patel16674e82011-02-22 18:56:36 +0000708 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Eric Christopheraa6eccc2012-08-04 00:11:22 +0000709 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
Eric Christopherd0a97c42012-08-07 00:18:40 +0000710 for (unsigned i = 0, e = FPT->getNumArgs(); i != e; ++i)
Eric Christopheraa6eccc2012-08-04 00:11:22 +0000711 EltTys.push_back(getOrCreateType(FPT->getArgType(i), Unit));
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000712 }
713
Jay Foadc556ef22011-04-24 10:11:03 +0000714 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(EltTys);
Eric Christopherd9f07d42012-05-16 22:02:36 +0000715 return DBuilder.createSubroutineType(Unit, EltTypeArray);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000716}
717
Eric Christopher42e75da2012-02-13 14:56:11 +0000718
Eric Christopher6faa5542012-01-26 01:57:13 +0000719void CGDebugInfo::
720CollectRecordStaticVars(const RecordDecl *RD, llvm::DIType FwdDecl) {
721
722 for (RecordDecl::decl_iterator I = RD->decls_begin(), E = RD->decls_end();
723 I != E; ++I)
724 if (const VarDecl *V = dyn_cast<VarDecl>(*I)) {
725 if (V->getInit()) {
726 const APValue *Value = V->evaluateValue();
727 if (Value && Value->isInt()) {
728 llvm::ConstantInt *CI
729 = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
730
731 // Create the descriptor for static variable.
732 llvm::DIFile VUnit = getOrCreateFile(V->getLocation());
733 StringRef VName = V->getName();
734 llvm::DIType VTy = getOrCreateType(V->getType(), VUnit);
735 // Do not use DIGlobalVariable for enums.
736 if (VTy.getTag() != llvm::dwarf::DW_TAG_enumeration_type) {
737 DBuilder.createStaticVariable(FwdDecl, VName, VName, VUnit,
738 getLineNumber(V->getLocation()),
739 VTy, true, CI);
740 }
741 }
742 }
743 }
744}
745
Chris Lattner5f9e2722011-07-23 10:55:15 +0000746llvm::DIType CGDebugInfo::createFieldType(StringRef name,
John McCall8178df32011-02-22 22:38:33 +0000747 QualType type,
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000748 uint64_t sizeInBitsOverride,
John McCall8178df32011-02-22 22:38:33 +0000749 SourceLocation loc,
750 AccessSpecifier AS,
751 uint64_t offsetInBits,
Devang Patel1d323e02011-06-24 22:00:59 +0000752 llvm::DIFile tunit,
753 llvm::DIDescriptor scope) {
John McCall8178df32011-02-22 22:38:33 +0000754 llvm::DIType debugType = getOrCreateType(type, tunit);
755
756 // Get the location for the field.
757 llvm::DIFile file = getOrCreateFile(loc);
758 unsigned line = getLineNumber(loc);
759
760 uint64_t sizeInBits = 0;
761 unsigned alignInBits = 0;
762 if (!type->isIncompleteArrayType()) {
763 llvm::tie(sizeInBits, alignInBits) = CGM.getContext().getTypeInfo(type);
764
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000765 if (sizeInBitsOverride)
766 sizeInBits = sizeInBitsOverride;
John McCall8178df32011-02-22 22:38:33 +0000767 }
768
769 unsigned flags = 0;
770 if (AS == clang::AS_private)
771 flags |= llvm::DIDescriptor::FlagPrivate;
772 else if (AS == clang::AS_protected)
773 flags |= llvm::DIDescriptor::FlagProtected;
774
Devang Patel1d323e02011-06-24 22:00:59 +0000775 return DBuilder.createMemberType(scope, name, file, line, sizeInBits,
776 alignInBits, offsetInBits, flags, debugType);
John McCall8178df32011-02-22 22:38:33 +0000777}
778
Devang Patel428deb52010-01-19 00:00:59 +0000779/// CollectRecordFields - A helper function to collect debug info for
780/// record fields. This is used while creating debug info entry for a Record.
781void CGDebugInfo::
John McCall8178df32011-02-22 22:38:33 +0000782CollectRecordFields(const RecordDecl *record, llvm::DIFile tunit,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000783 SmallVectorImpl<llvm::Value *> &elements,
Devang Patel1d323e02011-06-24 22:00:59 +0000784 llvm::DIType RecordTy) {
John McCall8178df32011-02-22 22:38:33 +0000785 unsigned fieldNo = 0;
786 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Eric Christopherad8de512012-03-01 21:36:52 +0000787 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
788
Eric Christopherda970d22012-06-28 01:20:05 +0000789 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
Eric Christopherad8de512012-03-01 21:36:52 +0000790 // has the name and the location of the variable so we should iterate over
791 // both concurrently.
792 if (CXXDecl && CXXDecl->isLambda()) {
793 RecordDecl::field_iterator Field = CXXDecl->field_begin();
794 unsigned fieldno = 0;
795 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
796 E = CXXDecl->captures_end(); I != E; ++I, ++Field, ++fieldno) {
797 const LambdaExpr::Capture C = *I;
Eric Christopherad8de512012-03-01 21:36:52 +0000798 if (C.capturesVariable()) {
799 VarDecl *V = C.getCapturedVar();
800 llvm::DIFile VUnit = getOrCreateFile(C.getLocation());
801 StringRef VName = V->getName();
802 uint64_t SizeInBitsOverride = 0;
803 if (Field->isBitField()) {
804 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
805 assert(SizeInBitsOverride && "found named 0-width bitfield");
806 }
807 llvm::DIType fieldType
808 = createFieldType(VName, Field->getType(), SizeInBitsOverride, C.getLocation(),
809 Field->getAccess(), layout.getFieldOffset(fieldno),
810 VUnit, RecordTy);
811 elements.push_back(fieldType);
Eric Christopher28e3c992012-09-19 21:47:34 +0000812 } else {
Eric Christopher20ec2c42012-09-19 22:01:42 +0000813 // TODO: Need to handle 'this' in some way by probably renaming the
814 // this of the lambda class and having a field member of 'this' or
Eric Christopher847665d2012-09-19 22:40:44 +0000815 // by using AT_object_pointer for the function and having that be
Eric Christopher20ec2c42012-09-19 22:01:42 +0000816 // used as 'this' for semantic references.
Eric Christopher28e3c992012-09-19 21:47:34 +0000817 assert(C.capturesThis() && "Field that isn't captured and isn't this?");
818 FieldDecl *f = *Field;
819 llvm::DIFile VUnit = getOrCreateFile(f->getLocation());
820 QualType type = f->getType();
821 llvm::DIType fieldType
822 = createFieldType("this", type, 0, f->getLocation(), f->getAccess(),
823 layout.getFieldOffset(fieldNo), VUnit, RecordTy);
824
825 elements.push_back(fieldType);
Eric Christopherad8de512012-03-01 21:36:52 +0000826 }
827 }
828 } else {
Eli Friedman5f608ae2012-10-12 23:29:20 +0000829 bool IsMsStruct = record->isMsStruct(CGM.getContext());
Eric Christopherad8de512012-03-01 21:36:52 +0000830 const FieldDecl *LastFD = 0;
831 for (RecordDecl::field_iterator I = record->field_begin(),
832 E = record->field_end();
833 I != E; ++I, ++fieldNo) {
David Blaikie581deb32012-06-06 20:45:41 +0000834 FieldDecl *field = *I;
Eric Christopherad8de512012-03-01 21:36:52 +0000835
836 if (IsMsStruct) {
837 // Zero-length bitfields following non-bitfield members are ignored
838 if (CGM.getContext().ZeroBitfieldFollowsNonBitfield((field), LastFD)) {
839 --fieldNo;
840 continue;
841 }
842 LastFD = field;
843 }
844
845 StringRef name = field->getName();
846 QualType type = field->getType();
847
848 // Ignore unnamed fields unless they're anonymous structs/unions.
849 if (name.empty() && !type->isRecordType()) {
850 LastFD = field;
Fariborz Jahanianfbc3cc62011-04-28 23:43:23 +0000851 continue;
852 }
Eric Christopherad8de512012-03-01 21:36:52 +0000853
854 uint64_t SizeInBitsOverride = 0;
855 if (field->isBitField()) {
856 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
857 assert(SizeInBitsOverride && "found named 0-width bitfield");
858 }
859
860 llvm::DIType fieldType
861 = createFieldType(name, type, SizeInBitsOverride,
862 field->getLocation(), field->getAccess(),
863 layout.getFieldOffset(fieldNo), tunit, RecordTy);
864
865 elements.push_back(fieldType);
Fariborz Jahanianfbc3cc62011-04-28 23:43:23 +0000866 }
Devang Patel428deb52010-01-19 00:00:59 +0000867 }
868}
869
Devang Patela6da1922010-01-28 00:28:01 +0000870/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
871/// function type is not updated to include implicit "this" pointer. Use this
872/// routine to get a method type which includes "this" pointer.
873llvm::DIType
874CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000875 llvm::DIFile Unit) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +0000876 llvm::DIType FnTy
877 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
878 0),
879 Unit);
Eric Christopher3b10cfe2012-03-13 23:40:48 +0000880
Devang Patela6da1922010-01-28 00:28:01 +0000881 // Add "this" pointer.
Devang Patelab699792010-05-07 18:12:35 +0000882 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patela6da1922010-01-28 00:28:01 +0000883 assert (Args.getNumElements() && "Invalid number of arguments!");
884
Chris Lattner5f9e2722011-07-23 10:55:15 +0000885 SmallVector<llvm::Value *, 16> Elts;
Devang Patela6da1922010-01-28 00:28:01 +0000886
887 // First element is always return type. For 'void' functions it is NULL.
888 Elts.push_back(Args.getElement(0));
889
Eric Christopher2121cda2011-09-14 01:10:50 +0000890 if (!Method->isStatic()) {
891 // "this" pointer is always first argument.
892 QualType ThisPtr = Method->getThisType(CGM.getContext());
Devang Patelef8857d2011-10-28 21:12:13 +0000893
894 const CXXRecordDecl *RD = Method->getParent();
895 if (isa<ClassTemplateSpecializationDecl>(RD)) {
896 // Create pointer type directly in this case.
897 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
898 QualType PointeeTy = ThisPtrTy->getPointeeType();
899 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
900 uint64_t Size = CGM.getContext().getTargetInfo().getPointerWidth(AS);
901 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
Nick Lewyckyd4c100e2011-11-09 04:25:21 +0000902 llvm::DIType PointeeType = getOrCreateType(PointeeTy, Unit);
Eric Christopher3b8e1972012-02-09 07:26:21 +0000903 llvm::DIType ThisPtrType = DBuilder.createPointerType(PointeeType, Size, Align);
Devang Patelef8857d2011-10-28 21:12:13 +0000904 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
Eric Christopher3b8e1972012-02-09 07:26:21 +0000905 // TODO: This and the artificial type below are misleading, the
906 // types aren't artificial the argument is, but the current
907 // metadata doesn't represent that.
Eric Christopherd5a73dc2012-09-12 23:36:49 +0000908 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
Devang Patelef8857d2011-10-28 21:12:13 +0000909 Elts.push_back(ThisPtrType);
910 } else {
Eric Christopher3b8e1972012-02-09 07:26:21 +0000911 llvm::DIType ThisPtrType = getOrCreateType(ThisPtr, Unit);
Devang Patelef8857d2011-10-28 21:12:13 +0000912 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
Eric Christopherd5a73dc2012-09-12 23:36:49 +0000913 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
Devang Patelef8857d2011-10-28 21:12:13 +0000914 Elts.push_back(ThisPtrType);
915 }
Eric Christopher2121cda2011-09-14 01:10:50 +0000916 }
Devang Patela6da1922010-01-28 00:28:01 +0000917
918 // Copy rest of the arguments.
919 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
920 Elts.push_back(Args.getElement(i));
921
Jay Foadc556ef22011-04-24 10:11:03 +0000922 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(Elts);
Devang Patela6da1922010-01-28 00:28:01 +0000923
Devang Patel16674e82011-02-22 18:56:36 +0000924 return DBuilder.createSubroutineType(Unit, EltTypeArray);
Devang Patela6da1922010-01-28 00:28:01 +0000925}
926
Devang Patel58faf202010-10-22 17:11:50 +0000927/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
928/// inside a function.
929static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
Nick Lewycky7480d962011-11-10 00:34:02 +0000930 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
Devang Patel58faf202010-10-22 17:11:50 +0000931 return isFunctionLocalClass(NRD);
Nick Lewycky7480d962011-11-10 00:34:02 +0000932 if (isa<FunctionDecl>(RD->getDeclContext()))
Devang Patel58faf202010-10-22 17:11:50 +0000933 return true;
934 return false;
Devang Patel58faf202010-10-22 17:11:50 +0000935}
Nick Lewyckyd4c100e2011-11-09 04:25:21 +0000936
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000937/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
938/// a single member function GlobalDecl.
939llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000940CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000941 llvm::DIFile Unit,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000942 llvm::DIType RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000943 bool IsCtorOrDtor =
944 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
945
Chris Lattner5f9e2722011-07-23 10:55:15 +0000946 StringRef MethodName = getFunctionName(Method);
Devang Patela6da1922010-01-28 00:28:01 +0000947 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Eric Christopheraa6eccc2012-08-04 00:11:22 +0000948
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000949 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
950 // make sense to give a single ctor/dtor a linkage name.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000951 StringRef MethodLinkageName;
Devang Patel58faf202010-10-22 17:11:50 +0000952 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
Anders Carlsson9a20d552010-06-22 16:16:50 +0000953 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000954
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000955 // Get the location for the method.
Devang Patel8ab870d2010-05-12 23:46:38 +0000956 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
957 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000958
959 // Collect virtual method info.
960 llvm::DIType ContainingType;
961 unsigned Virtuality = 0;
962 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000963
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000964 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000965 if (Method->isPure())
966 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
967 else
968 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
969
970 // It doesn't make sense to give a virtual destructor a vtable index,
971 // since a single destructor has two entries in the vtable.
972 if (!isa<CXXDestructorDecl>(Method))
Peter Collingbourne1d2b3172011-09-26 01:56:30 +0000973 VIndex = CGM.getVTableContext().getMethodVTableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000974 ContainingType = RecordTy;
975 }
976
Devang Patele2472482010-09-29 21:05:52 +0000977 unsigned Flags = 0;
978 if (Method->isImplicit())
979 Flags |= llvm::DIDescriptor::FlagArtificial;
Devang Patel10a7a6a2010-09-29 21:46:16 +0000980 AccessSpecifier Access = Method->getAccess();
981 if (Access == clang::AS_private)
982 Flags |= llvm::DIDescriptor::FlagPrivate;
983 else if (Access == clang::AS_protected)
984 Flags |= llvm::DIDescriptor::FlagProtected;
Devang Pateld78a0192010-10-01 23:32:17 +0000985 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
986 if (CXXC->isExplicit())
987 Flags |= llvm::DIDescriptor::FlagExplicit;
988 } else if (const CXXConversionDecl *CXXC =
989 dyn_cast<CXXConversionDecl>(Method)) {
990 if (CXXC->isExplicit())
991 Flags |= llvm::DIDescriptor::FlagExplicit;
992 }
Devang Patel3951e712010-10-07 22:03:49 +0000993 if (Method->hasPrototype())
994 Flags |= llvm::DIDescriptor::FlagPrototyped;
Eric Christopher3b10cfe2012-03-13 23:40:48 +0000995
996 llvm::DIArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000997 llvm::DISubprogram SP =
Nick Lewycky7803ec82011-09-01 21:49:51 +0000998 DBuilder.createMethod(RecordTy, MethodName, MethodLinkageName,
Devang Patel823d8e92010-12-08 22:42:58 +0000999 MethodDefUnit, MethodLine,
1000 MethodTy, /*isLocalToUnit=*/false,
1001 /* isDefinition=*/ false,
1002 Virtuality, VIndex, ContainingType,
Eric Christopher3b10cfe2012-03-13 23:40:48 +00001003 Flags, CGM.getLangOpts().Optimize, NULL,
1004 TParamsArray);
Anders Carlsson4433f1c2010-01-26 05:19:50 +00001005
Eric Christopherdeae6a82011-11-17 23:45:00 +00001006 SPCache[Method->getCanonicalDecl()] = llvm::WeakVH(SP);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +00001007
1008 return SP;
1009}
1010
Devang Patel4125fd22010-01-19 01:54:44 +00001011/// CollectCXXMemberFunctions - A helper function to collect debug info for
Eric Christopher7c9b2fd2012-01-12 01:26:51 +00001012/// C++ member functions. This is used while creating debug info entry for
Devang Patel4125fd22010-01-19 01:54:44 +00001013/// a Record.
1014void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +00001015CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001016 SmallVectorImpl<llvm::Value *> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +00001017 llvm::DIType RecordTy) {
Eric Christopher3b10cfe2012-03-13 23:40:48 +00001018
1019 // Since we want more than just the individual member decls if we
1020 // have templated functions iterate over every declaration to gather
1021 // the functions.
1022 for(DeclContext::decl_iterator I = RD->decls_begin(),
1023 E = RD->decls_end(); I != E; ++I) {
1024 Decl *D = *I;
1025 if (D->isImplicit() && !D->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +00001026 continue;
Devang Patel4125fd22010-01-19 01:54:44 +00001027
Eric Christopher9556b392012-10-17 17:37:17 +00001028 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
1029 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Eric Christopher3b10cfe2012-03-13 23:40:48 +00001030 else if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D))
1031 for (FunctionTemplateDecl::spec_iterator SI = FTD->spec_begin(),
Eric Christopher860de6b2012-08-13 02:07:42 +00001032 SE = FTD->spec_end(); SI != SE; ++SI)
1033 EltTys.push_back(CreateCXXMemberFunction(cast<CXXMethodDecl>(*SI), Unit,
1034 RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +00001035 }
1036}
1037
Devang Patel2ed8f002010-08-27 17:47:47 +00001038/// CollectCXXFriends - A helper function to collect debug info for
1039/// C++ base classes. This is used while creating debug info entry for
1040/// a Record.
1041void CGDebugInfo::
1042CollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001043 SmallVectorImpl<llvm::Value *> &EltTys,
Devang Patel2ed8f002010-08-27 17:47:47 +00001044 llvm::DIType RecordTy) {
Eric Christopher121c67d2012-01-12 01:26:58 +00001045 for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(),
Devang Patel2ed8f002010-08-27 17:47:47 +00001046 BE = RD->friend_end(); BI != BE; ++BI) {
Nick Lewycky7803ec82011-09-01 21:49:51 +00001047 if ((*BI)->isUnsupportedFriend())
1048 continue;
Devang Patel823d8e92010-12-08 22:42:58 +00001049 if (TypeSourceInfo *TInfo = (*BI)->getFriendType())
Devang Patel16674e82011-02-22 18:56:36 +00001050 EltTys.push_back(DBuilder.createFriend(RecordTy,
Devang Patel823d8e92010-12-08 22:42:58 +00001051 getOrCreateType(TInfo->getType(),
1052 Unit)));
Devang Patel2ed8f002010-08-27 17:47:47 +00001053 }
1054}
1055
Devang Patela245c5b2010-01-25 23:32:18 +00001056/// CollectCXXBases - A helper function to collect debug info for
1057/// C++ base classes. This is used while creating debug info entry for
1058/// a Record.
1059void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +00001060CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001061 SmallVectorImpl<llvm::Value *> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +00001062 llvm::DIType RecordTy) {
Devang Patela245c5b2010-01-25 23:32:18 +00001063
Devang Patel239cec62010-02-01 21:39:52 +00001064 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1065 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
1066 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +00001067 unsigned BFlags = 0;
Devang Patel62c117d2011-04-04 20:36:06 +00001068 uint64_t BaseOffset;
Devang Patelca7daed2010-01-28 21:54:15 +00001069
1070 const CXXRecordDecl *Base =
1071 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
1072
1073 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +00001074 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +00001075 // expression where it expects +ve number.
Ken Dyck14c65ca2011-04-07 12:37:09 +00001076 BaseOffset =
Peter Collingbourne1d2b3172011-09-26 01:56:30 +00001077 0 - CGM.getVTableContext()
1078 .getVirtualBaseOffsetOffset(RD, Base).getQuantity();
Devang Patele2472482010-09-29 21:05:52 +00001079 BFlags = llvm::DIDescriptor::FlagVirtual;
Devang Patelca7daed2010-01-28 21:54:15 +00001080 } else
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001081 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
Ken Dyck14c65ca2011-04-07 12:37:09 +00001082 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1083 // BI->isVirtual() and bits when not.
Devang Patelca7daed2010-01-28 21:54:15 +00001084
1085 AccessSpecifier Access = BI->getAccessSpecifier();
1086 if (Access == clang::AS_private)
Devang Patele2472482010-09-29 21:05:52 +00001087 BFlags |= llvm::DIDescriptor::FlagPrivate;
Devang Patelca7daed2010-01-28 21:54:15 +00001088 else if (Access == clang::AS_protected)
Devang Patele2472482010-09-29 21:05:52 +00001089 BFlags |= llvm::DIDescriptor::FlagProtected;
Devang Patelca7daed2010-01-28 21:54:15 +00001090
Devang Patel823d8e92010-12-08 22:42:58 +00001091 llvm::DIType DTy =
Devang Patel16674e82011-02-22 18:56:36 +00001092 DBuilder.createInheritance(RecordTy,
Devang Patel823d8e92010-12-08 22:42:58 +00001093 getOrCreateType(BI->getType(), Unit),
Devang Patel62c117d2011-04-04 20:36:06 +00001094 BaseOffset, BFlags);
Devang Patelca7daed2010-01-28 21:54:15 +00001095 EltTys.push_back(DTy);
1096 }
Devang Patela245c5b2010-01-25 23:32:18 +00001097}
1098
Devang Patel5ecb1df2011-04-05 22:54:11 +00001099/// CollectTemplateParams - A helper function to collect template parameters.
Devang Patel9c1714b2011-04-05 17:30:54 +00001100llvm::DIArray CGDebugInfo::
Devang Patel5ecb1df2011-04-05 22:54:11 +00001101CollectTemplateParams(const TemplateParameterList *TPList,
1102 const TemplateArgumentList &TAList,
1103 llvm::DIFile Unit) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001104 SmallVector<llvm::Value *, 16> TemplateParams;
Devang Patelc5ce2972011-04-05 20:15:06 +00001105 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1106 const TemplateArgument &TA = TAList[i];
Devang Patel5ecb1df2011-04-05 22:54:11 +00001107 const NamedDecl *ND = TPList->getParam(i);
Devang Patel9c1714b2011-04-05 17:30:54 +00001108 if (TA.getKind() == TemplateArgument::Type) {
1109 llvm::DIType TTy = getOrCreateType(TA.getAsType(), Unit);
1110 llvm::DITemplateTypeParameter TTP =
Devang Patelc5ce2972011-04-05 20:15:06 +00001111 DBuilder.createTemplateTypeParameter(TheCU, ND->getName(), TTy);
Devang Patel9c1714b2011-04-05 17:30:54 +00001112 TemplateParams.push_back(TTP);
1113 } else if (TA.getKind() == TemplateArgument::Integral) {
1114 llvm::DIType TTy = getOrCreateType(TA.getIntegralType(), Unit);
Devang Patel9c1714b2011-04-05 17:30:54 +00001115 llvm::DITemplateValueParameter TVP =
Devang Patelc5ce2972011-04-05 20:15:06 +00001116 DBuilder.createTemplateValueParameter(TheCU, ND->getName(), TTy,
Benjamin Kramer85524372012-06-07 15:09:51 +00001117 TA.getAsIntegral().getZExtValue());
Devang Patel9c1714b2011-04-05 17:30:54 +00001118 TemplateParams.push_back(TVP);
1119 }
1120 }
Jay Foadc556ef22011-04-24 10:11:03 +00001121 return DBuilder.getOrCreateArray(TemplateParams);
Devang Patel9c1714b2011-04-05 17:30:54 +00001122}
1123
Devang Patel5ecb1df2011-04-05 22:54:11 +00001124/// CollectFunctionTemplateParams - A helper function to collect debug
1125/// info for function template parameters.
1126llvm::DIArray CGDebugInfo::
1127CollectFunctionTemplateParams(const FunctionDecl *FD, llvm::DIFile Unit) {
Eric Christopherab5278e2011-10-11 23:00:51 +00001128 if (FD->getTemplatedKind() ==
1129 FunctionDecl::TK_FunctionTemplateSpecialization) {
Devang Patel5ecb1df2011-04-05 22:54:11 +00001130 const TemplateParameterList *TList =
Eric Christopherab5278e2011-10-11 23:00:51 +00001131 FD->getTemplateSpecializationInfo()->getTemplate()
1132 ->getTemplateParameters();
Devang Patel5ecb1df2011-04-05 22:54:11 +00001133 return
1134 CollectTemplateParams(TList, *FD->getTemplateSpecializationArgs(), Unit);
1135 }
1136 return llvm::DIArray();
1137}
1138
1139/// CollectCXXTemplateParams - A helper function to collect debug info for
1140/// template parameters.
1141llvm::DIArray CGDebugInfo::
1142CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TSpecial,
1143 llvm::DIFile Unit) {
1144 llvm::PointerUnion<ClassTemplateDecl *,
1145 ClassTemplatePartialSpecializationDecl *>
1146 PU = TSpecial->getSpecializedTemplateOrPartial();
1147
1148 TemplateParameterList *TPList = PU.is<ClassTemplateDecl *>() ?
1149 PU.get<ClassTemplateDecl *>()->getTemplateParameters() :
1150 PU.get<ClassTemplatePartialSpecializationDecl *>()->getTemplateParameters();
1151 const TemplateArgumentList &TAList = TSpecial->getTemplateInstantiationArgs();
1152 return CollectTemplateParams(TPList, TAList, Unit);
1153}
1154
Devang Patel4ce3f202010-01-28 18:11:52 +00001155/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +00001156llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +00001157 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +00001158 return VTablePtrType;
1159
1160 ASTContext &Context = CGM.getContext();
1161
1162 /* Function type */
Devang Patel823d8e92010-12-08 22:42:58 +00001163 llvm::Value *STy = getOrCreateType(Context.IntTy, Unit);
Jay Foadc556ef22011-04-24 10:11:03 +00001164 llvm::DIArray SElements = DBuilder.getOrCreateArray(STy);
Devang Patel16674e82011-02-22 18:56:36 +00001165 llvm::DIType SubTy = DBuilder.createSubroutineType(Unit, SElements);
Devang Patel4ce3f202010-01-28 18:11:52 +00001166 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Devang Patel16674e82011-02-22 18:56:36 +00001167 llvm::DIType vtbl_ptr_type = DBuilder.createPointerType(SubTy, Size, 0,
Devang Patel823d8e92010-12-08 22:42:58 +00001168 "__vtbl_ptr_type");
Devang Patel16674e82011-02-22 18:56:36 +00001169 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
Devang Patel4ce3f202010-01-28 18:11:52 +00001170 return VTablePtrType;
1171}
1172
Anders Carlsson046c2942010-04-17 20:15:18 +00001173/// getVTableName - Get vtable name for the given Class.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001174StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Eric Christopher51cb75a2012-01-25 21:47:09 +00001175 // Construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +00001176 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +00001177
1178 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +00001179 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +00001180 memcpy(StrPtr, Name.data(), Name.length());
Chris Lattner5f9e2722011-07-23 10:55:15 +00001181 return StringRef(StrPtr, Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +00001182}
1183
1184
Anders Carlsson046c2942010-04-17 20:15:18 +00001185/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel4ce3f202010-01-28 18:11:52 +00001186/// debug info entry in EltTys vector.
1187void CGDebugInfo::
Anders Carlsson046c2942010-04-17 20:15:18 +00001188CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001189 SmallVectorImpl<llvm::Value *> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +00001190 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +00001191
1192 // If there is a primary base then it will hold vtable info.
1193 if (RL.getPrimaryBase())
1194 return;
1195
1196 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +00001197 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +00001198 return;
1199
1200 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
1201 llvm::DIType VPTR
Devang Patel1d323e02011-06-24 22:00:59 +00001202 = DBuilder.createMemberType(Unit, getVTableName(RD), Unit,
Devang Patel823d8e92010-12-08 22:42:58 +00001203 0, Size, 0, 0, 0,
1204 getOrCreateVTablePtrType(Unit));
Devang Patel4ce3f202010-01-28 18:11:52 +00001205 EltTys.push_back(VPTR);
1206}
1207
Devang Patelc69e1cf2010-09-30 19:05:55 +00001208/// getOrCreateRecordType - Emit record type's standalone debug info.
1209llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
1210 SourceLocation Loc) {
Alexey Samsonovfd00eec2012-05-04 07:39:27 +00001211 assert(CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo);
Nick Lewyckyd4c100e2011-11-09 04:25:21 +00001212 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
Devang Patelc69e1cf2010-09-30 19:05:55 +00001213 return T;
1214}
1215
Eric Christopherbe6c6862012-04-11 05:56:05 +00001216/// getOrCreateInterfaceType - Emit an objective c interface type standalone
1217/// debug info.
1218llvm::DIType CGDebugInfo::getOrCreateInterfaceType(QualType D,
1219 SourceLocation Loc) {
Alexey Samsonovfd00eec2012-05-04 07:39:27 +00001220 assert(CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo);
Eric Christopherbe6c6862012-04-11 05:56:05 +00001221 llvm::DIType T = getOrCreateType(D, getOrCreateFile(Loc));
1222 DBuilder.retainType(T);
1223 return T;
1224}
1225
Devang Patel65e99f22009-02-25 01:36:11 +00001226/// CreateType - get structure or union type.
Devang Patel31f7d022011-01-17 22:23:07 +00001227llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001228 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001229
Chris Lattner9c85ba32008-11-10 06:08:34 +00001230 // Get overall information about the record type for the debug info.
Devang Patel8ab870d2010-05-12 23:46:38 +00001231 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001232
Chris Lattner9c85ba32008-11-10 06:08:34 +00001233 // Records and classes and unions can all be recursive. To handle them, we
1234 // first generate a debug descriptor for the struct as a forward declaration.
1235 // Then (if it is a definition) we go through and get debug info for all of
1236 // its members. Finally, we create a descriptor for the complete type (which
1237 // may refer to the forward decl if the struct is recursive) and replace all
1238 // uses of the forward declaration with the final definition.
Eric Christopher4ddca8a2012-01-20 22:10:15 +00001239
Eric Christopher9965dea2012-02-16 22:54:45 +00001240 llvm::DIType FwdDecl = getOrCreateLimitedType(QualType(Ty, 0), DefUnit);
Devang Patel0b897992010-07-08 19:56:29 +00001241
Eric Christopher9965dea2012-02-16 22:54:45 +00001242 if (FwdDecl.isForwardDecl())
1243 return FwdDecl;
Benjamin Kramer6181e562012-03-20 19:49:14 +00001244
1245 llvm::TrackingVH<llvm::MDNode> FwdDeclNode(FwdDecl);
1246
Devang Patele4c1ea02010-03-11 20:01:48 +00001247 // Push the struct on region stack.
Eric Christopheraa2164c2011-09-29 00:00:45 +00001248 LexicalBlockStack.push_back(FwdDeclNode);
Devang Patelab699792010-05-07 18:12:35 +00001249 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001250
Eric Christopher9965dea2012-02-16 22:54:45 +00001251 // Add this to the completed types cache since we're completing it.
1252 CompletedTypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
1253
Chris Lattner9c85ba32008-11-10 06:08:34 +00001254 // Convert all the elements.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001255 SmallVector<llvm::Value *, 16> EltTys;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001256
Eric Christopher1c081d92012-01-26 07:01:04 +00001257 // Note: The split of CXXDecl information here is intentional, the
1258 // gdb tests will depend on a certain ordering at printout. The debug
1259 // information offsets are still correct if we merge them all together
1260 // though.
Devang Pateld6c5a262010-02-01 21:52:22 +00001261 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +00001262 if (CXXDecl) {
Eric Christopher3ee8c912012-01-26 06:20:57 +00001263 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1264 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
Eric Christopher1c081d92012-01-26 07:01:04 +00001265 }
1266
1267 // Collect static variables with initializers and other fields.
1268 CollectRecordStaticVars(RD, FwdDecl);
1269 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
1270 llvm::DIArray TParamsArray;
1271 if (CXXDecl) {
Eric Christopher3ee8c912012-01-26 06:20:57 +00001272 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
1273 CollectCXXFriends(CXXDecl, DefUnit, EltTys, FwdDecl);
Devang Patel9c1714b2011-04-05 17:30:54 +00001274 if (const ClassTemplateSpecializationDecl *TSpecial
1275 = dyn_cast<ClassTemplateSpecializationDecl>(RD))
Eric Christopher3ee8c912012-01-26 06:20:57 +00001276 TParamsArray = CollectCXXTemplateParams(TSpecial, DefUnit);
Devang Patel823d8e92010-12-08 22:42:58 +00001277 }
Devang Patel0ac8f312010-01-28 00:54:21 +00001278
Eric Christopheraa2164c2011-09-29 00:00:45 +00001279 LexicalBlockStack.pop_back();
Benjamin Kramer7e423922012-03-24 18:22:12 +00001280 RegionMap.erase(Ty->getDecl());
Devang Patel823d8e92010-12-08 22:42:58 +00001281
Jay Foadc556ef22011-04-24 10:11:03 +00001282 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopher9965dea2012-02-16 22:54:45 +00001283 // FIXME: Magic numbers ahoy! These should be changed when we
1284 // get some enums in llvm/Analysis/DebugInfo.h to refer to
1285 // them.
Eric Christopher64a04302012-02-15 23:51:20 +00001286 if (RD->isUnion())
Benjamin Kramer6181e562012-03-20 19:49:14 +00001287 FwdDeclNode->replaceOperandWith(10, Elements);
Eric Christopher64a04302012-02-15 23:51:20 +00001288 else if (CXXDecl) {
Benjamin Kramer6181e562012-03-20 19:49:14 +00001289 FwdDeclNode->replaceOperandWith(10, Elements);
1290 FwdDeclNode->replaceOperandWith(13, TParamsArray);
Eric Christopher64a04302012-02-15 23:51:20 +00001291 } else
Benjamin Kramer6181e562012-03-20 19:49:14 +00001292 FwdDeclNode->replaceOperandWith(10, Elements);
Eric Christopher64a04302012-02-15 23:51:20 +00001293
Benjamin Kramer6181e562012-03-20 19:49:14 +00001294 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDeclNode);
1295 return llvm::DIType(FwdDeclNode);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001296}
1297
John McCallc12c5bb2010-05-15 11:32:37 +00001298/// CreateType - get objective-c object type.
1299llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1300 llvm::DIFile Unit) {
1301 // Ignore protocols.
1302 return getOrCreateType(Ty->getBaseType(), Unit);
1303}
1304
Devang Patel9ca36b62009-02-26 21:10:26 +00001305/// CreateType - get objective-c interface type.
1306llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001307 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001308 ObjCInterfaceDecl *ID = Ty->getDecl();
Douglas Gregora6a28972010-11-30 06:38:09 +00001309 if (!ID)
1310 return llvm::DIType();
Devang Patel9ca36b62009-02-26 21:10:26 +00001311
1312 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +00001313 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001314 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel17800552010-03-09 00:44:50 +00001315 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +00001316
Eric Christopherd1ab1a22011-10-06 00:31:18 +00001317 // If this is just a forward declaration return a special forward-declaration
1318 // debug type since we won't be able to lay out the entire type.
Douglas Gregor7c1f1f12011-12-15 23:32:29 +00001319 ObjCInterfaceDecl *Def = ID->getDefinition();
1320 if (!Def) {
Devang Patel823d8e92010-12-08 22:42:58 +00001321 llvm::DIType FwdDecl =
Eric Christopher917bc8d2012-02-20 18:05:04 +00001322 DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
Eric Christopher87380aa2012-04-23 19:00:24 +00001323 ID->getName(), TheCU, DefUnit, Line,
Eric Christopher917bc8d2012-02-20 18:05:04 +00001324 RuntimeLang);
Dan Gohman45f7c782010-08-23 21:15:56 +00001325 return FwdDecl;
1326 }
Eric Christopherbe6c6862012-04-11 05:56:05 +00001327
Douglas Gregor7c1f1f12011-12-15 23:32:29 +00001328 ID = Def;
Dan Gohman45f7c782010-08-23 21:15:56 +00001329
Eric Christopher9965dea2012-02-16 22:54:45 +00001330 // Bit size, align and offset of the type.
1331 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1332 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001333
Eric Christopher9965dea2012-02-16 22:54:45 +00001334 unsigned Flags = 0;
1335 if (ID->getImplementation())
1336 Flags |= llvm::DIDescriptor::FlagObjcClassComplete;
1337
1338 llvm::DIType RealDecl =
1339 DBuilder.createStructType(Unit, ID->getName(), DefUnit,
1340 Line, Size, Align, Flags,
1341 llvm::DIArray(), RuntimeLang);
Eric Christopherbe6c6862012-04-11 05:56:05 +00001342
Eric Christopheraf3db7d2012-02-27 08:23:23 +00001343 // Otherwise, insert it into the CompletedTypeCache so that recursive uses
1344 // will find it and we're emitting the complete type.
1345 CompletedTypeCache[QualType(Ty, 0).getAsOpaquePtr()] = RealDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +00001346 // Push the struct on region stack.
Benjamin Kramer6181e562012-03-20 19:49:14 +00001347 llvm::TrackingVH<llvm::MDNode> FwdDeclNode(RealDecl);
Eric Christopher9965dea2012-02-16 22:54:45 +00001348
Eric Christopheraa2164c2011-09-29 00:00:45 +00001349 LexicalBlockStack.push_back(FwdDeclNode);
Eric Christopher9965dea2012-02-16 22:54:45 +00001350 RegionMap[Ty->getDecl()] = llvm::WeakVH(RealDecl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001351
1352 // Convert all the elements.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001353 SmallVector<llvm::Value *, 16> EltTys;
Devang Patel9ca36b62009-02-26 21:10:26 +00001354
Devang Pateld6c5a262010-02-01 21:52:22 +00001355 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +00001356 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +00001357 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001358 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Douglas Gregora6a28972010-11-30 06:38:09 +00001359 if (!SClassTy.isValid())
1360 return llvm::DIType();
1361
Mike Stump1eb44332009-09-09 15:08:12 +00001362 llvm::DIType InhTag =
Eric Christopher9965dea2012-02-16 22:54:45 +00001363 DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Devang Patelfbe899f2009-03-10 21:30:26 +00001364 EltTys.push_back(InhTag);
1365 }
1366
Devang Patel693fcaa2012-02-07 18:40:30 +00001367 for (ObjCContainerDecl::prop_iterator I = ID->prop_begin(),
1368 E = ID->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001369 const ObjCPropertyDecl *PD = *I;
Eric Christopher51c03712012-03-29 08:43:37 +00001370 SourceLocation Loc = PD->getLocation();
1371 llvm::DIFile PUnit = getOrCreateFile(Loc);
1372 unsigned PLine = getLineNumber(Loc);
Eric Christopher78af8fd2012-04-05 22:03:32 +00001373 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1374 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Devang Patel693fcaa2012-02-07 18:40:30 +00001375 llvm::MDNode *PropertyNode =
1376 DBuilder.createObjCProperty(PD->getName(),
Eric Christopher51c03712012-03-29 08:43:37 +00001377 PUnit, PLine,
Eric Christopher78af8fd2012-04-05 22:03:32 +00001378 (Getter && Getter->isImplicit()) ? "" :
Eric Christopherecae5962012-03-29 17:31:33 +00001379 getSelectorName(PD->getGetterName()),
Eric Christopher78af8fd2012-04-05 22:03:32 +00001380 (Setter && Setter->isImplicit()) ? "" :
Eric Christopherecae5962012-03-29 17:31:33 +00001381 getSelectorName(PD->getSetterName()),
Eric Christopher51c03712012-03-29 08:43:37 +00001382 PD->getPropertyAttributes(),
1383 getOrCreateType(PD->getType(), PUnit));
Devang Patel693fcaa2012-02-07 18:40:30 +00001384 EltTys.push_back(PropertyNode);
1385 }
1386
Devang Pateld6c5a262010-02-01 21:52:22 +00001387 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001388 unsigned FieldNo = 0;
Fariborz Jahanian97477392010-10-01 00:01:53 +00001389 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
Fariborz Jahanianfe8fdba2010-10-11 23:55:47 +00001390 Field = Field->getNextIvar(), ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001391 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Douglas Gregora6a28972010-11-30 06:38:09 +00001392 if (!FieldTy.isValid())
1393 return llvm::DIType();
1394
Chris Lattner5f9e2722011-07-23 10:55:15 +00001395 StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001396
Devang Patelde135022009-04-27 22:40:36 +00001397 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +00001398 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +00001399 continue;
1400
Devang Patel9ca36b62009-02-26 21:10:26 +00001401 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +00001402 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1403 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel99c20eb2009-03-20 18:24:39 +00001404 QualType FType = Field->getType();
1405 uint64_t FieldSize = 0;
1406 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +00001407
Devang Patel99c20eb2009-03-20 18:24:39 +00001408 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001409
Devang Patel99c20eb2009-03-20 18:24:39 +00001410 // Bit size, align and offset of the type.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001411 FieldSize = Field->isBitField()
1412 ? Field->getBitWidthValue(CGM.getContext())
1413 : CGM.getContext().getTypeSize(FType);
1414 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001415 }
1416
Eric Christopherd1ab1a22011-10-06 00:31:18 +00001417 // We can't know the offset of our ivar in the structure if we're using
1418 // the non-fragile abi and the debugger should ignore the value anyways.
1419 // Call it the FieldNo+1 due to how debuggers use the information,
1420 // e.g. negating the value when it needs a lookup in the dynamic table.
John McCall260611a2012-06-20 06:18:46 +00001421 uint64_t FieldOffset = CGM.getLangOpts().ObjCRuntime.isNonFragile()
1422 ? FieldNo+1 : RL.getFieldOffset(FieldNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001423
Devang Patelc20482b2009-03-19 00:23:53 +00001424 unsigned Flags = 0;
1425 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Devang Patele2472482010-09-29 21:05:52 +00001426 Flags = llvm::DIDescriptor::FlagProtected;
Devang Patelc20482b2009-03-19 00:23:53 +00001427 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Devang Patele2472482010-09-29 21:05:52 +00001428 Flags = llvm::DIDescriptor::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +00001429
Devang Patel693a70d2012-02-04 01:15:04 +00001430 llvm::MDNode *PropertyNode = NULL;
Devang Patel693fcaa2012-02-07 18:40:30 +00001431 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Devang Patel8c6f9c42011-09-19 18:54:16 +00001432 if (ObjCPropertyImplDecl *PImpD =
Devang Patel693fcaa2012-02-07 18:40:30 +00001433 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
1434 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopher51c03712012-03-29 08:43:37 +00001435 SourceLocation Loc = PD->getLocation();
1436 llvm::DIFile PUnit = getOrCreateFile(Loc);
1437 unsigned PLine = getLineNumber(Loc);
Eric Christopher78af8fd2012-04-05 22:03:32 +00001438 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1439 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
1440 PropertyNode =
1441 DBuilder.createObjCProperty(PD->getName(),
1442 PUnit, PLine,
1443 (Getter && Getter->isImplicit()) ? "" :
Eric Christopherecae5962012-03-29 17:31:33 +00001444 getSelectorName(PD->getGetterName()),
Eric Christopher78af8fd2012-04-05 22:03:32 +00001445 (Setter && Setter->isImplicit()) ? "" :
Eric Christopherecae5962012-03-29 17:31:33 +00001446 getSelectorName(PD->getSetterName()),
Eric Christopher78af8fd2012-04-05 22:03:32 +00001447 PD->getPropertyAttributes(),
1448 getOrCreateType(PD->getType(), PUnit));
Devang Patel53bc5182012-02-08 00:10:20 +00001449 }
Devang Patel693fcaa2012-02-07 18:40:30 +00001450 }
Devang Patel693a70d2012-02-04 01:15:04 +00001451 }
Devang Patelfa936d82011-04-16 00:12:55 +00001452 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit,
1453 FieldLine, FieldSize, FieldAlign,
1454 FieldOffset, Flags, FieldTy,
Devang Patel5f3c7fa2012-02-06 18:20:02 +00001455 PropertyNode);
Devang Patel9ca36b62009-02-26 21:10:26 +00001456 EltTys.push_back(FieldTy);
1457 }
Mike Stump1eb44332009-09-09 15:08:12 +00001458
Jay Foadc556ef22011-04-24 10:11:03 +00001459 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Benjamin Kramer6181e562012-03-20 19:49:14 +00001460 FwdDeclNode->replaceOperandWith(10, Elements);
Eric Christopher9965dea2012-02-16 22:54:45 +00001461
Eric Christopheraa2164c2011-09-29 00:00:45 +00001462 LexicalBlockStack.pop_back();
Benjamin Kramer6181e562012-03-20 19:49:14 +00001463 return llvm::DIType(FwdDeclNode);
Devang Patel9ca36b62009-02-26 21:10:26 +00001464}
1465
Nick Lewyckyd4c100e2011-11-09 04:25:21 +00001466llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty, llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001467 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Devang Patel6cf37dd2011-04-08 21:56:52 +00001468 int64_t NumElems = Ty->getNumElements();
1469 int64_t LowerBound = 0;
1470 if (NumElems == 0)
1471 // If number of elements are not known then this is an unbounded array.
1472 // Use Low = 1, Hi = 0 to express such arrays.
1473 LowerBound = 1;
1474 else
Devang Patel70c23cd2010-02-23 22:59:39 +00001475 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001476
Devang Patel6cf37dd2011-04-08 21:56:52 +00001477 llvm::Value *Subscript = DBuilder.getOrCreateSubrange(LowerBound, NumElems);
Jay Foadc556ef22011-04-24 10:11:03 +00001478 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Devang Patel70c23cd2010-02-23 22:59:39 +00001479
1480 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1481 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1482
1483 return
Devang Patel16674e82011-02-22 18:56:36 +00001484 DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
Devang Patel70c23cd2010-02-23 22:59:39 +00001485}
1486
Chris Lattner9c85ba32008-11-10 06:08:34 +00001487llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001488 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001489 uint64_t Size;
1490 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001491
Nuno Lopes010d5142009-01-28 00:35:17 +00001492 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001493 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001494 Size = 0;
1495 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001496 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001497 } else if (Ty->isIncompleteArrayType()) {
1498 Size = 0;
Eric Christopherc7fb7482012-08-07 00:48:43 +00001499 if (Ty->getElementType()->isIncompleteType())
1500 Align = 0;
1501 else
1502 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Devang Patelba690a42011-04-04 23:18:38 +00001503 } else if (Ty->isDependentSizedArrayType() || Ty->isIncompleteType()) {
Devang Patelae503df2011-04-01 19:02:33 +00001504 Size = 0;
1505 Align = 0;
Anders Carlsson835c9092009-01-05 01:23:29 +00001506 } else {
1507 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001508 Size = CGM.getContext().getTypeSize(Ty);
1509 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001510 }
Mike Stump1eb44332009-09-09 15:08:12 +00001511
Chris Lattner9c85ba32008-11-10 06:08:34 +00001512 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1513 // interior arrays, do we care? Why aren't nested arrays represented the
1514 // obvious/recursive way?
Chris Lattner5f9e2722011-07-23 10:55:15 +00001515 SmallVector<llvm::Value *, 8> Subscripts;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001516 QualType EltTy(Ty, 0);
Eric Christophere6d11972012-05-21 22:13:23 +00001517 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1518 int64_t UpperBound = 0;
1519 int64_t LowerBound = 0;
1520 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty)) {
1521 if (CAT->getSize().getZExtValue())
1522 UpperBound = CAT->getSize().getZExtValue() - 1;
1523 } else
1524 // This is an unbounded array. Use Low = 1, Hi = 0 to express such
1525 // arrays.
1526 LowerBound = 1;
1527
1528 // FIXME: Verify this is right for VLAs.
1529 Subscripts.push_back(DBuilder.getOrCreateSubrange(LowerBound,
1530 UpperBound));
Chris Lattner9c85ba32008-11-10 06:08:34 +00001531 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +00001532 }
Mike Stump1eb44332009-09-09 15:08:12 +00001533
Jay Foadc556ef22011-04-24 10:11:03 +00001534 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001535
Devang Patelca80a5f2009-10-20 19:55:01 +00001536 llvm::DIType DbgTy =
Devang Patel16674e82011-02-22 18:56:36 +00001537 DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
Devang Patel823d8e92010-12-08 22:42:58 +00001538 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001539 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001540}
1541
Anders Carlssona031b352009-11-06 19:19:55 +00001542llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001543 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001544 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1545 Ty, Ty->getPointeeType(), Unit);
1546}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001547
Douglas Gregor36b8ee62011-01-22 01:58:15 +00001548llvm::DIType CGDebugInfo::CreateType(const RValueReferenceType *Ty,
1549 llvm::DIFile Unit) {
1550 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type,
1551 Ty, Ty->getPointeeType(), Unit);
1552}
1553
Anders Carlsson20f12a22009-12-06 18:00:51 +00001554llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001555 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001556 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1557 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1558
1559 if (!Ty->getPointeeType()->isFunctionType()) {
1560 // We have a data member pointer type.
1561 return PointerDiffDITy;
1562 }
1563
1564 // We have a member function pointer type. Treat it as a struct with two
1565 // ptrdiff_t members.
1566 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1567
1568 uint64_t FieldOffset = 0;
Devang Patel823d8e92010-12-08 22:42:58 +00001569 llvm::Value *ElementTypes[2];
Anders Carlsson20f12a22009-12-06 18:00:51 +00001570
Eric Christopher3e078812012-08-04 00:11:20 +00001571 // FIXME: This should be a DW_TAG_pointer_to_member type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001572 ElementTypes[0] =
Devang Patel1d323e02011-06-24 22:00:59 +00001573 DBuilder.createMemberType(U, "ptr", U, 0,
Devang Patel823d8e92010-12-08 22:42:58 +00001574 Info.first, Info.second, FieldOffset, 0,
1575 PointerDiffDITy);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001576 FieldOffset += Info.first;
1577
1578 ElementTypes[1] =
Devang Patel1d323e02011-06-24 22:00:59 +00001579 DBuilder.createMemberType(U, "ptr", U, 0,
Devang Patel823d8e92010-12-08 22:42:58 +00001580 Info.first, Info.second, FieldOffset, 0,
1581 PointerDiffDITy);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001582
Jay Foadc556ef22011-04-24 10:11:03 +00001583 llvm::DIArray Elements = DBuilder.getOrCreateArray(ElementTypes);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001584
Chris Lattner5f9e2722011-07-23 10:55:15 +00001585 return DBuilder.createStructType(U, StringRef("test"),
Devang Patel823d8e92010-12-08 22:42:58 +00001586 U, 0, FieldOffset,
1587 0, 0, Elements);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001588}
1589
Eli Friedmanb001de72011-10-06 23:00:33 +00001590llvm::DIType CGDebugInfo::CreateType(const AtomicType *Ty,
1591 llvm::DIFile U) {
1592 // Ignore the atomic wrapping
1593 // FIXME: What is the correct representation?
1594 return getOrCreateType(Ty->getValueType(), U);
1595}
1596
Devang Patel6237cea2010-08-23 22:07:25 +00001597/// CreateEnumType - get enumeration type.
Devang Patel31f7d022011-01-17 22:23:07 +00001598llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED) {
Eli Friedmane6b39bc2012-10-05 01:49:33 +00001599 uint64_t Size = 0;
1600 uint64_t Align = 0;
1601 if (!ED->getTypeForDecl()->isIncompleteType()) {
1602 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1603 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1604 }
1605
1606 // If this is just a forward declaration, construct an appropriately
1607 // marked node and just return it.
1608 if (!ED->getDefinition()) {
1609 llvm::DIDescriptor EDContext;
1610 EDContext = getContextDescriptor(cast<Decl>(ED->getDeclContext()));
1611 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1612 unsigned Line = getLineNumber(ED->getLocation());
1613 StringRef EDName = ED->getName();
1614 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_enumeration_type,
1615 EDName, EDContext, DefUnit, Line, 0,
1616 Size, Align);
1617 }
Devang Patel6237cea2010-08-23 22:07:25 +00001618
1619 // Create DIEnumerator elements for each enumerator.
Eli Friedmane6b39bc2012-10-05 01:49:33 +00001620 SmallVector<llvm::Value *, 16> Enumerators;
1621 ED = ED->getDefinition();
Devang Patel6237cea2010-08-23 22:07:25 +00001622 for (EnumDecl::enumerator_iterator
1623 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
1624 Enum != EnumEnd; ++Enum) {
Devang Patel823d8e92010-12-08 22:42:58 +00001625 Enumerators.push_back(
Devang Patel16674e82011-02-22 18:56:36 +00001626 DBuilder.createEnumerator(Enum->getName(),
Devang Patel823d8e92010-12-08 22:42:58 +00001627 Enum->getInitVal().getZExtValue()));
Devang Patel6237cea2010-08-23 22:07:25 +00001628 }
1629
1630 // Return a CompositeType for the enum itself.
Jay Foadc556ef22011-04-24 10:11:03 +00001631 llvm::DIArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Devang Patel6237cea2010-08-23 22:07:25 +00001632
1633 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1634 unsigned Line = getLineNumber(ED->getLocation());
Devang Patel4bc48872010-10-27 23:23:58 +00001635 llvm::DIDescriptor EnumContext =
John McCall8178df32011-02-22 22:38:33 +00001636 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
Eric Christopher9ee5f462012-05-23 00:09:47 +00001637 llvm::DIType ClassTy = ED->isScopedUsingClassTag() ?
1638 getOrCreateType(ED->getIntegerType(), DefUnit) : llvm::DIType();
Devang Patel6237cea2010-08-23 22:07:25 +00001639 llvm::DIType DbgTy =
Devang Patel16674e82011-02-22 18:56:36 +00001640 DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit, Line,
Eric Christopher9ee5f462012-05-23 00:09:47 +00001641 Size, Align, EltArray,
Eli Friedmane6b39bc2012-10-05 01:49:33 +00001642 ClassTy);
Devang Patel6237cea2010-08-23 22:07:25 +00001643 return DbgTy;
1644}
1645
Douglas Gregor840943d2009-12-21 20:18:30 +00001646static QualType UnwrapTypeForDebugInfo(QualType T) {
1647 do {
1648 QualType LastT = T;
1649 switch (T->getTypeClass()) {
1650 default:
1651 return T;
1652 case Type::TemplateSpecialization:
1653 T = cast<TemplateSpecializationType>(T)->desugar();
1654 break;
John McCallf4c73712011-01-19 06:33:43 +00001655 case Type::TypeOfExpr:
1656 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001657 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001658 case Type::TypeOf:
1659 T = cast<TypeOfType>(T)->getUnderlyingType();
1660 break;
1661 case Type::Decltype:
1662 T = cast<DecltypeType>(T)->getUnderlyingType();
1663 break;
Sean Huntca63c202011-05-24 22:41:36 +00001664 case Type::UnaryTransform:
1665 T = cast<UnaryTransformType>(T)->getUnderlyingType();
1666 break;
John McCall9d156a72011-01-06 01:58:22 +00001667 case Type::Attributed:
1668 T = cast<AttributedType>(T)->getEquivalentType();
John McCall14aa2172011-03-04 04:00:19 +00001669 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001670 case Type::Elaborated:
1671 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001672 break;
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001673 case Type::Paren:
1674 T = cast<ParenType>(T)->getInnerType();
1675 break;
Eric Christopher363e5ac2012-08-07 00:14:25 +00001676 case Type::SubstTemplateTypeParm: {
1677 // We need to keep the qualifiers handy since getReplacementType()
1678 // will strip them away.
1679 unsigned Quals = T.getLocalFastQualifiers();
Douglas Gregor840943d2009-12-21 20:18:30 +00001680 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Eric Christopher363e5ac2012-08-07 00:14:25 +00001681 T.addFastQualifiers(Quals);
1682 }
Douglas Gregor840943d2009-12-21 20:18:30 +00001683 break;
Anders Carlssonebc32792011-03-06 16:43:04 +00001684 case Type::Auto:
1685 T = cast<AutoType>(T)->getDeducedType();
1686 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001687 }
1688
1689 assert(T != LastT && "Type unwrapping failed to unwrap!");
1690 if (T == LastT)
1691 return T;
1692 } while (true);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001693}
1694
Eric Christopher973bbb62011-12-16 23:40:18 +00001695/// getType - Get the type from the cache or return null type if it doesn't exist.
1696llvm::DIType CGDebugInfo::getTypeOrNull(QualType Ty) {
Mike Stump1eb44332009-09-09 15:08:12 +00001697
Douglas Gregor840943d2009-12-21 20:18:30 +00001698 // Unwrap the type as needed for debug information.
1699 Ty = UnwrapTypeForDebugInfo(Ty);
Eric Christopher42e75da2012-02-13 14:56:11 +00001700
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001701 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001702 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001703 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001704 if (it != TypeCache.end()) {
1705 // Verify that the debug info still exists.
Richard Smithe7259aa2012-08-17 04:17:54 +00001706 if (llvm::Value *V = it->second)
1707 return llvm::DIType(cast<llvm::MDNode>(V));
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001708 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001709
Eric Christopher973bbb62011-12-16 23:40:18 +00001710 return llvm::DIType();
1711}
1712
Eric Christopher9965dea2012-02-16 22:54:45 +00001713/// getCompletedTypeOrNull - Get the type from the cache or return null if it
1714/// doesn't exist.
1715llvm::DIType CGDebugInfo::getCompletedTypeOrNull(QualType Ty) {
1716
1717 // Unwrap the type as needed for debug information.
1718 Ty = UnwrapTypeForDebugInfo(Ty);
1719
1720 // Check for existing entry.
1721 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
1722 CompletedTypeCache.find(Ty.getAsOpaquePtr());
1723 if (it != CompletedTypeCache.end()) {
1724 // Verify that the debug info still exists.
Richard Smithe7259aa2012-08-17 04:17:54 +00001725 if (llvm::Value *V = it->second)
1726 return llvm::DIType(cast<llvm::MDNode>(V));
Eric Christopher9965dea2012-02-16 22:54:45 +00001727 }
1728
1729 return llvm::DIType();
1730}
1731
1732
Eric Christopher973bbb62011-12-16 23:40:18 +00001733/// getOrCreateType - Get the type from the cache or create a new
1734/// one if necessary.
1735llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile Unit) {
1736 if (Ty.isNull())
1737 return llvm::DIType();
1738
1739 // Unwrap the type as needed for debug information.
1740 Ty = UnwrapTypeForDebugInfo(Ty);
Eric Christopher363e5ac2012-08-07 00:14:25 +00001741
Eric Christopher9965dea2012-02-16 22:54:45 +00001742 llvm::DIType T = getCompletedTypeOrNull(Ty);
1743
Eric Christopher363e5ac2012-08-07 00:14:25 +00001744 if (T.Verify())
1745 return T;
Eric Christopher973bbb62011-12-16 23:40:18 +00001746
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001747 // Otherwise create the type.
1748 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Eric Christopher7ff0c5d2012-02-18 00:50:17 +00001749
1750 llvm::DIType TC = getTypeOrNull(Ty);
1751 if (TC.Verify() && TC.isForwardDecl())
Michael J. Spencer50e3faa2012-06-08 23:47:12 +00001752 ReplaceMap.push_back(std::make_pair(Ty.getAsOpaquePtr(),
1753 static_cast<llvm::Value*>(TC)));
Eric Christopher9965dea2012-02-16 22:54:45 +00001754
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001755 // And update the type cache.
Eric Christopher9965dea2012-02-16 22:54:45 +00001756 TypeCache[Ty.getAsOpaquePtr()] = Res;
1757
1758 if (!Res.isForwardDecl())
1759 CompletedTypeCache[Ty.getAsOpaquePtr()] = Res;
Eric Christopher363e5ac2012-08-07 00:14:25 +00001760
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001761 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001762}
1763
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001764/// CreateTypeNode - Create a new debug type node.
Nick Lewycky7b3819d2011-11-09 04:27:23 +00001765llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001766 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001767 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001768 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001769
Douglas Gregor2101a822009-12-21 19:57:21 +00001770 const char *Diag = 0;
1771
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001772 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001773 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001774#define TYPE(Class, Base)
1775#define ABSTRACT_TYPE(Class, Base)
1776#define NON_CANONICAL_TYPE(Class, Base)
1777#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1778#include "clang/AST/TypeNodes.def"
David Blaikieb219cfc2011-09-23 05:06:16 +00001779 llvm_unreachable("Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001780
Anders Carlssonbfe69952009-11-06 18:24:04 +00001781 case Type::ExtVector:
Devang Patel70c23cd2010-02-23 22:59:39 +00001782 case Type::Vector:
1783 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001784 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001785 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCallc12c5bb2010-05-15 11:32:37 +00001786 case Type::ObjCObject:
1787 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001788 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001789 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
Nick Lewyckyd4c100e2011-11-09 04:25:21 +00001790 case Type::Builtin:
1791 return CreateType(cast<BuiltinType>(Ty));
1792 case Type::Complex:
1793 return CreateType(cast<ComplexType>(Ty));
1794 case Type::Pointer:
1795 return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001796 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001797 return CreateType(cast<BlockPointerType>(Ty), Unit);
Nick Lewyckyd4c100e2011-11-09 04:25:21 +00001798 case Type::Typedef:
1799 return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001800 case Type::Record:
Nick Lewyckyd4c100e2011-11-09 04:25:21 +00001801 return CreateType(cast<RecordType>(Ty));
Douglas Gregor72564e72009-02-26 23:50:07 +00001802 case Type::Enum:
Nick Lewyckyd4c100e2011-11-09 04:25:21 +00001803 return CreateEnumType(cast<EnumType>(Ty)->getDecl());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001804 case Type::FunctionProto:
1805 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001806 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001807 case Type::ConstantArray:
1808 case Type::VariableArray:
1809 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001810 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001811
1812 case Type::LValueReference:
1813 return CreateType(cast<LValueReferenceType>(Ty), Unit);
Douglas Gregor36b8ee62011-01-22 01:58:15 +00001814 case Type::RValueReference:
1815 return CreateType(cast<RValueReferenceType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001816
Anders Carlsson20f12a22009-12-06 18:00:51 +00001817 case Type::MemberPointer:
1818 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001819
Eli Friedmanb001de72011-10-06 23:00:33 +00001820 case Type::Atomic:
1821 return CreateType(cast<AtomicType>(Ty), Unit);
1822
John McCall9d156a72011-01-06 01:58:22 +00001823 case Type::Attributed:
Douglas Gregor2101a822009-12-21 19:57:21 +00001824 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001825 case Type::Elaborated:
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001826 case Type::Paren:
Douglas Gregor2101a822009-12-21 19:57:21 +00001827 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001828 case Type::TypeOfExpr:
1829 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001830 case Type::Decltype:
Sean Huntca63c202011-05-24 22:41:36 +00001831 case Type::UnaryTransform:
Richard Smith34b41d92011-02-20 03:19:35 +00001832 case Type::Auto:
Douglas Gregor840943d2009-12-21 20:18:30 +00001833 llvm_unreachable("type should have been unwrapped!");
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001834 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001835
1836 assert(Diag && "Fall through without a diagnostic?");
David Blaikied6471f72011-09-25 23:23:43 +00001837 unsigned DiagID = CGM.getDiags().getCustomDiagID(DiagnosticsEngine::Error,
Douglas Gregor2101a822009-12-21 19:57:21 +00001838 "debug information for %0 is not yet supported");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00001839 CGM.getDiags().Report(DiagID)
Douglas Gregor2101a822009-12-21 19:57:21 +00001840 << Diag;
1841 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001842}
1843
Eric Christopher9965dea2012-02-16 22:54:45 +00001844/// getOrCreateLimitedType - Get the type from the cache or create a new
1845/// limited type if necessary.
1846llvm::DIType CGDebugInfo::getOrCreateLimitedType(QualType Ty,
1847 llvm::DIFile Unit) {
1848 if (Ty.isNull())
1849 return llvm::DIType();
1850
1851 // Unwrap the type as needed for debug information.
1852 Ty = UnwrapTypeForDebugInfo(Ty);
1853
1854 llvm::DIType T = getTypeOrNull(Ty);
1855
1856 // We may have cached a forward decl when we could have created
1857 // a non-forward decl. Go ahead and create a non-forward decl
1858 // now.
1859 if (T.Verify() && !T.isForwardDecl()) return T;
1860
1861 // Otherwise create the type.
1862 llvm::DIType Res = CreateLimitedTypeNode(Ty, Unit);
1863
Eric Christopher7ff0c5d2012-02-18 00:50:17 +00001864 if (T.Verify() && T.isForwardDecl())
Michael J. Spencer50e3faa2012-06-08 23:47:12 +00001865 ReplaceMap.push_back(std::make_pair(Ty.getAsOpaquePtr(),
1866 static_cast<llvm::Value*>(T)));
Eric Christopher7ff0c5d2012-02-18 00:50:17 +00001867
Eric Christopher9965dea2012-02-16 22:54:45 +00001868 // And update the type cache.
1869 TypeCache[Ty.getAsOpaquePtr()] = Res;
1870 return Res;
1871}
1872
1873// TODO: Currently used for context chains when limiting debug info.
1874llvm::DIType CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
1875 RecordDecl *RD = Ty->getDecl();
1876
1877 // Get overall information about the record type for the debug info.
1878 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
1879 unsigned Line = getLineNumber(RD->getLocation());
1880 StringRef RDName = RD->getName();
1881
1882 llvm::DIDescriptor RDContext;
Alexey Samsonov3a70cd62012-04-27 07:24:20 +00001883 if (CGM.getCodeGenOpts().DebugInfo == CodeGenOptions::LimitedDebugInfo)
Eric Christopher9965dea2012-02-16 22:54:45 +00001884 RDContext = createContextChain(cast<Decl>(RD->getDeclContext()));
1885 else
1886 RDContext = getContextDescriptor(cast<Decl>(RD->getDeclContext()));
1887
1888 // If this is just a forward declaration, construct an appropriately
1889 // marked node and just return it.
Eric Christopher7ff0c5d2012-02-18 00:50:17 +00001890 if (!RD->getDefinition())
1891 return createRecordFwdDecl(RD, RDContext);
Eric Christopher9965dea2012-02-16 22:54:45 +00001892
1893 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1894 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1895 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Benjamin Kramer6181e562012-03-20 19:49:14 +00001896 llvm::TrackingVH<llvm::MDNode> RealDecl;
Eric Christopher9965dea2012-02-16 22:54:45 +00001897
1898 if (RD->isUnion())
1899 RealDecl = DBuilder.createUnionType(RDContext, RDName, DefUnit, Line,
1900 Size, Align, 0, llvm::DIArray());
1901 else if (CXXDecl) {
1902 RDName = getClassName(RD);
1903
1904 // FIXME: This could be a struct type giving a default visibility different
1905 // than C++ class type, but needs llvm metadata changes first.
1906 RealDecl = DBuilder.createClassType(RDContext, RDName, DefUnit, Line,
1907 Size, Align, 0, 0, llvm::DIType(),
Eric Christopher86211df2012-02-20 18:05:24 +00001908 llvm::DIArray(), llvm::DIType(),
Eric Christopher9965dea2012-02-16 22:54:45 +00001909 llvm::DIArray());
1910 } else
1911 RealDecl = DBuilder.createStructType(RDContext, RDName, DefUnit, Line,
1912 Size, Align, 0, llvm::DIArray());
1913
1914 RegionMap[Ty->getDecl()] = llvm::WeakVH(RealDecl);
1915 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = llvm::DIType(RealDecl);
1916
1917 if (CXXDecl) {
1918 // A class's primary base or the class itself contains the vtable.
1919 llvm::MDNode *ContainingType = NULL;
1920 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1921 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
1922 // Seek non virtual primary base root.
1923 while (1) {
1924 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
1925 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
1926 if (PBT && !BRL.isPrimaryBaseVirtual())
1927 PBase = PBT;
1928 else
1929 break;
1930 }
1931 ContainingType =
1932 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), DefUnit);
1933 }
1934 else if (CXXDecl->isDynamicClass())
1935 ContainingType = RealDecl;
1936
Eric Christopher1e009d52012-02-17 07:09:48 +00001937 RealDecl->replaceOperandWith(12, ContainingType);
Eric Christopher9965dea2012-02-16 22:54:45 +00001938 }
1939 return llvm::DIType(RealDecl);
1940}
1941
1942/// CreateLimitedTypeNode - Create a new debug type node, but only forward
1943/// declare composite types that haven't been processed yet.
1944llvm::DIType CGDebugInfo::CreateLimitedTypeNode(QualType Ty,llvm::DIFile Unit) {
1945
1946 // Work out details of type.
1947 switch (Ty->getTypeClass()) {
1948#define TYPE(Class, Base)
1949#define ABSTRACT_TYPE(Class, Base)
1950#define NON_CANONICAL_TYPE(Class, Base)
1951#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1952 #include "clang/AST/TypeNodes.def"
1953 llvm_unreachable("Dependent types cannot show up in debug information");
1954
1955 case Type::Record:
1956 return CreateLimitedType(cast<RecordType>(Ty));
1957 default:
1958 return CreateTypeNode(Ty, Unit);
1959 }
1960}
1961
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001962/// CreateMemberType - Create new member and increase Offset by FType's size.
1963llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001964 StringRef Name,
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001965 uint64_t *Offset) {
1966 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1967 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1968 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel1d323e02011-06-24 22:00:59 +00001969 llvm::DIType Ty = DBuilder.createMemberType(Unit, Name, Unit, 0,
Devang Patel823d8e92010-12-08 22:42:58 +00001970 FieldSize, FieldAlign,
1971 *Offset, 0, FieldTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001972 *Offset += FieldSize;
1973 return Ty;
1974}
1975
Devang Patel120bf322011-04-23 00:08:01 +00001976/// getFunctionDeclaration - Return debug info descriptor to describe method
1977/// declaration for the given method definition.
1978llvm::DISubprogram CGDebugInfo::getFunctionDeclaration(const Decl *D) {
1979 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1980 if (!FD) return llvm::DISubprogram();
1981
1982 // Setup context.
1983 getContextDescriptor(cast<Decl>(D->getDeclContext()));
1984
Devang Patel22a5cdf2011-04-29 23:42:32 +00001985 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
Eric Christopherdeae6a82011-11-17 23:45:00 +00001986 MI = SPCache.find(FD->getCanonicalDecl());
Devang Patel22a5cdf2011-04-29 23:42:32 +00001987 if (MI != SPCache.end()) {
Richard Smithe7259aa2012-08-17 04:17:54 +00001988 llvm::Value *V = MI->second;
1989 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(V));
Devang Patel22a5cdf2011-04-29 23:42:32 +00001990 if (SP.isSubprogram() && !llvm::DISubprogram(SP).isDefinition())
1991 return SP;
1992 }
1993
Devang Patel120bf322011-04-23 00:08:01 +00001994 for (FunctionDecl::redecl_iterator I = FD->redecls_begin(),
1995 E = FD->redecls_end(); I != E; ++I) {
1996 const FunctionDecl *NextFD = *I;
1997 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
Eric Christopherdeae6a82011-11-17 23:45:00 +00001998 MI = SPCache.find(NextFD->getCanonicalDecl());
Devang Patel120bf322011-04-23 00:08:01 +00001999 if (MI != SPCache.end()) {
Richard Smithe7259aa2012-08-17 04:17:54 +00002000 llvm::Value *V = MI->second;
2001 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(V));
Devang Patel120bf322011-04-23 00:08:01 +00002002 if (SP.isSubprogram() && !llvm::DISubprogram(SP).isDefinition())
2003 return SP;
2004 }
2005 }
2006 return llvm::DISubprogram();
2007}
2008
Devang Patel1c296522011-05-31 20:46:46 +00002009// getOrCreateFunctionType - Construct DIType. If it is a c++ method, include
2010// implicit parameter "this".
Eric Christopher85f90bd2012-09-11 01:36:56 +00002011llvm::DIType CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Eric Christopherab5278e2011-10-11 23:00:51 +00002012 QualType FnType,
Devang Patel1c296522011-05-31 20:46:46 +00002013 llvm::DIFile F) {
Eric Christopher363e5ac2012-08-07 00:14:25 +00002014
Devang Patel1c296522011-05-31 20:46:46 +00002015 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2016 return getOrCreateMethodType(Method, F);
Nick Lewycky7480d962011-11-10 00:34:02 +00002017 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
Devang Patelc478f212011-05-31 21:18:50 +00002018 // Add "self" and "_cmd"
Chris Lattner5f9e2722011-07-23 10:55:15 +00002019 SmallVector<llvm::Value *, 16> Elts;
Devang Patelc478f212011-05-31 21:18:50 +00002020
2021 // First element is always return type. For 'void' functions it is NULL.
Devang Pateld127bcb2011-05-31 22:21:11 +00002022 Elts.push_back(getOrCreateType(OMethod->getResultType(), F));
Devang Patelc478f212011-05-31 21:18:50 +00002023 // "self" pointer is always first argument.
Eric Christopher3ed6b912012-09-11 01:36:54 +00002024 llvm::DIType SelfTy = getOrCreateType(OMethod->getSelfDecl()->getType(), F);
Eric Christopherd5a73dc2012-09-12 23:36:49 +00002025 Elts.push_back(DBuilder.createObjectPointerType(SelfTy));
Eric Christopher85f90bd2012-09-11 01:36:56 +00002026 // "_cmd" pointer is always second argument.
Eric Christopher3ed6b912012-09-11 01:36:54 +00002027 llvm::DIType CmdTy = getOrCreateType(OMethod->getCmdDecl()->getType(), F);
2028 Elts.push_back(DBuilder.createArtificialType(CmdTy));
Devang Pateld127bcb2011-05-31 22:21:11 +00002029 // Get rest of the arguments.
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002030 for (ObjCMethodDecl::param_const_iterator PI = OMethod->param_begin(),
Devang Pateld127bcb2011-05-31 22:21:11 +00002031 PE = OMethod->param_end(); PI != PE; ++PI)
2032 Elts.push_back(getOrCreateType((*PI)->getType(), F));
2033
Devang Patelc478f212011-05-31 21:18:50 +00002034 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(Elts);
2035 return DBuilder.createSubroutineType(F, EltTypeArray);
2036 }
Devang Patel1c296522011-05-31 20:46:46 +00002037 return getOrCreateType(FnType, F);
2038}
2039
Eric Christopher451b4412012-03-20 23:28:32 +00002040/// EmitFunctionStart - Constructs the debug code for entering a function.
Devang Patel9c6c3a02010-01-14 00:36:21 +00002041void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00002042 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00002043 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00002044
Chris Lattner5f9e2722011-07-23 10:55:15 +00002045 StringRef Name;
2046 StringRef LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00002047
Eric Christopheraa2164c2011-09-29 00:00:45 +00002048 FnBeginRegionCount.push_back(LexicalBlockStack.size());
Devang Patel5a6fbcf2010-07-22 22:29:16 +00002049
Devang Patel9c6c3a02010-01-14 00:36:21 +00002050 const Decl *D = GD.getDecl();
Eric Christopherea320472012-04-03 00:44:15 +00002051 // Use the location of the declaration.
2052 SourceLocation Loc = D->getLocation();
Eric Christopher73fb3502011-10-13 21:45:18 +00002053
Devang Patel3951e712010-10-07 22:03:49 +00002054 unsigned Flags = 0;
Eric Christopherea320472012-04-03 00:44:15 +00002055 llvm::DIFile Unit = getOrCreateFile(Loc);
Devang Patel0692f832010-10-11 21:58:41 +00002056 llvm::DIDescriptor FDContext(Unit);
Devang Patel5ecb1df2011-04-05 22:54:11 +00002057 llvm::DIArray TParamsArray;
Devang Patel9c6c3a02010-01-14 00:36:21 +00002058 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Eric Christopherbf979472011-11-14 18:55:02 +00002059 // If there is a DISubprogram for this function available then use it.
Devang Patel4125fd22010-01-19 01:54:44 +00002060 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
Eric Christopherdeae6a82011-11-17 23:45:00 +00002061 FI = SPCache.find(FD->getCanonicalDecl());
Devang Patel4125fd22010-01-19 01:54:44 +00002062 if (FI != SPCache.end()) {
Richard Smithe7259aa2012-08-17 04:17:54 +00002063 llvm::Value *V = FI->second;
2064 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(V));
Devang Patelab699792010-05-07 18:12:35 +00002065 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
2066 llvm::MDNode *SPN = SP;
Eric Christopheraa2164c2011-09-29 00:00:45 +00002067 LexicalBlockStack.push_back(SPN);
Devang Patelab699792010-05-07 18:12:35 +00002068 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00002069 return;
2070 }
2071 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00002072 Name = getFunctionName(FD);
2073 // Use mangled name as linkage name for c/c++ functions.
Eric Christopher43443de2012-04-12 00:35:06 +00002074 if (FD->hasPrototype()) {
Devang Patel2df74c02011-05-02 22:37:48 +00002075 LinkageName = CGM.getMangledName(GD);
Eric Christopher43443de2012-04-12 00:35:06 +00002076 Flags |= llvm::DIDescriptor::FlagPrototyped;
2077 }
Alexey Samsonovfd00eec2012-05-04 07:39:27 +00002078 if (LinkageName == Name ||
2079 CGM.getCodeGenOpts().DebugInfo <= CodeGenOptions::DebugLineTablesOnly)
Chris Lattner5f9e2722011-07-23 10:55:15 +00002080 LinkageName = StringRef();
Eric Christopher43443de2012-04-12 00:35:06 +00002081
Alexey Samsonovfd00eec2012-05-04 07:39:27 +00002082 if (CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo) {
2083 if (const NamespaceDecl *NSDecl =
2084 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2085 FDContext = getOrCreateNameSpace(NSDecl);
2086 else if (const RecordDecl *RDecl =
2087 dyn_cast_or_null<RecordDecl>(FD->getDeclContext()))
2088 FDContext = getContextDescriptor(cast<Decl>(RDecl->getDeclContext()));
Devang Patel5ecb1df2011-04-05 22:54:11 +00002089
Alexey Samsonovfd00eec2012-05-04 07:39:27 +00002090 // Collect template parameters.
2091 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2092 }
David Chisnall70b9b442010-09-02 17:16:32 +00002093 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
David Chisnall52044a22010-09-02 18:01:51 +00002094 Name = getObjCMethodName(OMD);
Devang Patel3951e712010-10-07 22:03:49 +00002095 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00002096 } else {
Devang Patel58faf202010-10-22 17:11:50 +00002097 // Use llvm function name.
Devang Patel9c6c3a02010-01-14 00:36:21 +00002098 Name = Fn->getName();
Devang Patel3951e712010-10-07 22:03:49 +00002099 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00002100 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00002101 if (!Name.empty() && Name[0] == '\01')
2102 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002103
Eric Christopherea320472012-04-03 00:44:15 +00002104 unsigned LineNo = getLineNumber(Loc);
Devang Patele2472482010-09-29 21:05:52 +00002105 if (D->isImplicit())
2106 Flags |= llvm::DIDescriptor::FlagArtificial;
Eric Christopherea320472012-04-03 00:44:15 +00002107
Alexey Samsonovfd00eec2012-05-04 07:39:27 +00002108 llvm::DIType DIFnType;
2109 llvm::DISubprogram SPDecl;
2110 if (CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo) {
2111 DIFnType = getOrCreateFunctionType(D, FnType, Unit);
2112 SPDecl = getFunctionDeclaration(D);
2113 } else {
2114 // Create fake but valid subroutine type. Otherwise
2115 // llvm::DISubprogram::Verify() would return false, and
2116 // subprogram DIE will miss DW_AT_decl_file and
2117 // DW_AT_decl_line fields.
2118 SmallVector<llvm::Value*, 16> Elts;
2119 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(Elts);
2120 DIFnType = DBuilder.createSubroutineType(Unit, EltTypeArray);
2121 }
2122 llvm::DISubprogram SP;
2123 SP = DBuilder.createFunction(FDContext, Name, LinkageName, Unit,
2124 LineNo, DIFnType,
2125 Fn->hasInternalLinkage(), true/*definition*/,
2126 getLineNumber(CurLoc), Flags,
2127 CGM.getLangOpts().Optimize,
2128 Fn, TParamsArray, SPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002129
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00002130 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00002131 llvm::MDNode *SPN = SP;
Eric Christopheraa2164c2011-09-29 00:00:45 +00002132 LexicalBlockStack.push_back(SPN);
Devang Patelab699792010-05-07 18:12:35 +00002133 RegionMap[D] = llvm::WeakVH(SP);
Eric Christopher69a1b742011-09-29 00:00:37 +00002134}
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00002135
Eric Christopher5321bc42011-09-29 00:00:41 +00002136/// EmitLocation - Emit metadata to indicate a change in line/column
2137/// information in the source file.
Eric Christopher73fb3502011-10-13 21:45:18 +00002138void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
2139
2140 // Update our current location
2141 setLocation(Loc);
2142
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00002143 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00002144
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00002145 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00002146 SourceManager &SM = CGM.getContext().getSourceManager();
Eric Christopher73fb3502011-10-13 21:45:18 +00002147 if (CurLoc == PrevLoc ||
Chandler Carruth40278532011-07-25 16:49:02 +00002148 SM.getExpansionLoc(CurLoc) == SM.getExpansionLoc(PrevLoc))
Devang Patel4800ea62010-04-05 21:09:15 +00002149 // New Builder may not be in sync with CGDebugInfo.
2150 if (!Builder.getCurrentDebugLocation().isUnknown())
2151 return;
Eric Christopher414ee4b2011-09-29 00:00:35 +00002152
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00002153 // Update last state.
2154 PrevLoc = CurLoc;
2155
Eric Christopheraa2164c2011-09-29 00:00:45 +00002156 llvm::MDNode *Scope = LexicalBlockStack.back();
Devang Patel8ab870d2010-05-12 23:46:38 +00002157 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
2158 getColumnNumber(CurLoc),
Chris Lattnere541d012010-04-02 20:21:43 +00002159 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00002160}
2161
Eric Christopher73fb3502011-10-13 21:45:18 +00002162/// CreateLexicalBlock - Creates a new lexical block node and pushes it on
2163/// the stack.
2164void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Devang Patel8fae0602009-11-13 19:10:24 +00002165 llvm::DIDescriptor D =
Eric Christopher73fb3502011-10-13 21:45:18 +00002166 DBuilder.createLexicalBlock(LexicalBlockStack.empty() ?
Devang Patel53bc5182012-02-08 00:10:20 +00002167 llvm::DIDescriptor() :
2168 llvm::DIDescriptor(LexicalBlockStack.back()),
2169 getOrCreateFile(CurLoc),
2170 getLineNumber(CurLoc),
2171 getColumnNumber(CurLoc));
Devang Patelab699792010-05-07 18:12:35 +00002172 llvm::MDNode *DN = D;
Eric Christopheraa2164c2011-09-29 00:00:45 +00002173 LexicalBlockStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00002174}
2175
Eric Christopher73fb3502011-10-13 21:45:18 +00002176/// EmitLexicalBlockStart - Constructs the debug code for entering a declarative
2177/// region - beginning of a DW_TAG_lexical_block.
2178void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc) {
2179 // Set our current location.
2180 setLocation(Loc);
2181
2182 // Create a new lexical block and push it on the stack.
2183 CreateLexicalBlock(Loc);
2184
2185 // Emit a line table change for the current location inside the new scope.
2186 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(Loc),
Devang Patel53bc5182012-02-08 00:10:20 +00002187 getColumnNumber(Loc),
2188 LexicalBlockStack.back()));
Eric Christopher73fb3502011-10-13 21:45:18 +00002189}
2190
Eric Christopheraa2164c2011-09-29 00:00:45 +00002191/// EmitLexicalBlockEnd - Constructs the debug code for exiting a declarative
Eric Christopher43202ae2011-09-26 15:03:22 +00002192/// region - end of a DW_TAG_lexical_block.
Eric Christopher73fb3502011-10-13 21:45:18 +00002193void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc) {
Eric Christopheraa2164c2011-09-29 00:00:45 +00002194 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherc852e9f2012-07-11 15:32:13 +00002195
2196 // Provide an entry in the line table for the end of the block.
2197 EmitLocation(Builder, Loc);
2198
Eric Christopheraa2164c2011-09-29 00:00:45 +00002199 LexicalBlockStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00002200}
2201
Devang Patel5a6fbcf2010-07-22 22:29:16 +00002202/// EmitFunctionEnd - Constructs the debug code for exiting a function.
2203void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
Eric Christopheraa2164c2011-09-29 00:00:45 +00002204 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Devang Patel5a6fbcf2010-07-22 22:29:16 +00002205 unsigned RCount = FnBeginRegionCount.back();
Eric Christopheraa2164c2011-09-29 00:00:45 +00002206 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
Devang Patel5a6fbcf2010-07-22 22:29:16 +00002207
2208 // Pop all regions for this function.
Eric Christopheraa2164c2011-09-29 00:00:45 +00002209 while (LexicalBlockStack.size() != RCount)
Eric Christopher73fb3502011-10-13 21:45:18 +00002210 EmitLexicalBlockEnd(Builder, CurLoc);
Devang Patel5a6fbcf2010-07-22 22:29:16 +00002211 FnBeginRegionCount.pop_back();
2212}
2213
Devang Patel809b9bb2010-02-10 18:49:08 +00002214// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
2215// See BuildByRefType.
2216llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
2217 uint64_t *XOffset) {
2218
Chris Lattner5f9e2722011-07-23 10:55:15 +00002219 SmallVector<llvm::Value *, 5> EltTys;
Devang Patel809b9bb2010-02-10 18:49:08 +00002220 QualType FType;
2221 uint64_t FieldSize, FieldOffset;
2222 unsigned FieldAlign;
2223
Devang Patel17800552010-03-09 00:44:50 +00002224 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00002225 QualType Type = VD->getType();
2226
2227 FieldOffset = 0;
2228 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00002229 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2230 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00002231 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00002232 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2233 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2234
John McCall6b5a61b2011-02-07 10:33:21 +00002235 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type);
Devang Patel809b9bb2010-02-10 18:49:08 +00002236 if (HasCopyAndDispose) {
2237 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00002238 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
2239 &FieldOffset));
2240 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
2241 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00002242 }
2243
2244 CharUnits Align = CGM.getContext().getDeclAlign(VD);
Ken Dyck573be632011-04-22 17:34:18 +00002245 if (Align > CGM.getContext().toCharUnitsFromBits(
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002246 CGM.getContext().getTargetInfo().getPointerAlign(0))) {
Ken Dyck573be632011-04-22 17:34:18 +00002247 CharUnits FieldOffsetInBytes
2248 = CGM.getContext().toCharUnitsFromBits(FieldOffset);
2249 CharUnits AlignedOffsetInBytes
2250 = FieldOffsetInBytes.RoundUpToAlignment(Align);
2251 CharUnits NumPaddingBytes
2252 = AlignedOffsetInBytes - FieldOffsetInBytes;
Devang Patel809b9bb2010-02-10 18:49:08 +00002253
Ken Dyck573be632011-04-22 17:34:18 +00002254 if (NumPaddingBytes.isPositive()) {
2255 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
Devang Patel809b9bb2010-02-10 18:49:08 +00002256 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2257 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00002258 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00002259 }
2260 }
2261
2262 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00002263 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00002264 FieldSize = CGM.getContext().getTypeSize(FType);
Ken Dyck573be632011-04-22 17:34:18 +00002265 FieldAlign = CGM.getContext().toBits(Align);
Devang Patel809b9bb2010-02-10 18:49:08 +00002266
2267 *XOffset = FieldOffset;
Devang Patel1d323e02011-06-24 22:00:59 +00002268 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit,
Devang Patel823d8e92010-12-08 22:42:58 +00002269 0, FieldSize, FieldAlign,
2270 FieldOffset, 0, FieldTy);
Devang Patel809b9bb2010-02-10 18:49:08 +00002271 EltTys.push_back(FieldTy);
2272 FieldOffset += FieldSize;
2273
Jay Foadc556ef22011-04-24 10:11:03 +00002274 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Devang Patel809b9bb2010-02-10 18:49:08 +00002275
Devang Patele2472482010-09-29 21:05:52 +00002276 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Devang Patel809b9bb2010-02-10 18:49:08 +00002277
Devang Patel16674e82011-02-22 18:56:36 +00002278 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Devang Patel823d8e92010-12-08 22:42:58 +00002279 Elements);
Devang Patel809b9bb2010-02-10 18:49:08 +00002280}
Devang Patel823d8e92010-12-08 22:42:58 +00002281
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00002282/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00002283void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Devang Patel093ac462011-03-03 20:13:15 +00002284 llvm::Value *Storage,
2285 unsigned ArgNo, CGBuilderTy &Builder) {
Alexey Samsonovfd00eec2012-05-04 07:39:27 +00002286 assert(CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo);
Eric Christopheraa2164c2011-09-29 00:00:45 +00002287 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Daniel Dunbar5273f512008-10-17 01:07:56 +00002288
Devang Patel17800552010-03-09 00:44:50 +00002289 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00002290 llvm::DIType Ty;
2291 uint64_t XOffset = 0;
2292 if (VD->hasAttr<BlocksAttr>())
2293 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
2294 else
2295 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00002296
Eric Christopher195ff582012-09-19 21:47:29 +00002297 // If there is no debug info for this type then do not emit debug info
Devang Patelf4e54a22010-05-07 23:05:55 +00002298 // for this variable.
2299 if (!Ty)
2300 return;
2301
Devang Patel34753802011-02-16 01:11:51 +00002302 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage)) {
2303 // If Storage is an aggregate returned as 'sret' then let debugger know
2304 // about this.
Devang Patel0691f932011-02-10 00:40:52 +00002305 if (Arg->hasStructRetAttr())
Eric Christopher37e4cea2012-05-19 01:36:50 +00002306 Ty = DBuilder.createReferenceType(llvm::dwarf::DW_TAG_reference_type, Ty);
Devang Patel34753802011-02-16 01:11:51 +00002307 else if (CXXRecordDecl *Record = VD->getType()->getAsCXXRecordDecl()) {
2308 // If an aggregate variable has non trivial destructor or non trivial copy
2309 // constructor than it is pass indirectly. Let debug info know about this
2310 // by using reference of the aggregate type as a argument type.
Eric Christopherab5278e2011-10-11 23:00:51 +00002311 if (!Record->hasTrivialCopyConstructor() ||
2312 !Record->hasTrivialDestructor())
Eric Christopher37e4cea2012-05-19 01:36:50 +00002313 Ty = DBuilder.createReferenceType(llvm::dwarf::DW_TAG_reference_type, Ty);
Devang Patel34753802011-02-16 01:11:51 +00002314 }
2315 }
Devang Patel0691f932011-02-10 00:40:52 +00002316
Chris Lattner9c85ba32008-11-10 06:08:34 +00002317 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00002318 unsigned Line = getLineNumber(VD->getLocation());
2319 unsigned Column = getColumnNumber(VD->getLocation());
Devang Patelaca745b2010-09-29 23:09:21 +00002320 unsigned Flags = 0;
2321 if (VD->isImplicit())
2322 Flags |= llvm::DIDescriptor::FlagArtificial;
Eric Christopherd5a73dc2012-09-12 23:36:49 +00002323 // If this is the first argument and it is implicit then
2324 // give it an object pointer flag.
2325 // FIXME: There has to be a better way to do this, but for static
2326 // functions there won't be an implicit param at arg1 and
2327 // otherwise it is 'self' or 'this'.
2328 if (isa<ImplicitParamDecl>(VD) && ArgNo == 1)
2329 Flags |= llvm::DIDescriptor::FlagObjectPointer;
2330
Eric Christopheraa2164c2011-09-29 00:00:45 +00002331 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christopherd5a73dc2012-09-12 23:36:49 +00002332
Chris Lattner5f9e2722011-07-23 10:55:15 +00002333 StringRef Name = VD->getName();
Devang Patelcebbedd2010-10-12 23:24:54 +00002334 if (!Name.empty()) {
Devang Patelb1fd0eb2011-01-11 00:30:27 +00002335 if (VD->hasAttr<BlocksAttr>()) {
2336 CharUnits offset = CharUnits::fromQuantity(32);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002337 SmallVector<llvm::Value *, 9> addr;
Chris Lattner8b418682012-02-07 00:39:47 +00002338 llvm::Type *Int64Ty = CGM.Int64Ty;
Devang Patel4a4e2ef2011-02-18 23:29:22 +00002339 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Devang Patelb1fd0eb2011-01-11 00:30:27 +00002340 // offset of __forwarding field
Ken Dyck0ebce0e2011-04-22 17:41:34 +00002341 offset = CGM.getContext().toCharUnitsFromBits(
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002342 CGM.getContext().getTargetInfo().getPointerWidth(0));
Devang Patelb1fd0eb2011-01-11 00:30:27 +00002343 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Devang Patel4a4e2ef2011-02-18 23:29:22 +00002344 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
2345 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Devang Patelb1fd0eb2011-01-11 00:30:27 +00002346 // offset of x field
Ken Dyck0ebce0e2011-04-22 17:41:34 +00002347 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Devang Patelb1fd0eb2011-01-11 00:30:27 +00002348 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
2349
2350 // Create the descriptor for the variable.
2351 llvm::DIVariable D =
Devang Patel16674e82011-02-22 18:56:36 +00002352 DBuilder.createComplexVariable(Tag,
Eric Christopherab5278e2011-10-11 23:00:51 +00002353 llvm::DIDescriptor(Scope),
Devang Patelb1fd0eb2011-01-11 00:30:27 +00002354 VD->getName(), Unit, Line, Ty,
Jay Foadc556ef22011-04-24 10:11:03 +00002355 addr, ArgNo);
Devang Patelb1fd0eb2011-01-11 00:30:27 +00002356
2357 // Insert an llvm.dbg.declare into the current block.
2358 llvm::Instruction *Call =
Devang Patel16674e82011-02-22 18:56:36 +00002359 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patelb1fd0eb2011-01-11 00:30:27 +00002360 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
2361 return;
Eric Christophera135f2c2012-05-08 18:56:47 +00002362 } else if (isa<VariableArrayType>(VD->getType())) {
2363 // These are "complex" variables in that they need an op_deref.
Devang Patelb1fd0eb2011-01-11 00:30:27 +00002364 // Create the descriptor for the variable.
Eric Christophera135f2c2012-05-08 18:56:47 +00002365 llvm::Value *Addr = llvm::ConstantInt::get(CGM.Int64Ty,
2366 llvm::DIBuilder::OpDeref);
2367 llvm::DIVariable D =
2368 DBuilder.createComplexVariable(Tag,
2369 llvm::DIDescriptor(Scope),
2370 Name, Unit, Line, Ty,
2371 Addr, ArgNo);
2372
2373 // Insert an llvm.dbg.declare into the current block.
2374 llvm::Instruction *Call =
2375 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
2376 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
2377 return;
2378 }
2379
2380 // Create the descriptor for the variable.
Devang Patelcebbedd2010-10-12 23:24:54 +00002381 llvm::DIVariable D =
Devang Patel16674e82011-02-22 18:56:36 +00002382 DBuilder.createLocalVariable(Tag, llvm::DIDescriptor(Scope),
Devang Patel823d8e92010-12-08 22:42:58 +00002383 Name, Unit, Line, Ty,
David Blaikie4e4d0842012-03-11 07:00:24 +00002384 CGM.getLangOpts().Optimize, Flags, ArgNo);
Devang Patelcebbedd2010-10-12 23:24:54 +00002385
2386 // Insert an llvm.dbg.declare into the current block.
2387 llvm::Instruction *Call =
Devang Patel16674e82011-02-22 18:56:36 +00002388 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patelcebbedd2010-10-12 23:24:54 +00002389 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Devang Patelf4dd9622010-10-29 16:21:19 +00002390 return;
Devang Patelcebbedd2010-10-12 23:24:54 +00002391 }
2392
2393 // If VD is an anonymous union then Storage represents value for
2394 // all union fields.
John McCall8178df32011-02-22 22:38:33 +00002395 if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2396 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
2397 if (RD->isUnion()) {
2398 for (RecordDecl::field_iterator I = RD->field_begin(),
2399 E = RD->field_end();
2400 I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002401 FieldDecl *Field = *I;
John McCall8178df32011-02-22 22:38:33 +00002402 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002403 StringRef FieldName = Field->getName();
Devang Patelcebbedd2010-10-12 23:24:54 +00002404
John McCall8178df32011-02-22 22:38:33 +00002405 // Ignore unnamed fields. Do not ignore unnamed records.
2406 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2407 continue;
Devang Patelcebbedd2010-10-12 23:24:54 +00002408
John McCall8178df32011-02-22 22:38:33 +00002409 // Use VarDecl's Tag, Scope and Line number.
2410 llvm::DIVariable D =
2411 DBuilder.createLocalVariable(Tag, llvm::DIDescriptor(Scope),
2412 FieldName, Unit, Line, FieldTy,
David Blaikie4e4d0842012-03-11 07:00:24 +00002413 CGM.getLangOpts().Optimize, Flags,
Devang Patel093ac462011-03-03 20:13:15 +00002414 ArgNo);
Devang Patelcebbedd2010-10-12 23:24:54 +00002415
John McCall8178df32011-02-22 22:38:33 +00002416 // Insert an llvm.dbg.declare into the current block.
2417 llvm::Instruction *Call =
2418 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
John McCall8178df32011-02-22 22:38:33 +00002419 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Devang Patelcebbedd2010-10-12 23:24:54 +00002420 }
John McCall8178df32011-02-22 22:38:33 +00002421 }
2422 }
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00002423}
2424
Devang Patele2d01912011-04-25 23:43:36 +00002425void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
2426 llvm::Value *Storage,
2427 CGBuilderTy &Builder) {
Alexey Samsonovfd00eec2012-05-04 07:39:27 +00002428 assert(CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo);
Devang Patele2d01912011-04-25 23:43:36 +00002429 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, 0, Builder);
2430}
Mike Stumpb1a6e682009-09-30 02:43:10 +00002431
Eric Christopher245d5a32012-09-21 22:18:42 +00002432void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(const VarDecl *VD,
2433 llvm::Value *Storage,
2434 CGBuilderTy &Builder,
2435 const CGBlockInfo &blockInfo) {
Alexey Samsonovfd00eec2012-05-04 07:39:27 +00002436 assert(CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo);
Eric Christopheraa2164c2011-09-29 00:00:45 +00002437 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Devang Patele2d01912011-04-25 23:43:36 +00002438
Devang Patel2b594b92010-04-26 23:28:46 +00002439 if (Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00002440 return;
Devang Patele2d01912011-04-25 23:43:36 +00002441
John McCall6b5a61b2011-02-07 10:33:21 +00002442 bool isByRef = VD->hasAttr<BlocksAttr>();
Devang Patele2d01912011-04-25 23:43:36 +00002443
Mike Stumpb1a6e682009-09-30 02:43:10 +00002444 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00002445 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00002446 llvm::DIType Ty;
John McCall6b5a61b2011-02-07 10:33:21 +00002447 if (isByRef)
Devang Patel809b9bb2010-02-10 18:49:08 +00002448 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
2449 else
2450 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00002451
Eric Christopher245d5a32012-09-21 22:18:42 +00002452 // Self is passed along as an implicit non-arg variable in a
2453 // block. Mark it as the object pointer.
2454 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
2455 Ty = DBuilder.createObjectPointerType(Ty);
2456
Mike Stumpb1a6e682009-09-30 02:43:10 +00002457 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00002458 unsigned Line = getLineNumber(VD->getLocation());
2459 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00002460
Micah Villmow25a6a842012-10-08 16:25:52 +00002461 const llvm::DataLayout &target = CGM.getDataLayout();
John McCall6b5a61b2011-02-07 10:33:21 +00002462
2463 CharUnits offset = CharUnits::fromQuantity(
2464 target.getStructLayout(blockInfo.StructureType)
2465 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
2466
Chris Lattner5f9e2722011-07-23 10:55:15 +00002467 SmallVector<llvm::Value *, 9> addr;
Chris Lattner8b418682012-02-07 00:39:47 +00002468 llvm::Type *Int64Ty = CGM.Int64Ty;
Devang Patel4a4e2ef2011-02-18 23:29:22 +00002469 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Chris Lattner14b1a362010-01-25 03:29:35 +00002470 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
John McCall6b5a61b2011-02-07 10:33:21 +00002471 if (isByRef) {
Devang Patel4a4e2ef2011-02-18 23:29:22 +00002472 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
2473 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00002474 // offset of __forwarding field
Eric Christopherab5278e2011-10-11 23:00:51 +00002475 offset = CGM.getContext()
Micah Villmowcadaf4b2012-10-11 17:21:41 +00002476 .toCharUnitsFromBits(target.getPointerSizeInBits(0));
Chris Lattner14b1a362010-01-25 03:29:35 +00002477 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Devang Patel4a4e2ef2011-02-18 23:29:22 +00002478 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
2479 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00002480 // offset of x field
Ken Dyck0ebce0e2011-04-22 17:41:34 +00002481 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Chris Lattner14b1a362010-01-25 03:29:35 +00002482 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00002483 }
2484
2485 // Create the descriptor for the variable.
2486 llvm::DIVariable D =
Devang Patele2d01912011-04-25 23:43:36 +00002487 DBuilder.createComplexVariable(llvm::dwarf::DW_TAG_auto_variable,
Eric Christopheraa2164c2011-09-29 00:00:45 +00002488 llvm::DIDescriptor(LexicalBlockStack.back()),
Jay Foadc556ef22011-04-24 10:11:03 +00002489 VD->getName(), Unit, Line, Ty, addr);
Mike Stumpb1a6e682009-09-30 02:43:10 +00002490 // Insert an llvm.dbg.declare into the current block.
Eric Christopher73fb3502011-10-13 21:45:18 +00002491 llvm::Instruction *Call =
Devang Patel50811d22011-04-25 23:52:27 +00002492 DBuilder.insertDeclare(Storage, D, Builder.GetInsertPoint());
Eric Christopher73fb3502011-10-13 21:45:18 +00002493 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column,
2494 LexicalBlockStack.back()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00002495}
2496
Chris Lattner9c85ba32008-11-10 06:08:34 +00002497/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
2498/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00002499void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Devang Patel093ac462011-03-03 20:13:15 +00002500 unsigned ArgNo,
Devang Patel34753802011-02-16 01:11:51 +00002501 CGBuilderTy &Builder) {
Alexey Samsonovfd00eec2012-05-04 07:39:27 +00002502 assert(CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo);
Devang Patel093ac462011-03-03 20:13:15 +00002503 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, ArgNo, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00002504}
2505
John McCall8178df32011-02-22 22:38:33 +00002506namespace {
2507 struct BlockLayoutChunk {
2508 uint64_t OffsetInBits;
2509 const BlockDecl::Capture *Capture;
2510 };
2511 bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
2512 return l.OffsetInBits < r.OffsetInBits;
2513 }
2514}
Chris Lattner9c85ba32008-11-10 06:08:34 +00002515
John McCall8178df32011-02-22 22:38:33 +00002516void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
2517 llvm::Value *addr,
2518 CGBuilderTy &Builder) {
Alexey Samsonovfd00eec2012-05-04 07:39:27 +00002519 assert(CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo);
John McCall8178df32011-02-22 22:38:33 +00002520 ASTContext &C = CGM.getContext();
2521 const BlockDecl *blockDecl = block.getBlockDecl();
2522
2523 // Collect some general information about the block's location.
2524 SourceLocation loc = blockDecl->getCaretLocation();
2525 llvm::DIFile tunit = getOrCreateFile(loc);
2526 unsigned line = getLineNumber(loc);
2527 unsigned column = getColumnNumber(loc);
2528
2529 // Build the debug-info type for the block literal.
Nick Lewycky7d4b1592011-05-02 01:41:48 +00002530 getContextDescriptor(cast<Decl>(blockDecl->getDeclContext()));
John McCall8178df32011-02-22 22:38:33 +00002531
2532 const llvm::StructLayout *blockLayout =
Micah Villmow25a6a842012-10-08 16:25:52 +00002533 CGM.getDataLayout().getStructLayout(block.StructureType);
John McCall8178df32011-02-22 22:38:33 +00002534
Chris Lattner5f9e2722011-07-23 10:55:15 +00002535 SmallVector<llvm::Value*, 16> fields;
John McCall8178df32011-02-22 22:38:33 +00002536 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
2537 blockLayout->getElementOffsetInBits(0),
Devang Patel1d323e02011-06-24 22:00:59 +00002538 tunit, tunit));
John McCall8178df32011-02-22 22:38:33 +00002539 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
2540 blockLayout->getElementOffsetInBits(1),
Devang Patel1d323e02011-06-24 22:00:59 +00002541 tunit, tunit));
John McCall8178df32011-02-22 22:38:33 +00002542 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
2543 blockLayout->getElementOffsetInBits(2),
Devang Patel1d323e02011-06-24 22:00:59 +00002544 tunit, tunit));
John McCall8178df32011-02-22 22:38:33 +00002545 fields.push_back(createFieldType("__FuncPtr", C.VoidPtrTy, 0, loc, AS_public,
2546 blockLayout->getElementOffsetInBits(3),
Devang Patel1d323e02011-06-24 22:00:59 +00002547 tunit, tunit));
John McCall8178df32011-02-22 22:38:33 +00002548 fields.push_back(createFieldType("__descriptor",
2549 C.getPointerType(block.NeedsCopyDispose ?
2550 C.getBlockDescriptorExtendedType() :
2551 C.getBlockDescriptorType()),
2552 0, loc, AS_public,
2553 blockLayout->getElementOffsetInBits(4),
Devang Patel1d323e02011-06-24 22:00:59 +00002554 tunit, tunit));
John McCall8178df32011-02-22 22:38:33 +00002555
2556 // We want to sort the captures by offset, not because DWARF
2557 // requires this, but because we're paranoid about debuggers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002558 SmallVector<BlockLayoutChunk, 8> chunks;
John McCall8178df32011-02-22 22:38:33 +00002559
2560 // 'this' capture.
2561 if (blockDecl->capturesCXXThis()) {
2562 BlockLayoutChunk chunk;
2563 chunk.OffsetInBits =
2564 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
2565 chunk.Capture = 0;
2566 chunks.push_back(chunk);
2567 }
2568
2569 // Variable captures.
2570 for (BlockDecl::capture_const_iterator
2571 i = blockDecl->capture_begin(), e = blockDecl->capture_end();
2572 i != e; ++i) {
2573 const BlockDecl::Capture &capture = *i;
2574 const VarDecl *variable = capture.getVariable();
2575 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
2576
2577 // Ignore constant captures.
2578 if (captureInfo.isConstant())
2579 continue;
2580
2581 BlockLayoutChunk chunk;
2582 chunk.OffsetInBits =
2583 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
2584 chunk.Capture = &capture;
2585 chunks.push_back(chunk);
2586 }
2587
2588 // Sort by offset.
2589 llvm::array_pod_sort(chunks.begin(), chunks.end());
2590
Chris Lattner5f9e2722011-07-23 10:55:15 +00002591 for (SmallVectorImpl<BlockLayoutChunk>::iterator
John McCall8178df32011-02-22 22:38:33 +00002592 i = chunks.begin(), e = chunks.end(); i != e; ++i) {
2593 uint64_t offsetInBits = i->OffsetInBits;
2594 const BlockDecl::Capture *capture = i->Capture;
2595
2596 // If we have a null capture, this must be the C++ 'this' capture.
2597 if (!capture) {
2598 const CXXMethodDecl *method =
2599 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
2600 QualType type = method->getThisType(C);
2601
2602 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
Devang Patel1d323e02011-06-24 22:00:59 +00002603 offsetInBits, tunit, tunit));
John McCall8178df32011-02-22 22:38:33 +00002604 continue;
2605 }
2606
2607 const VarDecl *variable = capture->getVariable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002608 StringRef name = variable->getName();
John McCalld113a6f2011-03-02 06:57:14 +00002609
2610 llvm::DIType fieldType;
2611 if (capture->isByRef()) {
2612 std::pair<uint64_t,unsigned> ptrInfo = C.getTypeInfo(C.VoidPtrTy);
2613
2614 // FIXME: this creates a second copy of this type!
2615 uint64_t xoffset;
2616 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
2617 fieldType = DBuilder.createPointerType(fieldType, ptrInfo.first);
Devang Patel1d323e02011-06-24 22:00:59 +00002618 fieldType = DBuilder.createMemberType(tunit, name, tunit, line,
John McCalld113a6f2011-03-02 06:57:14 +00002619 ptrInfo.first, ptrInfo.second,
2620 offsetInBits, 0, fieldType);
2621 } else {
2622 fieldType = createFieldType(name, variable->getType(), 0,
Devang Patel1d323e02011-06-24 22:00:59 +00002623 loc, AS_public, offsetInBits, tunit, tunit);
John McCalld113a6f2011-03-02 06:57:14 +00002624 }
2625 fields.push_back(fieldType);
John McCall8178df32011-02-22 22:38:33 +00002626 }
2627
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002628 SmallString<36> typeName;
John McCall8178df32011-02-22 22:38:33 +00002629 llvm::raw_svector_ostream(typeName)
2630 << "__block_literal_" << CGM.getUniqueBlockCount();
2631
Jay Foadc556ef22011-04-24 10:11:03 +00002632 llvm::DIArray fieldsArray = DBuilder.getOrCreateArray(fields);
John McCall8178df32011-02-22 22:38:33 +00002633
2634 llvm::DIType type =
2635 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
2636 CGM.getContext().toBits(block.BlockSize),
2637 CGM.getContext().toBits(block.BlockAlign),
2638 0, fieldsArray);
2639 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
2640
2641 // Get overall information about the block.
2642 unsigned flags = llvm::DIDescriptor::FlagArtificial;
Eric Christopheraa2164c2011-09-29 00:00:45 +00002643 llvm::MDNode *scope = LexicalBlockStack.back();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002644 StringRef name = ".block_descriptor";
John McCall8178df32011-02-22 22:38:33 +00002645
2646 // Create the descriptor for the parameter.
2647 llvm::DIVariable debugVar =
2648 DBuilder.createLocalVariable(llvm::dwarf::DW_TAG_arg_variable,
2649 llvm::DIDescriptor(scope),
2650 name, tunit, line, type,
David Blaikie4e4d0842012-03-11 07:00:24 +00002651 CGM.getLangOpts().Optimize, flags,
Devang Patel093ac462011-03-03 20:13:15 +00002652 cast<llvm::Argument>(addr)->getArgNo() + 1);
John McCall8178df32011-02-22 22:38:33 +00002653
2654 // Insert an llvm.dbg.value into the current block.
2655 llvm::Instruction *declare =
2656 DBuilder.insertDbgValueIntrinsic(addr, 0, debugVar,
2657 Builder.GetInsertBlock());
2658 declare->setDebugLoc(llvm::DebugLoc::get(line, column, scope));
2659}
Chris Lattner9c85ba32008-11-10 06:08:34 +00002660
Sanjiv Gupta686226b2008-06-05 08:59:10 +00002661/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00002662void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00002663 const VarDecl *D) {
Alexey Samsonovfd00eec2012-05-04 07:39:27 +00002664 assert(CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00002665 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00002666 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00002667 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +00002668
Eric Christopher73fb3502011-10-13 21:45:18 +00002669 setLocation(D->getLocation());
2670
Devang Pateleb6d79b2010-02-01 21:34:11 +00002671 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00002672 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002673
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00002674 // CodeGen turns int[] into int[1] so we'll do the same here.
Benjamin Kramer65263b42012-08-04 17:00:46 +00002675 llvm::APInt ConstVal(32, 1);
Anders Carlsson20f12a22009-12-06 18:00:51 +00002676 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00002677
Anders Carlsson20f12a22009-12-06 18:00:51 +00002678 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Nick Lewyckyd4c100e2011-11-09 04:25:21 +00002679 ArrayType::Normal, 0);
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00002680 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002681 StringRef DeclName = D->getName();
2682 StringRef LinkageName;
Devang Pateleb4c45b2011-02-09 19:16:38 +00002683 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext())
2684 && !isa<ObjCMethodDecl>(D->getDeclContext()))
Devang Patel8b90a782010-05-13 23:52:37 +00002685 LinkageName = Var->getName();
Devang Patel58faf202010-10-22 17:11:50 +00002686 if (LinkageName == DeclName)
Chris Lattner5f9e2722011-07-23 10:55:15 +00002687 LinkageName = StringRef();
Devang Pateleb6d79b2010-02-01 21:34:11 +00002688 llvm::DIDescriptor DContext =
Devang Patel170cef32010-12-09 00:33:05 +00002689 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()));
Devang Patel16674e82011-02-22 18:56:36 +00002690 DBuilder.createStaticVariable(DContext, DeclName, LinkageName,
Devang Patel823d8e92010-12-08 22:42:58 +00002691 Unit, LineNo, getOrCreateType(T, Unit),
2692 Var->hasInternalLinkage(), Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00002693}
2694
Devang Patel9ca36b62009-02-26 21:10:26 +00002695/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00002696void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00002697 ObjCInterfaceDecl *ID) {
Alexey Samsonovfd00eec2012-05-04 07:39:27 +00002698 assert(CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo);
Devang Patel9ca36b62009-02-26 21:10:26 +00002699 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00002700 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00002701 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patel9ca36b62009-02-26 21:10:26 +00002702
Chris Lattner5f9e2722011-07-23 10:55:15 +00002703 StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00002704
Devang Pateld6c5a262010-02-01 21:52:22 +00002705 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00002706 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002707
Devang Patel9ca36b62009-02-26 21:10:26 +00002708 // CodeGen turns int[] into int[1] so we'll do the same here.
Benjamin Kramer65263b42012-08-04 17:00:46 +00002709 llvm::APInt ConstVal(32, 1);
Anders Carlsson20f12a22009-12-06 18:00:51 +00002710 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00002711
Anders Carlsson20f12a22009-12-06 18:00:51 +00002712 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00002713 ArrayType::Normal, 0);
2714 }
2715
Devang Patel16674e82011-02-22 18:56:36 +00002716 DBuilder.createGlobalVariable(Name, Unit, LineNo,
Devang Patel823d8e92010-12-08 22:42:58 +00002717 getOrCreateType(T, Unit),
2718 Var->hasInternalLinkage(), Var);
Devang Patel9ca36b62009-02-26 21:10:26 +00002719}
Devang Patelabb485f2010-02-01 19:16:32 +00002720
Devang Patel25c2c8f2010-08-10 17:53:33 +00002721/// EmitGlobalVariable - Emit global variable's debug info.
2722void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
John McCall189d6ef2010-10-09 01:34:31 +00002723 llvm::Constant *Init) {
Alexey Samsonovfd00eec2012-05-04 07:39:27 +00002724 assert(CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo);
Devang Patel8d308382010-08-10 07:24:25 +00002725 // Create the descriptor for the variable.
2726 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Chris Lattner5f9e2722011-07-23 10:55:15 +00002727 StringRef Name = VD->getName();
Devang Patel0317ab02010-08-10 18:27:15 +00002728 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
Devang Patel6237cea2010-08-23 22:07:25 +00002729 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
Benjamin Kramer527e6162012-06-20 18:11:18 +00002730 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
2731 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
2732 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
Devang Patel6237cea2010-08-23 22:07:25 +00002733 }
Devang Patel0317ab02010-08-10 18:27:15 +00002734 // Do not use DIGlobalVariable for enums.
2735 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
2736 return;
Devang Patel16674e82011-02-22 18:56:36 +00002737 DBuilder.createStaticVariable(Unit, Name, Name, Unit,
Devang Patel823d8e92010-12-08 22:42:58 +00002738 getLineNumber(VD->getLocation()),
2739 Ty, true, Init);
Devang Patel8d308382010-08-10 07:24:25 +00002740}
2741
Devang Patelabb485f2010-02-01 19:16:32 +00002742/// getOrCreateNamesSpace - Return namespace descriptor for the given
2743/// namespace decl.
2744llvm::DINameSpace
Devang Patel170cef32010-12-09 00:33:05 +00002745CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
Devang Patelabb485f2010-02-01 19:16:32 +00002746 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
2747 NameSpaceCache.find(NSDecl);
2748 if (I != NameSpaceCache.end())
2749 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
2750
Devang Patel8ab870d2010-05-12 23:46:38 +00002751 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patel8c376682010-10-28 19:12:46 +00002752 llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation());
Devang Patelabb485f2010-02-01 19:16:32 +00002753 llvm::DIDescriptor Context =
Devang Patel170cef32010-12-09 00:33:05 +00002754 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()));
Devang Patelabb485f2010-02-01 19:16:32 +00002755 llvm::DINameSpace NS =
Devang Patel16674e82011-02-22 18:56:36 +00002756 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Devang Patelab699792010-05-07 18:12:35 +00002757 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00002758 return NS;
2759}
Eric Christopher7ff0c5d2012-02-18 00:50:17 +00002760
2761void CGDebugInfo::finalize(void) {
2762 for (std::vector<std::pair<void *, llvm::WeakVH> >::const_iterator VI
2763 = ReplaceMap.begin(), VE = ReplaceMap.end(); VI != VE; ++VI) {
2764 llvm::DIType Ty, RepTy;
2765 // Verify that the debug info still exists.
Richard Smithe7259aa2012-08-17 04:17:54 +00002766 if (llvm::Value *V = VI->second)
2767 Ty = llvm::DIType(cast<llvm::MDNode>(V));
Eric Christopher7ff0c5d2012-02-18 00:50:17 +00002768
2769 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
2770 TypeCache.find(VI->first);
2771 if (it != TypeCache.end()) {
2772 // Verify that the debug info still exists.
Richard Smithe7259aa2012-08-17 04:17:54 +00002773 if (llvm::Value *V = it->second)
2774 RepTy = llvm::DIType(cast<llvm::MDNode>(V));
Eric Christopher7ff0c5d2012-02-18 00:50:17 +00002775 }
2776
Eric Christopher86211df2012-02-20 18:05:24 +00002777 if (Ty.Verify() && Ty.isForwardDecl() && RepTy.Verify()) {
Eric Christopher7ff0c5d2012-02-18 00:50:17 +00002778 Ty.replaceAllUsesWith(RepTy);
Eric Christopher86211df2012-02-20 18:05:24 +00002779 }
Eric Christopher7ff0c5d2012-02-18 00:50:17 +00002780 }
2781 DBuilder.finalize();
2782}