blob: 43f47a005de07710a360b95f0333c18e1fb238a9 [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 Patel9ca36b62009-02-26 21:10:26 +000018#include "clang/AST/DeclObjC.h"
Chris Lattner3cc5c402008-11-11 07:01:36 +000019#include "clang/AST/Expr.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000020#include "clang/AST/RecordLayout.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000021#include "clang/Basic/SourceManager.h"
22#include "clang/Basic/FileManager.h"
Mike Stump5a862172009-09-15 21:48:34 +000023#include "clang/Basic/Version.h"
Chandler Carruth2811ccf2009-11-12 17:24:48 +000024#include "clang/CodeGen/CodeGenOptions.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000025#include "llvm/Constants.h"
26#include "llvm/DerivedTypes.h"
27#include "llvm/Instructions.h"
28#include "llvm/Intrinsics.h"
29#include "llvm/Module.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000030#include "llvm/ADT/StringExtras.h"
31#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000032#include "llvm/Support/Dwarf.h"
Devang Patel446c6192009-04-17 21:06:59 +000033#include "llvm/System/Path.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000034#include "llvm/Target/TargetMachine.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000035using namespace clang;
36using namespace clang::CodeGen;
37
Anders Carlsson20f12a22009-12-06 18:00:51 +000038CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Devang Patel17800552010-03-09 00:44:50 +000039 : CGM(CGM), DebugFactory(CGM.getModule()),
Devang Patel7573f8b2010-03-09 21:32:27 +000040 FwdDeclCount(0), BlockLiteralGenericSet(false) {
Devang Patel17800552010-03-09 00:44:50 +000041 CreateCompileUnit();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000042}
43
Chris Lattner9c85ba32008-11-10 06:08:34 +000044CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar66031a52008-10-17 16:15:48 +000045 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000046}
47
Chris Lattner9c85ba32008-11-10 06:08:34 +000048void CGDebugInfo::setLocation(SourceLocation Loc) {
49 if (Loc.isValid())
Anders Carlsson20f12a22009-12-06 18:00:51 +000050 CurLoc = CGM.getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000051}
52
Devang Patel33583052010-01-28 23:15:27 +000053/// getContextDescriptor - Get context info for the decl.
Devang Pateleb6d79b2010-02-01 21:34:11 +000054llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context,
Devang Patel33583052010-01-28 23:15:27 +000055 llvm::DIDescriptor &CompileUnit) {
Devang Pateleb6d79b2010-02-01 21:34:11 +000056 if (!Context)
57 return CompileUnit;
58
59 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
60 I = RegionMap.find(Context);
61 if (I != RegionMap.end())
62 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(I->second));
Devang Patel411894b2010-02-01 22:40:08 +000063
Devang Pateleb6d79b2010-02-01 21:34:11 +000064 // Check namespace.
65 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
66 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl, CompileUnit));
67
Devang Patel979ec2e2009-10-06 00:35:31 +000068 return CompileUnit;
69}
70
Devang Patel9c6c3a02010-01-14 00:36:21 +000071/// getFunctionName - Get function name for the given FunctionDecl. If the
72/// name is constructred on demand (e.g. C++ destructor) then the name
73/// is stored on the side.
74llvm::StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
75 assert (FD && "Invalid FunctionDecl!");
76 IdentifierInfo *FII = FD->getIdentifier();
77 if (FII)
78 return FII->getName();
79
80 // Otherwise construct human readable name for debug info.
81 std::string NS = FD->getNameAsString();
82
83 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +000084 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer1b627dc2010-01-23 18:16:07 +000085 memcpy(StrPtr, NS.data(), NS.length());
86 return llvm::StringRef(StrPtr, NS.length());
Devang Patel9c6c3a02010-01-14 00:36:21 +000087}
88
Devang Patel17800552010-03-09 00:44:50 +000089/// getOrCreateFile - Get the file debug info descriptor for the input location.
90llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Ted Kremenek9c250392010-03-30 00:27:51 +000091 if (!Loc.isValid())
Devang Patel17800552010-03-09 00:44:50 +000092 // If Location is not valid then use main input file.
93 return DebugFactory.CreateFile(TheCU.getFilename(), TheCU.getDirectory(),
94 TheCU);
Anders Carlsson20f12a22009-12-06 18:00:51 +000095 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel17800552010-03-09 00:44:50 +000096 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Ted Kremenek9c250392010-03-30 00:27:51 +000097
98 // Cache the results.
99 const char *fname = PLoc.getFilename();
100 llvm::DenseMap<const char *, llvm::WeakVH>::iterator it =
101 DIFileCache.find(fname);
102
103 if (it != DIFileCache.end()) {
104 // Verify that the information still exists.
105 if (&*it->second)
106 return llvm::DIFile(cast<llvm::MDNode>(it->second));
107 }
108
109 // FIXME: We shouldn't even need to call 'makeAbsolute()' in the cases
110 // where we can consult the FileEntry.
Devang Patel17800552010-03-09 00:44:50 +0000111 llvm::sys::Path AbsFileName(PLoc.getFilename());
Benjamin Kramer47daf682009-12-08 11:02:29 +0000112 AbsFileName.makeAbsolute();
Devang Patel446c6192009-04-17 21:06:59 +0000113
Ted Kremenek9c250392010-03-30 00:27:51 +0000114 llvm::DIFile F = DebugFactory.CreateFile(AbsFileName.getLast(),
115 AbsFileName.getDirname(), TheCU);
116
Devang Patelab699792010-05-07 18:12:35 +0000117 DIFileCache[fname] = F;
Ted Kremenek9c250392010-03-30 00:27:51 +0000118 return F;
119
Devang Patel17800552010-03-09 00:44:50 +0000120}
121/// CreateCompileUnit - Create new compile unit.
122void CGDebugInfo::CreateCompileUnit() {
123
124 // Get absolute path name.
Douglas Gregorac91b4c2010-03-18 23:46:43 +0000125 SourceManager &SM = CGM.getContext().getSourceManager();
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000126 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
127 if (MainFileName.empty())
Devang Patel22fe5852010-03-12 21:04:27 +0000128 MainFileName = "<unknown>";
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000129
Devang Patel22fe5852010-03-12 21:04:27 +0000130 llvm::sys::Path AbsFileName(MainFileName);
Devang Patel17800552010-03-09 00:44:50 +0000131 AbsFileName.makeAbsolute();
Daniel Dunbarc9abc042009-04-08 05:11:16 +0000132
Douglas Gregorf6728fc2010-03-22 21:28:29 +0000133 // The main file name provided via the "-main-file-name" option contains just
134 // the file name itself with no path information. This file name may have had
135 // a relative path, so we look into the actual file entry for the main
136 // file to determine the real absolute path for the file.
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000137 std::string MainFileDir;
138 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID()))
139 MainFileDir = MainFile->getDir()->getName();
140 else
141 MainFileDir = AbsFileName.getDirname();
142
Chris Lattner515455a2009-03-25 03:28:08 +0000143 unsigned LangTag;
Devang Patel17800552010-03-09 00:44:50 +0000144 const LangOptions &LO = CGM.getLangOptions();
Chris Lattner515455a2009-03-25 03:28:08 +0000145 if (LO.CPlusPlus) {
146 if (LO.ObjC1)
147 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
148 else
149 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
150 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000151 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000152 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000153 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000154 } else {
155 LangTag = llvm::dwarf::DW_LANG_C89;
156 }
Devang Patel446c6192009-04-17 21:06:59 +0000157
Benjamin Kramer47daf682009-12-08 11:02:29 +0000158 const char *Producer =
Mike Stumpd8945d62009-10-09 18:38:12 +0000159#ifdef CLANG_VENDOR
160 CLANG_VENDOR
161#endif
162 "clang " CLANG_VERSION_STRING;
Chris Lattner4c2577a2009-05-02 01:00:04 +0000163
164 // Figure out which version of the ObjC runtime we have.
165 unsigned RuntimeVers = 0;
166 if (LO.ObjC1)
167 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000168
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000169 // Create new compile unit.
Devang Patel17800552010-03-09 00:44:50 +0000170 TheCU = DebugFactory.CreateCompileUnit(
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000171 LangTag, AbsFileName.getLast(), MainFileDir, Producer, true,
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000172 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000173}
174
Devang Patel65e99f22009-02-25 01:36:11 +0000175/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000176/// one if necessary.
177llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel17800552010-03-09 00:44:50 +0000178 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000179 unsigned Encoding = 0;
180 switch (BT->getKind()) {
181 default:
182 case BuiltinType::Void:
183 return llvm::DIType();
184 case BuiltinType::UChar:
185 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
186 case BuiltinType::Char_S:
187 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
188 case BuiltinType::UShort:
189 case BuiltinType::UInt:
190 case BuiltinType::ULong:
191 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
192 case BuiltinType::Short:
193 case BuiltinType::Int:
194 case BuiltinType::Long:
195 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
196 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
197 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000198 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000199 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000200 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000201 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000202 uint64_t Size = CGM.getContext().getTypeSize(BT);
203 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000204 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000205
Devang Patelca80a5f2009-10-20 19:55:01 +0000206 llvm::DIType DbgTy =
207 DebugFactory.CreateBasicType(Unit,
Anders Carlsson20f12a22009-12-06 18:00:51 +0000208 BT->getName(CGM.getContext().getLangOptions()),
Devang Patelca80a5f2009-10-20 19:55:01 +0000209 Unit, 0, Size, Align,
210 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000211 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000212}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000213
Chris Lattnerb7003772009-04-23 06:13:01 +0000214llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000215 llvm::DIFile Unit) {
Chris Lattnerb7003772009-04-23 06:13:01 +0000216 // Bit size, align and offset of the type.
217 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
218 if (Ty->isComplexIntegerType())
219 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000220
Anders Carlsson20f12a22009-12-06 18:00:51 +0000221 uint64_t Size = CGM.getContext().getTypeSize(Ty);
222 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattnerb7003772009-04-23 06:13:01 +0000223 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Devang Patelca80a5f2009-10-20 19:55:01 +0000225 llvm::DIType DbgTy =
226 DebugFactory.CreateBasicType(Unit, "complex",
227 Unit, 0, Size, Align,
228 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000229 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000230}
231
John McCalla1805292009-09-25 01:40:47 +0000232/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000233/// a new one if necessary.
Devang Patel17800552010-03-09 00:44:50 +0000234llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +0000235 QualifierCollector Qc;
236 const Type *T = Qc.strip(Ty);
237
238 // Ignore these qualifiers for now.
239 Qc.removeObjCGCAttr();
240 Qc.removeAddressSpace();
241
Chris Lattner9c85ba32008-11-10 06:08:34 +0000242 // We will create one Derived type for one qualifier and recurse to handle any
243 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000244 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000245 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000246 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000247 Qc.removeConst();
248 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000249 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000250 Qc.removeVolatile();
251 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000252 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000253 Qc.removeRestrict();
254 } else {
255 assert(Qc.empty() && "Unknown type qualifier for debug info");
256 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000257 }
Mike Stump1eb44332009-09-09 15:08:12 +0000258
John McCalla1805292009-09-25 01:40:47 +0000259 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
260
Daniel Dunbar3845f862008-10-31 03:54:29 +0000261 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
262 // CVR derived types.
Devang Patelca80a5f2009-10-20 19:55:01 +0000263 llvm::DIType DbgTy =
Devang Pateld58562e2010-03-09 22:49:11 +0000264 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000265 0, 0, 0, 0, 0, FromTy);
Devang Patelca80a5f2009-10-20 19:55:01 +0000266 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000267}
268
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000269llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000270 llvm::DIFile Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000271 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000272 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
273 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000274 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000275}
276
Chris Lattner9c85ba32008-11-10 06:08:34 +0000277llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000278 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000279 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
280 Ty->getPointeeType(), Unit);
281}
282
283llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
284 const Type *Ty,
285 QualType PointeeTy,
Devang Patel17800552010-03-09 00:44:50 +0000286 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000287 llvm::DIType EltTy = getOrCreateType(PointeeTy, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000289 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000290
291 // Size is always the size of a pointer. We can't use getTypeSize here
292 // because that does not return the correct value for references.
293 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000294 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
295 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Devang Patelca80a5f2009-10-20 19:55:01 +0000297 return
Devang Pateld58562e2010-03-09 22:49:11 +0000298 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000299 0, Size, Align, 0, 0, EltTy);
Anders Carlssona031b352009-11-06 19:19:55 +0000300
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000301}
302
Mike Stump9bc093c2009-05-14 02:03:51 +0000303llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000304 llvm::DIFile Unit) {
Mike Stump9bc093c2009-05-14 02:03:51 +0000305 if (BlockLiteralGenericSet)
306 return BlockLiteralGeneric;
307
Mike Stump9bc093c2009-05-14 02:03:51 +0000308 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
309
310 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
311
312 llvm::DIType FieldTy;
313
314 QualType FType;
315 uint64_t FieldSize, FieldOffset;
316 unsigned FieldAlign;
317
318 llvm::DIArray Elements;
319 llvm::DIType EltTy, DescTy;
320
321 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000322 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000323 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
324 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000325
Daniel Dunbarca308df2009-05-26 19:40:20 +0000326 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000327 EltTys.clear();
328
Mike Stump3d363c52009-10-02 02:30:50 +0000329 unsigned Flags = llvm::DIType::FlagAppleBlock;
330
Mike Stump9bc093c2009-05-14 02:03:51 +0000331 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Devang Pateld58562e2010-03-09 22:49:11 +0000332 Unit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000333 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Mike Stump9bc093c2009-05-14 02:03:51 +0000335 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000336 uint64_t Size = CGM.getContext().getTypeSize(Ty);
337 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Mike Stump9bc093c2009-05-14 02:03:51 +0000339 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000340 Unit, "", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000341 0, Size, Align, 0, 0, EltTy);
342
343 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000344 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000345 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson20f12a22009-12-06 18:00:51 +0000346 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000347 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
348 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramerd3651cc2010-04-24 20:26:20 +0000349 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000350 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000351
Anders Carlsson20f12a22009-12-06 18:00:51 +0000352 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000353 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000354 FieldSize = CGM.getContext().getTypeSize(Ty);
355 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000356 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000357 "__descriptor", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000358 0, FieldSize, FieldAlign,
359 FieldOffset, 0, FieldTy);
360 EltTys.push_back(FieldTy);
361
362 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000363 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000364
365 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Devang Pateld58562e2010-03-09 22:49:11 +0000366 Unit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000367 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Mike Stump9bc093c2009-05-14 02:03:51 +0000369 BlockLiteralGenericSet = true;
370 BlockLiteralGeneric
371 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000372 "", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000373 0, Size, Align, 0, 0, EltTy);
374 return BlockLiteralGeneric;
375}
376
Chris Lattner9c85ba32008-11-10 06:08:34 +0000377llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000378 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000379 // Typedefs are derived from some other type. If we have a typedef of a
380 // typedef, make sure to emit the whole chain.
381 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Chris Lattner9c85ba32008-11-10 06:08:34 +0000383 // We don't set size information, but do specify where the typedef was
384 // declared.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000385 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateld5289052010-01-29 22:29:31 +0000386 PresumedLoc PLoc = SM.getPresumedLoc(Ty->getDecl()->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000387 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000388
Devang Pateleb6d79b2010-02-01 21:34:11 +0000389 llvm::DIDescriptor TyContext
390 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
391 Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000392 llvm::DIType DbgTy =
Devang Pateld5289052010-01-29 22:29:31 +0000393 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Pateleb6d79b2010-02-01 21:34:11 +0000394 TyContext,
Devang Pateld5289052010-01-29 22:29:31 +0000395 Ty->getDecl()->getName(), Unit,
396 Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000397 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000398}
399
Chris Lattner9c85ba32008-11-10 06:08:34 +0000400llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000401 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000402 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000403
Chris Lattner9c85ba32008-11-10 06:08:34 +0000404 // Add the result type at least.
405 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Chris Lattner9c85ba32008-11-10 06:08:34 +0000407 // Set up remainder of arguments if there is a prototype.
408 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000409 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000410 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
411 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
412 } else {
413 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000414 }
415
Chris Lattner9c85ba32008-11-10 06:08:34 +0000416 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000417 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Devang Patelca80a5f2009-10-20 19:55:01 +0000419 llvm::DIType DbgTy =
420 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000421 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000422 0, 0, 0, 0, 0,
423 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000424 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000425}
426
Devang Patel428deb52010-01-19 00:00:59 +0000427/// CollectRecordFields - A helper function to collect debug info for
428/// record fields. This is used while creating debug info entry for a Record.
429void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000430CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit,
Devang Patel428deb52010-01-19 00:00:59 +0000431 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
432 unsigned FieldNo = 0;
433 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel239cec62010-02-01 21:39:52 +0000434 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
435 for (RecordDecl::field_iterator I = RD->field_begin(),
436 E = RD->field_end();
Devang Patel428deb52010-01-19 00:00:59 +0000437 I != E; ++I, ++FieldNo) {
438 FieldDecl *Field = *I;
439 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
440
441 llvm::StringRef FieldName = Field->getName();
442
Devang Patel4835fdd2010-02-12 01:31:06 +0000443 // Ignore unnamed fields. Do not ignore unnamed records.
444 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel428deb52010-01-19 00:00:59 +0000445 continue;
446
447 // Get the location for the field.
448 SourceLocation FieldDefLoc = Field->getLocation();
449 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
Devang Patel17800552010-03-09 00:44:50 +0000450 llvm::DIFile FieldDefUnit;
Devang Patel428deb52010-01-19 00:00:59 +0000451 unsigned FieldLine = 0;
452
453 if (!PLoc.isInvalid()) {
Devang Patel17800552010-03-09 00:44:50 +0000454 FieldDefUnit = getOrCreateFile(FieldDefLoc);
Devang Patel428deb52010-01-19 00:00:59 +0000455 FieldLine = PLoc.getLine();
456 }
457
458 QualType FType = Field->getType();
459 uint64_t FieldSize = 0;
460 unsigned FieldAlign = 0;
461 if (!FType->isIncompleteArrayType()) {
462
463 // Bit size, align and offset of the type.
464 FieldSize = CGM.getContext().getTypeSize(FType);
465 Expr *BitWidth = Field->getBitWidth();
466 if (BitWidth)
467 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
468
469 FieldAlign = CGM.getContext().getTypeAlign(FType);
470 }
471
472 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
473
Devang Patel71f4ff62010-04-21 23:12:37 +0000474 unsigned Flags = 0;
475 AccessSpecifier Access = I->getAccess();
476 if (Access == clang::AS_private)
477 Flags |= llvm::DIType::FlagPrivate;
478 else if (Access == clang::AS_protected)
479 Flags |= llvm::DIType::FlagProtected;
480
Devang Patel428deb52010-01-19 00:00:59 +0000481 // Create a DW_TAG_member node to remember the offset of this field in the
482 // struct. FIXME: This is an absolutely insane way to capture this
483 // information. When we gut debug info, this should be fixed.
484 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
485 FieldName, FieldDefUnit,
486 FieldLine, FieldSize, FieldAlign,
Devang Patel71f4ff62010-04-21 23:12:37 +0000487 FieldOffset, Flags, FieldTy);
Devang Patel428deb52010-01-19 00:00:59 +0000488 EltTys.push_back(FieldTy);
489 }
490}
491
Devang Patela6da1922010-01-28 00:28:01 +0000492/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
493/// function type is not updated to include implicit "this" pointer. Use this
494/// routine to get a method type which includes "this" pointer.
495llvm::DIType
496CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000497 llvm::DIFile Unit) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +0000498 llvm::DIType FnTy
499 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
500 0),
501 Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000502
503 // Static methods do not need "this" pointer argument.
504 if (Method->isStatic())
505 return FnTy;
506
Devang Patela6da1922010-01-28 00:28:01 +0000507 // Add "this" pointer.
508
Devang Patelab699792010-05-07 18:12:35 +0000509 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patela6da1922010-01-28 00:28:01 +0000510 assert (Args.getNumElements() && "Invalid number of arguments!");
511
512 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
513
514 // First element is always return type. For 'void' functions it is NULL.
515 Elts.push_back(Args.getElement(0));
516
517 // "this" pointer is always first argument.
518 ASTContext &Context = CGM.getContext();
519 QualType ThisPtr =
520 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
Devang Patel337472d2010-02-09 17:57:50 +0000521 llvm::DIType ThisPtrType =
522 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
Devang Patelab699792010-05-07 18:12:35 +0000523 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
Devang Patel337472d2010-02-09 17:57:50 +0000524 Elts.push_back(ThisPtrType);
Devang Patela6da1922010-01-28 00:28:01 +0000525
526 // Copy rest of the arguments.
527 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
528 Elts.push_back(Args.getElement(i));
529
530 llvm::DIArray EltTypeArray =
531 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
532
533 return
534 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000535 Unit, "", Unit,
Devang Patela6da1922010-01-28 00:28:01 +0000536 0, 0, 0, 0, 0,
537 llvm::DIType(), EltTypeArray);
538}
539
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000540/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
541/// a single member function GlobalDecl.
542llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000543CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000544 llvm::DIFile Unit,
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000545 llvm::DICompositeType &RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000546 bool IsCtorOrDtor =
547 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
548
549 llvm::StringRef MethodName = getFunctionName(Method);
Devang Patela6da1922010-01-28 00:28:01 +0000550 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000551
552 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
553 // make sense to give a single ctor/dtor a linkage name.
John McCallf746aa62010-03-19 23:29:14 +0000554 MangleBuffer MethodLinkageName;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000555 if (!IsCtorOrDtor)
John McCallf746aa62010-03-19 23:29:14 +0000556 CGM.getMangledName(MethodLinkageName, Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000557
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000558 SourceManager &SM = CGM.getContext().getSourceManager();
559
560 // Get the location for the method.
561 SourceLocation MethodDefLoc = Method->getLocation();
562 PresumedLoc PLoc = SM.getPresumedLoc(MethodDefLoc);
Devang Patel17800552010-03-09 00:44:50 +0000563 llvm::DIFile MethodDefUnit;
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000564 unsigned MethodLine = 0;
565
566 if (!PLoc.isInvalid()) {
Devang Patel17800552010-03-09 00:44:50 +0000567 MethodDefUnit = getOrCreateFile(MethodDefLoc);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000568 MethodLine = PLoc.getLine();
569 }
570
571 // Collect virtual method info.
572 llvm::DIType ContainingType;
573 unsigned Virtuality = 0;
574 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000575
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000576 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000577 if (Method->isPure())
578 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
579 else
580 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
581
582 // It doesn't make sense to give a virtual destructor a vtable index,
583 // since a single destructor has two entries in the vtable.
584 if (!isa<CXXDestructorDecl>(Method))
Anders Carlsson046c2942010-04-17 20:15:18 +0000585 VIndex = CGM.getVTables().getMethodVTableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000586 ContainingType = RecordTy;
587 }
588
589 llvm::DISubprogram SP =
590 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
591 MethodLinkageName,
592 MethodDefUnit, MethodLine,
593 MethodTy, /*isLocalToUnit=*/false,
594 Method->isThisDeclarationADefinition(),
595 Virtuality, VIndex, ContainingType);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000596
597 // Don't cache ctors or dtors since we have to emit multiple functions for
598 // a single ctor or dtor.
599 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
Devang Patelab699792010-05-07 18:12:35 +0000600 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000601
602 return SP;
603}
604
Devang Patel4125fd22010-01-19 01:54:44 +0000605/// CollectCXXMemberFunctions - A helper function to collect debug info for
606/// C++ member functions.This is used while creating debug info entry for
607/// a Record.
608void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000609CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000610 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
611 llvm::DICompositeType &RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000612 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
613 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000614 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000615
Devang Pateld5322da2010-02-09 19:09:28 +0000616 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000617 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000618
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000619 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000620 }
621}
622
Devang Patela245c5b2010-01-25 23:32:18 +0000623/// CollectCXXBases - A helper function to collect debug info for
624/// C++ base classes. This is used while creating debug info entry for
625/// a Record.
626void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000627CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000628 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
629 llvm::DICompositeType &RecordTy) {
630
Devang Patel239cec62010-02-01 21:39:52 +0000631 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
632 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
633 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000634 unsigned BFlags = 0;
635 uint64_t BaseOffset;
636
637 const CXXRecordDecl *Base =
638 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
639
640 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000641 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000642 // expression where it expects +ve number.
Anders Carlssonaf440352010-03-23 04:11:45 +0000643 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base);
Devang Patelca7daed2010-01-28 21:54:15 +0000644 BFlags = llvm::DIType::FlagVirtual;
645 } else
646 BaseOffset = RL.getBaseClassOffset(Base);
647
648 AccessSpecifier Access = BI->getAccessSpecifier();
649 if (Access == clang::AS_private)
650 BFlags |= llvm::DIType::FlagPrivate;
651 else if (Access == clang::AS_protected)
652 BFlags |= llvm::DIType::FlagProtected;
653
654 llvm::DIType DTy =
655 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
656 RecordTy, llvm::StringRef(),
Devang Pateld58562e2010-03-09 22:49:11 +0000657 Unit, 0, 0, 0,
Devang Patelca7daed2010-01-28 21:54:15 +0000658 BaseOffset, BFlags,
659 getOrCreateType(BI->getType(),
660 Unit));
661 EltTys.push_back(DTy);
662 }
Devang Patela245c5b2010-01-25 23:32:18 +0000663}
664
Devang Patel4ce3f202010-01-28 18:11:52 +0000665/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000666llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000667 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000668 return VTablePtrType;
669
670 ASTContext &Context = CGM.getContext();
671
672 /* Function type */
Benjamin Kramerad468862010-03-30 11:36:44 +0000673 llvm::DIDescriptor STy = getOrCreateType(Context.IntTy, Unit);
674 llvm::DIArray SElements = DebugFactory.GetOrCreateArray(&STy, 1);
Devang Patel4ce3f202010-01-28 18:11:52 +0000675 llvm::DIType SubTy =
676 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000677 Unit, "", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000678 0, 0, 0, 0, 0, llvm::DIType(), SElements);
679
680 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
681 llvm::DIType vtbl_ptr_type
682 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000683 Unit, "__vtbl_ptr_type", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000684 0, Size, 0, 0, 0, SubTy);
685
Devang Pateld58562e2010-03-09 22:49:11 +0000686 VTablePtrType =
687 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
688 Unit, "", Unit,
689 0, Size, 0, 0, 0, vtbl_ptr_type);
Devang Patel4ce3f202010-01-28 18:11:52 +0000690 return VTablePtrType;
691}
692
Anders Carlsson046c2942010-04-17 20:15:18 +0000693/// getVTableName - Get vtable name for the given Class.
694llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000695 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000696 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000697
698 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000699 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000700 memcpy(StrPtr, Name.data(), Name.length());
701 return llvm::StringRef(StrPtr, Name.length());
702}
703
704
Anders Carlsson046c2942010-04-17 20:15:18 +0000705/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel4ce3f202010-01-28 18:11:52 +0000706/// debug info entry in EltTys vector.
707void CGDebugInfo::
Anders Carlsson046c2942010-04-17 20:15:18 +0000708CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000709 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000710 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000711
712 // If there is a primary base then it will hold vtable info.
713 if (RL.getPrimaryBase())
714 return;
715
716 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000717 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000718 return;
719
720 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
721 llvm::DIType VPTR
722 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Anders Carlsson046c2942010-04-17 20:15:18 +0000723 getVTableName(RD), Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000724 0, Size, 0, 0, 0,
725 getOrCreateVTablePtrType(Unit));
726 EltTys.push_back(VPTR);
727}
728
Devang Patel65e99f22009-02-25 01:36:11 +0000729/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000730llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000731 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000732 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000733
Chris Lattner9c85ba32008-11-10 06:08:34 +0000734 unsigned Tag;
Devang Pateld6c5a262010-02-01 21:52:22 +0000735 if (RD->isStruct())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000736 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Pateld6c5a262010-02-01 21:52:22 +0000737 else if (RD->isUnion())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000738 Tag = llvm::dwarf::DW_TAG_union_type;
739 else {
Devang Pateld6c5a262010-02-01 21:52:22 +0000740 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000741 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000742 }
743
Anders Carlsson20f12a22009-12-06 18:00:51 +0000744 SourceManager &SM = CGM.getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000745
Chris Lattner9c85ba32008-11-10 06:08:34 +0000746 // Get overall information about the record type for the debug info.
Devang Pateld6c5a262010-02-01 21:52:22 +0000747 PresumedLoc PLoc = SM.getPresumedLoc(RD->getLocation());
Devang Patel17800552010-03-09 00:44:50 +0000748 llvm::DIFile DefUnit;
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000749 unsigned Line = 0;
750 if (!PLoc.isInvalid()) {
Devang Patel17800552010-03-09 00:44:50 +0000751 DefUnit = getOrCreateFile(RD->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000752 Line = PLoc.getLine();
753 }
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Chris Lattner9c85ba32008-11-10 06:08:34 +0000755 // Records and classes and unions can all be recursive. To handle them, we
756 // first generate a debug descriptor for the struct as a forward declaration.
757 // Then (if it is a definition) we go through and get debug info for all of
758 // its members. Finally, we create a descriptor for the complete type (which
759 // may refer to the forward decl if the struct is recursive) and replace all
760 // uses of the forward declaration with the final definition.
Devang Pateld0f251b2010-01-20 23:56:40 +0000761
Devang Pateld6c5a262010-02-01 21:52:22 +0000762 // A RD->getName() is not unique. However, the debug info descriptors
Devang Patelce78c972010-02-01 22:51:29 +0000763 // are uniqued so use type name to ensure uniquness.
Benjamin Kramerfea3d4d2010-03-13 12:06:51 +0000764 llvm::SmallString<128> FwdDeclName;
765 llvm::raw_svector_ostream(FwdDeclName) << "fwd.type." << FwdDeclCount++;
Devang Patel411894b2010-02-01 22:40:08 +0000766 llvm::DIDescriptor FDContext =
767 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel0ce73f62009-07-22 18:57:00 +0000768 llvm::DICompositeType FwdDecl =
Devang Patel7573f8b2010-03-09 21:32:27 +0000769 DebugFactory.CreateCompositeType(Tag, FDContext, FwdDeclName,
Devang Patelab71ff52009-11-12 00:51:46 +0000770 DefUnit, Line, 0, 0, 0, 0,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000771 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000772
Chris Lattner9c85ba32008-11-10 06:08:34 +0000773 // If this is just a forward declaration, return it.
Douglas Gregor952b0172010-02-11 01:04:33 +0000774 if (!RD->getDefinition())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000775 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000776
Devang Patelab699792010-05-07 18:12:35 +0000777 llvm::MDNode *MN = FwdDecl;
778 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000779 // Otherwise, insert it into the TypeCache so that recursive uses will find
780 // it.
Devang Patelab699792010-05-07 18:12:35 +0000781 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000782 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000783 RegionStack.push_back(FwdDeclNode);
784 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000785
786 // Convert all the elements.
787 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
788
Devang Pateld6c5a262010-02-01 21:52:22 +0000789 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000790 if (CXXDecl) {
791 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson046c2942010-04-17 20:15:18 +0000792 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000793 }
Devang Pateld6c5a262010-02-01 21:52:22 +0000794 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +0000795 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +0000796 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +0000797 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +0000798
799 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +0000800 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel0ac8f312010-01-28 00:54:21 +0000801 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
802 ContainingType =
Devang Patelab699792010-05-07 18:12:35 +0000803 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patel0ac8f312010-01-28 00:54:21 +0000804 else if (CXXDecl->isDynamicClass())
Devang Patelab699792010-05-07 18:12:35 +0000805 ContainingType = FwdDecl;
Devang Patela245c5b2010-01-25 23:32:18 +0000806 }
Mike Stump1eb44332009-09-09 15:08:12 +0000807
Chris Lattner9c85ba32008-11-10 06:08:34 +0000808 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000809 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000810
811 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000812 uint64_t Size = CGM.getContext().getTypeSize(Ty);
813 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000814
Devang Patele4c1ea02010-03-11 20:01:48 +0000815 RegionStack.pop_back();
816 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
817 RegionMap.find(Ty->getDecl());
818 if (RI != RegionMap.end())
819 RegionMap.erase(RI);
820
Devang Patel411894b2010-02-01 22:40:08 +0000821 llvm::DIDescriptor RDContext =
822 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel0ce73f62009-07-22 18:57:00 +0000823 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +0000824 DebugFactory.CreateCompositeType(Tag, RDContext,
825 RD->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000826 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +0000827 llvm::DIType(), Elements,
828 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000829
830 // Now that we have a real decl for the struct, replace anything using the
831 // old decl with the new one. This will recursively update the debug info.
Eli Friedman14d63652009-11-16 21:04:30 +0000832 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +0000833 RegionMap[RD] = llvm::WeakVH(RealDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000834 return RealDecl;
835}
836
Devang Patel9ca36b62009-02-26 21:10:26 +0000837/// CreateType - get objective-c interface type.
838llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000839 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000840 ObjCInterfaceDecl *ID = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000841
Devang Patel9ca36b62009-02-26 21:10:26 +0000842 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000843 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel9ca36b62009-02-26 21:10:26 +0000844
845 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +0000846 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Pateld6c5a262010-02-01 21:52:22 +0000847 PresumedLoc PLoc = SM.getPresumedLoc(ID->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000848 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
849
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Devang Patel17800552010-03-09 00:44:50 +0000851 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000852
Devang Patel9ca36b62009-02-26 21:10:26 +0000853 // To handle recursive interface, we
854 // first generate a debug descriptor for the struct as a forward declaration.
855 // Then (if it is a definition) we go through and get debug info for all of
856 // its members. Finally, we create a descriptor for the complete type (which
857 // may refer to the forward decl if the struct is recursive) and replace all
858 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000859 llvm::DICompositeType FwdDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +0000860 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000861 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000862 llvm::DIType(), llvm::DIArray(),
863 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000864
Devang Patel9ca36b62009-02-26 21:10:26 +0000865 // If this is just a forward declaration, return it.
Devang Pateld6c5a262010-02-01 21:52:22 +0000866 if (ID->isForwardDecl())
Devang Patel9ca36b62009-02-26 21:10:26 +0000867 return FwdDecl;
868
Devang Patelab699792010-05-07 18:12:35 +0000869 llvm::MDNode *MN = FwdDecl;
870 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patel9ca36b62009-02-26 21:10:26 +0000871 // Otherwise, insert it into the TypeCache so that recursive uses will find
872 // it.
Devang Patelab699792010-05-07 18:12:35 +0000873 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000874 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000875 RegionStack.push_back(FwdDeclNode);
876 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000877
878 // Convert all the elements.
879 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
880
Devang Pateld6c5a262010-02-01 21:52:22 +0000881 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +0000882 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000883 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000884 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000885 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000886 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Pateld58562e2010-03-09 22:49:11 +0000887 Unit, "", Unit, 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000888 0 /* offset */, 0, SClassTy);
889 EltTys.push_back(InhTag);
890 }
891
Devang Pateld6c5a262010-02-01 21:52:22 +0000892 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +0000893
894 unsigned FieldNo = 0;
Devang Pateld6c5a262010-02-01 21:52:22 +0000895 for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(),
896 E = ID->ivar_end(); I != E; ++I, ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +0000897 ObjCIvarDecl *Field = *I;
898 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
899
Devang Patel73621622009-11-25 17:37:31 +0000900 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +0000901
Devang Patelde135022009-04-27 22:40:36 +0000902 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +0000903 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +0000904 continue;
905
Devang Patel9ca36b62009-02-26 21:10:26 +0000906 // Get the location for the field.
907 SourceLocation FieldDefLoc = Field->getLocation();
Devang Patel17800552010-03-09 00:44:50 +0000908 llvm::DIFile FieldDefUnit = getOrCreateFile(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000909 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
910 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
911
Mike Stump1eb44332009-09-09 15:08:12 +0000912
Devang Patel99c20eb2009-03-20 18:24:39 +0000913 QualType FType = Field->getType();
914 uint64_t FieldSize = 0;
915 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000916
Devang Patel99c20eb2009-03-20 18:24:39 +0000917 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000918
Devang Patel99c20eb2009-03-20 18:24:39 +0000919 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000920 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000921 Expr *BitWidth = Field->getBitWidth();
922 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +0000923 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000924
Anders Carlsson20f12a22009-12-06 18:00:51 +0000925 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000926 }
927
Mike Stump1eb44332009-09-09 15:08:12 +0000928 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
929
Devang Patelc20482b2009-03-19 00:23:53 +0000930 unsigned Flags = 0;
931 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
932 Flags = llvm::DIType::FlagProtected;
933 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
934 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +0000935
Devang Patel9ca36b62009-02-26 21:10:26 +0000936 // Create a DW_TAG_member node to remember the offset of this field in the
937 // struct. FIXME: This is an absolutely insane way to capture this
938 // information. When we gut debug info, this should be fixed.
939 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
940 FieldName, FieldDefUnit,
941 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000942 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000943 EltTys.push_back(FieldTy);
944 }
Mike Stump1eb44332009-09-09 15:08:12 +0000945
Devang Patel9ca36b62009-02-26 21:10:26 +0000946 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000947 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +0000948
Devang Patele4c1ea02010-03-11 20:01:48 +0000949 RegionStack.pop_back();
950 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
951 RegionMap.find(Ty->getDecl());
952 if (RI != RegionMap.end())
953 RegionMap.erase(RI);
954
Devang Patel9ca36b62009-02-26 21:10:26 +0000955 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000956 uint64_t Size = CGM.getContext().getTypeSize(Ty);
957 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000958
Devang Patel6c1fddf2009-07-27 18:42:03 +0000959 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +0000960 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +0000961 Line, Size, Align, 0, 0, llvm::DIType(),
962 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000963
964 // Now that we have a real decl for the struct, replace anything using the
965 // old decl with the new one. This will recursively update the debug info.
Devang Patelffffb032009-11-16 20:09:38 +0000966 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +0000967 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000968
Devang Patel9ca36b62009-02-26 21:10:26 +0000969 return RealDecl;
970}
971
Chris Lattner9c85ba32008-11-10 06:08:34 +0000972llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000973 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000974 EnumDecl *ED = Ty->getDecl();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000975
976 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
977
978 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +0000979 for (EnumDecl::enumerator_iterator
Devang Pateld6c5a262010-02-01 21:52:22 +0000980 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000981 Enum != EnumEnd; ++Enum) {
Devang Patel73621622009-11-25 17:37:31 +0000982 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor44b43212008-12-11 16:49:14 +0000983 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000984 }
Mike Stump1eb44332009-09-09 15:08:12 +0000985
Chris Lattner9c85ba32008-11-10 06:08:34 +0000986 // Return a CompositeType for the enum itself.
987 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000988 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000989
Devang Pateld6c5a262010-02-01 21:52:22 +0000990 SourceLocation DefLoc = ED->getLocation();
Devang Patel17800552010-03-09 00:44:50 +0000991 llvm::DIFile DefUnit = getOrCreateFile(DefLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000992 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000993 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
994 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
995
Mike Stump1eb44332009-09-09 15:08:12 +0000996
Chris Lattner9c85ba32008-11-10 06:08:34 +0000997 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +0000998 uint64_t Size = 0;
999 unsigned Align = 0;
1000 if (!Ty->isIncompleteType()) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001001 Size = CGM.getContext().getTypeSize(Ty);
1002 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman3189e4b2009-05-04 04:39:55 +00001003 }
Mike Stump1eb44332009-09-09 15:08:12 +00001004
Devang Patelca80a5f2009-10-20 19:55:01 +00001005 llvm::DIType DbgTy =
1006 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Pateld6c5a262010-02-01 21:52:22 +00001007 Unit, ED->getName(), DefUnit, Line,
Devang Patelca80a5f2009-10-20 19:55:01 +00001008 Size, Align, 0, 0,
1009 llvm::DIType(), EltArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001010 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001011}
1012
1013llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001014 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001015 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1016 return CreateType(RT, Unit);
1017 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1018 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Chris Lattner9c85ba32008-11-10 06:08:34 +00001020 return llvm::DIType();
1021}
1022
Devang Patel70c23cd2010-02-23 22:59:39 +00001023llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001024 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001025 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1026 uint64_t NumElems = Ty->getNumElements();
1027 if (NumElems > 0)
1028 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001029
Benjamin Kramerad468862010-03-30 11:36:44 +00001030 llvm::DIDescriptor Subscript = DebugFactory.GetOrCreateSubrange(0, NumElems);
1031 llvm::DIArray SubscriptArray = DebugFactory.GetOrCreateArray(&Subscript, 1);
Devang Patel70c23cd2010-02-23 22:59:39 +00001032
1033 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1034 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1035
1036 return
1037 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001038 Unit, "", Unit,
Devang Patel70c23cd2010-02-23 22:59:39 +00001039 0, Size, Align, 0, 0,
1040 ElementTy, SubscriptArray);
1041}
1042
Chris Lattner9c85ba32008-11-10 06:08:34 +00001043llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001044 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001045 uint64_t Size;
1046 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001047
1048
Nuno Lopes010d5142009-01-28 00:35:17 +00001049 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001050 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001051 Size = 0;
1052 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001053 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001054 } else if (Ty->isIncompleteArrayType()) {
1055 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001056 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001057 } else {
1058 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001059 Size = CGM.getContext().getTypeSize(Ty);
1060 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001061 }
Mike Stump1eb44332009-09-09 15:08:12 +00001062
Chris Lattner9c85ba32008-11-10 06:08:34 +00001063 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1064 // interior arrays, do we care? Why aren't nested arrays represented the
1065 // obvious/recursive way?
1066 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1067 QualType EltTy(Ty, 0);
1068 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +00001069 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001070 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +00001071 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +00001072 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001073 // FIXME: Verify this is right for VLAs.
1074 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1075 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +00001076 }
Mike Stump1eb44332009-09-09 15:08:12 +00001077
Chris Lattner9c85ba32008-11-10 06:08:34 +00001078 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001079 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001080
Devang Patelca80a5f2009-10-20 19:55:01 +00001081 llvm::DIType DbgTy =
1082 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001083 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +00001084 0, Size, Align, 0, 0,
1085 getOrCreateType(EltTy, Unit),
1086 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001087 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001088}
1089
Anders Carlssona031b352009-11-06 19:19:55 +00001090llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001091 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001092 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1093 Ty, Ty->getPointeeType(), Unit);
1094}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001095
Anders Carlsson20f12a22009-12-06 18:00:51 +00001096llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001097 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001098 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1099 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1100
1101 if (!Ty->getPointeeType()->isFunctionType()) {
1102 // We have a data member pointer type.
1103 return PointerDiffDITy;
1104 }
1105
1106 // We have a member function pointer type. Treat it as a struct with two
1107 // ptrdiff_t members.
1108 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1109
1110 uint64_t FieldOffset = 0;
1111 llvm::DIDescriptor ElementTypes[2];
1112
1113 // FIXME: This should probably be a function type instead.
1114 ElementTypes[0] =
1115 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001116 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001117 Info.first, Info.second, FieldOffset, 0,
1118 PointerDiffDITy);
1119 FieldOffset += Info.first;
1120
1121 ElementTypes[1] =
1122 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001123 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001124 Info.first, Info.second, FieldOffset, 0,
1125 PointerDiffDITy);
1126
1127 llvm::DIArray Elements =
1128 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1129 llvm::array_lengthof(ElementTypes));
1130
1131 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1132 U, llvm::StringRef("test"),
Devang Pateld58562e2010-03-09 22:49:11 +00001133 U, 0, FieldOffset,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001134 0, 0, 0, llvm::DIType(), Elements);
1135}
1136
Douglas Gregor840943d2009-12-21 20:18:30 +00001137static QualType UnwrapTypeForDebugInfo(QualType T) {
1138 do {
1139 QualType LastT = T;
1140 switch (T->getTypeClass()) {
1141 default:
1142 return T;
1143 case Type::TemplateSpecialization:
1144 T = cast<TemplateSpecializationType>(T)->desugar();
1145 break;
1146 case Type::TypeOfExpr: {
1147 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1148 T = Ty->getUnderlyingExpr()->getType();
1149 break;
1150 }
1151 case Type::TypeOf:
1152 T = cast<TypeOfType>(T)->getUnderlyingType();
1153 break;
1154 case Type::Decltype:
1155 T = cast<DecltypeType>(T)->getUnderlyingType();
1156 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001157 case Type::Elaborated:
1158 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001159 break;
1160 case Type::SubstTemplateTypeParm:
1161 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1162 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001163 }
1164
1165 assert(T != LastT && "Type unwrapping failed to unwrap!");
1166 if (T == LastT)
1167 return T;
1168 } while (true);
1169
1170 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001171}
1172
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001173/// getOrCreateType - Get the type from the cache or create a new
1174/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001175llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001176 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001177 if (Ty.isNull())
1178 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001179
Douglas Gregor840943d2009-12-21 20:18:30 +00001180 // Unwrap the type as needed for debug information.
1181 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001182
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001183 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001184 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001185 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001186 if (it != TypeCache.end()) {
1187 // Verify that the debug info still exists.
1188 if (&*it->second)
1189 return llvm::DIType(cast<llvm::MDNode>(it->second));
1190 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001191
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001192 // Otherwise create the type.
1193 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001194
1195 // And update the type cache.
Devang Patelab699792010-05-07 18:12:35 +00001196 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001197 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001198}
1199
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001200/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001201llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001202 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001203 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001204 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001205 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001206
Douglas Gregor2101a822009-12-21 19:57:21 +00001207 const char *Diag = 0;
1208
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001209 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001210 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001211#define TYPE(Class, Base)
1212#define ABSTRACT_TYPE(Class, Base)
1213#define NON_CANONICAL_TYPE(Class, Base)
1214#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1215#include "clang/AST/TypeNodes.def"
1216 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001217
Anders Carlssonbfe69952009-11-06 18:24:04 +00001218 // FIXME: Handle these.
1219 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001220 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001221
1222 case Type::Vector:
1223 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001224 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001225 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001226 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001227 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1228 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1229 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1230 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001231 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001232 return CreateType(cast<BlockPointerType>(Ty), Unit);
1233 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001234 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001235 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001236 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001237 case Type::FunctionProto:
1238 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001239 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001240 case Type::ConstantArray:
1241 case Type::VariableArray:
1242 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001243 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001244
1245 case Type::LValueReference:
1246 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1247
Anders Carlsson20f12a22009-12-06 18:00:51 +00001248 case Type::MemberPointer:
1249 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001250
1251 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001252 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001253 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001254 case Type::TypeOfExpr:
1255 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001256 case Type::Decltype:
1257 llvm_unreachable("type should have been unwrapped!");
1258 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001259
1260 case Type::RValueReference:
1261 // FIXME: Implement!
1262 Diag = "rvalue references";
1263 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001264 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001265
1266 assert(Diag && "Fall through without a diagnostic?");
1267 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1268 "debug information for %0 is not yet supported");
1269 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1270 << Diag;
1271 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001272}
1273
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001274/// CreateMemberType - Create new member and increase Offset by FType's size.
1275llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
1276 llvm::StringRef Name,
1277 uint64_t *Offset) {
1278 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1279 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1280 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
1281 llvm::DIType Ty = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1282 Unit, Name, Unit, 0,
1283 FieldSize, FieldAlign,
1284 *Offset, 0, FieldTy);
1285 *Offset += FieldSize;
1286 return Ty;
1287}
1288
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001289/// EmitFunctionStart - Constructs the debug code for entering a function -
1290/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001291void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001292 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001293 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001294
Devang Patel9c6c3a02010-01-14 00:36:21 +00001295 llvm::StringRef Name;
John McCallf746aa62010-03-19 23:29:14 +00001296 MangleBuffer LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001297
1298 const Decl *D = GD.getDecl();
1299 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001300 // If there is a DISubprogram for this function available then use it.
1301 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1302 FI = SPCache.find(FD);
1303 if (FI != SPCache.end()) {
Devang Patel0804e6e2010-03-08 20:53:17 +00001304 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
Devang Patelab699792010-05-07 18:12:35 +00001305 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1306 llvm::MDNode *SPN = SP;
1307 RegionStack.push_back(SPN);
1308 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00001309 return;
1310 }
1311 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001312 Name = getFunctionName(FD);
1313 // Use mangled name as linkage name for c/c++ functions.
John McCallf746aa62010-03-19 23:29:14 +00001314 CGM.getMangledName(LinkageName, GD);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001315 } else {
1316 // Use llvm function name as linkage name.
1317 Name = Fn->getName();
John McCallf746aa62010-03-19 23:29:14 +00001318 LinkageName.setString(Name);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001319 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001320 if (!Name.empty() && Name[0] == '\01')
1321 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001322
Devang Patel970c6182010-04-24 00:49:16 +00001323 // It is expected that CurLoc is set before using EmitFunctionStart.
1324 // Usually, CurLoc points to the left bracket location of compound
1325 // statement representing function body.
1326 llvm::DIFile Unit = getOrCreateFile(CurLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001327 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +00001328 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Mike Stump1eb44332009-09-09 15:08:12 +00001329
Chris Lattner9c85ba32008-11-10 06:08:34 +00001330 llvm::DISubprogram SP =
Devang Patel970c6182010-04-24 00:49:16 +00001331 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001332 getOrCreateType(FnType, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001333 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump1eb44332009-09-09 15:08:12 +00001334
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001335 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001336 llvm::MDNode *SPN = SP;
1337 RegionStack.push_back(SPN);
1338 RegionMap[D] = llvm::WeakVH(SP);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001339}
1340
1341
Chris Lattner9c85ba32008-11-10 06:08:34 +00001342void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001343 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001344
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001345 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001346 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001347 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001348 || (SM.getInstantiationLineNumber(CurLoc) ==
1349 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001350 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patel4800ea62010-04-05 21:09:15 +00001351 // New Builder may not be in sync with CGDebugInfo.
1352 if (!Builder.getCurrentDebugLocation().isUnknown())
1353 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001354
1355 // Update last state.
1356 PrevLoc = CurLoc;
1357
1358 // Get the appropriate compile unit.
Devang Patel17800552010-03-09 00:44:50 +00001359 llvm::DIFile Unit = getOrCreateFile(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +00001360 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patelbbd9fa42009-10-06 18:36:08 +00001361
Chris Lattnerc6034632010-04-01 06:31:43 +00001362 llvm::MDNode *Scope = RegionStack.back();
Chris Lattnere541d012010-04-02 20:21:43 +00001363 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(PLoc.getLine(),
1364 PLoc.getColumn(),
1365 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001366}
1367
1368/// EmitRegionStart- Constructs the debug code for entering a declarative
1369/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +00001370void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
Devang Pateld19429f2010-02-16 21:41:20 +00001371 SourceManager &SM = CGM.getContext().getSourceManager();
1372 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patel8fae0602009-11-13 19:10:24 +00001373 llvm::DIDescriptor D =
1374 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1375 llvm::DIDescriptor() :
Devang Pateld19429f2010-02-16 21:41:20 +00001376 llvm::DIDescriptor(RegionStack.back()),
1377 PLoc.getLine(), PLoc.getColumn());
Devang Patelab699792010-05-07 18:12:35 +00001378 llvm::MDNode *DN = D;
1379 RegionStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001380}
1381
1382/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1383/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +00001384void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001385 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1386
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001387 // Provide an region stop point.
1388 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001389
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001390 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001391}
1392
Devang Patel809b9bb2010-02-10 18:49:08 +00001393// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1394// See BuildByRefType.
1395llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1396 uint64_t *XOffset) {
1397
1398 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1399
1400 QualType FType;
1401 uint64_t FieldSize, FieldOffset;
1402 unsigned FieldAlign;
1403
Devang Patel17800552010-03-09 00:44:50 +00001404 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001405 QualType Type = VD->getType();
1406
1407 FieldOffset = 0;
1408 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001409 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1410 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001411 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001412 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1413 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1414
Devang Patel809b9bb2010-02-10 18:49:08 +00001415 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1416 if (HasCopyAndDispose) {
1417 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001418 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1419 &FieldOffset));
1420 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1421 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001422 }
1423
1424 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1425 if (Align > CharUnits::fromQuantity(
1426 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1427 unsigned AlignedOffsetInBytes
1428 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1429 unsigned NumPaddingBytes
1430 = AlignedOffsetInBytes - FieldOffset/8;
1431
1432 if (NumPaddingBytes > 0) {
1433 llvm::APInt pad(32, NumPaddingBytes);
1434 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1435 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001436 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001437 }
1438 }
1439
1440 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001441 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00001442 FieldSize = CGM.getContext().getTypeSize(FType);
1443 FieldAlign = Align.getQuantity()*8;
1444
1445 *XOffset = FieldOffset;
1446 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001447 VD->getName(), Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001448 0, FieldSize, FieldAlign,
1449 FieldOffset, 0, FieldTy);
1450 EltTys.push_back(FieldTy);
1451 FieldOffset += FieldSize;
1452
1453 llvm::DIArray Elements =
1454 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1455
1456 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1457
1458 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001459 Unit, "", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001460 0, FieldOffset, 0, 0, Flags,
1461 llvm::DIType(), Elements);
1462
1463}
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001464/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001465void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001466 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001467 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1468
Devang Patel17800552010-03-09 00:44:50 +00001469 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001470 llvm::DIType Ty;
1471 uint64_t XOffset = 0;
1472 if (VD->hasAttr<BlocksAttr>())
1473 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1474 else
1475 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001476
Devang Patelf4e54a22010-05-07 23:05:55 +00001477 // If there is not any debug info for type then do not emit debug info
1478 // for this variable.
1479 if (!Ty)
1480 return;
1481
Chris Lattner9c85ba32008-11-10 06:08:34 +00001482 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001483 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel239cec62010-02-01 21:39:52 +00001484 PresumedLoc PLoc = SM.getPresumedLoc(VD->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +00001485 unsigned Line = 0;
Eli Friedman1468ac72009-11-16 20:33:31 +00001486 unsigned Column = 0;
Devang Pateld263e7b2010-02-10 01:09:50 +00001487 if (PLoc.isInvalid())
1488 PLoc = SM.getPresumedLoc(CurLoc);
1489 if (PLoc.isValid()) {
Chris Lattner650cea92009-05-05 04:57:08 +00001490 Line = PLoc.getLine();
Eli Friedman1468ac72009-11-16 20:33:31 +00001491 Column = PLoc.getColumn();
Devang Patel17800552010-03-09 00:44:50 +00001492 Unit = getOrCreateFile(CurLoc);
Eli Friedman1468ac72009-11-16 20:33:31 +00001493 } else {
Devang Patel17800552010-03-09 00:44:50 +00001494 Unit = llvm::DIFile();
Eli Friedman1468ac72009-11-16 20:33:31 +00001495 }
Mike Stump1eb44332009-09-09 15:08:12 +00001496
Chris Lattner9c85ba32008-11-10 06:08:34 +00001497 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001498 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001499 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel239cec62010-02-01 21:39:52 +00001500 VD->getName(),
Chris Lattner650cea92009-05-05 04:57:08 +00001501 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001502 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001503 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001504 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001505
Chris Lattnerc6034632010-04-01 06:31:43 +00001506 llvm::MDNode *Scope = RegionStack.back();
Chris Lattnere541d012010-04-02 20:21:43 +00001507 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001508}
1509
Mike Stumpb1a6e682009-09-30 02:43:10 +00001510/// EmitDeclare - Emit local variable declaration debug info.
1511void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1512 llvm::Value *Storage, CGBuilderTy &Builder,
1513 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001514 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001515 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1516
Devang Patel2b594b92010-04-26 23:28:46 +00001517 if (Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001518 return;
1519
1520 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001521 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001522 llvm::DIType Ty;
1523 if (VD->hasAttr<BlocksAttr>())
1524 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1525 else
1526 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001527
1528 // Get location information.
Devang Patelf8e10a52010-05-10 23:48:38 +00001529 unsigned Line = 0;
1530 unsigned Column = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001531 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateld6c5a262010-02-01 21:52:22 +00001532 PresumedLoc PLoc = SM.getPresumedLoc(VD->getLocation());
Devang Patelf8e10a52010-05-10 23:48:38 +00001533 if (PLoc.isInvalid())
1534 // If variable location is invalid then try current location.
1535 PLoc = SM.getPresumedLoc(CurLoc);
1536 if (!PLoc.isInvalid()) {
Mike Stumpb1a6e682009-09-30 02:43:10 +00001537 Line = PLoc.getLine();
Devang Patelf8e10a52010-05-10 23:48:38 +00001538 Column = PLoc.getColumn();
1539 }
1540 else {
1541 // If current location is also invalid, then use main compile unit.
Devang Patel258cf272010-05-10 17:24:58 +00001542 Unit = getOrCreateFile(CurLoc);
Devang Patelf8e10a52010-05-10 23:48:38 +00001543 }
Mike Stumpb1a6e682009-09-30 02:43:10 +00001544
Devang Pateld6c5a262010-02-01 21:52:22 +00001545 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001546 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001547 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1548 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1549 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1550 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001551 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001552 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1553 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001554 // offset of __forwarding field
1555 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001556 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1557 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1558 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001559 // offset of x field
1560 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001561 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001562 }
1563
1564 // Create the descriptor for the variable.
1565 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001566 DebugFactory.CreateComplexVariable(Tag,
1567 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001568 VD->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001569 addr);
1570 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001571 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001572 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattnerd5b89022009-12-28 21:44:41 +00001573
Chris Lattnerc6034632010-04-01 06:31:43 +00001574 llvm::MDNode *Scope = RegionStack.back();
Devang Patelf8e10a52010-05-10 23:48:38 +00001575 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001576}
1577
Devang Pateld6c5a262010-02-01 21:52:22 +00001578void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001579 llvm::Value *Storage,
1580 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001581 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001582}
1583
Mike Stumpb1a6e682009-09-30 02:43:10 +00001584void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1585 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1586 CodeGenFunction *CGF) {
1587 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1588}
1589
Chris Lattner9c85ba32008-11-10 06:08:34 +00001590/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1591/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001592void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001593 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001594 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001595}
1596
1597
1598
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001599/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001600void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001601 const VarDecl *D) {
1602
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001603 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001604 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001605 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001606 PresumedLoc PLoc = SM.getPresumedLoc(D->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +00001607 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001608
Devang Pateleb6d79b2010-02-01 21:34:11 +00001609 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001610 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001611
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001612 // CodeGen turns int[] into int[1] so we'll do the same here.
1613 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001614
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001615 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001616 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001617
Anders Carlsson20f12a22009-12-06 18:00:51 +00001618 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001619 ArrayType::Normal, 0);
1620 }
Devang Patel5d822f02010-04-29 17:48:37 +00001621 llvm::StringRef DeclName = D->getName();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001622 llvm::DIDescriptor DContext =
1623 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
1624 DebugFactory.CreateGlobalVariable(DContext, DeclName,
Devang Patel33583052010-01-28 23:15:27 +00001625 DeclName, llvm::StringRef(), Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001626 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001627 Var->hasInternalLinkage(),
1628 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001629}
1630
Devang Patel9ca36b62009-02-26 21:10:26 +00001631/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001632void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001633 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001634 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001635 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001636 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateld6c5a262010-02-01 21:52:22 +00001637 PresumedLoc PLoc = SM.getPresumedLoc(ID->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +00001638 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +00001639
Devang Pateld6c5a262010-02-01 21:52:22 +00001640 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001641
Devang Pateld6c5a262010-02-01 21:52:22 +00001642 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001643 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001644
Devang Patel9ca36b62009-02-26 21:10:26 +00001645 // CodeGen turns int[] into int[1] so we'll do the same here.
1646 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001647
Devang Patel9ca36b62009-02-26 21:10:26 +00001648 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001649 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001650
Anders Carlsson20f12a22009-12-06 18:00:51 +00001651 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001652 ArrayType::Normal, 0);
1653 }
1654
Devang Patelf6a39b72009-10-20 18:26:30 +00001655 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001656 getOrCreateType(T, Unit),
1657 Var->hasInternalLinkage(),
1658 true/*definition*/, Var);
1659}
Devang Patelabb485f2010-02-01 19:16:32 +00001660
1661/// getOrCreateNamesSpace - Return namespace descriptor for the given
1662/// namespace decl.
1663llvm::DINameSpace
1664CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1665 llvm::DIDescriptor Unit) {
1666 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1667 NameSpaceCache.find(NSDecl);
1668 if (I != NameSpaceCache.end())
1669 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1670
1671 SourceManager &SM = CGM.getContext().getSourceManager();
1672 PresumedLoc PLoc = SM.getPresumedLoc(NSDecl->getLocation());
1673 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
1674
1675 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00001676 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00001677 llvm::DINameSpace NS =
1678 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
Devang Patelab699792010-05-07 18:12:35 +00001679 llvm::DIFile(Unit), LineNo);
1680 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00001681 return NS;
1682}