blob: 97639db887abca0ed5ae756b5baa9adb6ea8fd2c [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"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000017#include "clang/AST/ASTContext.h"
Devang Patel2ed8f002010-08-27 17:47:47 +000018#include "clang/AST/DeclFriend.h"
Devang Patel9ca36b62009-02-26 21:10:26 +000019#include "clang/AST/DeclObjC.h"
Devang Patel700a1cb2010-07-20 20:24:18 +000020#include "clang/AST/DeclTemplate.h"
Chris Lattner3cc5c402008-11-11 07:01:36 +000021#include "clang/AST/Expr.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000022#include "clang/AST/RecordLayout.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000023#include "clang/Basic/SourceManager.h"
24#include "clang/Basic/FileManager.h"
Mike Stump5a862172009-09-15 21:48:34 +000025#include "clang/Basic/Version.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000026#include "clang/Frontend/CodeGenOptions.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000027#include "llvm/Constants.h"
28#include "llvm/DerivedTypes.h"
29#include "llvm/Instructions.h"
30#include "llvm/Intrinsics.h"
31#include "llvm/Module.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000032#include "llvm/ADT/StringExtras.h"
33#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000034#include "llvm/Support/Dwarf.h"
Devang Patel446c6192009-04-17 21:06:59 +000035#include "llvm/System/Path.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000036#include "llvm/Target/TargetMachine.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000037using namespace clang;
38using namespace clang::CodeGen;
39
Anders Carlsson20f12a22009-12-06 18:00:51 +000040CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Devang Patel17800552010-03-09 00:44:50 +000041 : CGM(CGM), DebugFactory(CGM.getModule()),
Dan Gohman4cac5b42010-08-20 22:02:57 +000042 BlockLiteralGenericSet(false) {
Devang Patel17800552010-03-09 00:44:50 +000043 CreateCompileUnit();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000044}
45
Chris Lattner9c85ba32008-11-10 06:08:34 +000046CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar66031a52008-10-17 16:15:48 +000047 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000048}
49
Chris Lattner9c85ba32008-11-10 06:08:34 +000050void CGDebugInfo::setLocation(SourceLocation Loc) {
51 if (Loc.isValid())
Anders Carlsson20f12a22009-12-06 18:00:51 +000052 CurLoc = CGM.getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000053}
54
Devang Patel33583052010-01-28 23:15:27 +000055/// getContextDescriptor - Get context info for the decl.
Devang Pateleb6d79b2010-02-01 21:34:11 +000056llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context,
Devang Patel33583052010-01-28 23:15:27 +000057 llvm::DIDescriptor &CompileUnit) {
Devang Pateleb6d79b2010-02-01 21:34:11 +000058 if (!Context)
59 return CompileUnit;
60
61 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
62 I = RegionMap.find(Context);
63 if (I != RegionMap.end())
Gabor Greif38c9b172010-09-18 13:00:17 +000064 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(&*I->second));
Devang Patel411894b2010-02-01 22:40:08 +000065
Devang Pateleb6d79b2010-02-01 21:34:11 +000066 // Check namespace.
67 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
68 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl, CompileUnit));
Devang Patel8b90a782010-05-13 23:52:37 +000069
70 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context)) {
71 if (!RDecl->isDependentType()) {
Devang Patela2e57692010-10-28 17:27:32 +000072 llvm::DIType Ty = getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Devang Patel8b90a782010-05-13 23:52:37 +000073 llvm::DIFile(CompileUnit));
74 return llvm::DIDescriptor(Ty);
75 }
76 }
Devang Patel979ec2e2009-10-06 00:35:31 +000077 return CompileUnit;
78}
79
Devang Patel9c6c3a02010-01-14 00:36:21 +000080/// getFunctionName - Get function name for the given FunctionDecl. If the
81/// name is constructred on demand (e.g. C++ destructor) then the name
82/// is stored on the side.
83llvm::StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
84 assert (FD && "Invalid FunctionDecl!");
85 IdentifierInfo *FII = FD->getIdentifier();
86 if (FII)
87 return FII->getName();
88
89 // Otherwise construct human readable name for debug info.
90 std::string NS = FD->getNameAsString();
91
92 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +000093 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer1b627dc2010-01-23 18:16:07 +000094 memcpy(StrPtr, NS.data(), NS.length());
95 return llvm::StringRef(StrPtr, NS.length());
Devang Patel9c6c3a02010-01-14 00:36:21 +000096}
97
David Chisnall52044a22010-09-02 18:01:51 +000098llvm::StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
99 llvm::SmallString<256> MethodName;
100 llvm::raw_svector_ostream OS(MethodName);
101 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
102 const DeclContext *DC = OMD->getDeclContext();
Devang Patela2e57692010-10-28 17:27:32 +0000103 if (const ObjCImplementationDecl *OID =
104 dyn_cast<const ObjCImplementationDecl>(DC)) {
David Chisnall52044a22010-09-02 18:01:51 +0000105 OS << OID->getName();
Devang Patela2e57692010-10-28 17:27:32 +0000106 } else if (const ObjCInterfaceDecl *OID =
107 dyn_cast<const ObjCInterfaceDecl>(DC)) {
Fariborz Jahanian1a4c9372010-10-18 17:51:06 +0000108 OS << OID->getName();
Devang Patela2e57692010-10-28 17:27:32 +0000109 } else if (const ObjCCategoryImplDecl *OCD =
110 dyn_cast<const ObjCCategoryImplDecl>(DC)){
David Chisnall52044a22010-09-02 18:01:51 +0000111 OS << ((NamedDecl *)OCD)->getIdentifier()->getNameStart() << '(' <<
112 OCD->getIdentifier()->getNameStart() << ')';
113 }
114 OS << ' ' << OMD->getSelector().getAsString() << ']';
115
116 char *StrPtr = DebugInfoNames.Allocate<char>(OS.tell());
117 memcpy(StrPtr, MethodName.begin(), OS.tell());
118 return llvm::StringRef(StrPtr, OS.tell());
119}
120
Devang Patel700a1cb2010-07-20 20:24:18 +0000121/// getClassName - Get class name including template argument list.
122llvm::StringRef
123CGDebugInfo::getClassName(RecordDecl *RD) {
124 ClassTemplateSpecializationDecl *Spec
125 = dyn_cast<ClassTemplateSpecializationDecl>(RD);
126 if (!Spec)
127 return RD->getName();
128
129 const TemplateArgument *Args;
130 unsigned NumArgs;
131 std::string Buffer;
132 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
133 const TemplateSpecializationType *TST =
134 cast<TemplateSpecializationType>(TAW->getType());
135 Args = TST->getArgs();
136 NumArgs = TST->getNumArgs();
137 } else {
138 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
139 Args = TemplateArgs.getFlatArgumentList();
140 NumArgs = TemplateArgs.flat_size();
141 }
142 Buffer = RD->getIdentifier()->getNameStart();
143 PrintingPolicy Policy(CGM.getLangOptions());
144 Buffer += TemplateSpecializationType::PrintTemplateArgumentList(Args,
145 NumArgs,
146 Policy);
147
148 // Copy this name on the side and use its reference.
149 char *StrPtr = DebugInfoNames.Allocate<char>(Buffer.length());
150 memcpy(StrPtr, Buffer.data(), Buffer.length());
151 return llvm::StringRef(StrPtr, Buffer.length());
Devang Patel700a1cb2010-07-20 20:24:18 +0000152}
153
Devang Patel17800552010-03-09 00:44:50 +0000154/// getOrCreateFile - Get the file debug info descriptor for the input location.
155llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Ted Kremenek9c250392010-03-30 00:27:51 +0000156 if (!Loc.isValid())
Devang Patel17800552010-03-09 00:44:50 +0000157 // If Location is not valid then use main input file.
158 return DebugFactory.CreateFile(TheCU.getFilename(), TheCU.getDirectory(),
159 TheCU);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000160 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel17800552010-03-09 00:44:50 +0000161 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Ted Kremenek9c250392010-03-30 00:27:51 +0000162
163 // Cache the results.
164 const char *fname = PLoc.getFilename();
165 llvm::DenseMap<const char *, llvm::WeakVH>::iterator it =
166 DIFileCache.find(fname);
167
168 if (it != DIFileCache.end()) {
169 // Verify that the information still exists.
170 if (&*it->second)
171 return llvm::DIFile(cast<llvm::MDNode>(it->second));
172 }
173
Devang Patelac4d13c2010-07-27 15:17:16 +0000174 llvm::DIFile F = DebugFactory.CreateFile(PLoc.getFilename(),
175 getCurrentDirname(), TheCU);
Ted Kremenek9c250392010-03-30 00:27:51 +0000176
Devang Patelab699792010-05-07 18:12:35 +0000177 DIFileCache[fname] = F;
Ted Kremenek9c250392010-03-30 00:27:51 +0000178 return F;
179
Devang Patel17800552010-03-09 00:44:50 +0000180}
Devang Patel8ab870d2010-05-12 23:46:38 +0000181
182/// getLineNumber - Get line number for the location. If location is invalid
183/// then use current location.
184unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
185 assert (CurLoc.isValid() && "Invalid current location!");
186 SourceManager &SM = CGM.getContext().getSourceManager();
187 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
188 return PLoc.getLine();
189}
190
191/// getColumnNumber - Get column number for the location. If location is
192/// invalid then use current location.
193unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) {
194 assert (CurLoc.isValid() && "Invalid current location!");
195 SourceManager &SM = CGM.getContext().getSourceManager();
196 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
197 return PLoc.getColumn();
198}
199
Devang Patelac4d13c2010-07-27 15:17:16 +0000200llvm::StringRef CGDebugInfo::getCurrentDirname() {
201 if (!CWDName.empty())
202 return CWDName;
203 char *CompDirnamePtr = NULL;
204 llvm::sys::Path CWD = llvm::sys::Path::GetCurrentDirectory();
205 CompDirnamePtr = DebugInfoNames.Allocate<char>(CWD.size());
206 memcpy(CompDirnamePtr, CWD.c_str(), CWD.size());
207 return CWDName = llvm::StringRef(CompDirnamePtr, CWD.size());
208}
209
Devang Patel17800552010-03-09 00:44:50 +0000210/// CreateCompileUnit - Create new compile unit.
211void CGDebugInfo::CreateCompileUnit() {
212
213 // Get absolute path name.
Douglas Gregorac91b4c2010-03-18 23:46:43 +0000214 SourceManager &SM = CGM.getContext().getSourceManager();
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000215 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
216 if (MainFileName.empty())
Devang Patel22fe5852010-03-12 21:04:27 +0000217 MainFileName = "<unknown>";
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000218
Douglas Gregorf6728fc2010-03-22 21:28:29 +0000219 // The main file name provided via the "-main-file-name" option contains just
220 // the file name itself with no path information. This file name may have had
221 // a relative path, so we look into the actual file entry for the main
222 // file to determine the real absolute path for the file.
Devang Patel6e6bc392010-07-23 23:04:28 +0000223 std::string MainFileDir;
Devang Patelac4d13c2010-07-27 15:17:16 +0000224 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000225 MainFileDir = MainFile->getDir()->getName();
Devang Patelac4d13c2010-07-27 15:17:16 +0000226 if (MainFileDir != ".")
227 MainFileName = MainFileDir + "/" + MainFileName;
228 }
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000229
Devang Patelac4d13c2010-07-27 15:17:16 +0000230 // Save filename string.
231 char *FilenamePtr = DebugInfoNames.Allocate<char>(MainFileName.length());
232 memcpy(FilenamePtr, MainFileName.c_str(), MainFileName.length());
233 llvm::StringRef Filename(FilenamePtr, MainFileName.length());
234
Chris Lattner515455a2009-03-25 03:28:08 +0000235 unsigned LangTag;
Devang Patel17800552010-03-09 00:44:50 +0000236 const LangOptions &LO = CGM.getLangOptions();
Chris Lattner515455a2009-03-25 03:28:08 +0000237 if (LO.CPlusPlus) {
238 if (LO.ObjC1)
239 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
240 else
241 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
242 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000243 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000244 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000245 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000246 } else {
247 LangTag = llvm::dwarf::DW_LANG_C89;
248 }
Devang Patel446c6192009-04-17 21:06:59 +0000249
Daniel Dunbar19f19832010-08-24 17:41:09 +0000250 std::string Producer = getClangFullVersion();
Chris Lattner4c2577a2009-05-02 01:00:04 +0000251
252 // Figure out which version of the ObjC runtime we have.
253 unsigned RuntimeVers = 0;
254 if (LO.ObjC1)
255 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000257 // Create new compile unit.
Devang Patel17800552010-03-09 00:44:50 +0000258 TheCU = DebugFactory.CreateCompileUnit(
Devang Patel58115002010-07-27 20:49:59 +0000259 LangTag, Filename, getCurrentDirname(),
Devang Patelac4d13c2010-07-27 15:17:16 +0000260 Producer, true,
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000261 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000262}
263
Devang Patel65e99f22009-02-25 01:36:11 +0000264/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000265/// one if necessary.
266llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel17800552010-03-09 00:44:50 +0000267 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000268 unsigned Encoding = 0;
Devang Patel05127ca2010-07-28 23:23:29 +0000269 const char *BTName = NULL;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000270 switch (BT->getKind()) {
271 default:
272 case BuiltinType::Void:
273 return llvm::DIType();
Devang Patelc8972c62010-07-28 01:33:15 +0000274 case BuiltinType::ObjCClass:
Devang Pateleaf5ee92010-07-21 22:41:25 +0000275 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Patela2e57692010-10-28 17:27:32 +0000276 Unit, "objc_class", Unit, 0, 0, 0,
277 0, llvm::DIDescriptor::FlagFwdDecl,
Devang Pateleaf5ee92010-07-21 22:41:25 +0000278 llvm::DIType(), llvm::DIArray());
Devang Patelc8972c62010-07-28 01:33:15 +0000279 case BuiltinType::ObjCId: {
280 // typedef struct objc_class *Class;
281 // typedef struct objc_object {
282 // Class isa;
283 // } *id;
284
285 llvm::DIType OCTy =
286 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
287 Unit, "objc_class", Unit, 0, 0, 0, 0,
Devang Patele2472482010-09-29 21:05:52 +0000288 llvm::DIDescriptor::FlagFwdDecl,
Devang Patelc8972c62010-07-28 01:33:15 +0000289 llvm::DIType(), llvm::DIArray());
290 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
291
292 llvm::DIType ISATy =
293 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
294 Unit, "", Unit,
295 0, Size, 0, 0, 0, OCTy);
296
297 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
298
299 llvm::DIType FieldTy =
300 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
301 "isa", Unit,
302 0,Size, 0, 0, 0, ISATy);
303 EltTys.push_back(FieldTy);
304 llvm::DIArray Elements =
305 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
306
307 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Patela2e57692010-10-28 17:27:32 +0000308 Unit, "objc_object", Unit, 0, 0, 0,
309 0, 0,
Devang Patelc8972c62010-07-28 01:33:15 +0000310 llvm::DIType(), Elements);
311 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000312 case BuiltinType::UChar:
313 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
314 case BuiltinType::Char_S:
315 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
316 case BuiltinType::UShort:
317 case BuiltinType::UInt:
318 case BuiltinType::ULong:
319 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
320 case BuiltinType::Short:
321 case BuiltinType::Int:
322 case BuiltinType::Long:
323 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
324 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
325 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000326 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000327 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000328 }
Devang Patel05127ca2010-07-28 23:23:29 +0000329
330 switch (BT->getKind()) {
331 case BuiltinType::Long: BTName = "long int"; break;
332 case BuiltinType::LongLong: BTName = "long long int"; break;
333 case BuiltinType::ULong: BTName = "long unsigned int"; break;
334 case BuiltinType::ULongLong: BTName = "long long unsigned int"; break;
335 default:
336 BTName = BT->getName(CGM.getContext().getLangOptions());
337 break;
338 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000339 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000340 uint64_t Size = CGM.getContext().getTypeSize(BT);
341 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000342 uint64_t Offset = 0;
Devang Patel05127ca2010-07-28 23:23:29 +0000343
Devang Patelca80a5f2009-10-20 19:55:01 +0000344 llvm::DIType DbgTy =
Devang Patel05127ca2010-07-28 23:23:29 +0000345 DebugFactory.CreateBasicType(Unit, BTName,
Devang Patelca80a5f2009-10-20 19:55:01 +0000346 Unit, 0, Size, Align,
347 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000348 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000349}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000350
Chris Lattnerb7003772009-04-23 06:13:01 +0000351llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000352 llvm::DIFile Unit) {
Chris Lattnerb7003772009-04-23 06:13:01 +0000353 // Bit size, align and offset of the type.
354 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
355 if (Ty->isComplexIntegerType())
356 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Anders Carlsson20f12a22009-12-06 18:00:51 +0000358 uint64_t Size = CGM.getContext().getTypeSize(Ty);
359 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattnerb7003772009-04-23 06:13:01 +0000360 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Devang Patelca80a5f2009-10-20 19:55:01 +0000362 llvm::DIType DbgTy =
363 DebugFactory.CreateBasicType(Unit, "complex",
364 Unit, 0, Size, Align,
365 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000366 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000367}
368
John McCalla1805292009-09-25 01:40:47 +0000369/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000370/// a new one if necessary.
Devang Patel17800552010-03-09 00:44:50 +0000371llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +0000372 QualifierCollector Qc;
373 const Type *T = Qc.strip(Ty);
374
375 // Ignore these qualifiers for now.
376 Qc.removeObjCGCAttr();
377 Qc.removeAddressSpace();
378
Chris Lattner9c85ba32008-11-10 06:08:34 +0000379 // We will create one Derived type for one qualifier and recurse to handle any
380 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000381 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000382 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000383 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000384 Qc.removeConst();
385 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000386 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000387 Qc.removeVolatile();
388 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000389 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000390 Qc.removeRestrict();
391 } else {
392 assert(Qc.empty() && "Unknown type qualifier for debug info");
393 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000394 }
Mike Stump1eb44332009-09-09 15:08:12 +0000395
John McCalla1805292009-09-25 01:40:47 +0000396 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
397
Daniel Dunbar3845f862008-10-31 03:54:29 +0000398 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
399 // CVR derived types.
Devang Patelca80a5f2009-10-20 19:55:01 +0000400 llvm::DIType DbgTy =
Devang Pateld58562e2010-03-09 22:49:11 +0000401 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000402 0, 0, 0, 0, 0, FromTy);
Devang Patelca80a5f2009-10-20 19:55:01 +0000403 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000404}
405
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000406llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000407 llvm::DIFile Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000408 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000409 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
410 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000411 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000412}
413
Chris Lattner9c85ba32008-11-10 06:08:34 +0000414llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000415 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000416 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
417 Ty->getPointeeType(), Unit);
418}
419
Devang Patelc69e1cf2010-09-30 19:05:55 +0000420/// CreatePointeeType - Create PointTee type. If Pointee is a record
421/// then emit record's fwd if debug info size reduction is enabled.
422llvm::DIType CGDebugInfo::CreatePointeeType(QualType PointeeTy,
423 llvm::DIFile Unit) {
424 if (!CGM.getCodeGenOpts().LimitDebugInfo)
425 return getOrCreateType(PointeeTy, Unit);
426
427 if (const RecordType *RTy = dyn_cast<RecordType>(PointeeTy)) {
428 RecordDecl *RD = RTy->getDecl();
429 unsigned RTag;
430 if (RD->isStruct())
431 RTag = llvm::dwarf::DW_TAG_structure_type;
432 else if (RD->isUnion())
433 RTag = llvm::dwarf::DW_TAG_union_type;
434 else {
435 assert(RD->isClass() && "Unknown RecordType!");
436 RTag = llvm::dwarf::DW_TAG_class_type;
437 }
438
439 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
440 unsigned Line = getLineNumber(RD->getLocation());
441 llvm::DIDescriptor FDContext =
442 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
443
444 return
445 DebugFactory.CreateCompositeType(RTag, FDContext, RD->getName(),
446 DefUnit, Line, 0, 0, 0,
447 llvm::DIType::FlagFwdDecl,
448 llvm::DIType(), llvm::DIArray());
449 }
450 return getOrCreateType(PointeeTy, Unit);
451
452}
453
Anders Carlssona031b352009-11-06 19:19:55 +0000454llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
455 const Type *Ty,
456 QualType PointeeTy,
Devang Patel17800552010-03-09 00:44:50 +0000457 llvm::DIFile Unit) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000458 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000459
460 // Size is always the size of a pointer. We can't use getTypeSize here
461 // because that does not return the correct value for references.
462 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000463 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
464 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000465
Devang Patelc69e1cf2010-09-30 19:05:55 +0000466 return DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
467 0, Size, Align, 0, 0,
468 CreatePointeeType(PointeeTy, Unit));
469
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000470}
471
Mike Stump9bc093c2009-05-14 02:03:51 +0000472llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000473 llvm::DIFile Unit) {
Mike Stump9bc093c2009-05-14 02:03:51 +0000474 if (BlockLiteralGenericSet)
475 return BlockLiteralGeneric;
476
Mike Stump9bc093c2009-05-14 02:03:51 +0000477 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
478
479 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
480
481 llvm::DIType FieldTy;
482
483 QualType FType;
484 uint64_t FieldSize, FieldOffset;
485 unsigned FieldAlign;
486
487 llvm::DIArray Elements;
488 llvm::DIType EltTy, DescTy;
489
490 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000491 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000492 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
493 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000494
Daniel Dunbarca308df2009-05-26 19:40:20 +0000495 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000496 EltTys.clear();
497
Devang Patele2472482010-09-29 21:05:52 +0000498 unsigned Flags = llvm::DIDescriptor::FlagAppleBlock;
Devang Patel8ab870d2010-05-12 23:46:38 +0000499 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump3d363c52009-10-02 02:30:50 +0000500
Mike Stump9bc093c2009-05-14 02:03:51 +0000501 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Devang Patel8ab870d2010-05-12 23:46:38 +0000502 Unit, LineNo, FieldOffset, 0, 0,
503 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Mike Stump9bc093c2009-05-14 02:03:51 +0000505 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000506 uint64_t Size = CGM.getContext().getTypeSize(Ty);
507 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Mike Stump9bc093c2009-05-14 02:03:51 +0000509 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000510 Unit, "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000511 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000512
513 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000514 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000515 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson20f12a22009-12-06 18:00:51 +0000516 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000517 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
518 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramerd3651cc2010-04-24 20:26:20 +0000519 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000520 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000521
Anders Carlsson20f12a22009-12-06 18:00:51 +0000522 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000523 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000524 FieldSize = CGM.getContext().getTypeSize(Ty);
525 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000526 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000527 "__descriptor", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000528 LineNo, FieldSize, FieldAlign,
Mike Stump9bc093c2009-05-14 02:03:51 +0000529 FieldOffset, 0, FieldTy);
530 EltTys.push_back(FieldTy);
531
532 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000533 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000534
535 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Devang Patel8ab870d2010-05-12 23:46:38 +0000536 Unit, LineNo, FieldOffset, 0, 0,
537 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Mike Stump9bc093c2009-05-14 02:03:51 +0000539 BlockLiteralGenericSet = true;
540 BlockLiteralGeneric
541 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000542 "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000543 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000544 return BlockLiteralGeneric;
545}
546
Chris Lattner9c85ba32008-11-10 06:08:34 +0000547llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000548 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000549 // Typedefs are derived from some other type. If we have a typedef of a
550 // typedef, make sure to emit the whole chain.
551 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Chris Lattner9c85ba32008-11-10 06:08:34 +0000553 // We don't set size information, but do specify where the typedef was
554 // declared.
Devang Patel8ab870d2010-05-12 23:46:38 +0000555 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000556
Devang Pateleb6d79b2010-02-01 21:34:11 +0000557 llvm::DIDescriptor TyContext
558 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
559 Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000560 llvm::DIType DbgTy =
Devang Pateld5289052010-01-29 22:29:31 +0000561 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Pateleb6d79b2010-02-01 21:34:11 +0000562 TyContext,
Devang Pateld5289052010-01-29 22:29:31 +0000563 Ty->getDecl()->getName(), Unit,
564 Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000565 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000566}
567
Chris Lattner9c85ba32008-11-10 06:08:34 +0000568llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000569 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000570 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000571
Chris Lattner9c85ba32008-11-10 06:08:34 +0000572 // Add the result type at least.
573 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000574
Chris Lattner9c85ba32008-11-10 06:08:34 +0000575 // Set up remainder of arguments if there is a prototype.
576 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Devang Patelaf164bb2010-10-06 20:51:45 +0000577 if (isa<FunctionNoProtoType>(Ty))
578 EltTys.push_back(DebugFactory.CreateUnspecifiedParameter());
579 else if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000580 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
581 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000582 }
583
Chris Lattner9c85ba32008-11-10 06:08:34 +0000584 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000585 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000586
Devang Patelca80a5f2009-10-20 19:55:01 +0000587 llvm::DIType DbgTy =
588 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000589 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000590 0, 0, 0, 0, 0,
591 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000592 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000593}
594
Devang Patel428deb52010-01-19 00:00:59 +0000595/// CollectRecordFields - A helper function to collect debug info for
596/// record fields. This is used while creating debug info entry for a Record.
597void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000598CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit,
Devang Patel428deb52010-01-19 00:00:59 +0000599 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
600 unsigned FieldNo = 0;
Devang Patel239cec62010-02-01 21:39:52 +0000601 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
602 for (RecordDecl::field_iterator I = RD->field_begin(),
603 E = RD->field_end();
Devang Patel428deb52010-01-19 00:00:59 +0000604 I != E; ++I, ++FieldNo) {
605 FieldDecl *Field = *I;
606 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Devang Patel428deb52010-01-19 00:00:59 +0000607 llvm::StringRef FieldName = Field->getName();
608
Devang Patel4835fdd2010-02-12 01:31:06 +0000609 // Ignore unnamed fields. Do not ignore unnamed records.
610 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel428deb52010-01-19 00:00:59 +0000611 continue;
612
613 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +0000614 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
615 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel428deb52010-01-19 00:00:59 +0000616 QualType FType = Field->getType();
617 uint64_t FieldSize = 0;
618 unsigned FieldAlign = 0;
619 if (!FType->isIncompleteArrayType()) {
620
621 // Bit size, align and offset of the type.
622 FieldSize = CGM.getContext().getTypeSize(FType);
623 Expr *BitWidth = Field->getBitWidth();
624 if (BitWidth)
625 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Devang Patel428deb52010-01-19 00:00:59 +0000626 FieldAlign = CGM.getContext().getTypeAlign(FType);
627 }
628
629 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
630
Devang Patel71f4ff62010-04-21 23:12:37 +0000631 unsigned Flags = 0;
632 AccessSpecifier Access = I->getAccess();
633 if (Access == clang::AS_private)
Devang Patele2472482010-09-29 21:05:52 +0000634 Flags |= llvm::DIDescriptor::FlagPrivate;
Devang Patel71f4ff62010-04-21 23:12:37 +0000635 else if (Access == clang::AS_protected)
Devang Patele2472482010-09-29 21:05:52 +0000636 Flags |= llvm::DIDescriptor::FlagProtected;
Devang Patel71f4ff62010-04-21 23:12:37 +0000637
Devang Patel428deb52010-01-19 00:00:59 +0000638 // Create a DW_TAG_member node to remember the offset of this field in the
639 // struct. FIXME: This is an absolutely insane way to capture this
640 // information. When we gut debug info, this should be fixed.
641 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
642 FieldName, FieldDefUnit,
643 FieldLine, FieldSize, FieldAlign,
Devang Patel71f4ff62010-04-21 23:12:37 +0000644 FieldOffset, Flags, FieldTy);
Devang Patel428deb52010-01-19 00:00:59 +0000645 EltTys.push_back(FieldTy);
646 }
647}
648
Devang Patela6da1922010-01-28 00:28:01 +0000649/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
650/// function type is not updated to include implicit "this" pointer. Use this
651/// routine to get a method type which includes "this" pointer.
652llvm::DIType
653CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000654 llvm::DIFile Unit) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +0000655 llvm::DIType FnTy
656 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
657 0),
658 Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000659
Devang Patela6da1922010-01-28 00:28:01 +0000660 // Add "this" pointer.
661
Devang Patelab699792010-05-07 18:12:35 +0000662 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patela6da1922010-01-28 00:28:01 +0000663 assert (Args.getNumElements() && "Invalid number of arguments!");
664
665 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
666
667 // First element is always return type. For 'void' functions it is NULL.
668 Elts.push_back(Args.getElement(0));
669
Devang Patel2ed8f002010-08-27 17:47:47 +0000670 if (!Method->isStatic())
671 {
672 // "this" pointer is always first argument.
673 ASTContext &Context = CGM.getContext();
674 QualType ThisPtr =
675 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
676 llvm::DIType ThisPtrType =
677 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
Devang Patel769640e2010-07-13 00:24:30 +0000678
Devang Patel2ed8f002010-08-27 17:47:47 +0000679 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
680 Elts.push_back(ThisPtrType);
681 }
Devang Patela6da1922010-01-28 00:28:01 +0000682
683 // Copy rest of the arguments.
684 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
685 Elts.push_back(Args.getElement(i));
686
687 llvm::DIArray EltTypeArray =
688 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
689
690 return
691 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000692 Unit, "", Unit,
Devang Patela6da1922010-01-28 00:28:01 +0000693 0, 0, 0, 0, 0,
694 llvm::DIType(), EltTypeArray);
695}
696
Devang Patel58faf202010-10-22 17:11:50 +0000697/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
698/// inside a function.
699static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
700 if (const CXXRecordDecl *NRD =
701 dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
702 return isFunctionLocalClass(NRD);
703 else if (isa<FunctionDecl>(RD->getDeclContext()))
704 return true;
705 return false;
706
707}
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000708/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
709/// a single member function GlobalDecl.
710llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000711CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000712 llvm::DIFile Unit,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000713 llvm::DIType RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000714 bool IsCtorOrDtor =
715 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
716
717 llvm::StringRef MethodName = getFunctionName(Method);
Devang Patela6da1922010-01-28 00:28:01 +0000718 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000719
720 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
721 // make sense to give a single ctor/dtor a linkage name.
Anders Carlsson9a20d552010-06-22 16:16:50 +0000722 llvm::StringRef MethodLinkageName;
Devang Patel58faf202010-10-22 17:11:50 +0000723 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
Anders Carlsson9a20d552010-06-22 16:16:50 +0000724 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000725
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000726 // Get the location for the method.
Devang Patel8ab870d2010-05-12 23:46:38 +0000727 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
728 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000729
730 // Collect virtual method info.
731 llvm::DIType ContainingType;
732 unsigned Virtuality = 0;
733 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000734
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000735 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000736 if (Method->isPure())
737 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
738 else
739 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
740
741 // It doesn't make sense to give a virtual destructor a vtable index,
742 // since a single destructor has two entries in the vtable.
743 if (!isa<CXXDestructorDecl>(Method))
Anders Carlsson046c2942010-04-17 20:15:18 +0000744 VIndex = CGM.getVTables().getMethodVTableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000745 ContainingType = RecordTy;
746 }
747
Devang Patele2472482010-09-29 21:05:52 +0000748 unsigned Flags = 0;
749 if (Method->isImplicit())
750 Flags |= llvm::DIDescriptor::FlagArtificial;
Devang Patel10a7a6a2010-09-29 21:46:16 +0000751 AccessSpecifier Access = Method->getAccess();
752 if (Access == clang::AS_private)
753 Flags |= llvm::DIDescriptor::FlagPrivate;
754 else if (Access == clang::AS_protected)
755 Flags |= llvm::DIDescriptor::FlagProtected;
Devang Pateld78a0192010-10-01 23:32:17 +0000756 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
757 if (CXXC->isExplicit())
758 Flags |= llvm::DIDescriptor::FlagExplicit;
759 } else if (const CXXConversionDecl *CXXC =
760 dyn_cast<CXXConversionDecl>(Method)) {
761 if (CXXC->isExplicit())
762 Flags |= llvm::DIDescriptor::FlagExplicit;
763 }
Devang Patel3951e712010-10-07 22:03:49 +0000764 if (Method->hasPrototype())
765 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Pateld78a0192010-10-01 23:32:17 +0000766
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000767 llvm::DISubprogram SP =
768 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
769 MethodLinkageName,
770 MethodDefUnit, MethodLine,
771 MethodTy, /*isLocalToUnit=*/false,
Devang Patel33133572010-10-22 18:31:12 +0000772 /* isDefinition=*/ false,
Devang Patel40894912010-07-15 22:57:00 +0000773 Virtuality, VIndex, ContainingType,
Devang Patele2472482010-09-29 21:05:52 +0000774 Flags,
Devang Patel15a3d7d2010-07-15 23:09:46 +0000775 CGM.getLangOptions().Optimize);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000776
777 // Don't cache ctors or dtors since we have to emit multiple functions for
778 // a single ctor or dtor.
779 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
Devang Patelab699792010-05-07 18:12:35 +0000780 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000781
782 return SP;
783}
784
Devang Patel4125fd22010-01-19 01:54:44 +0000785/// CollectCXXMemberFunctions - A helper function to collect debug info for
786/// C++ member functions.This is used while creating debug info entry for
787/// a Record.
788void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000789CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000790 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000791 llvm::DIType RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000792 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
793 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000794 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000795
Devang Pateld5322da2010-02-09 19:09:28 +0000796 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000797 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000798
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000799 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000800 }
801}
802
Devang Patel2ed8f002010-08-27 17:47:47 +0000803/// CollectCXXFriends - A helper function to collect debug info for
804/// C++ base classes. This is used while creating debug info entry for
805/// a Record.
806void CGDebugInfo::
807CollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit,
808 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
809 llvm::DIType RecordTy) {
810
811 for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(),
812 BE = RD->friend_end(); BI != BE; ++BI) {
813
814 TypeSourceInfo *TInfo = (*BI)->getFriendType();
815 if(TInfo)
816 {
817 llvm::DIType Ty = getOrCreateType(TInfo->getType(), Unit);
818
819 llvm::DIType DTy =
820 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_friend,
821 RecordTy, llvm::StringRef(),
822 Unit, 0, 0, 0,
823 0, 0, Ty);
824
825 EltTys.push_back(DTy);
826 }
827
828 }
829}
830
Devang Patela245c5b2010-01-25 23:32:18 +0000831/// CollectCXXBases - A helper function to collect debug info for
832/// C++ base classes. This is used while creating debug info entry for
833/// a Record.
834void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000835CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000836 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000837 llvm::DIType RecordTy) {
Devang Patela245c5b2010-01-25 23:32:18 +0000838
Devang Patel239cec62010-02-01 21:39:52 +0000839 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
840 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
841 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000842 unsigned BFlags = 0;
843 uint64_t BaseOffset;
844
845 const CXXRecordDecl *Base =
846 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
847
848 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000849 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000850 // expression where it expects +ve number.
Anders Carlssonaf440352010-03-23 04:11:45 +0000851 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base);
Devang Patele2472482010-09-29 21:05:52 +0000852 BFlags = llvm::DIDescriptor::FlagVirtual;
Devang Patelca7daed2010-01-28 21:54:15 +0000853 } else
854 BaseOffset = RL.getBaseClassOffset(Base);
855
856 AccessSpecifier Access = BI->getAccessSpecifier();
857 if (Access == clang::AS_private)
Devang Patele2472482010-09-29 21:05:52 +0000858 BFlags |= llvm::DIDescriptor::FlagPrivate;
Devang Patelca7daed2010-01-28 21:54:15 +0000859 else if (Access == clang::AS_protected)
Devang Patele2472482010-09-29 21:05:52 +0000860 BFlags |= llvm::DIDescriptor::FlagProtected;
Devang Patelca7daed2010-01-28 21:54:15 +0000861
862 llvm::DIType DTy =
863 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
864 RecordTy, llvm::StringRef(),
Devang Pateld58562e2010-03-09 22:49:11 +0000865 Unit, 0, 0, 0,
Devang Patelca7daed2010-01-28 21:54:15 +0000866 BaseOffset, BFlags,
867 getOrCreateType(BI->getType(),
868 Unit));
869 EltTys.push_back(DTy);
870 }
Devang Patela245c5b2010-01-25 23:32:18 +0000871}
872
Devang Patel4ce3f202010-01-28 18:11:52 +0000873/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000874llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000875 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000876 return VTablePtrType;
877
878 ASTContext &Context = CGM.getContext();
879
880 /* Function type */
Benjamin Kramerad468862010-03-30 11:36:44 +0000881 llvm::DIDescriptor STy = getOrCreateType(Context.IntTy, Unit);
882 llvm::DIArray SElements = DebugFactory.GetOrCreateArray(&STy, 1);
Devang Patel4ce3f202010-01-28 18:11:52 +0000883 llvm::DIType SubTy =
884 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000885 Unit, "", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000886 0, 0, 0, 0, 0, llvm::DIType(), SElements);
887
888 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
889 llvm::DIType vtbl_ptr_type
890 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000891 Unit, "__vtbl_ptr_type", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000892 0, Size, 0, 0, 0, SubTy);
893
Devang Pateld58562e2010-03-09 22:49:11 +0000894 VTablePtrType =
895 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
896 Unit, "", Unit,
897 0, Size, 0, 0, 0, vtbl_ptr_type);
Devang Patel4ce3f202010-01-28 18:11:52 +0000898 return VTablePtrType;
899}
900
Anders Carlsson046c2942010-04-17 20:15:18 +0000901/// getVTableName - Get vtable name for the given Class.
902llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000903 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000904 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000905
906 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000907 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000908 memcpy(StrPtr, Name.data(), Name.length());
909 return llvm::StringRef(StrPtr, Name.length());
910}
911
912
Anders Carlsson046c2942010-04-17 20:15:18 +0000913/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel4ce3f202010-01-28 18:11:52 +0000914/// debug info entry in EltTys vector.
915void CGDebugInfo::
Anders Carlsson046c2942010-04-17 20:15:18 +0000916CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000917 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000918 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000919
920 // If there is a primary base then it will hold vtable info.
921 if (RL.getPrimaryBase())
922 return;
923
924 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000925 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000926 return;
927
928 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
929 llvm::DIType VPTR
930 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Anders Carlsson046c2942010-04-17 20:15:18 +0000931 getVTableName(RD), Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000932 0, Size, 0, 0, 0,
933 getOrCreateVTablePtrType(Unit));
934 EltTys.push_back(VPTR);
935}
936
Devang Patelc69e1cf2010-09-30 19:05:55 +0000937/// getOrCreateRecordType - Emit record type's standalone debug info.
938llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
939 SourceLocation Loc) {
940 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
941 DebugFactory.RecordType(T);
942 return T;
943}
944
Devang Patel65e99f22009-02-25 01:36:11 +0000945/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000946llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000947 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000948 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000949
Chris Lattner9c85ba32008-11-10 06:08:34 +0000950 unsigned Tag;
Devang Pateld6c5a262010-02-01 21:52:22 +0000951 if (RD->isStruct())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000952 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Pateld6c5a262010-02-01 21:52:22 +0000953 else if (RD->isUnion())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000954 Tag = llvm::dwarf::DW_TAG_union_type;
955 else {
Devang Pateld6c5a262010-02-01 21:52:22 +0000956 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000957 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000958 }
959
Chris Lattner9c85ba32008-11-10 06:08:34 +0000960 // Get overall information about the record type for the debug info.
Devang Patel8ab870d2010-05-12 23:46:38 +0000961 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
962 unsigned Line = getLineNumber(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000963
Chris Lattner9c85ba32008-11-10 06:08:34 +0000964 // Records and classes and unions can all be recursive. To handle them, we
965 // first generate a debug descriptor for the struct as a forward declaration.
966 // Then (if it is a definition) we go through and get debug info for all of
967 // its members. Finally, we create a descriptor for the complete type (which
968 // may refer to the forward decl if the struct is recursive) and replace all
969 // uses of the forward declaration with the final definition.
Devang Patel0b897992010-07-08 19:56:29 +0000970 llvm::DIDescriptor FDContext =
971 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
972
973 // If this is just a forward declaration, construct an appropriately
974 // marked node and just return it.
975 if (!RD->getDefinition()) {
976 llvm::DICompositeType FwdDecl =
977 DebugFactory.CreateCompositeType(Tag, FDContext, RD->getName(),
978 DefUnit, Line, 0, 0, 0,
Devang Patele2472482010-09-29 21:05:52 +0000979 llvm::DIDescriptor::FlagFwdDecl,
Devang Patel0b897992010-07-08 19:56:29 +0000980 llvm::DIType(), llvm::DIArray());
981
982 return FwdDecl;
983 }
Devang Pateld0f251b2010-01-20 23:56:40 +0000984
Dan Gohman86968372010-08-20 22:39:57 +0000985 llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType();
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Devang Patelab699792010-05-07 18:12:35 +0000987 llvm::MDNode *MN = FwdDecl;
988 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000989 // Otherwise, insert it into the TypeCache so that recursive uses will find
990 // it.
Devang Patelab699792010-05-07 18:12:35 +0000991 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000992 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000993 RegionStack.push_back(FwdDeclNode);
994 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000995
996 // Convert all the elements.
997 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
998
Devang Pateld6c5a262010-02-01 21:52:22 +0000999 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +00001000 if (CXXDecl) {
1001 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson046c2942010-04-17 20:15:18 +00001002 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +00001003 }
Devang Pateldabc3e92010-08-12 00:02:44 +00001004
1005 // Collect static variables with initializers.
1006 for (RecordDecl::decl_iterator I = RD->decls_begin(), E = RD->decls_end();
1007 I != E; ++I)
1008 if (const VarDecl *V = dyn_cast<VarDecl>(*I)) {
1009 if (const Expr *Init = V->getInit()) {
1010 Expr::EvalResult Result;
1011 if (Init->Evaluate(Result, CGM.getContext()) && Result.Val.isInt()) {
1012 llvm::ConstantInt *CI
1013 = llvm::ConstantInt::get(CGM.getLLVMContext(), Result.Val.getInt());
1014
1015 // Create the descriptor for static variable.
1016 llvm::DIFile VUnit = getOrCreateFile(V->getLocation());
1017 llvm::StringRef VName = V->getName();
1018 llvm::DIType VTy = getOrCreateType(V->getType(), VUnit);
1019 // Do not use DIGlobalVariable for enums.
1020 if (VTy.getTag() != llvm::dwarf::DW_TAG_enumeration_type) {
1021 DebugFactory.CreateGlobalVariable(FwdDecl, VName, VName, VName, VUnit,
1022 getLineNumber(V->getLocation()),
1023 VTy, true, true, CI);
1024 }
1025 }
1026 }
1027 }
1028
Devang Pateld6c5a262010-02-01 21:52:22 +00001029 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +00001030 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +00001031 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +00001032 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel2ed8f002010-08-27 17:47:47 +00001033 CollectCXXFriends(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +00001034
1035 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +00001036 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel5bc794f2010-10-14 22:59:23 +00001037 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
1038 // Seek non virtual primary base root.
1039 while (1) {
1040 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
1041 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
1042 if (PBT && !BRL.getPrimaryBaseWasVirtual())
1043 PBase = PBT;
1044 else
1045 break;
1046 }
Devang Patel0ac8f312010-01-28 00:54:21 +00001047 ContainingType =
Devang Patelab699792010-05-07 18:12:35 +00001048 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patel5bc794f2010-10-14 22:59:23 +00001049 }
Devang Patel0ac8f312010-01-28 00:54:21 +00001050 else if (CXXDecl->isDynamicClass())
Devang Patelab699792010-05-07 18:12:35 +00001051 ContainingType = FwdDecl;
Devang Patela245c5b2010-01-25 23:32:18 +00001052 }
Mike Stump1eb44332009-09-09 15:08:12 +00001053
Chris Lattner9c85ba32008-11-10 06:08:34 +00001054 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001055 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001056
1057 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001058 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1059 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001060
Devang Patele4c1ea02010-03-11 20:01:48 +00001061 RegionStack.pop_back();
1062 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1063 RegionMap.find(Ty->getDecl());
1064 if (RI != RegionMap.end())
1065 RegionMap.erase(RI);
1066
Devang Patel411894b2010-02-01 22:40:08 +00001067 llvm::DIDescriptor RDContext =
1068 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel700a1cb2010-07-20 20:24:18 +00001069
1070 llvm::StringRef RDName = RD->getName();
1071 // If this is a class, include the template arguments also.
1072 if (Tag == llvm::dwarf::DW_TAG_class_type)
1073 RDName = getClassName(RD);
1074
Devang Patel0ce73f62009-07-22 18:57:00 +00001075 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +00001076 DebugFactory.CreateCompositeType(Tag, RDContext,
Devang Patel700a1cb2010-07-20 20:24:18 +00001077 RDName,
Devang Patelab71ff52009-11-12 00:51:46 +00001078 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +00001079 llvm::DIType(), Elements,
1080 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001081
1082 // Now that we have a real decl for the struct, replace anything using the
1083 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001084 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001085 RegionMap[RD] = llvm::WeakVH(RealDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001086 return RealDecl;
1087}
1088
John McCallc12c5bb2010-05-15 11:32:37 +00001089/// CreateType - get objective-c object type.
1090llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1091 llvm::DIFile Unit) {
1092 // Ignore protocols.
1093 return getOrCreateType(Ty->getBaseType(), Unit);
1094}
1095
Devang Patel9ca36b62009-02-26 21:10:26 +00001096/// CreateType - get objective-c interface type.
1097llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001098 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001099 ObjCInterfaceDecl *ID = Ty->getDecl();
Devang Patel9ca36b62009-02-26 21:10:26 +00001100 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Patel9ca36b62009-02-26 21:10:26 +00001101
1102 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +00001103 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001104 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel17800552010-03-09 00:44:50 +00001105 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +00001106
Dan Gohman45f7c782010-08-23 21:15:56 +00001107 // If this is just a forward declaration, return a special forward-declaration
1108 // debug type.
1109 if (ID->isForwardDecl()) {
1110 llvm::DICompositeType FwdDecl =
1111 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
1112 DefUnit, Line, 0, 0, 0, 0,
1113 llvm::DIType(), llvm::DIArray(),
1114 RuntimeLang);
1115 return FwdDecl;
1116 }
1117
Devang Patel9ca36b62009-02-26 21:10:26 +00001118 // To handle recursive interface, we
1119 // first generate a debug descriptor for the struct as a forward declaration.
1120 // Then (if it is a definition) we go through and get debug info for all of
1121 // its members. Finally, we create a descriptor for the complete type (which
1122 // may refer to the forward decl if the struct is recursive) and replace all
1123 // uses of the forward declaration with the final definition.
Dan Gohman86968372010-08-20 22:39:57 +00001124 llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType();
Mike Stump1eb44332009-09-09 15:08:12 +00001125
Devang Patelab699792010-05-07 18:12:35 +00001126 llvm::MDNode *MN = FwdDecl;
1127 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patel9ca36b62009-02-26 21:10:26 +00001128 // Otherwise, insert it into the TypeCache so that recursive uses will find
1129 // it.
Devang Patelab699792010-05-07 18:12:35 +00001130 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +00001131 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001132 RegionStack.push_back(FwdDeclNode);
1133 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001134
1135 // Convert all the elements.
1136 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
1137
Devang Pateld6c5a262010-02-01 21:52:22 +00001138 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +00001139 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +00001140 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001141 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001142 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +00001143 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Pateld58562e2010-03-09 22:49:11 +00001144 Unit, "", Unit, 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +00001145 0 /* offset */, 0, SClassTy);
1146 EltTys.push_back(InhTag);
1147 }
1148
Devang Pateld6c5a262010-02-01 21:52:22 +00001149 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001150
1151 unsigned FieldNo = 0;
Fariborz Jahanian97477392010-10-01 00:01:53 +00001152 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
Fariborz Jahanianfe8fdba2010-10-11 23:55:47 +00001153 Field = Field->getNextIvar(), ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001154 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1155
Devang Patel73621622009-11-25 17:37:31 +00001156 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001157
Devang Patelde135022009-04-27 22:40:36 +00001158 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +00001159 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +00001160 continue;
1161
Devang Patel9ca36b62009-02-26 21:10:26 +00001162 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +00001163 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1164 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel99c20eb2009-03-20 18:24:39 +00001165 QualType FType = Field->getType();
1166 uint64_t FieldSize = 0;
1167 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +00001168
Devang Patel99c20eb2009-03-20 18:24:39 +00001169 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001170
Devang Patel99c20eb2009-03-20 18:24:39 +00001171 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001172 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001173 Expr *BitWidth = Field->getBitWidth();
1174 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +00001175 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +00001176
Anders Carlsson20f12a22009-12-06 18:00:51 +00001177 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001178 }
1179
Mike Stump1eb44332009-09-09 15:08:12 +00001180 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
1181
Devang Patelc20482b2009-03-19 00:23:53 +00001182 unsigned Flags = 0;
1183 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Devang Patele2472482010-09-29 21:05:52 +00001184 Flags = llvm::DIDescriptor::FlagProtected;
Devang Patelc20482b2009-03-19 00:23:53 +00001185 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Devang Patele2472482010-09-29 21:05:52 +00001186 Flags = llvm::DIDescriptor::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +00001187
Devang Patel9ca36b62009-02-26 21:10:26 +00001188 // Create a DW_TAG_member node to remember the offset of this field in the
1189 // struct. FIXME: This is an absolutely insane way to capture this
1190 // information. When we gut debug info, this should be fixed.
1191 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1192 FieldName, FieldDefUnit,
1193 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +00001194 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +00001195 EltTys.push_back(FieldTy);
1196 }
Mike Stump1eb44332009-09-09 15:08:12 +00001197
Devang Patel9ca36b62009-02-26 21:10:26 +00001198 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +00001199 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +00001200
Devang Patele4c1ea02010-03-11 20:01:48 +00001201 RegionStack.pop_back();
1202 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1203 RegionMap.find(Ty->getDecl());
1204 if (RI != RegionMap.end())
1205 RegionMap.erase(RI);
1206
Devang Patel9ca36b62009-02-26 21:10:26 +00001207 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001208 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1209 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001210
Devang Patel6c1fddf2009-07-27 18:42:03 +00001211 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +00001212 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +00001213 Line, Size, Align, 0, 0, llvm::DIType(),
1214 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +00001215
1216 // Now that we have a real decl for the struct, replace anything using the
1217 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001218 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001219 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +00001220
Devang Patel9ca36b62009-02-26 21:10:26 +00001221 return RealDecl;
1222}
1223
Chris Lattner9c85ba32008-11-10 06:08:34 +00001224llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001225 llvm::DIFile Unit) {
Devang Patel6237cea2010-08-23 22:07:25 +00001226 return CreateEnumType(Ty->getDecl(), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001227
Chris Lattner9c85ba32008-11-10 06:08:34 +00001228}
1229
1230llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001231 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001232 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1233 return CreateType(RT, Unit);
1234 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1235 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001236
Chris Lattner9c85ba32008-11-10 06:08:34 +00001237 return llvm::DIType();
1238}
1239
Devang Patel70c23cd2010-02-23 22:59:39 +00001240llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Eli Friedmana7e68452010-08-22 01:00:03 +00001241 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001242 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1243 uint64_t NumElems = Ty->getNumElements();
1244 if (NumElems > 0)
1245 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001246
Benjamin Kramerad468862010-03-30 11:36:44 +00001247 llvm::DIDescriptor Subscript = DebugFactory.GetOrCreateSubrange(0, NumElems);
1248 llvm::DIArray SubscriptArray = DebugFactory.GetOrCreateArray(&Subscript, 1);
Devang Patel70c23cd2010-02-23 22:59:39 +00001249
1250 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1251 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1252
1253 return
1254 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001255 Unit, "", Unit,
Devang Patel70c23cd2010-02-23 22:59:39 +00001256 0, Size, Align, 0, 0,
Eli Friedmana7e68452010-08-22 01:00:03 +00001257 ElementTy, SubscriptArray);
Devang Patel70c23cd2010-02-23 22:59:39 +00001258}
1259
Chris Lattner9c85ba32008-11-10 06:08:34 +00001260llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001261 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001262 uint64_t Size;
1263 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001264
1265
Nuno Lopes010d5142009-01-28 00:35:17 +00001266 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001267 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001268 Size = 0;
1269 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001270 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001271 } else if (Ty->isIncompleteArrayType()) {
1272 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001273 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001274 } else {
1275 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001276 Size = CGM.getContext().getTypeSize(Ty);
1277 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001278 }
Mike Stump1eb44332009-09-09 15:08:12 +00001279
Chris Lattner9c85ba32008-11-10 06:08:34 +00001280 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1281 // interior arrays, do we care? Why aren't nested arrays represented the
1282 // obvious/recursive way?
1283 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1284 QualType EltTy(Ty, 0);
Devang Patelcdf523c2010-10-06 18:30:00 +00001285 if (Ty->isIncompleteArrayType())
Chris Lattner9c85ba32008-11-10 06:08:34 +00001286 EltTy = Ty->getElementType();
Devang Patelcdf523c2010-10-06 18:30:00 +00001287 else {
1288 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1289 uint64_t Upper = 0;
1290 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1291 if (CAT->getSize().getZExtValue())
1292 Upper = CAT->getSize().getZExtValue() - 1;
1293 // FIXME: Verify this is right for VLAs.
1294 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1295 EltTy = Ty->getElementType();
1296 }
Sanjiv Gupta507de852008-06-09 10:47:41 +00001297 }
Mike Stump1eb44332009-09-09 15:08:12 +00001298
Chris Lattner9c85ba32008-11-10 06:08:34 +00001299 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001300 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001301
Devang Patelca80a5f2009-10-20 19:55:01 +00001302 llvm::DIType DbgTy =
1303 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001304 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +00001305 0, Size, Align, 0, 0,
1306 getOrCreateType(EltTy, Unit),
1307 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001308 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001309}
1310
Anders Carlssona031b352009-11-06 19:19:55 +00001311llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001312 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001313 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1314 Ty, Ty->getPointeeType(), Unit);
1315}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001316
Anders Carlsson20f12a22009-12-06 18:00:51 +00001317llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001318 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001319 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1320 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1321
1322 if (!Ty->getPointeeType()->isFunctionType()) {
1323 // We have a data member pointer type.
1324 return PointerDiffDITy;
1325 }
1326
1327 // We have a member function pointer type. Treat it as a struct with two
1328 // ptrdiff_t members.
1329 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1330
1331 uint64_t FieldOffset = 0;
1332 llvm::DIDescriptor ElementTypes[2];
1333
1334 // FIXME: This should probably be a function type instead.
1335 ElementTypes[0] =
1336 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001337 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001338 Info.first, Info.second, FieldOffset, 0,
1339 PointerDiffDITy);
1340 FieldOffset += Info.first;
1341
1342 ElementTypes[1] =
1343 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001344 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001345 Info.first, Info.second, FieldOffset, 0,
1346 PointerDiffDITy);
1347
1348 llvm::DIArray Elements =
1349 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1350 llvm::array_lengthof(ElementTypes));
1351
1352 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1353 U, llvm::StringRef("test"),
Devang Pateld58562e2010-03-09 22:49:11 +00001354 U, 0, FieldOffset,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001355 0, 0, 0, llvm::DIType(), Elements);
1356}
1357
Devang Patel6237cea2010-08-23 22:07:25 +00001358/// CreateEnumType - get enumeration type.
1359llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED, llvm::DIFile Unit){
1360 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
1361
1362 // Create DIEnumerator elements for each enumerator.
1363 for (EnumDecl::enumerator_iterator
1364 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
1365 Enum != EnumEnd; ++Enum) {
1366 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
1367 Enum->getInitVal().getZExtValue()));
1368 }
1369
1370 // Return a CompositeType for the enum itself.
1371 llvm::DIArray EltArray =
1372 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
1373
1374 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1375 unsigned Line = getLineNumber(ED->getLocation());
1376 uint64_t Size = 0;
Devang Patelffc52e72010-08-24 18:14:06 +00001377 uint64_t Align = 0;
1378 if (!ED->getTypeForDecl()->isIncompleteType()) {
1379 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1380 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1381 }
Devang Patel4bc48872010-10-27 23:23:58 +00001382 llvm::DIDescriptor EnumContext =
1383 getContextDescriptor(dyn_cast<Decl>(ED->getDeclContext()), Unit);
Devang Patel6237cea2010-08-23 22:07:25 +00001384 llvm::DIType DbgTy =
1385 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Patel4bc48872010-10-27 23:23:58 +00001386 EnumContext, ED->getName(),
1387 DefUnit, Line, Size, Align, 0, 0,
Devang Patel6237cea2010-08-23 22:07:25 +00001388 llvm::DIType(), EltArray);
1389 return DbgTy;
1390}
1391
Douglas Gregor840943d2009-12-21 20:18:30 +00001392static QualType UnwrapTypeForDebugInfo(QualType T) {
1393 do {
1394 QualType LastT = T;
1395 switch (T->getTypeClass()) {
1396 default:
1397 return T;
1398 case Type::TemplateSpecialization:
1399 T = cast<TemplateSpecializationType>(T)->desugar();
1400 break;
1401 case Type::TypeOfExpr: {
1402 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1403 T = Ty->getUnderlyingExpr()->getType();
1404 break;
1405 }
1406 case Type::TypeOf:
1407 T = cast<TypeOfType>(T)->getUnderlyingType();
1408 break;
1409 case Type::Decltype:
1410 T = cast<DecltypeType>(T)->getUnderlyingType();
1411 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001412 case Type::Elaborated:
1413 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001414 break;
1415 case Type::SubstTemplateTypeParm:
1416 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1417 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001418 }
1419
1420 assert(T != LastT && "Type unwrapping failed to unwrap!");
1421 if (T == LastT)
1422 return T;
1423 } while (true);
1424
1425 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001426}
1427
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001428/// getOrCreateType - Get the type from the cache or create a new
1429/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001430llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001431 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001432 if (Ty.isNull())
1433 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001434
Douglas Gregor840943d2009-12-21 20:18:30 +00001435 // Unwrap the type as needed for debug information.
1436 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001437
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001438 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001439 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001440 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001441 if (it != TypeCache.end()) {
1442 // Verify that the debug info still exists.
1443 if (&*it->second)
1444 return llvm::DIType(cast<llvm::MDNode>(it->second));
1445 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001446
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001447 // Otherwise create the type.
1448 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001449
1450 // And update the type cache.
Devang Patelab699792010-05-07 18:12:35 +00001451 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001452 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001453}
1454
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001455/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001456llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001457 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001458 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001459 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001460 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001461
Douglas Gregor2101a822009-12-21 19:57:21 +00001462 const char *Diag = 0;
1463
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001464 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001465 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001466#define TYPE(Class, Base)
1467#define ABSTRACT_TYPE(Class, Base)
1468#define NON_CANONICAL_TYPE(Class, Base)
1469#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1470#include "clang/AST/TypeNodes.def"
1471 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001472
Anders Carlssonbfe69952009-11-06 18:24:04 +00001473 // FIXME: Handle these.
1474 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001475 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001476
1477 case Type::Vector:
1478 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001479 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001480 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCallc12c5bb2010-05-15 11:32:37 +00001481 case Type::ObjCObject:
1482 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001483 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001484 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1485 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1486 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1487 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001488 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001489 return CreateType(cast<BlockPointerType>(Ty), Unit);
1490 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001491 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001492 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001493 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001494 case Type::FunctionProto:
1495 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001496 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001497 case Type::ConstantArray:
1498 case Type::VariableArray:
1499 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001500 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001501
1502 case Type::LValueReference:
1503 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1504
Anders Carlsson20f12a22009-12-06 18:00:51 +00001505 case Type::MemberPointer:
1506 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001507
1508 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001509 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001510 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001511 case Type::TypeOfExpr:
1512 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001513 case Type::Decltype:
1514 llvm_unreachable("type should have been unwrapped!");
1515 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001516
1517 case Type::RValueReference:
1518 // FIXME: Implement!
1519 Diag = "rvalue references";
1520 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001521 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001522
1523 assert(Diag && "Fall through without a diagnostic?");
1524 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1525 "debug information for %0 is not yet supported");
1526 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1527 << Diag;
1528 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001529}
1530
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001531/// CreateMemberType - Create new member and increase Offset by FType's size.
1532llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
1533 llvm::StringRef Name,
1534 uint64_t *Offset) {
1535 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1536 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1537 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
1538 llvm::DIType Ty = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1539 Unit, Name, Unit, 0,
1540 FieldSize, FieldAlign,
1541 *Offset, 0, FieldTy);
1542 *Offset += FieldSize;
1543 return Ty;
1544}
1545
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001546/// EmitFunctionStart - Constructs the debug code for entering a function -
1547/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001548void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001549 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001550 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001551
Devang Patel9c6c3a02010-01-14 00:36:21 +00001552 llvm::StringRef Name;
Anders Carlsson9a20d552010-06-22 16:16:50 +00001553 llvm::StringRef LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001554
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001555 FnBeginRegionCount.push_back(RegionStack.size());
1556
Devang Patel9c6c3a02010-01-14 00:36:21 +00001557 const Decl *D = GD.getDecl();
Devang Patel3951e712010-10-07 22:03:49 +00001558 unsigned Flags = 0;
Devang Patel0692f832010-10-11 21:58:41 +00001559 llvm::DIFile Unit = getOrCreateFile(CurLoc);
1560 llvm::DIDescriptor FDContext(Unit);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001561 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001562 // If there is a DISubprogram for this function available then use it.
1563 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1564 FI = SPCache.find(FD);
1565 if (FI != SPCache.end()) {
Gabor Greif38c9b172010-09-18 13:00:17 +00001566 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(&*FI->second));
Devang Patelab699792010-05-07 18:12:35 +00001567 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1568 llvm::MDNode *SPN = SP;
1569 RegionStack.push_back(SPN);
1570 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00001571 return;
1572 }
1573 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001574 Name = getFunctionName(FD);
1575 // Use mangled name as linkage name for c/c++ functions.
Anders Carlsson9a20d552010-06-22 16:16:50 +00001576 LinkageName = CGM.getMangledName(GD);
Devang Patel58faf202010-10-22 17:11:50 +00001577 if (LinkageName == Name)
1578 LinkageName = llvm::StringRef();
Devang Patel3951e712010-10-07 22:03:49 +00001579 if (FD->hasPrototype())
1580 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel0692f832010-10-11 21:58:41 +00001581 if (const NamespaceDecl *NSDecl =
1582 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
1583 FDContext = getOrCreateNameSpace(NSDecl, Unit);
David Chisnall70b9b442010-09-02 17:16:32 +00001584 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
David Chisnall52044a22010-09-02 18:01:51 +00001585 Name = getObjCMethodName(OMD);
Devang Patel3951e712010-10-07 22:03:49 +00001586 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001587 } else {
Devang Patel58faf202010-10-22 17:11:50 +00001588 // Use llvm function name.
Devang Patel9c6c3a02010-01-14 00:36:21 +00001589 Name = Fn->getName();
Devang Patel3951e712010-10-07 22:03:49 +00001590 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001591 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001592 if (!Name.empty() && Name[0] == '\01')
1593 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001594
Devang Patel970c6182010-04-24 00:49:16 +00001595 // It is expected that CurLoc is set before using EmitFunctionStart.
1596 // Usually, CurLoc points to the left bracket location of compound
1597 // statement representing function body.
Devang Patel8ab870d2010-05-12 23:46:38 +00001598 unsigned LineNo = getLineNumber(CurLoc);
Devang Patele2472482010-09-29 21:05:52 +00001599 if (D->isImplicit())
1600 Flags |= llvm::DIDescriptor::FlagArtificial;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001601 llvm::DISubprogram SP =
Devang Patela2e57692010-10-28 17:27:32 +00001602 DebugFactory.CreateSubprogram(FDContext, Name, Name, LinkageName, Unit,
1603 LineNo, getOrCreateType(FnType, Unit),
Devang Patel15a3d7d2010-07-15 23:09:46 +00001604 Fn->hasInternalLinkage(), true/*definition*/,
1605 0, 0, llvm::DIType(),
Devang Patele2472482010-09-29 21:05:52 +00001606 Flags,
Devang Patel15a3d7d2010-07-15 23:09:46 +00001607 CGM.getLangOptions().Optimize, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +00001608
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001609 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001610 llvm::MDNode *SPN = SP;
1611 RegionStack.push_back(SPN);
1612 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001613
1614 // Clear stack used to keep track of #line directives.
1615 LineDirectiveFiles.clear();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001616}
1617
1618
Devang Patel4d939e62010-07-20 22:20:10 +00001619void CGDebugInfo::EmitStopPoint(CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001620 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001621
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001622 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001623 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001624 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001625 || (SM.getInstantiationLineNumber(CurLoc) ==
1626 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001627 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patel4800ea62010-04-05 21:09:15 +00001628 // New Builder may not be in sync with CGDebugInfo.
1629 if (!Builder.getCurrentDebugLocation().isUnknown())
1630 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001631
1632 // Update last state.
1633 PrevLoc = CurLoc;
1634
Chris Lattnerc6034632010-04-01 06:31:43 +00001635 llvm::MDNode *Scope = RegionStack.back();
Devang Patel8ab870d2010-05-12 23:46:38 +00001636 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1637 getColumnNumber(CurLoc),
Chris Lattnere541d012010-04-02 20:21:43 +00001638 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001639}
1640
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001641/// UpdateLineDirectiveRegion - Update region stack only if #line directive
1642/// has introduced scope change.
1643void CGDebugInfo::UpdateLineDirectiveRegion(CGBuilderTy &Builder) {
1644 if (CurLoc.isInvalid() || CurLoc.isMacroID() ||
1645 PrevLoc.isInvalid() || PrevLoc.isMacroID())
1646 return;
1647 SourceManager &SM = CGM.getContext().getSourceManager();
1648 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
1649 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc);
1650
1651 if (!strcmp(PPLoc.getFilename(), PCLoc.getFilename()))
1652 return;
1653
1654 // If #line directive stack is empty then we are entering a new scope.
1655 if (LineDirectiveFiles.empty()) {
1656 EmitRegionStart(Builder);
1657 LineDirectiveFiles.push_back(PCLoc.getFilename());
1658 return;
1659 }
1660
1661 assert (RegionStack.size() >= LineDirectiveFiles.size()
1662 && "error handling #line regions!");
1663
1664 bool SeenThisFile = false;
Devang Patel424a5c62010-09-15 20:50:40 +00001665 // Chek if current file is already seen earlier.
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001666 for(std::vector<const char *>::iterator I = LineDirectiveFiles.begin(),
1667 E = LineDirectiveFiles.end(); I != E; ++I)
Devang Patel424a5c62010-09-15 20:50:40 +00001668 if (!strcmp(PCLoc.getFilename(), *I)) {
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001669 SeenThisFile = true;
1670 break;
1671 }
1672
1673 // If #line for this file is seen earlier then pop out #line regions.
1674 if (SeenThisFile) {
1675 while (!LineDirectiveFiles.empty()) {
1676 const char *LastFile = LineDirectiveFiles.back();
1677 RegionStack.pop_back();
1678 LineDirectiveFiles.pop_back();
1679 if (!strcmp(PPLoc.getFilename(), LastFile))
1680 break;
1681 }
1682 return;
1683 }
1684
1685 // .. otherwise insert new #line region.
1686 EmitRegionStart(Builder);
1687 LineDirectiveFiles.push_back(PCLoc.getFilename());
1688
1689 return;
1690}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001691/// EmitRegionStart- Constructs the debug code for entering a declarative
1692/// region - "llvm.dbg.region.start.".
Devang Patel4d939e62010-07-20 22:20:10 +00001693void CGDebugInfo::EmitRegionStart(CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001694 llvm::DIDescriptor D =
1695 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1696 llvm::DIDescriptor() :
Devang Pateld19429f2010-02-16 21:41:20 +00001697 llvm::DIDescriptor(RegionStack.back()),
Stuart Hastings257d1d32010-07-19 23:56:31 +00001698 getOrCreateFile(CurLoc),
Devang Patel8ab870d2010-05-12 23:46:38 +00001699 getLineNumber(CurLoc),
1700 getColumnNumber(CurLoc));
Devang Patelab699792010-05-07 18:12:35 +00001701 llvm::MDNode *DN = D;
1702 RegionStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001703}
1704
1705/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1706/// region - "llvm.dbg.region.end."
Devang Patel4d939e62010-07-20 22:20:10 +00001707void CGDebugInfo::EmitRegionEnd(CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001708 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1709
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001710 // Provide an region stop point.
Devang Patel4d939e62010-07-20 22:20:10 +00001711 EmitStopPoint(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001712
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001713 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001714}
1715
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001716/// EmitFunctionEnd - Constructs the debug code for exiting a function.
1717void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
1718 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1719 unsigned RCount = FnBeginRegionCount.back();
1720 assert(RCount <= RegionStack.size() && "Region stack mismatch");
1721
1722 // Pop all regions for this function.
1723 while (RegionStack.size() != RCount)
1724 EmitRegionEnd(Builder);
1725 FnBeginRegionCount.pop_back();
1726}
1727
Devang Patel809b9bb2010-02-10 18:49:08 +00001728// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1729// See BuildByRefType.
1730llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1731 uint64_t *XOffset) {
1732
1733 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1734
1735 QualType FType;
1736 uint64_t FieldSize, FieldOffset;
1737 unsigned FieldAlign;
1738
Devang Patel17800552010-03-09 00:44:50 +00001739 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001740 QualType Type = VD->getType();
1741
1742 FieldOffset = 0;
1743 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001744 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1745 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001746 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001747 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1748 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1749
Devang Patel809b9bb2010-02-10 18:49:08 +00001750 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1751 if (HasCopyAndDispose) {
1752 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001753 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1754 &FieldOffset));
1755 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1756 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001757 }
1758
1759 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1760 if (Align > CharUnits::fromQuantity(
1761 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1762 unsigned AlignedOffsetInBytes
1763 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1764 unsigned NumPaddingBytes
1765 = AlignedOffsetInBytes - FieldOffset/8;
1766
1767 if (NumPaddingBytes > 0) {
1768 llvm::APInt pad(32, NumPaddingBytes);
1769 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1770 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001771 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001772 }
1773 }
1774
1775 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001776 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00001777 FieldSize = CGM.getContext().getTypeSize(FType);
1778 FieldAlign = Align.getQuantity()*8;
1779
1780 *XOffset = FieldOffset;
1781 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001782 VD->getName(), Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001783 0, FieldSize, FieldAlign,
1784 FieldOffset, 0, FieldTy);
1785 EltTys.push_back(FieldTy);
1786 FieldOffset += FieldSize;
1787
1788 llvm::DIArray Elements =
1789 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1790
Devang Patele2472482010-09-29 21:05:52 +00001791 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Devang Patel809b9bb2010-02-10 18:49:08 +00001792
1793 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001794 Unit, "", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001795 0, FieldOffset, 0, 0, Flags,
1796 llvm::DIType(), Elements);
1797
1798}
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001799/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001800void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001801 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001802 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1803
Devang Patel17800552010-03-09 00:44:50 +00001804 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001805 llvm::DIType Ty;
1806 uint64_t XOffset = 0;
1807 if (VD->hasAttr<BlocksAttr>())
1808 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1809 else
1810 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001811
Devang Patelf4e54a22010-05-07 23:05:55 +00001812 // If there is not any debug info for type then do not emit debug info
1813 // for this variable.
1814 if (!Ty)
1815 return;
1816
Chris Lattner9c85ba32008-11-10 06:08:34 +00001817 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001818 unsigned Line = getLineNumber(VD->getLocation());
1819 unsigned Column = getColumnNumber(VD->getLocation());
Devang Patelaca745b2010-09-29 23:09:21 +00001820 unsigned Flags = 0;
1821 if (VD->isImplicit())
1822 Flags |= llvm::DIDescriptor::FlagArtificial;
Chris Lattnerc6034632010-04-01 06:31:43 +00001823 llvm::MDNode *Scope = RegionStack.back();
Devang Patelcebbedd2010-10-12 23:24:54 +00001824
1825 llvm::StringRef Name = VD->getName();
1826 if (!Name.empty()) {
1827 // Create the descriptor for the variable.
1828 llvm::DIVariable D =
1829 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(Scope),
1830 Name, Unit, Line, Ty,
1831 CGM.getLangOptions().Optimize, Flags);
1832
1833 // Insert an llvm.dbg.declare into the current block.
1834 llvm::Instruction *Call =
1835 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
1836
1837 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
1838 }
1839
1840 // If VD is an anonymous union then Storage represents value for
1841 // all union fields.
1842 if (const RecordType *RT = dyn_cast<RecordType>(VD->getType()))
1843 if (const RecordDecl *RD = dyn_cast<RecordDecl>(RT->getDecl()))
1844 if (RD->isUnion()) {
1845 for (RecordDecl::field_iterator I = RD->field_begin(),
1846 E = RD->field_end();
1847 I != E; ++I) {
1848 FieldDecl *Field = *I;
1849 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1850 llvm::StringRef FieldName = Field->getName();
1851
1852 // Ignore unnamed fields. Do not ignore unnamed records.
1853 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
1854 continue;
1855
1856 // Use VarDecl's Tag, Scope and Line number.
1857 llvm::DIVariable D =
1858 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(Scope),
1859 FieldName, Unit, Line, FieldTy,
1860 CGM.getLangOptions().Optimize, Flags);
1861
1862 // Insert an llvm.dbg.declare into the current block.
1863 llvm::Instruction *Call =
1864 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
1865
1866 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
1867 }
1868 }
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001869}
1870
Mike Stumpb1a6e682009-09-30 02:43:10 +00001871/// EmitDeclare - Emit local variable declaration debug info.
1872void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1873 llvm::Value *Storage, CGBuilderTy &Builder,
1874 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001875 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001876 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1877
Devang Patel2b594b92010-04-26 23:28:46 +00001878 if (Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001879 return;
1880
1881 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001882 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001883 llvm::DIType Ty;
1884 if (VD->hasAttr<BlocksAttr>())
1885 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1886 else
1887 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001888
1889 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001890 unsigned Line = getLineNumber(VD->getLocation());
1891 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001892
Devang Pateld6c5a262010-02-01 21:52:22 +00001893 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001894 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001895 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1896 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1897 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1898 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001899 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001900 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1901 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001902 // offset of __forwarding field
1903 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001904 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1905 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1906 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001907 // offset of x field
1908 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001909 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001910 }
1911
1912 // Create the descriptor for the variable.
1913 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001914 DebugFactory.CreateComplexVariable(Tag,
1915 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001916 VD->getName(), Unit, Line, Ty,
Benjamin Kramer3475cfe2010-09-21 15:59:59 +00001917 addr.data(), addr.size());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001918 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001919 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001920 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattnerd5b89022009-12-28 21:44:41 +00001921
Chris Lattnerc6034632010-04-01 06:31:43 +00001922 llvm::MDNode *Scope = RegionStack.back();
Devang Patelf8e10a52010-05-10 23:48:38 +00001923 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001924}
1925
Devang Pateld6c5a262010-02-01 21:52:22 +00001926void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001927 llvm::Value *Storage,
1928 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001929 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001930}
1931
Mike Stumpb1a6e682009-09-30 02:43:10 +00001932void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1933 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1934 CodeGenFunction *CGF) {
1935 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1936}
1937
Chris Lattner9c85ba32008-11-10 06:08:34 +00001938/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1939/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001940void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001941 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001942 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001943}
1944
1945
1946
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001947/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001948void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001949 const VarDecl *D) {
1950
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001951 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001952 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001953 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +00001954
Devang Pateleb6d79b2010-02-01 21:34:11 +00001955 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001956 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001957
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001958 // CodeGen turns int[] into int[1] so we'll do the same here.
1959 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001960
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001961 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001962 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001963
Anders Carlsson20f12a22009-12-06 18:00:51 +00001964 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001965 ArrayType::Normal, 0);
1966 }
Devang Patel5d822f02010-04-29 17:48:37 +00001967 llvm::StringRef DeclName = D->getName();
Devang Patel8b90a782010-05-13 23:52:37 +00001968 llvm::StringRef LinkageName;
Devang Patel0fd3d1f2010-05-14 16:55:25 +00001969 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()))
Devang Patel8b90a782010-05-13 23:52:37 +00001970 LinkageName = Var->getName();
Devang Patel58faf202010-10-22 17:11:50 +00001971 if (LinkageName == DeclName)
1972 LinkageName = llvm::StringRef();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001973 llvm::DIDescriptor DContext =
1974 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
Devang Patel8b90a782010-05-13 23:52:37 +00001975 DebugFactory.CreateGlobalVariable(DContext, DeclName, DeclName, LinkageName,
1976 Unit, LineNo, getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001977 Var->hasInternalLinkage(),
1978 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001979}
1980
Devang Patel9ca36b62009-02-26 21:10:26 +00001981/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001982void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001983 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001984 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001985 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001986 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patel9ca36b62009-02-26 21:10:26 +00001987
Devang Pateld6c5a262010-02-01 21:52:22 +00001988 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001989
Devang Pateld6c5a262010-02-01 21:52:22 +00001990 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001991 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001992
Devang Patel9ca36b62009-02-26 21:10:26 +00001993 // CodeGen turns int[] into int[1] so we'll do the same here.
1994 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001995
Devang Patel9ca36b62009-02-26 21:10:26 +00001996 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001997 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001998
Anders Carlsson20f12a22009-12-06 18:00:51 +00001999 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00002000 ArrayType::Normal, 0);
2001 }
2002
Devang Patelf6a39b72009-10-20 18:26:30 +00002003 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00002004 getOrCreateType(T, Unit),
2005 Var->hasInternalLinkage(),
2006 true/*definition*/, Var);
2007}
Devang Patelabb485f2010-02-01 19:16:32 +00002008
Devang Patel25c2c8f2010-08-10 17:53:33 +00002009/// EmitGlobalVariable - Emit global variable's debug info.
2010void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
John McCall189d6ef2010-10-09 01:34:31 +00002011 llvm::Constant *Init) {
Devang Patel8d308382010-08-10 07:24:25 +00002012 // Create the descriptor for the variable.
2013 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
2014 llvm::StringRef Name = VD->getName();
Devang Patel0317ab02010-08-10 18:27:15 +00002015 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
Devang Patel6237cea2010-08-23 22:07:25 +00002016 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
2017 if (const EnumDecl *ED = dyn_cast<EnumDecl>(ECD->getDeclContext()))
2018 Ty = CreateEnumType(ED, Unit);
2019 }
Devang Patel0317ab02010-08-10 18:27:15 +00002020 // Do not use DIGlobalVariable for enums.
2021 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
2022 return;
Devang Patel8d308382010-08-10 07:24:25 +00002023 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit,
2024 getLineNumber(VD->getLocation()),
Devang Patel4c4acc02010-08-10 20:16:57 +00002025 Ty, true, true, Init);
Devang Patel8d308382010-08-10 07:24:25 +00002026}
2027
Devang Patelabb485f2010-02-01 19:16:32 +00002028/// getOrCreateNamesSpace - Return namespace descriptor for the given
2029/// namespace decl.
2030llvm::DINameSpace
2031CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
2032 llvm::DIDescriptor Unit) {
2033 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
2034 NameSpaceCache.find(NSDecl);
2035 if (I != NameSpaceCache.end())
2036 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
2037
Devang Patel8ab870d2010-05-12 23:46:38 +00002038 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patel8c376682010-10-28 19:12:46 +00002039 llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation());
Devang Patelabb485f2010-02-01 19:16:32 +00002040 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00002041 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00002042 llvm::DINameSpace NS =
Devang Patel8c376682010-10-28 19:12:46 +00002043 DebugFactory.CreateNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Devang Patelab699792010-05-07 18:12:35 +00002044 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00002045 return NS;
2046}