blob: 6f905c09ba5d5a2a4b26cd1e815cd342152ce08c [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()) {
72 llvm::DIType Ty = getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
73 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();
103 if (const ObjCImplementationDecl *OID = dyn_cast<const ObjCImplementationDecl>(DC)) {
104 OS << OID->getName();
Fariborz Jahanian1a4c9372010-10-18 17:51:06 +0000105 } else if (const ObjCInterfaceDecl *OID = dyn_cast<const ObjCInterfaceDecl>(DC)) {
106 OS << OID->getName();
David Chisnall52044a22010-09-02 18:01:51 +0000107 } else if (const ObjCCategoryImplDecl *OCD = dyn_cast<const ObjCCategoryImplDecl>(DC)){
108 OS << ((NamedDecl *)OCD)->getIdentifier()->getNameStart() << '(' <<
109 OCD->getIdentifier()->getNameStart() << ')';
110 }
111 OS << ' ' << OMD->getSelector().getAsString() << ']';
112
113 char *StrPtr = DebugInfoNames.Allocate<char>(OS.tell());
114 memcpy(StrPtr, MethodName.begin(), OS.tell());
115 return llvm::StringRef(StrPtr, OS.tell());
116}
117
Devang Patel700a1cb2010-07-20 20:24:18 +0000118/// getClassName - Get class name including template argument list.
119llvm::StringRef
120CGDebugInfo::getClassName(RecordDecl *RD) {
121 ClassTemplateSpecializationDecl *Spec
122 = dyn_cast<ClassTemplateSpecializationDecl>(RD);
123 if (!Spec)
124 return RD->getName();
125
126 const TemplateArgument *Args;
127 unsigned NumArgs;
128 std::string Buffer;
129 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
130 const TemplateSpecializationType *TST =
131 cast<TemplateSpecializationType>(TAW->getType());
132 Args = TST->getArgs();
133 NumArgs = TST->getNumArgs();
134 } else {
135 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
136 Args = TemplateArgs.getFlatArgumentList();
137 NumArgs = TemplateArgs.flat_size();
138 }
139 Buffer = RD->getIdentifier()->getNameStart();
140 PrintingPolicy Policy(CGM.getLangOptions());
141 Buffer += TemplateSpecializationType::PrintTemplateArgumentList(Args,
142 NumArgs,
143 Policy);
144
145 // Copy this name on the side and use its reference.
146 char *StrPtr = DebugInfoNames.Allocate<char>(Buffer.length());
147 memcpy(StrPtr, Buffer.data(), Buffer.length());
148 return llvm::StringRef(StrPtr, Buffer.length());
Devang Patel700a1cb2010-07-20 20:24:18 +0000149}
150
Devang Patel17800552010-03-09 00:44:50 +0000151/// getOrCreateFile - Get the file debug info descriptor for the input location.
152llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Ted Kremenek9c250392010-03-30 00:27:51 +0000153 if (!Loc.isValid())
Devang Patel17800552010-03-09 00:44:50 +0000154 // If Location is not valid then use main input file.
155 return DebugFactory.CreateFile(TheCU.getFilename(), TheCU.getDirectory(),
156 TheCU);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000157 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel17800552010-03-09 00:44:50 +0000158 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Ted Kremenek9c250392010-03-30 00:27:51 +0000159
160 // Cache the results.
161 const char *fname = PLoc.getFilename();
162 llvm::DenseMap<const char *, llvm::WeakVH>::iterator it =
163 DIFileCache.find(fname);
164
165 if (it != DIFileCache.end()) {
166 // Verify that the information still exists.
167 if (&*it->second)
168 return llvm::DIFile(cast<llvm::MDNode>(it->second));
169 }
170
Devang Patelac4d13c2010-07-27 15:17:16 +0000171 llvm::DIFile F = DebugFactory.CreateFile(PLoc.getFilename(),
172 getCurrentDirname(), TheCU);
Ted Kremenek9c250392010-03-30 00:27:51 +0000173
Devang Patelab699792010-05-07 18:12:35 +0000174 DIFileCache[fname] = F;
Ted Kremenek9c250392010-03-30 00:27:51 +0000175 return F;
176
Devang Patel17800552010-03-09 00:44:50 +0000177}
Devang Patel8ab870d2010-05-12 23:46:38 +0000178
179/// getLineNumber - Get line number for the location. If location is invalid
180/// then use current location.
181unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
182 assert (CurLoc.isValid() && "Invalid current location!");
183 SourceManager &SM = CGM.getContext().getSourceManager();
184 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
185 return PLoc.getLine();
186}
187
188/// getColumnNumber - Get column number for the location. If location is
189/// invalid then use current location.
190unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) {
191 assert (CurLoc.isValid() && "Invalid current location!");
192 SourceManager &SM = CGM.getContext().getSourceManager();
193 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
194 return PLoc.getColumn();
195}
196
Devang Patelac4d13c2010-07-27 15:17:16 +0000197llvm::StringRef CGDebugInfo::getCurrentDirname() {
198 if (!CWDName.empty())
199 return CWDName;
200 char *CompDirnamePtr = NULL;
201 llvm::sys::Path CWD = llvm::sys::Path::GetCurrentDirectory();
202 CompDirnamePtr = DebugInfoNames.Allocate<char>(CWD.size());
203 memcpy(CompDirnamePtr, CWD.c_str(), CWD.size());
204 return CWDName = llvm::StringRef(CompDirnamePtr, CWD.size());
205}
206
Devang Patel17800552010-03-09 00:44:50 +0000207/// CreateCompileUnit - Create new compile unit.
208void CGDebugInfo::CreateCompileUnit() {
209
210 // Get absolute path name.
Douglas Gregorac91b4c2010-03-18 23:46:43 +0000211 SourceManager &SM = CGM.getContext().getSourceManager();
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000212 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
213 if (MainFileName.empty())
Devang Patel22fe5852010-03-12 21:04:27 +0000214 MainFileName = "<unknown>";
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000215
Douglas Gregorf6728fc2010-03-22 21:28:29 +0000216 // The main file name provided via the "-main-file-name" option contains just
217 // the file name itself with no path information. This file name may have had
218 // a relative path, so we look into the actual file entry for the main
219 // file to determine the real absolute path for the file.
Devang Patel6e6bc392010-07-23 23:04:28 +0000220 std::string MainFileDir;
Devang Patelac4d13c2010-07-27 15:17:16 +0000221 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000222 MainFileDir = MainFile->getDir()->getName();
Devang Patelac4d13c2010-07-27 15:17:16 +0000223 if (MainFileDir != ".")
224 MainFileName = MainFileDir + "/" + MainFileName;
225 }
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000226
Devang Patelac4d13c2010-07-27 15:17:16 +0000227 // Save filename string.
228 char *FilenamePtr = DebugInfoNames.Allocate<char>(MainFileName.length());
229 memcpy(FilenamePtr, MainFileName.c_str(), MainFileName.length());
230 llvm::StringRef Filename(FilenamePtr, MainFileName.length());
231
Chris Lattner515455a2009-03-25 03:28:08 +0000232 unsigned LangTag;
Devang Patel17800552010-03-09 00:44:50 +0000233 const LangOptions &LO = CGM.getLangOptions();
Chris Lattner515455a2009-03-25 03:28:08 +0000234 if (LO.CPlusPlus) {
235 if (LO.ObjC1)
236 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
237 else
238 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
239 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000240 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000241 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000242 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000243 } else {
244 LangTag = llvm::dwarf::DW_LANG_C89;
245 }
Devang Patel446c6192009-04-17 21:06:59 +0000246
Daniel Dunbar19f19832010-08-24 17:41:09 +0000247 std::string Producer = getClangFullVersion();
Chris Lattner4c2577a2009-05-02 01:00:04 +0000248
249 // Figure out which version of the ObjC runtime we have.
250 unsigned RuntimeVers = 0;
251 if (LO.ObjC1)
252 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000253
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000254 // Create new compile unit.
Devang Patel17800552010-03-09 00:44:50 +0000255 TheCU = DebugFactory.CreateCompileUnit(
Devang Patel58115002010-07-27 20:49:59 +0000256 LangTag, Filename, getCurrentDirname(),
Devang Patelac4d13c2010-07-27 15:17:16 +0000257 Producer, true,
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000258 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000259}
260
Devang Patel65e99f22009-02-25 01:36:11 +0000261/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000262/// one if necessary.
263llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel17800552010-03-09 00:44:50 +0000264 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000265 unsigned Encoding = 0;
Devang Patel05127ca2010-07-28 23:23:29 +0000266 const char *BTName = NULL;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000267 switch (BT->getKind()) {
268 default:
269 case BuiltinType::Void:
270 return llvm::DIType();
Devang Patelc8972c62010-07-28 01:33:15 +0000271 case BuiltinType::ObjCClass:
Devang Pateleaf5ee92010-07-21 22:41:25 +0000272 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Patelc8972c62010-07-28 01:33:15 +0000273 Unit, "objc_class", Unit, 0, 0, 0, 0,
Devang Patele2472482010-09-29 21:05:52 +0000274 llvm::DIDescriptor::FlagFwdDecl,
Devang Pateleaf5ee92010-07-21 22:41:25 +0000275 llvm::DIType(), llvm::DIArray());
Devang Patelc8972c62010-07-28 01:33:15 +0000276 case BuiltinType::ObjCId: {
277 // typedef struct objc_class *Class;
278 // typedef struct objc_object {
279 // Class isa;
280 // } *id;
281
282 llvm::DIType OCTy =
283 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
284 Unit, "objc_class", Unit, 0, 0, 0, 0,
Devang Patele2472482010-09-29 21:05:52 +0000285 llvm::DIDescriptor::FlagFwdDecl,
Devang Patelc8972c62010-07-28 01:33:15 +0000286 llvm::DIType(), llvm::DIArray());
287 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
288
289 llvm::DIType ISATy =
290 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
291 Unit, "", Unit,
292 0, Size, 0, 0, 0, OCTy);
293
294 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
295
296 llvm::DIType FieldTy =
297 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
298 "isa", Unit,
299 0,Size, 0, 0, 0, ISATy);
300 EltTys.push_back(FieldTy);
301 llvm::DIArray Elements =
302 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
303
304 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
305 Unit, "objc_object", Unit, 0, 0, 0, 0,
306 0,
307 llvm::DIType(), Elements);
308 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000309 case BuiltinType::UChar:
310 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
311 case BuiltinType::Char_S:
312 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
313 case BuiltinType::UShort:
314 case BuiltinType::UInt:
315 case BuiltinType::ULong:
316 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
317 case BuiltinType::Short:
318 case BuiltinType::Int:
319 case BuiltinType::Long:
320 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
321 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
322 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000323 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000324 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000325 }
Devang Patel05127ca2010-07-28 23:23:29 +0000326
327 switch (BT->getKind()) {
328 case BuiltinType::Long: BTName = "long int"; break;
329 case BuiltinType::LongLong: BTName = "long long int"; break;
330 case BuiltinType::ULong: BTName = "long unsigned int"; break;
331 case BuiltinType::ULongLong: BTName = "long long unsigned int"; break;
332 default:
333 BTName = BT->getName(CGM.getContext().getLangOptions());
334 break;
335 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000336 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000337 uint64_t Size = CGM.getContext().getTypeSize(BT);
338 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000339 uint64_t Offset = 0;
Devang Patel05127ca2010-07-28 23:23:29 +0000340
Devang Patelca80a5f2009-10-20 19:55:01 +0000341 llvm::DIType DbgTy =
Devang Patel05127ca2010-07-28 23:23:29 +0000342 DebugFactory.CreateBasicType(Unit, BTName,
Devang Patelca80a5f2009-10-20 19:55:01 +0000343 Unit, 0, Size, Align,
344 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000345 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000346}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000347
Chris Lattnerb7003772009-04-23 06:13:01 +0000348llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000349 llvm::DIFile Unit) {
Chris Lattnerb7003772009-04-23 06:13:01 +0000350 // Bit size, align and offset of the type.
351 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
352 if (Ty->isComplexIntegerType())
353 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Anders Carlsson20f12a22009-12-06 18:00:51 +0000355 uint64_t Size = CGM.getContext().getTypeSize(Ty);
356 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattnerb7003772009-04-23 06:13:01 +0000357 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Devang Patelca80a5f2009-10-20 19:55:01 +0000359 llvm::DIType DbgTy =
360 DebugFactory.CreateBasicType(Unit, "complex",
361 Unit, 0, Size, Align,
362 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000363 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000364}
365
John McCalla1805292009-09-25 01:40:47 +0000366/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000367/// a new one if necessary.
Devang Patel17800552010-03-09 00:44:50 +0000368llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +0000369 QualifierCollector Qc;
370 const Type *T = Qc.strip(Ty);
371
372 // Ignore these qualifiers for now.
373 Qc.removeObjCGCAttr();
374 Qc.removeAddressSpace();
375
Chris Lattner9c85ba32008-11-10 06:08:34 +0000376 // We will create one Derived type for one qualifier and recurse to handle any
377 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000378 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000379 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000380 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000381 Qc.removeConst();
382 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000383 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000384 Qc.removeVolatile();
385 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000386 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000387 Qc.removeRestrict();
388 } else {
389 assert(Qc.empty() && "Unknown type qualifier for debug info");
390 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000391 }
Mike Stump1eb44332009-09-09 15:08:12 +0000392
John McCalla1805292009-09-25 01:40:47 +0000393 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
394
Daniel Dunbar3845f862008-10-31 03:54:29 +0000395 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
396 // CVR derived types.
Devang Patelca80a5f2009-10-20 19:55:01 +0000397 llvm::DIType DbgTy =
Devang Pateld58562e2010-03-09 22:49:11 +0000398 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000399 0, 0, 0, 0, 0, FromTy);
Devang Patelca80a5f2009-10-20 19:55:01 +0000400 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000401}
402
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000403llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000404 llvm::DIFile Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000405 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000406 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
407 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000408 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000409}
410
Chris Lattner9c85ba32008-11-10 06:08:34 +0000411llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000412 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000413 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
414 Ty->getPointeeType(), Unit);
415}
416
Devang Patelc69e1cf2010-09-30 19:05:55 +0000417/// CreatePointeeType - Create PointTee type. If Pointee is a record
418/// then emit record's fwd if debug info size reduction is enabled.
419llvm::DIType CGDebugInfo::CreatePointeeType(QualType PointeeTy,
420 llvm::DIFile Unit) {
421 if (!CGM.getCodeGenOpts().LimitDebugInfo)
422 return getOrCreateType(PointeeTy, Unit);
423
424 if (const RecordType *RTy = dyn_cast<RecordType>(PointeeTy)) {
425 RecordDecl *RD = RTy->getDecl();
426 unsigned RTag;
427 if (RD->isStruct())
428 RTag = llvm::dwarf::DW_TAG_structure_type;
429 else if (RD->isUnion())
430 RTag = llvm::dwarf::DW_TAG_union_type;
431 else {
432 assert(RD->isClass() && "Unknown RecordType!");
433 RTag = llvm::dwarf::DW_TAG_class_type;
434 }
435
436 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
437 unsigned Line = getLineNumber(RD->getLocation());
438 llvm::DIDescriptor FDContext =
439 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
440
441 return
442 DebugFactory.CreateCompositeType(RTag, FDContext, RD->getName(),
443 DefUnit, Line, 0, 0, 0,
444 llvm::DIType::FlagFwdDecl,
445 llvm::DIType(), llvm::DIArray());
446 }
447 return getOrCreateType(PointeeTy, Unit);
448
449}
450
Anders Carlssona031b352009-11-06 19:19:55 +0000451llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
452 const Type *Ty,
453 QualType PointeeTy,
Devang Patel17800552010-03-09 00:44:50 +0000454 llvm::DIFile Unit) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000455 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000456
457 // Size is always the size of a pointer. We can't use getTypeSize here
458 // because that does not return the correct value for references.
459 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000460 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
461 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Devang Patelc69e1cf2010-09-30 19:05:55 +0000463 return DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
464 0, Size, Align, 0, 0,
465 CreatePointeeType(PointeeTy, Unit));
466
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000467}
468
Mike Stump9bc093c2009-05-14 02:03:51 +0000469llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000470 llvm::DIFile Unit) {
Mike Stump9bc093c2009-05-14 02:03:51 +0000471 if (BlockLiteralGenericSet)
472 return BlockLiteralGeneric;
473
Mike Stump9bc093c2009-05-14 02:03:51 +0000474 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
475
476 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
477
478 llvm::DIType FieldTy;
479
480 QualType FType;
481 uint64_t FieldSize, FieldOffset;
482 unsigned FieldAlign;
483
484 llvm::DIArray Elements;
485 llvm::DIType EltTy, DescTy;
486
487 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000488 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000489 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
490 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000491
Daniel Dunbarca308df2009-05-26 19:40:20 +0000492 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000493 EltTys.clear();
494
Devang Patele2472482010-09-29 21:05:52 +0000495 unsigned Flags = llvm::DIDescriptor::FlagAppleBlock;
Devang Patel8ab870d2010-05-12 23:46:38 +0000496 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump3d363c52009-10-02 02:30:50 +0000497
Mike Stump9bc093c2009-05-14 02:03:51 +0000498 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Devang Patel8ab870d2010-05-12 23:46:38 +0000499 Unit, LineNo, FieldOffset, 0, 0,
500 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Mike Stump9bc093c2009-05-14 02:03:51 +0000502 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000503 uint64_t Size = CGM.getContext().getTypeSize(Ty);
504 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Mike Stump9bc093c2009-05-14 02:03:51 +0000506 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000507 Unit, "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000508 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000509
510 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000511 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000512 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson20f12a22009-12-06 18:00:51 +0000513 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000514 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
515 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramerd3651cc2010-04-24 20:26:20 +0000516 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000517 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000518
Anders Carlsson20f12a22009-12-06 18:00:51 +0000519 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000520 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000521 FieldSize = CGM.getContext().getTypeSize(Ty);
522 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000523 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000524 "__descriptor", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000525 LineNo, FieldSize, FieldAlign,
Mike Stump9bc093c2009-05-14 02:03:51 +0000526 FieldOffset, 0, FieldTy);
527 EltTys.push_back(FieldTy);
528
529 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000530 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000531
532 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Devang Patel8ab870d2010-05-12 23:46:38 +0000533 Unit, LineNo, FieldOffset, 0, 0,
534 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Mike Stump9bc093c2009-05-14 02:03:51 +0000536 BlockLiteralGenericSet = true;
537 BlockLiteralGeneric
538 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000539 "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000540 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000541 return BlockLiteralGeneric;
542}
543
Chris Lattner9c85ba32008-11-10 06:08:34 +0000544llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000545 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000546 // Typedefs are derived from some other type. If we have a typedef of a
547 // typedef, make sure to emit the whole chain.
548 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Chris Lattner9c85ba32008-11-10 06:08:34 +0000550 // We don't set size information, but do specify where the typedef was
551 // declared.
Devang Patel8ab870d2010-05-12 23:46:38 +0000552 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000553
Devang Pateleb6d79b2010-02-01 21:34:11 +0000554 llvm::DIDescriptor TyContext
555 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
556 Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000557 llvm::DIType DbgTy =
Devang Pateld5289052010-01-29 22:29:31 +0000558 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Pateleb6d79b2010-02-01 21:34:11 +0000559 TyContext,
Devang Pateld5289052010-01-29 22:29:31 +0000560 Ty->getDecl()->getName(), Unit,
561 Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000562 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000563}
564
Chris Lattner9c85ba32008-11-10 06:08:34 +0000565llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000566 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000567 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000568
Chris Lattner9c85ba32008-11-10 06:08:34 +0000569 // Add the result type at least.
570 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000571
Chris Lattner9c85ba32008-11-10 06:08:34 +0000572 // Set up remainder of arguments if there is a prototype.
573 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Devang Patelaf164bb2010-10-06 20:51:45 +0000574 if (isa<FunctionNoProtoType>(Ty))
575 EltTys.push_back(DebugFactory.CreateUnspecifiedParameter());
576 else if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000577 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
578 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000579 }
580
Chris Lattner9c85ba32008-11-10 06:08:34 +0000581 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000582 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000583
Devang Patelca80a5f2009-10-20 19:55:01 +0000584 llvm::DIType DbgTy =
585 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000586 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000587 0, 0, 0, 0, 0,
588 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000589 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000590}
591
Devang Patel428deb52010-01-19 00:00:59 +0000592/// CollectRecordFields - A helper function to collect debug info for
593/// record fields. This is used while creating debug info entry for a Record.
594void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000595CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit,
Devang Patel428deb52010-01-19 00:00:59 +0000596 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
597 unsigned FieldNo = 0;
Devang Patel239cec62010-02-01 21:39:52 +0000598 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
599 for (RecordDecl::field_iterator I = RD->field_begin(),
600 E = RD->field_end();
Devang Patel428deb52010-01-19 00:00:59 +0000601 I != E; ++I, ++FieldNo) {
602 FieldDecl *Field = *I;
603 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Devang Patel428deb52010-01-19 00:00:59 +0000604 llvm::StringRef FieldName = Field->getName();
605
Devang Patel4835fdd2010-02-12 01:31:06 +0000606 // Ignore unnamed fields. Do not ignore unnamed records.
607 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel428deb52010-01-19 00:00:59 +0000608 continue;
609
610 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +0000611 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
612 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel428deb52010-01-19 00:00:59 +0000613 QualType FType = Field->getType();
614 uint64_t FieldSize = 0;
615 unsigned FieldAlign = 0;
616 if (!FType->isIncompleteArrayType()) {
617
618 // Bit size, align and offset of the type.
619 FieldSize = CGM.getContext().getTypeSize(FType);
620 Expr *BitWidth = Field->getBitWidth();
621 if (BitWidth)
622 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Devang Patel428deb52010-01-19 00:00:59 +0000623 FieldAlign = CGM.getContext().getTypeAlign(FType);
624 }
625
626 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
627
Devang Patel71f4ff62010-04-21 23:12:37 +0000628 unsigned Flags = 0;
629 AccessSpecifier Access = I->getAccess();
630 if (Access == clang::AS_private)
Devang Patele2472482010-09-29 21:05:52 +0000631 Flags |= llvm::DIDescriptor::FlagPrivate;
Devang Patel71f4ff62010-04-21 23:12:37 +0000632 else if (Access == clang::AS_protected)
Devang Patele2472482010-09-29 21:05:52 +0000633 Flags |= llvm::DIDescriptor::FlagProtected;
Devang Patel71f4ff62010-04-21 23:12:37 +0000634
Devang Patel428deb52010-01-19 00:00:59 +0000635 // Create a DW_TAG_member node to remember the offset of this field in the
636 // struct. FIXME: This is an absolutely insane way to capture this
637 // information. When we gut debug info, this should be fixed.
638 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
639 FieldName, FieldDefUnit,
640 FieldLine, FieldSize, FieldAlign,
Devang Patel71f4ff62010-04-21 23:12:37 +0000641 FieldOffset, Flags, FieldTy);
Devang Patel428deb52010-01-19 00:00:59 +0000642 EltTys.push_back(FieldTy);
643 }
644}
645
Devang Patela6da1922010-01-28 00:28:01 +0000646/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
647/// function type is not updated to include implicit "this" pointer. Use this
648/// routine to get a method type which includes "this" pointer.
649llvm::DIType
650CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000651 llvm::DIFile Unit) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +0000652 llvm::DIType FnTy
653 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
654 0),
655 Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000656
Devang Patela6da1922010-01-28 00:28:01 +0000657 // Add "this" pointer.
658
Devang Patelab699792010-05-07 18:12:35 +0000659 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patela6da1922010-01-28 00:28:01 +0000660 assert (Args.getNumElements() && "Invalid number of arguments!");
661
662 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
663
664 // First element is always return type. For 'void' functions it is NULL.
665 Elts.push_back(Args.getElement(0));
666
Devang Patel2ed8f002010-08-27 17:47:47 +0000667 if (!Method->isStatic())
668 {
669 // "this" pointer is always first argument.
670 ASTContext &Context = CGM.getContext();
671 QualType ThisPtr =
672 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
673 llvm::DIType ThisPtrType =
674 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
Devang Patel769640e2010-07-13 00:24:30 +0000675
Devang Patel2ed8f002010-08-27 17:47:47 +0000676 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
677 Elts.push_back(ThisPtrType);
678 }
Devang Patela6da1922010-01-28 00:28:01 +0000679
680 // Copy rest of the arguments.
681 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
682 Elts.push_back(Args.getElement(i));
683
684 llvm::DIArray EltTypeArray =
685 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
686
687 return
688 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000689 Unit, "", Unit,
Devang Patela6da1922010-01-28 00:28:01 +0000690 0, 0, 0, 0, 0,
691 llvm::DIType(), EltTypeArray);
692}
693
Devang Patel58faf202010-10-22 17:11:50 +0000694/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
695/// inside a function.
696static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
697 if (const CXXRecordDecl *NRD =
698 dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
699 return isFunctionLocalClass(NRD);
700 else if (isa<FunctionDecl>(RD->getDeclContext()))
701 return true;
702 return false;
703
704}
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000705/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
706/// a single member function GlobalDecl.
707llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000708CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000709 llvm::DIFile Unit,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000710 llvm::DIType RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000711 bool IsCtorOrDtor =
712 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
713
714 llvm::StringRef MethodName = getFunctionName(Method);
Devang Patela6da1922010-01-28 00:28:01 +0000715 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000716
717 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
718 // make sense to give a single ctor/dtor a linkage name.
Anders Carlsson9a20d552010-06-22 16:16:50 +0000719 llvm::StringRef MethodLinkageName;
Devang Patel58faf202010-10-22 17:11:50 +0000720 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
Anders Carlsson9a20d552010-06-22 16:16:50 +0000721 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000722
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000723 // Get the location for the method.
Devang Patel8ab870d2010-05-12 23:46:38 +0000724 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
725 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000726
727 // Collect virtual method info.
728 llvm::DIType ContainingType;
729 unsigned Virtuality = 0;
730 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000731
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000732 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000733 if (Method->isPure())
734 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
735 else
736 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
737
738 // It doesn't make sense to give a virtual destructor a vtable index,
739 // since a single destructor has two entries in the vtable.
740 if (!isa<CXXDestructorDecl>(Method))
Anders Carlsson046c2942010-04-17 20:15:18 +0000741 VIndex = CGM.getVTables().getMethodVTableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000742 ContainingType = RecordTy;
743 }
744
Devang Patele2472482010-09-29 21:05:52 +0000745 unsigned Flags = 0;
746 if (Method->isImplicit())
747 Flags |= llvm::DIDescriptor::FlagArtificial;
Devang Patel10a7a6a2010-09-29 21:46:16 +0000748 AccessSpecifier Access = Method->getAccess();
749 if (Access == clang::AS_private)
750 Flags |= llvm::DIDescriptor::FlagPrivate;
751 else if (Access == clang::AS_protected)
752 Flags |= llvm::DIDescriptor::FlagProtected;
Devang Pateld78a0192010-10-01 23:32:17 +0000753 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
754 if (CXXC->isExplicit())
755 Flags |= llvm::DIDescriptor::FlagExplicit;
756 } else if (const CXXConversionDecl *CXXC =
757 dyn_cast<CXXConversionDecl>(Method)) {
758 if (CXXC->isExplicit())
759 Flags |= llvm::DIDescriptor::FlagExplicit;
760 }
Devang Patel3951e712010-10-07 22:03:49 +0000761 if (Method->hasPrototype())
762 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Pateld78a0192010-10-01 23:32:17 +0000763
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000764 llvm::DISubprogram SP =
765 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
766 MethodLinkageName,
767 MethodDefUnit, MethodLine,
768 MethodTy, /*isLocalToUnit=*/false,
Devang Patel33133572010-10-22 18:31:12 +0000769 /* isDefinition=*/ false,
Devang Patel40894912010-07-15 22:57:00 +0000770 Virtuality, VIndex, ContainingType,
Devang Patele2472482010-09-29 21:05:52 +0000771 Flags,
Devang Patel15a3d7d2010-07-15 23:09:46 +0000772 CGM.getLangOptions().Optimize);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000773
774 // Don't cache ctors or dtors since we have to emit multiple functions for
775 // a single ctor or dtor.
776 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
Devang Patelab699792010-05-07 18:12:35 +0000777 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000778
779 return SP;
780}
781
Devang Patel4125fd22010-01-19 01:54:44 +0000782/// CollectCXXMemberFunctions - A helper function to collect debug info for
783/// C++ member functions.This is used while creating debug info entry for
784/// a Record.
785void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000786CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000787 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000788 llvm::DIType RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000789 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
790 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000791 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000792
Devang Pateld5322da2010-02-09 19:09:28 +0000793 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000794 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000795
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000796 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000797 }
798}
799
Devang Patel2ed8f002010-08-27 17:47:47 +0000800/// CollectCXXFriends - A helper function to collect debug info for
801/// C++ base classes. This is used while creating debug info entry for
802/// a Record.
803void CGDebugInfo::
804CollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit,
805 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
806 llvm::DIType RecordTy) {
807
808 for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(),
809 BE = RD->friend_end(); BI != BE; ++BI) {
810
811 TypeSourceInfo *TInfo = (*BI)->getFriendType();
812 if(TInfo)
813 {
814 llvm::DIType Ty = getOrCreateType(TInfo->getType(), Unit);
815
816 llvm::DIType DTy =
817 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_friend,
818 RecordTy, llvm::StringRef(),
819 Unit, 0, 0, 0,
820 0, 0, Ty);
821
822 EltTys.push_back(DTy);
823 }
824
825 }
826}
827
Devang Patela245c5b2010-01-25 23:32:18 +0000828/// CollectCXXBases - A helper function to collect debug info for
829/// C++ base classes. This is used while creating debug info entry for
830/// a Record.
831void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000832CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000833 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000834 llvm::DIType RecordTy) {
Devang Patela245c5b2010-01-25 23:32:18 +0000835
Devang Patel239cec62010-02-01 21:39:52 +0000836 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
837 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
838 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000839 unsigned BFlags = 0;
840 uint64_t BaseOffset;
841
842 const CXXRecordDecl *Base =
843 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
844
845 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000846 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000847 // expression where it expects +ve number.
Anders Carlssonaf440352010-03-23 04:11:45 +0000848 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base);
Devang Patele2472482010-09-29 21:05:52 +0000849 BFlags = llvm::DIDescriptor::FlagVirtual;
Devang Patelca7daed2010-01-28 21:54:15 +0000850 } else
851 BaseOffset = RL.getBaseClassOffset(Base);
852
853 AccessSpecifier Access = BI->getAccessSpecifier();
854 if (Access == clang::AS_private)
Devang Patele2472482010-09-29 21:05:52 +0000855 BFlags |= llvm::DIDescriptor::FlagPrivate;
Devang Patelca7daed2010-01-28 21:54:15 +0000856 else if (Access == clang::AS_protected)
Devang Patele2472482010-09-29 21:05:52 +0000857 BFlags |= llvm::DIDescriptor::FlagProtected;
Devang Patelca7daed2010-01-28 21:54:15 +0000858
859 llvm::DIType DTy =
860 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
861 RecordTy, llvm::StringRef(),
Devang Pateld58562e2010-03-09 22:49:11 +0000862 Unit, 0, 0, 0,
Devang Patelca7daed2010-01-28 21:54:15 +0000863 BaseOffset, BFlags,
864 getOrCreateType(BI->getType(),
865 Unit));
866 EltTys.push_back(DTy);
867 }
Devang Patela245c5b2010-01-25 23:32:18 +0000868}
869
Devang Patel4ce3f202010-01-28 18:11:52 +0000870/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000871llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000872 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000873 return VTablePtrType;
874
875 ASTContext &Context = CGM.getContext();
876
877 /* Function type */
Benjamin Kramerad468862010-03-30 11:36:44 +0000878 llvm::DIDescriptor STy = getOrCreateType(Context.IntTy, Unit);
879 llvm::DIArray SElements = DebugFactory.GetOrCreateArray(&STy, 1);
Devang Patel4ce3f202010-01-28 18:11:52 +0000880 llvm::DIType SubTy =
881 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000882 Unit, "", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000883 0, 0, 0, 0, 0, llvm::DIType(), SElements);
884
885 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
886 llvm::DIType vtbl_ptr_type
887 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000888 Unit, "__vtbl_ptr_type", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000889 0, Size, 0, 0, 0, SubTy);
890
Devang Pateld58562e2010-03-09 22:49:11 +0000891 VTablePtrType =
892 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
893 Unit, "", Unit,
894 0, Size, 0, 0, 0, vtbl_ptr_type);
Devang Patel4ce3f202010-01-28 18:11:52 +0000895 return VTablePtrType;
896}
897
Anders Carlsson046c2942010-04-17 20:15:18 +0000898/// getVTableName - Get vtable name for the given Class.
899llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000900 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000901 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000902
903 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000904 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000905 memcpy(StrPtr, Name.data(), Name.length());
906 return llvm::StringRef(StrPtr, Name.length());
907}
908
909
Anders Carlsson046c2942010-04-17 20:15:18 +0000910/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel4ce3f202010-01-28 18:11:52 +0000911/// debug info entry in EltTys vector.
912void CGDebugInfo::
Anders Carlsson046c2942010-04-17 20:15:18 +0000913CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000914 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000915 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000916
917 // If there is a primary base then it will hold vtable info.
918 if (RL.getPrimaryBase())
919 return;
920
921 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000922 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000923 return;
924
925 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
926 llvm::DIType VPTR
927 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Anders Carlsson046c2942010-04-17 20:15:18 +0000928 getVTableName(RD), Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000929 0, Size, 0, 0, 0,
930 getOrCreateVTablePtrType(Unit));
931 EltTys.push_back(VPTR);
932}
933
Devang Patelc69e1cf2010-09-30 19:05:55 +0000934/// getOrCreateRecordType - Emit record type's standalone debug info.
935llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
936 SourceLocation Loc) {
937 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
938 DebugFactory.RecordType(T);
939 return T;
940}
941
Devang Patel65e99f22009-02-25 01:36:11 +0000942/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000943llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000944 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000945 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000946
Chris Lattner9c85ba32008-11-10 06:08:34 +0000947 unsigned Tag;
Devang Pateld6c5a262010-02-01 21:52:22 +0000948 if (RD->isStruct())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000949 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Pateld6c5a262010-02-01 21:52:22 +0000950 else if (RD->isUnion())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000951 Tag = llvm::dwarf::DW_TAG_union_type;
952 else {
Devang Pateld6c5a262010-02-01 21:52:22 +0000953 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000954 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000955 }
956
Chris Lattner9c85ba32008-11-10 06:08:34 +0000957 // Get overall information about the record type for the debug info.
Devang Patel8ab870d2010-05-12 23:46:38 +0000958 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
959 unsigned Line = getLineNumber(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000960
Chris Lattner9c85ba32008-11-10 06:08:34 +0000961 // Records and classes and unions can all be recursive. To handle them, we
962 // first generate a debug descriptor for the struct as a forward declaration.
963 // Then (if it is a definition) we go through and get debug info for all of
964 // its members. Finally, we create a descriptor for the complete type (which
965 // may refer to the forward decl if the struct is recursive) and replace all
966 // uses of the forward declaration with the final definition.
Devang Patel0b897992010-07-08 19:56:29 +0000967 llvm::DIDescriptor FDContext =
968 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
969
970 // If this is just a forward declaration, construct an appropriately
971 // marked node and just return it.
972 if (!RD->getDefinition()) {
973 llvm::DICompositeType FwdDecl =
974 DebugFactory.CreateCompositeType(Tag, FDContext, RD->getName(),
975 DefUnit, Line, 0, 0, 0,
Devang Patele2472482010-09-29 21:05:52 +0000976 llvm::DIDescriptor::FlagFwdDecl,
Devang Patel0b897992010-07-08 19:56:29 +0000977 llvm::DIType(), llvm::DIArray());
978
979 return FwdDecl;
980 }
Devang Pateld0f251b2010-01-20 23:56:40 +0000981
Dan Gohman86968372010-08-20 22:39:57 +0000982 llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType();
Mike Stump1eb44332009-09-09 15:08:12 +0000983
Devang Patelab699792010-05-07 18:12:35 +0000984 llvm::MDNode *MN = FwdDecl;
985 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000986 // Otherwise, insert it into the TypeCache so that recursive uses will find
987 // it.
Devang Patelab699792010-05-07 18:12:35 +0000988 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000989 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000990 RegionStack.push_back(FwdDeclNode);
991 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000992
993 // Convert all the elements.
994 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
995
Devang Pateld6c5a262010-02-01 21:52:22 +0000996 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000997 if (CXXDecl) {
998 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson046c2942010-04-17 20:15:18 +0000999 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +00001000 }
Devang Pateldabc3e92010-08-12 00:02:44 +00001001
1002 // Collect static variables with initializers.
1003 for (RecordDecl::decl_iterator I = RD->decls_begin(), E = RD->decls_end();
1004 I != E; ++I)
1005 if (const VarDecl *V = dyn_cast<VarDecl>(*I)) {
1006 if (const Expr *Init = V->getInit()) {
1007 Expr::EvalResult Result;
1008 if (Init->Evaluate(Result, CGM.getContext()) && Result.Val.isInt()) {
1009 llvm::ConstantInt *CI
1010 = llvm::ConstantInt::get(CGM.getLLVMContext(), Result.Val.getInt());
1011
1012 // Create the descriptor for static variable.
1013 llvm::DIFile VUnit = getOrCreateFile(V->getLocation());
1014 llvm::StringRef VName = V->getName();
1015 llvm::DIType VTy = getOrCreateType(V->getType(), VUnit);
1016 // Do not use DIGlobalVariable for enums.
1017 if (VTy.getTag() != llvm::dwarf::DW_TAG_enumeration_type) {
1018 DebugFactory.CreateGlobalVariable(FwdDecl, VName, VName, VName, VUnit,
1019 getLineNumber(V->getLocation()),
1020 VTy, true, true, CI);
1021 }
1022 }
1023 }
1024 }
1025
Devang Pateld6c5a262010-02-01 21:52:22 +00001026 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +00001027 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +00001028 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +00001029 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel2ed8f002010-08-27 17:47:47 +00001030 CollectCXXFriends(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +00001031
1032 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +00001033 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel5bc794f2010-10-14 22:59:23 +00001034 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
1035 // Seek non virtual primary base root.
1036 while (1) {
1037 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
1038 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
1039 if (PBT && !BRL.getPrimaryBaseWasVirtual())
1040 PBase = PBT;
1041 else
1042 break;
1043 }
Devang Patel0ac8f312010-01-28 00:54:21 +00001044 ContainingType =
Devang Patelab699792010-05-07 18:12:35 +00001045 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patel5bc794f2010-10-14 22:59:23 +00001046 }
Devang Patel0ac8f312010-01-28 00:54:21 +00001047 else if (CXXDecl->isDynamicClass())
Devang Patelab699792010-05-07 18:12:35 +00001048 ContainingType = FwdDecl;
Devang Patela245c5b2010-01-25 23:32:18 +00001049 }
Mike Stump1eb44332009-09-09 15:08:12 +00001050
Chris Lattner9c85ba32008-11-10 06:08:34 +00001051 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001052 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001053
1054 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001055 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1056 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001057
Devang Patele4c1ea02010-03-11 20:01:48 +00001058 RegionStack.pop_back();
1059 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1060 RegionMap.find(Ty->getDecl());
1061 if (RI != RegionMap.end())
1062 RegionMap.erase(RI);
1063
Devang Patel411894b2010-02-01 22:40:08 +00001064 llvm::DIDescriptor RDContext =
1065 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel700a1cb2010-07-20 20:24:18 +00001066
1067 llvm::StringRef RDName = RD->getName();
1068 // If this is a class, include the template arguments also.
1069 if (Tag == llvm::dwarf::DW_TAG_class_type)
1070 RDName = getClassName(RD);
1071
Devang Patel0ce73f62009-07-22 18:57:00 +00001072 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +00001073 DebugFactory.CreateCompositeType(Tag, RDContext,
Devang Patel700a1cb2010-07-20 20:24:18 +00001074 RDName,
Devang Patelab71ff52009-11-12 00:51:46 +00001075 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +00001076 llvm::DIType(), Elements,
1077 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001078
1079 // Now that we have a real decl for the struct, replace anything using the
1080 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001081 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001082 RegionMap[RD] = llvm::WeakVH(RealDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001083 return RealDecl;
1084}
1085
John McCallc12c5bb2010-05-15 11:32:37 +00001086/// CreateType - get objective-c object type.
1087llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1088 llvm::DIFile Unit) {
1089 // Ignore protocols.
1090 return getOrCreateType(Ty->getBaseType(), Unit);
1091}
1092
Devang Patel9ca36b62009-02-26 21:10:26 +00001093/// CreateType - get objective-c interface type.
1094llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001095 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001096 ObjCInterfaceDecl *ID = Ty->getDecl();
Devang Patel9ca36b62009-02-26 21:10:26 +00001097 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Patel9ca36b62009-02-26 21:10:26 +00001098
1099 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +00001100 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001101 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel17800552010-03-09 00:44:50 +00001102 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +00001103
Dan Gohman45f7c782010-08-23 21:15:56 +00001104 // If this is just a forward declaration, return a special forward-declaration
1105 // debug type.
1106 if (ID->isForwardDecl()) {
1107 llvm::DICompositeType FwdDecl =
1108 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
1109 DefUnit, Line, 0, 0, 0, 0,
1110 llvm::DIType(), llvm::DIArray(),
1111 RuntimeLang);
1112 return FwdDecl;
1113 }
1114
Devang Patel9ca36b62009-02-26 21:10:26 +00001115 // To handle recursive interface, we
1116 // first generate a debug descriptor for the struct as a forward declaration.
1117 // Then (if it is a definition) we go through and get debug info for all of
1118 // its members. Finally, we create a descriptor for the complete type (which
1119 // may refer to the forward decl if the struct is recursive) and replace all
1120 // uses of the forward declaration with the final definition.
Dan Gohman86968372010-08-20 22:39:57 +00001121 llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType();
Mike Stump1eb44332009-09-09 15:08:12 +00001122
Devang Patelab699792010-05-07 18:12:35 +00001123 llvm::MDNode *MN = FwdDecl;
1124 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patel9ca36b62009-02-26 21:10:26 +00001125 // Otherwise, insert it into the TypeCache so that recursive uses will find
1126 // it.
Devang Patelab699792010-05-07 18:12:35 +00001127 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +00001128 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001129 RegionStack.push_back(FwdDeclNode);
1130 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001131
1132 // Convert all the elements.
1133 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
1134
Devang Pateld6c5a262010-02-01 21:52:22 +00001135 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +00001136 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +00001137 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001138 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001139 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +00001140 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Pateld58562e2010-03-09 22:49:11 +00001141 Unit, "", Unit, 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +00001142 0 /* offset */, 0, SClassTy);
1143 EltTys.push_back(InhTag);
1144 }
1145
Devang Pateld6c5a262010-02-01 21:52:22 +00001146 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001147
1148 unsigned FieldNo = 0;
Fariborz Jahanian97477392010-10-01 00:01:53 +00001149 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
Fariborz Jahanianfe8fdba2010-10-11 23:55:47 +00001150 Field = Field->getNextIvar(), ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001151 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1152
Devang Patel73621622009-11-25 17:37:31 +00001153 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001154
Devang Patelde135022009-04-27 22:40:36 +00001155 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +00001156 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +00001157 continue;
1158
Devang Patel9ca36b62009-02-26 21:10:26 +00001159 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +00001160 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1161 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel99c20eb2009-03-20 18:24:39 +00001162 QualType FType = Field->getType();
1163 uint64_t FieldSize = 0;
1164 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +00001165
Devang Patel99c20eb2009-03-20 18:24:39 +00001166 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001167
Devang Patel99c20eb2009-03-20 18:24:39 +00001168 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001169 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001170 Expr *BitWidth = Field->getBitWidth();
1171 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +00001172 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +00001173
Anders Carlsson20f12a22009-12-06 18:00:51 +00001174 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001175 }
1176
Mike Stump1eb44332009-09-09 15:08:12 +00001177 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
1178
Devang Patelc20482b2009-03-19 00:23:53 +00001179 unsigned Flags = 0;
1180 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Devang Patele2472482010-09-29 21:05:52 +00001181 Flags = llvm::DIDescriptor::FlagProtected;
Devang Patelc20482b2009-03-19 00:23:53 +00001182 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Devang Patele2472482010-09-29 21:05:52 +00001183 Flags = llvm::DIDescriptor::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +00001184
Devang Patel9ca36b62009-02-26 21:10:26 +00001185 // Create a DW_TAG_member node to remember the offset of this field in the
1186 // struct. FIXME: This is an absolutely insane way to capture this
1187 // information. When we gut debug info, this should be fixed.
1188 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1189 FieldName, FieldDefUnit,
1190 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +00001191 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +00001192 EltTys.push_back(FieldTy);
1193 }
Mike Stump1eb44332009-09-09 15:08:12 +00001194
Devang Patel9ca36b62009-02-26 21:10:26 +00001195 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +00001196 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +00001197
Devang Patele4c1ea02010-03-11 20:01:48 +00001198 RegionStack.pop_back();
1199 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1200 RegionMap.find(Ty->getDecl());
1201 if (RI != RegionMap.end())
1202 RegionMap.erase(RI);
1203
Devang Patel9ca36b62009-02-26 21:10:26 +00001204 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001205 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1206 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001207
Devang Patel6c1fddf2009-07-27 18:42:03 +00001208 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +00001209 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +00001210 Line, Size, Align, 0, 0, llvm::DIType(),
1211 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +00001212
1213 // Now that we have a real decl for the struct, replace anything using the
1214 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001215 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001216 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +00001217
Devang Patel9ca36b62009-02-26 21:10:26 +00001218 return RealDecl;
1219}
1220
Chris Lattner9c85ba32008-11-10 06:08:34 +00001221llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001222 llvm::DIFile Unit) {
Devang Patel6237cea2010-08-23 22:07:25 +00001223 return CreateEnumType(Ty->getDecl(), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001224
Chris Lattner9c85ba32008-11-10 06:08:34 +00001225}
1226
1227llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001228 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001229 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1230 return CreateType(RT, Unit);
1231 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1232 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001233
Chris Lattner9c85ba32008-11-10 06:08:34 +00001234 return llvm::DIType();
1235}
1236
Devang Patel70c23cd2010-02-23 22:59:39 +00001237llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Eli Friedmana7e68452010-08-22 01:00:03 +00001238 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001239 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1240 uint64_t NumElems = Ty->getNumElements();
1241 if (NumElems > 0)
1242 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001243
Benjamin Kramerad468862010-03-30 11:36:44 +00001244 llvm::DIDescriptor Subscript = DebugFactory.GetOrCreateSubrange(0, NumElems);
1245 llvm::DIArray SubscriptArray = DebugFactory.GetOrCreateArray(&Subscript, 1);
Devang Patel70c23cd2010-02-23 22:59:39 +00001246
1247 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1248 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1249
1250 return
1251 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001252 Unit, "", Unit,
Devang Patel70c23cd2010-02-23 22:59:39 +00001253 0, Size, Align, 0, 0,
Eli Friedmana7e68452010-08-22 01:00:03 +00001254 ElementTy, SubscriptArray);
Devang Patel70c23cd2010-02-23 22:59:39 +00001255}
1256
Chris Lattner9c85ba32008-11-10 06:08:34 +00001257llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001258 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001259 uint64_t Size;
1260 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001261
1262
Nuno Lopes010d5142009-01-28 00:35:17 +00001263 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001264 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001265 Size = 0;
1266 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001267 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001268 } else if (Ty->isIncompleteArrayType()) {
1269 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001270 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001271 } else {
1272 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001273 Size = CGM.getContext().getTypeSize(Ty);
1274 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001275 }
Mike Stump1eb44332009-09-09 15:08:12 +00001276
Chris Lattner9c85ba32008-11-10 06:08:34 +00001277 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1278 // interior arrays, do we care? Why aren't nested arrays represented the
1279 // obvious/recursive way?
1280 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1281 QualType EltTy(Ty, 0);
Devang Patelcdf523c2010-10-06 18:30:00 +00001282 if (Ty->isIncompleteArrayType())
Chris Lattner9c85ba32008-11-10 06:08:34 +00001283 EltTy = Ty->getElementType();
Devang Patelcdf523c2010-10-06 18:30:00 +00001284 else {
1285 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1286 uint64_t Upper = 0;
1287 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1288 if (CAT->getSize().getZExtValue())
1289 Upper = CAT->getSize().getZExtValue() - 1;
1290 // FIXME: Verify this is right for VLAs.
1291 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1292 EltTy = Ty->getElementType();
1293 }
Sanjiv Gupta507de852008-06-09 10:47:41 +00001294 }
Mike Stump1eb44332009-09-09 15:08:12 +00001295
Chris Lattner9c85ba32008-11-10 06:08:34 +00001296 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001297 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001298
Devang Patelca80a5f2009-10-20 19:55:01 +00001299 llvm::DIType DbgTy =
1300 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001301 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +00001302 0, Size, Align, 0, 0,
1303 getOrCreateType(EltTy, Unit),
1304 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001305 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001306}
1307
Anders Carlssona031b352009-11-06 19:19:55 +00001308llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001309 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001310 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1311 Ty, Ty->getPointeeType(), Unit);
1312}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001313
Anders Carlsson20f12a22009-12-06 18:00:51 +00001314llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001315 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001316 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1317 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1318
1319 if (!Ty->getPointeeType()->isFunctionType()) {
1320 // We have a data member pointer type.
1321 return PointerDiffDITy;
1322 }
1323
1324 // We have a member function pointer type. Treat it as a struct with two
1325 // ptrdiff_t members.
1326 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1327
1328 uint64_t FieldOffset = 0;
1329 llvm::DIDescriptor ElementTypes[2];
1330
1331 // FIXME: This should probably be a function type instead.
1332 ElementTypes[0] =
1333 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001334 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001335 Info.first, Info.second, FieldOffset, 0,
1336 PointerDiffDITy);
1337 FieldOffset += Info.first;
1338
1339 ElementTypes[1] =
1340 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001341 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001342 Info.first, Info.second, FieldOffset, 0,
1343 PointerDiffDITy);
1344
1345 llvm::DIArray Elements =
1346 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1347 llvm::array_lengthof(ElementTypes));
1348
1349 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1350 U, llvm::StringRef("test"),
Devang Pateld58562e2010-03-09 22:49:11 +00001351 U, 0, FieldOffset,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001352 0, 0, 0, llvm::DIType(), Elements);
1353}
1354
Devang Patel6237cea2010-08-23 22:07:25 +00001355/// CreateEnumType - get enumeration type.
1356llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED, llvm::DIFile Unit){
1357 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
1358
1359 // Create DIEnumerator elements for each enumerator.
1360 for (EnumDecl::enumerator_iterator
1361 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
1362 Enum != EnumEnd; ++Enum) {
1363 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
1364 Enum->getInitVal().getZExtValue()));
1365 }
1366
1367 // Return a CompositeType for the enum itself.
1368 llvm::DIArray EltArray =
1369 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
1370
1371 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1372 unsigned Line = getLineNumber(ED->getLocation());
1373 uint64_t Size = 0;
Devang Patelffc52e72010-08-24 18:14:06 +00001374 uint64_t Align = 0;
1375 if (!ED->getTypeForDecl()->isIncompleteType()) {
1376 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1377 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1378 }
Devang Patel4bc48872010-10-27 23:23:58 +00001379 llvm::DIDescriptor EnumContext =
1380 getContextDescriptor(dyn_cast<Decl>(ED->getDeclContext()), Unit);
Devang Patel6237cea2010-08-23 22:07:25 +00001381 llvm::DIType DbgTy =
1382 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Patel4bc48872010-10-27 23:23:58 +00001383 EnumContext, ED->getName(),
1384 DefUnit, Line, Size, Align, 0, 0,
Devang Patel6237cea2010-08-23 22:07:25 +00001385 llvm::DIType(), EltArray);
1386 return DbgTy;
1387}
1388
Douglas Gregor840943d2009-12-21 20:18:30 +00001389static QualType UnwrapTypeForDebugInfo(QualType T) {
1390 do {
1391 QualType LastT = T;
1392 switch (T->getTypeClass()) {
1393 default:
1394 return T;
1395 case Type::TemplateSpecialization:
1396 T = cast<TemplateSpecializationType>(T)->desugar();
1397 break;
1398 case Type::TypeOfExpr: {
1399 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1400 T = Ty->getUnderlyingExpr()->getType();
1401 break;
1402 }
1403 case Type::TypeOf:
1404 T = cast<TypeOfType>(T)->getUnderlyingType();
1405 break;
1406 case Type::Decltype:
1407 T = cast<DecltypeType>(T)->getUnderlyingType();
1408 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001409 case Type::Elaborated:
1410 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001411 break;
1412 case Type::SubstTemplateTypeParm:
1413 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1414 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001415 }
1416
1417 assert(T != LastT && "Type unwrapping failed to unwrap!");
1418 if (T == LastT)
1419 return T;
1420 } while (true);
1421
1422 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001423}
1424
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001425/// getOrCreateType - Get the type from the cache or create a new
1426/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001427llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001428 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001429 if (Ty.isNull())
1430 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001431
Douglas Gregor840943d2009-12-21 20:18:30 +00001432 // Unwrap the type as needed for debug information.
1433 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001434
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001435 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001436 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001437 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001438 if (it != TypeCache.end()) {
1439 // Verify that the debug info still exists.
1440 if (&*it->second)
1441 return llvm::DIType(cast<llvm::MDNode>(it->second));
1442 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001443
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001444 // Otherwise create the type.
1445 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001446
1447 // And update the type cache.
Devang Patelab699792010-05-07 18:12:35 +00001448 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001449 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001450}
1451
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001452/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001453llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001454 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001455 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001456 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001457 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001458
Douglas Gregor2101a822009-12-21 19:57:21 +00001459 const char *Diag = 0;
1460
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001461 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001462 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001463#define TYPE(Class, Base)
1464#define ABSTRACT_TYPE(Class, Base)
1465#define NON_CANONICAL_TYPE(Class, Base)
1466#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1467#include "clang/AST/TypeNodes.def"
1468 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001469
Anders Carlssonbfe69952009-11-06 18:24:04 +00001470 // FIXME: Handle these.
1471 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001472 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001473
1474 case Type::Vector:
1475 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001476 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001477 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCallc12c5bb2010-05-15 11:32:37 +00001478 case Type::ObjCObject:
1479 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001480 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001481 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1482 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1483 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1484 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001485 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001486 return CreateType(cast<BlockPointerType>(Ty), Unit);
1487 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001488 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001489 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001490 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001491 case Type::FunctionProto:
1492 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001493 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001494 case Type::ConstantArray:
1495 case Type::VariableArray:
1496 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001497 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001498
1499 case Type::LValueReference:
1500 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1501
Anders Carlsson20f12a22009-12-06 18:00:51 +00001502 case Type::MemberPointer:
1503 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001504
1505 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001506 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001507 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001508 case Type::TypeOfExpr:
1509 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001510 case Type::Decltype:
1511 llvm_unreachable("type should have been unwrapped!");
1512 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001513
1514 case Type::RValueReference:
1515 // FIXME: Implement!
1516 Diag = "rvalue references";
1517 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001518 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001519
1520 assert(Diag && "Fall through without a diagnostic?");
1521 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1522 "debug information for %0 is not yet supported");
1523 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1524 << Diag;
1525 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001526}
1527
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001528/// CreateMemberType - Create new member and increase Offset by FType's size.
1529llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
1530 llvm::StringRef Name,
1531 uint64_t *Offset) {
1532 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1533 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1534 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
1535 llvm::DIType Ty = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1536 Unit, Name, Unit, 0,
1537 FieldSize, FieldAlign,
1538 *Offset, 0, FieldTy);
1539 *Offset += FieldSize;
1540 return Ty;
1541}
1542
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001543/// EmitFunctionStart - Constructs the debug code for entering a function -
1544/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001545void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001546 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001547 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001548
Devang Patel9c6c3a02010-01-14 00:36:21 +00001549 llvm::StringRef Name;
Anders Carlsson9a20d552010-06-22 16:16:50 +00001550 llvm::StringRef LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001551
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001552 FnBeginRegionCount.push_back(RegionStack.size());
1553
Devang Patel9c6c3a02010-01-14 00:36:21 +00001554 const Decl *D = GD.getDecl();
Devang Patel3951e712010-10-07 22:03:49 +00001555 unsigned Flags = 0;
Devang Patel0692f832010-10-11 21:58:41 +00001556 llvm::DIFile Unit = getOrCreateFile(CurLoc);
1557 llvm::DIDescriptor FDContext(Unit);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001558 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001559 // If there is a DISubprogram for this function available then use it.
1560 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1561 FI = SPCache.find(FD);
1562 if (FI != SPCache.end()) {
Gabor Greif38c9b172010-09-18 13:00:17 +00001563 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(&*FI->second));
Devang Patelab699792010-05-07 18:12:35 +00001564 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1565 llvm::MDNode *SPN = SP;
1566 RegionStack.push_back(SPN);
1567 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00001568 return;
1569 }
1570 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001571 Name = getFunctionName(FD);
1572 // Use mangled name as linkage name for c/c++ functions.
Anders Carlsson9a20d552010-06-22 16:16:50 +00001573 LinkageName = CGM.getMangledName(GD);
Devang Patel58faf202010-10-22 17:11:50 +00001574 if (LinkageName == Name)
1575 LinkageName = llvm::StringRef();
Devang Patel3951e712010-10-07 22:03:49 +00001576 if (FD->hasPrototype())
1577 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel0692f832010-10-11 21:58:41 +00001578 if (const NamespaceDecl *NSDecl =
1579 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
1580 FDContext = getOrCreateNameSpace(NSDecl, Unit);
David Chisnall70b9b442010-09-02 17:16:32 +00001581 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
David Chisnall52044a22010-09-02 18:01:51 +00001582 Name = getObjCMethodName(OMD);
Devang Patel3951e712010-10-07 22:03:49 +00001583 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001584 } else {
Devang Patel58faf202010-10-22 17:11:50 +00001585 // Use llvm function name.
Devang Patel9c6c3a02010-01-14 00:36:21 +00001586 Name = Fn->getName();
Devang Patel3951e712010-10-07 22:03:49 +00001587 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001588 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001589 if (!Name.empty() && Name[0] == '\01')
1590 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001591
Devang Patel970c6182010-04-24 00:49:16 +00001592 // It is expected that CurLoc is set before using EmitFunctionStart.
1593 // Usually, CurLoc points to the left bracket location of compound
1594 // statement representing function body.
Devang Patel8ab870d2010-05-12 23:46:38 +00001595 unsigned LineNo = getLineNumber(CurLoc);
Devang Patele2472482010-09-29 21:05:52 +00001596 if (D->isImplicit())
1597 Flags |= llvm::DIDescriptor::FlagArtificial;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001598 llvm::DISubprogram SP =
Devang Patel0692f832010-10-11 21:58:41 +00001599 DebugFactory.CreateSubprogram(FDContext, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001600 getOrCreateType(FnType, Unit),
Devang Patel15a3d7d2010-07-15 23:09:46 +00001601 Fn->hasInternalLinkage(), true/*definition*/,
1602 0, 0, llvm::DIType(),
Devang Patele2472482010-09-29 21:05:52 +00001603 Flags,
Devang Patel15a3d7d2010-07-15 23:09:46 +00001604 CGM.getLangOptions().Optimize, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +00001605
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001606 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001607 llvm::MDNode *SPN = SP;
1608 RegionStack.push_back(SPN);
1609 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001610
1611 // Clear stack used to keep track of #line directives.
1612 LineDirectiveFiles.clear();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001613}
1614
1615
Devang Patel4d939e62010-07-20 22:20:10 +00001616void CGDebugInfo::EmitStopPoint(CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001617 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001618
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001619 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001620 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001621 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001622 || (SM.getInstantiationLineNumber(CurLoc) ==
1623 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001624 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patel4800ea62010-04-05 21:09:15 +00001625 // New Builder may not be in sync with CGDebugInfo.
1626 if (!Builder.getCurrentDebugLocation().isUnknown())
1627 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001628
1629 // Update last state.
1630 PrevLoc = CurLoc;
1631
Chris Lattnerc6034632010-04-01 06:31:43 +00001632 llvm::MDNode *Scope = RegionStack.back();
Devang Patel8ab870d2010-05-12 23:46:38 +00001633 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1634 getColumnNumber(CurLoc),
Chris Lattnere541d012010-04-02 20:21:43 +00001635 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001636}
1637
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001638/// UpdateLineDirectiveRegion - Update region stack only if #line directive
1639/// has introduced scope change.
1640void CGDebugInfo::UpdateLineDirectiveRegion(CGBuilderTy &Builder) {
1641 if (CurLoc.isInvalid() || CurLoc.isMacroID() ||
1642 PrevLoc.isInvalid() || PrevLoc.isMacroID())
1643 return;
1644 SourceManager &SM = CGM.getContext().getSourceManager();
1645 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
1646 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc);
1647
1648 if (!strcmp(PPLoc.getFilename(), PCLoc.getFilename()))
1649 return;
1650
1651 // If #line directive stack is empty then we are entering a new scope.
1652 if (LineDirectiveFiles.empty()) {
1653 EmitRegionStart(Builder);
1654 LineDirectiveFiles.push_back(PCLoc.getFilename());
1655 return;
1656 }
1657
1658 assert (RegionStack.size() >= LineDirectiveFiles.size()
1659 && "error handling #line regions!");
1660
1661 bool SeenThisFile = false;
Devang Patel424a5c62010-09-15 20:50:40 +00001662 // Chek if current file is already seen earlier.
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001663 for(std::vector<const char *>::iterator I = LineDirectiveFiles.begin(),
1664 E = LineDirectiveFiles.end(); I != E; ++I)
Devang Patel424a5c62010-09-15 20:50:40 +00001665 if (!strcmp(PCLoc.getFilename(), *I)) {
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001666 SeenThisFile = true;
1667 break;
1668 }
1669
1670 // If #line for this file is seen earlier then pop out #line regions.
1671 if (SeenThisFile) {
1672 while (!LineDirectiveFiles.empty()) {
1673 const char *LastFile = LineDirectiveFiles.back();
1674 RegionStack.pop_back();
1675 LineDirectiveFiles.pop_back();
1676 if (!strcmp(PPLoc.getFilename(), LastFile))
1677 break;
1678 }
1679 return;
1680 }
1681
1682 // .. otherwise insert new #line region.
1683 EmitRegionStart(Builder);
1684 LineDirectiveFiles.push_back(PCLoc.getFilename());
1685
1686 return;
1687}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001688/// EmitRegionStart- Constructs the debug code for entering a declarative
1689/// region - "llvm.dbg.region.start.".
Devang Patel4d939e62010-07-20 22:20:10 +00001690void CGDebugInfo::EmitRegionStart(CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001691 llvm::DIDescriptor D =
1692 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1693 llvm::DIDescriptor() :
Devang Pateld19429f2010-02-16 21:41:20 +00001694 llvm::DIDescriptor(RegionStack.back()),
Stuart Hastings257d1d32010-07-19 23:56:31 +00001695 getOrCreateFile(CurLoc),
Devang Patel8ab870d2010-05-12 23:46:38 +00001696 getLineNumber(CurLoc),
1697 getColumnNumber(CurLoc));
Devang Patelab699792010-05-07 18:12:35 +00001698 llvm::MDNode *DN = D;
1699 RegionStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001700}
1701
1702/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1703/// region - "llvm.dbg.region.end."
Devang Patel4d939e62010-07-20 22:20:10 +00001704void CGDebugInfo::EmitRegionEnd(CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001705 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1706
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001707 // Provide an region stop point.
Devang Patel4d939e62010-07-20 22:20:10 +00001708 EmitStopPoint(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001709
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001710 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001711}
1712
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001713/// EmitFunctionEnd - Constructs the debug code for exiting a function.
1714void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
1715 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1716 unsigned RCount = FnBeginRegionCount.back();
1717 assert(RCount <= RegionStack.size() && "Region stack mismatch");
1718
1719 // Pop all regions for this function.
1720 while (RegionStack.size() != RCount)
1721 EmitRegionEnd(Builder);
1722 FnBeginRegionCount.pop_back();
1723}
1724
Devang Patel809b9bb2010-02-10 18:49:08 +00001725// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1726// See BuildByRefType.
1727llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1728 uint64_t *XOffset) {
1729
1730 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1731
1732 QualType FType;
1733 uint64_t FieldSize, FieldOffset;
1734 unsigned FieldAlign;
1735
Devang Patel17800552010-03-09 00:44:50 +00001736 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001737 QualType Type = VD->getType();
1738
1739 FieldOffset = 0;
1740 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001741 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1742 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001743 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001744 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1745 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1746
Devang Patel809b9bb2010-02-10 18:49:08 +00001747 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1748 if (HasCopyAndDispose) {
1749 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001750 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1751 &FieldOffset));
1752 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1753 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001754 }
1755
1756 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1757 if (Align > CharUnits::fromQuantity(
1758 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1759 unsigned AlignedOffsetInBytes
1760 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1761 unsigned NumPaddingBytes
1762 = AlignedOffsetInBytes - FieldOffset/8;
1763
1764 if (NumPaddingBytes > 0) {
1765 llvm::APInt pad(32, NumPaddingBytes);
1766 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1767 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001768 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001769 }
1770 }
1771
1772 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001773 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00001774 FieldSize = CGM.getContext().getTypeSize(FType);
1775 FieldAlign = Align.getQuantity()*8;
1776
1777 *XOffset = FieldOffset;
1778 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001779 VD->getName(), Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001780 0, FieldSize, FieldAlign,
1781 FieldOffset, 0, FieldTy);
1782 EltTys.push_back(FieldTy);
1783 FieldOffset += FieldSize;
1784
1785 llvm::DIArray Elements =
1786 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1787
Devang Patele2472482010-09-29 21:05:52 +00001788 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Devang Patel809b9bb2010-02-10 18:49:08 +00001789
1790 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001791 Unit, "", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001792 0, FieldOffset, 0, 0, Flags,
1793 llvm::DIType(), Elements);
1794
1795}
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001796/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001797void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001798 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001799 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1800
Devang Patel17800552010-03-09 00:44:50 +00001801 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001802 llvm::DIType Ty;
1803 uint64_t XOffset = 0;
1804 if (VD->hasAttr<BlocksAttr>())
1805 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1806 else
1807 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001808
Devang Patelf4e54a22010-05-07 23:05:55 +00001809 // If there is not any debug info for type then do not emit debug info
1810 // for this variable.
1811 if (!Ty)
1812 return;
1813
Chris Lattner9c85ba32008-11-10 06:08:34 +00001814 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001815 unsigned Line = getLineNumber(VD->getLocation());
1816 unsigned Column = getColumnNumber(VD->getLocation());
Devang Patelaca745b2010-09-29 23:09:21 +00001817 unsigned Flags = 0;
1818 if (VD->isImplicit())
1819 Flags |= llvm::DIDescriptor::FlagArtificial;
Chris Lattnerc6034632010-04-01 06:31:43 +00001820 llvm::MDNode *Scope = RegionStack.back();
Devang Patelcebbedd2010-10-12 23:24:54 +00001821
1822 llvm::StringRef Name = VD->getName();
1823 if (!Name.empty()) {
1824 // Create the descriptor for the variable.
1825 llvm::DIVariable D =
1826 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(Scope),
1827 Name, Unit, Line, Ty,
1828 CGM.getLangOptions().Optimize, Flags);
1829
1830 // Insert an llvm.dbg.declare into the current block.
1831 llvm::Instruction *Call =
1832 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
1833
1834 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
1835 }
1836
1837 // If VD is an anonymous union then Storage represents value for
1838 // all union fields.
1839 if (const RecordType *RT = dyn_cast<RecordType>(VD->getType()))
1840 if (const RecordDecl *RD = dyn_cast<RecordDecl>(RT->getDecl()))
1841 if (RD->isUnion()) {
1842 for (RecordDecl::field_iterator I = RD->field_begin(),
1843 E = RD->field_end();
1844 I != E; ++I) {
1845 FieldDecl *Field = *I;
1846 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1847 llvm::StringRef FieldName = Field->getName();
1848
1849 // Ignore unnamed fields. Do not ignore unnamed records.
1850 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
1851 continue;
1852
1853 // Use VarDecl's Tag, Scope and Line number.
1854 llvm::DIVariable D =
1855 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(Scope),
1856 FieldName, Unit, Line, FieldTy,
1857 CGM.getLangOptions().Optimize, Flags);
1858
1859 // Insert an llvm.dbg.declare into the current block.
1860 llvm::Instruction *Call =
1861 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
1862
1863 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
1864 }
1865 }
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001866}
1867
Mike Stumpb1a6e682009-09-30 02:43:10 +00001868/// EmitDeclare - Emit local variable declaration debug info.
1869void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1870 llvm::Value *Storage, CGBuilderTy &Builder,
1871 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001872 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001873 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1874
Devang Patel2b594b92010-04-26 23:28:46 +00001875 if (Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001876 return;
1877
1878 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001879 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001880 llvm::DIType Ty;
1881 if (VD->hasAttr<BlocksAttr>())
1882 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1883 else
1884 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001885
1886 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001887 unsigned Line = getLineNumber(VD->getLocation());
1888 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001889
Devang Pateld6c5a262010-02-01 21:52:22 +00001890 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001891 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001892 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1893 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1894 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1895 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001896 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001897 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1898 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001899 // offset of __forwarding field
1900 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001901 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1902 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1903 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001904 // offset of x field
1905 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001906 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001907 }
1908
1909 // Create the descriptor for the variable.
1910 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001911 DebugFactory.CreateComplexVariable(Tag,
1912 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001913 VD->getName(), Unit, Line, Ty,
Benjamin Kramer3475cfe2010-09-21 15:59:59 +00001914 addr.data(), addr.size());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001915 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001916 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001917 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattnerd5b89022009-12-28 21:44:41 +00001918
Chris Lattnerc6034632010-04-01 06:31:43 +00001919 llvm::MDNode *Scope = RegionStack.back();
Devang Patelf8e10a52010-05-10 23:48:38 +00001920 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001921}
1922
Devang Pateld6c5a262010-02-01 21:52:22 +00001923void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001924 llvm::Value *Storage,
1925 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001926 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001927}
1928
Mike Stumpb1a6e682009-09-30 02:43:10 +00001929void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1930 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1931 CodeGenFunction *CGF) {
1932 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1933}
1934
Chris Lattner9c85ba32008-11-10 06:08:34 +00001935/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1936/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001937void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001938 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001939 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001940}
1941
1942
1943
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001944/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001945void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001946 const VarDecl *D) {
1947
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001948 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001949 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001950 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +00001951
Devang Pateleb6d79b2010-02-01 21:34:11 +00001952 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001953 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001954
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001955 // CodeGen turns int[] into int[1] so we'll do the same here.
1956 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001957
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001958 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001959 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001960
Anders Carlsson20f12a22009-12-06 18:00:51 +00001961 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001962 ArrayType::Normal, 0);
1963 }
Devang Patel5d822f02010-04-29 17:48:37 +00001964 llvm::StringRef DeclName = D->getName();
Devang Patel8b90a782010-05-13 23:52:37 +00001965 llvm::StringRef LinkageName;
Devang Patel0fd3d1f2010-05-14 16:55:25 +00001966 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()))
Devang Patel8b90a782010-05-13 23:52:37 +00001967 LinkageName = Var->getName();
Devang Patel58faf202010-10-22 17:11:50 +00001968 if (LinkageName == DeclName)
1969 LinkageName = llvm::StringRef();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001970 llvm::DIDescriptor DContext =
1971 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
Devang Patel8b90a782010-05-13 23:52:37 +00001972 DebugFactory.CreateGlobalVariable(DContext, DeclName, DeclName, LinkageName,
1973 Unit, LineNo, getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001974 Var->hasInternalLinkage(),
1975 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001976}
1977
Devang Patel9ca36b62009-02-26 21:10:26 +00001978/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001979void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001980 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001981 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001982 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001983 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patel9ca36b62009-02-26 21:10:26 +00001984
Devang Pateld6c5a262010-02-01 21:52:22 +00001985 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001986
Devang Pateld6c5a262010-02-01 21:52:22 +00001987 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001988 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001989
Devang Patel9ca36b62009-02-26 21:10:26 +00001990 // CodeGen turns int[] into int[1] so we'll do the same here.
1991 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001992
Devang Patel9ca36b62009-02-26 21:10:26 +00001993 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001994 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001995
Anders Carlsson20f12a22009-12-06 18:00:51 +00001996 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001997 ArrayType::Normal, 0);
1998 }
1999
Devang Patelf6a39b72009-10-20 18:26:30 +00002000 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00002001 getOrCreateType(T, Unit),
2002 Var->hasInternalLinkage(),
2003 true/*definition*/, Var);
2004}
Devang Patelabb485f2010-02-01 19:16:32 +00002005
Devang Patel25c2c8f2010-08-10 17:53:33 +00002006/// EmitGlobalVariable - Emit global variable's debug info.
2007void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
John McCall189d6ef2010-10-09 01:34:31 +00002008 llvm::Constant *Init) {
Devang Patel8d308382010-08-10 07:24:25 +00002009 // Create the descriptor for the variable.
2010 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
2011 llvm::StringRef Name = VD->getName();
Devang Patel0317ab02010-08-10 18:27:15 +00002012 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
Devang Patel6237cea2010-08-23 22:07:25 +00002013 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
2014 if (const EnumDecl *ED = dyn_cast<EnumDecl>(ECD->getDeclContext()))
2015 Ty = CreateEnumType(ED, Unit);
2016 }
Devang Patel0317ab02010-08-10 18:27:15 +00002017 // Do not use DIGlobalVariable for enums.
2018 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
2019 return;
Devang Patel8d308382010-08-10 07:24:25 +00002020 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit,
2021 getLineNumber(VD->getLocation()),
Devang Patel4c4acc02010-08-10 20:16:57 +00002022 Ty, true, true, Init);
Devang Patel8d308382010-08-10 07:24:25 +00002023}
2024
Devang Patelabb485f2010-02-01 19:16:32 +00002025/// getOrCreateNamesSpace - Return namespace descriptor for the given
2026/// namespace decl.
2027llvm::DINameSpace
2028CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
2029 llvm::DIDescriptor Unit) {
2030 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
2031 NameSpaceCache.find(NSDecl);
2032 if (I != NameSpaceCache.end())
2033 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
2034
Devang Patel8ab870d2010-05-12 23:46:38 +00002035 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patelabb485f2010-02-01 19:16:32 +00002036
2037 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00002038 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00002039 llvm::DINameSpace NS =
2040 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
Devang Patel2ed8f002010-08-27 17:47:47 +00002041 llvm::DIFile(Unit), LineNo);
Devang Patelab699792010-05-07 18:12:35 +00002042 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00002043 return NS;
2044}