blob: 1fdb2e5c20db5a648138c2a4f0b317138959b4cd [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
117 DIFileCache[fname] = F.getNode();
118 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;
Mike Stump9bc093c2009-05-14 02:03:51 +0000323 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000324 FieldSize = CGM.getContext().getTypeSize(FType);
325 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000326 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000327 "reserved", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000328 0, FieldSize, FieldAlign,
329 FieldOffset, 0, FieldTy);
330 EltTys.push_back(FieldTy);
331
332 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000333 FType = CGM.getContext().UnsignedLongTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000334 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000335 FieldSize = CGM.getContext().getTypeSize(FType);
336 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000337 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000338 "Size", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000339 0, FieldSize, FieldAlign,
340 FieldOffset, 0, FieldTy);
341 EltTys.push_back(FieldTy);
342
343 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000344 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000345 EltTys.clear();
346
Mike Stump3d363c52009-10-02 02:30:50 +0000347 unsigned Flags = llvm::DIType::FlagAppleBlock;
348
Mike Stump9bc093c2009-05-14 02:03:51 +0000349 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Devang Pateld58562e2010-03-09 22:49:11 +0000350 Unit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000351 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Mike Stump9bc093c2009-05-14 02:03:51 +0000353 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000354 uint64_t Size = CGM.getContext().getTypeSize(Ty);
355 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Mike Stump9bc093c2009-05-14 02:03:51 +0000357 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000358 Unit, "", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000359 0, Size, Align, 0, 0, EltTy);
360
361 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000362 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000363 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000364 FieldSize = CGM.getContext().getTypeSize(FType);
365 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000366 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000367 "__isa", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000368 0, FieldSize, FieldAlign,
369 FieldOffset, 0, FieldTy);
370 EltTys.push_back(FieldTy);
371
372 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000373 FType = CGM.getContext().IntTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000374 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000375 FieldSize = CGM.getContext().getTypeSize(FType);
376 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000377 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000378 "__flags", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000379 0, FieldSize, FieldAlign,
380 FieldOffset, 0, FieldTy);
381 EltTys.push_back(FieldTy);
382
383 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000384 FType = CGM.getContext().IntTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000385 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000386 FieldSize = CGM.getContext().getTypeSize(FType);
387 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000388 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000389 "__reserved", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000390 0, FieldSize, FieldAlign,
391 FieldOffset, 0, FieldTy);
392 EltTys.push_back(FieldTy);
393
394 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000395 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000396 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000397 FieldSize = CGM.getContext().getTypeSize(FType);
398 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000399 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000400 "__FuncPtr", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000401 0, FieldSize, FieldAlign,
402 FieldOffset, 0, FieldTy);
403 EltTys.push_back(FieldTy);
404
405 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000406 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000407 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000408 FieldSize = CGM.getContext().getTypeSize(Ty);
409 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000410 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000411 "__descriptor", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000412 0, FieldSize, FieldAlign,
413 FieldOffset, 0, FieldTy);
414 EltTys.push_back(FieldTy);
415
416 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000417 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000418
419 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Devang Pateld58562e2010-03-09 22:49:11 +0000420 Unit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000421 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Mike Stump9bc093c2009-05-14 02:03:51 +0000423 BlockLiteralGenericSet = true;
424 BlockLiteralGeneric
425 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000426 "", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000427 0, Size, Align, 0, 0, EltTy);
428 return BlockLiteralGeneric;
429}
430
Chris Lattner9c85ba32008-11-10 06:08:34 +0000431llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000432 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000433 // Typedefs are derived from some other type. If we have a typedef of a
434 // typedef, make sure to emit the whole chain.
435 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Chris Lattner9c85ba32008-11-10 06:08:34 +0000437 // We don't set size information, but do specify where the typedef was
438 // declared.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000439 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateld5289052010-01-29 22:29:31 +0000440 PresumedLoc PLoc = SM.getPresumedLoc(Ty->getDecl()->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000441 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000442
Devang Pateleb6d79b2010-02-01 21:34:11 +0000443 llvm::DIDescriptor TyContext
444 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
445 Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000446 llvm::DIType DbgTy =
Devang Pateld5289052010-01-29 22:29:31 +0000447 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Pateleb6d79b2010-02-01 21:34:11 +0000448 TyContext,
Devang Pateld5289052010-01-29 22:29:31 +0000449 Ty->getDecl()->getName(), Unit,
450 Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000451 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000452}
453
Chris Lattner9c85ba32008-11-10 06:08:34 +0000454llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000455 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000456 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000457
Chris Lattner9c85ba32008-11-10 06:08:34 +0000458 // Add the result type at least.
459 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Chris Lattner9c85ba32008-11-10 06:08:34 +0000461 // Set up remainder of arguments if there is a prototype.
462 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000463 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000464 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
465 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
466 } else {
467 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000468 }
469
Chris Lattner9c85ba32008-11-10 06:08:34 +0000470 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000471 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Devang Patelca80a5f2009-10-20 19:55:01 +0000473 llvm::DIType DbgTy =
474 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000475 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000476 0, 0, 0, 0, 0,
477 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000478 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000479}
480
Devang Patel428deb52010-01-19 00:00:59 +0000481/// CollectRecordFields - A helper function to collect debug info for
482/// record fields. This is used while creating debug info entry for a Record.
483void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000484CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit,
Devang Patel428deb52010-01-19 00:00:59 +0000485 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
486 unsigned FieldNo = 0;
487 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel239cec62010-02-01 21:39:52 +0000488 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
489 for (RecordDecl::field_iterator I = RD->field_begin(),
490 E = RD->field_end();
Devang Patel428deb52010-01-19 00:00:59 +0000491 I != E; ++I, ++FieldNo) {
492 FieldDecl *Field = *I;
493 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
494
495 llvm::StringRef FieldName = Field->getName();
496
Devang Patel4835fdd2010-02-12 01:31:06 +0000497 // Ignore unnamed fields. Do not ignore unnamed records.
498 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel428deb52010-01-19 00:00:59 +0000499 continue;
500
501 // Get the location for the field.
502 SourceLocation FieldDefLoc = Field->getLocation();
503 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
Devang Patel17800552010-03-09 00:44:50 +0000504 llvm::DIFile FieldDefUnit;
Devang Patel428deb52010-01-19 00:00:59 +0000505 unsigned FieldLine = 0;
506
507 if (!PLoc.isInvalid()) {
Devang Patel17800552010-03-09 00:44:50 +0000508 FieldDefUnit = getOrCreateFile(FieldDefLoc);
Devang Patel428deb52010-01-19 00:00:59 +0000509 FieldLine = PLoc.getLine();
510 }
511
512 QualType FType = Field->getType();
513 uint64_t FieldSize = 0;
514 unsigned FieldAlign = 0;
515 if (!FType->isIncompleteArrayType()) {
516
517 // Bit size, align and offset of the type.
518 FieldSize = CGM.getContext().getTypeSize(FType);
519 Expr *BitWidth = Field->getBitWidth();
520 if (BitWidth)
521 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
522
523 FieldAlign = CGM.getContext().getTypeAlign(FType);
524 }
525
526 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
527
528 // Create a DW_TAG_member node to remember the offset of this field in the
529 // struct. FIXME: This is an absolutely insane way to capture this
530 // information. When we gut debug info, this should be fixed.
531 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
532 FieldName, FieldDefUnit,
533 FieldLine, FieldSize, FieldAlign,
534 FieldOffset, 0, FieldTy);
535 EltTys.push_back(FieldTy);
536 }
537}
538
Devang Patela6da1922010-01-28 00:28:01 +0000539/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
540/// function type is not updated to include implicit "this" pointer. Use this
541/// routine to get a method type which includes "this" pointer.
542llvm::DIType
543CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000544 llvm::DIFile Unit) {
Devang Patela6da1922010-01-28 00:28:01 +0000545 llvm::DIType FnTy = getOrCreateType(Method->getType(), Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000546
547 // Static methods do not need "this" pointer argument.
548 if (Method->isStatic())
549 return FnTy;
550
Devang Patela6da1922010-01-28 00:28:01 +0000551 // Add "this" pointer.
552
553 llvm::DIArray Args = llvm::DICompositeType(FnTy.getNode()).getTypeArray();
554 assert (Args.getNumElements() && "Invalid number of arguments!");
555
556 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
557
558 // First element is always return type. For 'void' functions it is NULL.
559 Elts.push_back(Args.getElement(0));
560
561 // "this" pointer is always first argument.
562 ASTContext &Context = CGM.getContext();
563 QualType ThisPtr =
564 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
Devang Patel337472d2010-02-09 17:57:50 +0000565 llvm::DIType ThisPtrType =
566 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
567 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType.getNode();
568 Elts.push_back(ThisPtrType);
Devang Patela6da1922010-01-28 00:28:01 +0000569
570 // Copy rest of the arguments.
571 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
572 Elts.push_back(Args.getElement(i));
573
574 llvm::DIArray EltTypeArray =
575 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
576
577 return
578 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000579 Unit, "", Unit,
Devang Patela6da1922010-01-28 00:28:01 +0000580 0, 0, 0, 0, 0,
581 llvm::DIType(), EltTypeArray);
582}
583
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000584/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
585/// a single member function GlobalDecl.
586llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000587CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000588 llvm::DIFile Unit,
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000589 llvm::DICompositeType &RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000590 bool IsCtorOrDtor =
591 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
592
593 llvm::StringRef MethodName = getFunctionName(Method);
Devang Patela6da1922010-01-28 00:28:01 +0000594 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000595
596 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
597 // make sense to give a single ctor/dtor a linkage name.
John McCallf746aa62010-03-19 23:29:14 +0000598 MangleBuffer MethodLinkageName;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000599 if (!IsCtorOrDtor)
John McCallf746aa62010-03-19 23:29:14 +0000600 CGM.getMangledName(MethodLinkageName, Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000601
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000602 SourceManager &SM = CGM.getContext().getSourceManager();
603
604 // Get the location for the method.
605 SourceLocation MethodDefLoc = Method->getLocation();
606 PresumedLoc PLoc = SM.getPresumedLoc(MethodDefLoc);
Devang Patel17800552010-03-09 00:44:50 +0000607 llvm::DIFile MethodDefUnit;
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000608 unsigned MethodLine = 0;
609
610 if (!PLoc.isInvalid()) {
Devang Patel17800552010-03-09 00:44:50 +0000611 MethodDefUnit = getOrCreateFile(MethodDefLoc);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000612 MethodLine = PLoc.getLine();
613 }
614
615 // Collect virtual method info.
616 llvm::DIType ContainingType;
617 unsigned Virtuality = 0;
618 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000619
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000620 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000621 if (Method->isPure())
622 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
623 else
624 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
625
626 // It doesn't make sense to give a virtual destructor a vtable index,
627 // since a single destructor has two entries in the vtable.
628 if (!isa<CXXDestructorDecl>(Method))
Anders Carlsson046c2942010-04-17 20:15:18 +0000629 VIndex = CGM.getVTables().getMethodVTableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000630 ContainingType = RecordTy;
631 }
632
633 llvm::DISubprogram SP =
634 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
635 MethodLinkageName,
636 MethodDefUnit, MethodLine,
637 MethodTy, /*isLocalToUnit=*/false,
638 Method->isThisDeclarationADefinition(),
639 Virtuality, VIndex, ContainingType);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000640
641 // Don't cache ctors or dtors since we have to emit multiple functions for
642 // a single ctor or dtor.
643 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
644 SPCache[Method] = llvm::WeakVH(SP.getNode());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000645
646 return SP;
647}
648
Devang Patel4125fd22010-01-19 01:54:44 +0000649/// CollectCXXMemberFunctions - A helper function to collect debug info for
650/// C++ member functions.This is used while creating debug info entry for
651/// a Record.
652void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000653CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000654 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
655 llvm::DICompositeType &RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000656 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
657 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000658 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000659
Devang Pateld5322da2010-02-09 19:09:28 +0000660 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000661 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000662
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000663 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000664 }
665}
666
Devang Patela245c5b2010-01-25 23:32:18 +0000667/// CollectCXXBases - A helper function to collect debug info for
668/// C++ base classes. This is used while creating debug info entry for
669/// a Record.
670void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000671CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000672 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
673 llvm::DICompositeType &RecordTy) {
674
Devang Patel239cec62010-02-01 21:39:52 +0000675 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
676 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
677 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000678 unsigned BFlags = 0;
679 uint64_t BaseOffset;
680
681 const CXXRecordDecl *Base =
682 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
683
684 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000685 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000686 // expression where it expects +ve number.
Anders Carlssonaf440352010-03-23 04:11:45 +0000687 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base);
Devang Patelca7daed2010-01-28 21:54:15 +0000688 BFlags = llvm::DIType::FlagVirtual;
689 } else
690 BaseOffset = RL.getBaseClassOffset(Base);
691
692 AccessSpecifier Access = BI->getAccessSpecifier();
693 if (Access == clang::AS_private)
694 BFlags |= llvm::DIType::FlagPrivate;
695 else if (Access == clang::AS_protected)
696 BFlags |= llvm::DIType::FlagProtected;
697
698 llvm::DIType DTy =
699 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
700 RecordTy, llvm::StringRef(),
Devang Pateld58562e2010-03-09 22:49:11 +0000701 Unit, 0, 0, 0,
Devang Patelca7daed2010-01-28 21:54:15 +0000702 BaseOffset, BFlags,
703 getOrCreateType(BI->getType(),
704 Unit));
705 EltTys.push_back(DTy);
706 }
Devang Patela245c5b2010-01-25 23:32:18 +0000707}
708
Devang Patel4ce3f202010-01-28 18:11:52 +0000709/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000710llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000711 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000712 return VTablePtrType;
713
714 ASTContext &Context = CGM.getContext();
715
716 /* Function type */
Benjamin Kramerad468862010-03-30 11:36:44 +0000717 llvm::DIDescriptor STy = getOrCreateType(Context.IntTy, Unit);
718 llvm::DIArray SElements = DebugFactory.GetOrCreateArray(&STy, 1);
Devang Patel4ce3f202010-01-28 18:11:52 +0000719 llvm::DIType SubTy =
720 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000721 Unit, "", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000722 0, 0, 0, 0, 0, llvm::DIType(), SElements);
723
724 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
725 llvm::DIType vtbl_ptr_type
726 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000727 Unit, "__vtbl_ptr_type", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000728 0, Size, 0, 0, 0, SubTy);
729
Devang Pateld58562e2010-03-09 22:49:11 +0000730 VTablePtrType =
731 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
732 Unit, "", Unit,
733 0, Size, 0, 0, 0, vtbl_ptr_type);
Devang Patel4ce3f202010-01-28 18:11:52 +0000734 return VTablePtrType;
735}
736
Anders Carlsson046c2942010-04-17 20:15:18 +0000737/// getVTableName - Get vtable name for the given Class.
738llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000739 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000740 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000741
742 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000743 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000744 memcpy(StrPtr, Name.data(), Name.length());
745 return llvm::StringRef(StrPtr, Name.length());
746}
747
748
Anders Carlsson046c2942010-04-17 20:15:18 +0000749/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel4ce3f202010-01-28 18:11:52 +0000750/// debug info entry in EltTys vector.
751void CGDebugInfo::
Anders Carlsson046c2942010-04-17 20:15:18 +0000752CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000753 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000754 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000755
756 // If there is a primary base then it will hold vtable info.
757 if (RL.getPrimaryBase())
758 return;
759
760 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000761 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000762 return;
763
764 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
765 llvm::DIType VPTR
766 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Anders Carlsson046c2942010-04-17 20:15:18 +0000767 getVTableName(RD), Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000768 0, Size, 0, 0, 0,
769 getOrCreateVTablePtrType(Unit));
770 EltTys.push_back(VPTR);
771}
772
Devang Patel65e99f22009-02-25 01:36:11 +0000773/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000774llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000775 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000776 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000777
Chris Lattner9c85ba32008-11-10 06:08:34 +0000778 unsigned Tag;
Devang Pateld6c5a262010-02-01 21:52:22 +0000779 if (RD->isStruct())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000780 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Pateld6c5a262010-02-01 21:52:22 +0000781 else if (RD->isUnion())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000782 Tag = llvm::dwarf::DW_TAG_union_type;
783 else {
Devang Pateld6c5a262010-02-01 21:52:22 +0000784 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000785 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000786 }
787
Anders Carlsson20f12a22009-12-06 18:00:51 +0000788 SourceManager &SM = CGM.getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000789
Chris Lattner9c85ba32008-11-10 06:08:34 +0000790 // Get overall information about the record type for the debug info.
Devang Pateld6c5a262010-02-01 21:52:22 +0000791 PresumedLoc PLoc = SM.getPresumedLoc(RD->getLocation());
Devang Patel17800552010-03-09 00:44:50 +0000792 llvm::DIFile DefUnit;
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000793 unsigned Line = 0;
794 if (!PLoc.isInvalid()) {
Devang Patel17800552010-03-09 00:44:50 +0000795 DefUnit = getOrCreateFile(RD->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000796 Line = PLoc.getLine();
797 }
Mike Stump1eb44332009-09-09 15:08:12 +0000798
Chris Lattner9c85ba32008-11-10 06:08:34 +0000799 // Records and classes and unions can all be recursive. To handle them, we
800 // first generate a debug descriptor for the struct as a forward declaration.
801 // Then (if it is a definition) we go through and get debug info for all of
802 // its members. Finally, we create a descriptor for the complete type (which
803 // may refer to the forward decl if the struct is recursive) and replace all
804 // uses of the forward declaration with the final definition.
Devang Pateld0f251b2010-01-20 23:56:40 +0000805
Devang Pateld6c5a262010-02-01 21:52:22 +0000806 // A RD->getName() is not unique. However, the debug info descriptors
Devang Patelce78c972010-02-01 22:51:29 +0000807 // are uniqued so use type name to ensure uniquness.
Benjamin Kramerfea3d4d2010-03-13 12:06:51 +0000808 llvm::SmallString<128> FwdDeclName;
809 llvm::raw_svector_ostream(FwdDeclName) << "fwd.type." << FwdDeclCount++;
Devang Patel411894b2010-02-01 22:40:08 +0000810 llvm::DIDescriptor FDContext =
811 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel0ce73f62009-07-22 18:57:00 +0000812 llvm::DICompositeType FwdDecl =
Devang Patel7573f8b2010-03-09 21:32:27 +0000813 DebugFactory.CreateCompositeType(Tag, FDContext, FwdDeclName,
Devang Patelab71ff52009-11-12 00:51:46 +0000814 DefUnit, Line, 0, 0, 0, 0,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000815 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000816
Chris Lattner9c85ba32008-11-10 06:08:34 +0000817 // If this is just a forward declaration, return it.
Douglas Gregor952b0172010-02-11 01:04:33 +0000818 if (!RD->getDefinition())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000819 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000820
Eli Friedman14d63652009-11-16 21:04:30 +0000821 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000822 // Otherwise, insert it into the TypeCache so that recursive uses will find
823 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000824 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Devang Patele4c1ea02010-03-11 20:01:48 +0000825 // Push the struct on region stack.
826 RegionStack.push_back(FwdDecl.getNode());
827 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl.getNode());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000828
829 // Convert all the elements.
830 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
831
Devang Pateld6c5a262010-02-01 21:52:22 +0000832 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000833 if (CXXDecl) {
834 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson046c2942010-04-17 20:15:18 +0000835 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000836 }
Devang Pateld6c5a262010-02-01 21:52:22 +0000837 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +0000838 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +0000839 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +0000840 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +0000841
842 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +0000843 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel0ac8f312010-01-28 00:54:21 +0000844 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
845 ContainingType =
846 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit).getNode();
847 else if (CXXDecl->isDynamicClass())
848 ContainingType = FwdDecl.getNode();
Devang Patela245c5b2010-01-25 23:32:18 +0000849 }
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Chris Lattner9c85ba32008-11-10 06:08:34 +0000851 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000852 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000853
854 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000855 uint64_t Size = CGM.getContext().getTypeSize(Ty);
856 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000857
Devang Patele4c1ea02010-03-11 20:01:48 +0000858 RegionStack.pop_back();
859 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
860 RegionMap.find(Ty->getDecl());
861 if (RI != RegionMap.end())
862 RegionMap.erase(RI);
863
Devang Patel411894b2010-02-01 22:40:08 +0000864 llvm::DIDescriptor RDContext =
865 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel0ce73f62009-07-22 18:57:00 +0000866 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +0000867 DebugFactory.CreateCompositeType(Tag, RDContext,
868 RD->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000869 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +0000870 llvm::DIType(), Elements,
871 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000872
873 // Now that we have a real decl for the struct, replace anything using the
874 // old decl with the new one. This will recursively update the debug info.
Eli Friedman14d63652009-11-16 21:04:30 +0000875 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patele4c1ea02010-03-11 20:01:48 +0000876 RegionMap[RD] = llvm::WeakVH(RealDecl.getNode());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000877 return RealDecl;
878}
879
Devang Patel9ca36b62009-02-26 21:10:26 +0000880/// CreateType - get objective-c interface type.
881llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000882 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000883 ObjCInterfaceDecl *ID = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000884
Devang Patel9ca36b62009-02-26 21:10:26 +0000885 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000886 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel9ca36b62009-02-26 21:10:26 +0000887
888 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +0000889 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Pateld6c5a262010-02-01 21:52:22 +0000890 PresumedLoc PLoc = SM.getPresumedLoc(ID->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000891 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
892
Mike Stump1eb44332009-09-09 15:08:12 +0000893
Devang Patel17800552010-03-09 00:44:50 +0000894 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000895
Devang Patel9ca36b62009-02-26 21:10:26 +0000896 // To handle recursive interface, we
897 // first generate a debug descriptor for the struct as a forward declaration.
898 // Then (if it is a definition) we go through and get debug info for all of
899 // its members. Finally, we create a descriptor for the complete type (which
900 // may refer to the forward decl if the struct is recursive) and replace all
901 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000902 llvm::DICompositeType FwdDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +0000903 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000904 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000905 llvm::DIType(), llvm::DIArray(),
906 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000907
Devang Patel9ca36b62009-02-26 21:10:26 +0000908 // If this is just a forward declaration, return it.
Devang Pateld6c5a262010-02-01 21:52:22 +0000909 if (ID->isForwardDecl())
Devang Patel9ca36b62009-02-26 21:10:26 +0000910 return FwdDecl;
911
Devang Patelffffb032009-11-16 20:09:38 +0000912 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000913 // Otherwise, insert it into the TypeCache so that recursive uses will find
914 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000915 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Devang Patele4c1ea02010-03-11 20:01:48 +0000916 // Push the struct on region stack.
917 RegionStack.push_back(FwdDecl.getNode());
918 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl.getNode());
Devang Patel9ca36b62009-02-26 21:10:26 +0000919
920 // Convert all the elements.
921 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
922
Devang Pateld6c5a262010-02-01 21:52:22 +0000923 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +0000924 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000925 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000926 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000927 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000928 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Pateld58562e2010-03-09 22:49:11 +0000929 Unit, "", Unit, 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000930 0 /* offset */, 0, SClassTy);
931 EltTys.push_back(InhTag);
932 }
933
Devang Pateld6c5a262010-02-01 21:52:22 +0000934 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +0000935
936 unsigned FieldNo = 0;
Devang Pateld6c5a262010-02-01 21:52:22 +0000937 for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(),
938 E = ID->ivar_end(); I != E; ++I, ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +0000939 ObjCIvarDecl *Field = *I;
940 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
941
Devang Patel73621622009-11-25 17:37:31 +0000942 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +0000943
Devang Patelde135022009-04-27 22:40:36 +0000944 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +0000945 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +0000946 continue;
947
Devang Patel9ca36b62009-02-26 21:10:26 +0000948 // Get the location for the field.
949 SourceLocation FieldDefLoc = Field->getLocation();
Devang Patel17800552010-03-09 00:44:50 +0000950 llvm::DIFile FieldDefUnit = getOrCreateFile(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000951 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
952 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
953
Mike Stump1eb44332009-09-09 15:08:12 +0000954
Devang Patel99c20eb2009-03-20 18:24:39 +0000955 QualType FType = Field->getType();
956 uint64_t FieldSize = 0;
957 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000958
Devang Patel99c20eb2009-03-20 18:24:39 +0000959 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000960
Devang Patel99c20eb2009-03-20 18:24:39 +0000961 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000962 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000963 Expr *BitWidth = Field->getBitWidth();
964 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +0000965 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000966
Anders Carlsson20f12a22009-12-06 18:00:51 +0000967 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000968 }
969
Mike Stump1eb44332009-09-09 15:08:12 +0000970 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
971
Devang Patelc20482b2009-03-19 00:23:53 +0000972 unsigned Flags = 0;
973 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
974 Flags = llvm::DIType::FlagProtected;
975 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
976 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +0000977
Devang Patel9ca36b62009-02-26 21:10:26 +0000978 // Create a DW_TAG_member node to remember the offset of this field in the
979 // struct. FIXME: This is an absolutely insane way to capture this
980 // information. When we gut debug info, this should be fixed.
981 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
982 FieldName, FieldDefUnit,
983 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000984 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000985 EltTys.push_back(FieldTy);
986 }
Mike Stump1eb44332009-09-09 15:08:12 +0000987
Devang Patel9ca36b62009-02-26 21:10:26 +0000988 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000989 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +0000990
Devang Patele4c1ea02010-03-11 20:01:48 +0000991 RegionStack.pop_back();
992 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
993 RegionMap.find(Ty->getDecl());
994 if (RI != RegionMap.end())
995 RegionMap.erase(RI);
996
Devang Patel9ca36b62009-02-26 21:10:26 +0000997 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000998 uint64_t Size = CGM.getContext().getTypeSize(Ty);
999 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001000
Devang Patel6c1fddf2009-07-27 18:42:03 +00001001 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +00001002 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +00001003 Line, Size, Align, 0, 0, llvm::DIType(),
1004 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +00001005
1006 // Now that we have a real decl for the struct, replace anything using the
1007 // old decl with the new one. This will recursively update the debug info.
Devang Patelffffb032009-11-16 20:09:38 +00001008 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patele4c1ea02010-03-11 20:01:48 +00001009 RegionMap[ID] = llvm::WeakVH(RealDecl.getNode());
Devang Patelfe09eab2009-07-13 17:03:14 +00001010
Devang Patel9ca36b62009-02-26 21:10:26 +00001011 return RealDecl;
1012}
1013
Chris Lattner9c85ba32008-11-10 06:08:34 +00001014llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001015 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001016 EnumDecl *ED = Ty->getDecl();
Chris Lattner9c85ba32008-11-10 06:08:34 +00001017
1018 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
1019
1020 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +00001021 for (EnumDecl::enumerator_iterator
Devang Pateld6c5a262010-02-01 21:52:22 +00001022 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +00001023 Enum != EnumEnd; ++Enum) {
Devang Patel73621622009-11-25 17:37:31 +00001024 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001025 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +00001026 }
Mike Stump1eb44332009-09-09 15:08:12 +00001027
Chris Lattner9c85ba32008-11-10 06:08:34 +00001028 // Return a CompositeType for the enum itself.
1029 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +00001030 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001031
Devang Pateld6c5a262010-02-01 21:52:22 +00001032 SourceLocation DefLoc = ED->getLocation();
Devang Patel17800552010-03-09 00:44:50 +00001033 llvm::DIFile DefUnit = getOrCreateFile(DefLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001034 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001035 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
1036 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
1037
Mike Stump1eb44332009-09-09 15:08:12 +00001038
Chris Lattner9c85ba32008-11-10 06:08:34 +00001039 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +00001040 uint64_t Size = 0;
1041 unsigned Align = 0;
1042 if (!Ty->isIncompleteType()) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001043 Size = CGM.getContext().getTypeSize(Ty);
1044 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman3189e4b2009-05-04 04:39:55 +00001045 }
Mike Stump1eb44332009-09-09 15:08:12 +00001046
Devang Patelca80a5f2009-10-20 19:55:01 +00001047 llvm::DIType DbgTy =
1048 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Pateld6c5a262010-02-01 21:52:22 +00001049 Unit, ED->getName(), DefUnit, Line,
Devang Patelca80a5f2009-10-20 19:55:01 +00001050 Size, Align, 0, 0,
1051 llvm::DIType(), EltArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001052 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001053}
1054
1055llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001056 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001057 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1058 return CreateType(RT, Unit);
1059 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1060 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001061
Chris Lattner9c85ba32008-11-10 06:08:34 +00001062 return llvm::DIType();
1063}
1064
Devang Patel70c23cd2010-02-23 22:59:39 +00001065llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001066 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001067 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1068 uint64_t NumElems = Ty->getNumElements();
1069 if (NumElems > 0)
1070 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001071
Benjamin Kramerad468862010-03-30 11:36:44 +00001072 llvm::DIDescriptor Subscript = DebugFactory.GetOrCreateSubrange(0, NumElems);
1073 llvm::DIArray SubscriptArray = DebugFactory.GetOrCreateArray(&Subscript, 1);
Devang Patel70c23cd2010-02-23 22:59:39 +00001074
1075 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1076 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1077
1078 return
1079 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001080 Unit, "", Unit,
Devang Patel70c23cd2010-02-23 22:59:39 +00001081 0, Size, Align, 0, 0,
1082 ElementTy, SubscriptArray);
1083}
1084
Chris Lattner9c85ba32008-11-10 06:08:34 +00001085llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001086 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001087 uint64_t Size;
1088 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001089
1090
Nuno Lopes010d5142009-01-28 00:35:17 +00001091 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001092 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001093 Size = 0;
1094 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001095 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001096 } else if (Ty->isIncompleteArrayType()) {
1097 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001098 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001099 } else {
1100 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001101 Size = CGM.getContext().getTypeSize(Ty);
1102 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001103 }
Mike Stump1eb44332009-09-09 15:08:12 +00001104
Chris Lattner9c85ba32008-11-10 06:08:34 +00001105 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1106 // interior arrays, do we care? Why aren't nested arrays represented the
1107 // obvious/recursive way?
1108 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1109 QualType EltTy(Ty, 0);
1110 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +00001111 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001112 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +00001113 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +00001114 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001115 // FIXME: Verify this is right for VLAs.
1116 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1117 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +00001118 }
Mike Stump1eb44332009-09-09 15:08:12 +00001119
Chris Lattner9c85ba32008-11-10 06:08:34 +00001120 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001121 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001122
Devang Patelca80a5f2009-10-20 19:55:01 +00001123 llvm::DIType DbgTy =
1124 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001125 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +00001126 0, Size, Align, 0, 0,
1127 getOrCreateType(EltTy, Unit),
1128 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001129 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001130}
1131
Anders Carlssona031b352009-11-06 19:19:55 +00001132llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001133 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001134 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1135 Ty, Ty->getPointeeType(), Unit);
1136}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001137
Anders Carlsson20f12a22009-12-06 18:00:51 +00001138llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001139 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001140 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1141 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1142
1143 if (!Ty->getPointeeType()->isFunctionType()) {
1144 // We have a data member pointer type.
1145 return PointerDiffDITy;
1146 }
1147
1148 // We have a member function pointer type. Treat it as a struct with two
1149 // ptrdiff_t members.
1150 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1151
1152 uint64_t FieldOffset = 0;
1153 llvm::DIDescriptor ElementTypes[2];
1154
1155 // FIXME: This should probably be a function type instead.
1156 ElementTypes[0] =
1157 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001158 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001159 Info.first, Info.second, FieldOffset, 0,
1160 PointerDiffDITy);
1161 FieldOffset += Info.first;
1162
1163 ElementTypes[1] =
1164 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001165 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001166 Info.first, Info.second, FieldOffset, 0,
1167 PointerDiffDITy);
1168
1169 llvm::DIArray Elements =
1170 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1171 llvm::array_lengthof(ElementTypes));
1172
1173 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1174 U, llvm::StringRef("test"),
Devang Pateld58562e2010-03-09 22:49:11 +00001175 U, 0, FieldOffset,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001176 0, 0, 0, llvm::DIType(), Elements);
1177}
1178
Douglas Gregor840943d2009-12-21 20:18:30 +00001179static QualType UnwrapTypeForDebugInfo(QualType T) {
1180 do {
1181 QualType LastT = T;
1182 switch (T->getTypeClass()) {
1183 default:
1184 return T;
1185 case Type::TemplateSpecialization:
1186 T = cast<TemplateSpecializationType>(T)->desugar();
1187 break;
1188 case Type::TypeOfExpr: {
1189 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1190 T = Ty->getUnderlyingExpr()->getType();
1191 break;
1192 }
1193 case Type::TypeOf:
1194 T = cast<TypeOfType>(T)->getUnderlyingType();
1195 break;
1196 case Type::Decltype:
1197 T = cast<DecltypeType>(T)->getUnderlyingType();
1198 break;
1199 case Type::QualifiedName:
1200 T = cast<QualifiedNameType>(T)->getNamedType();
1201 break;
1202 case Type::SubstTemplateTypeParm:
1203 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1204 break;
1205 case Type::Elaborated:
1206 T = cast<ElaboratedType>(T)->getUnderlyingType();
1207 break;
1208 }
1209
1210 assert(T != LastT && "Type unwrapping failed to unwrap!");
1211 if (T == LastT)
1212 return T;
1213 } while (true);
1214
1215 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001216}
1217
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001218/// getOrCreateType - Get the type from the cache or create a new
1219/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001220llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001221 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001222 if (Ty.isNull())
1223 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001224
Douglas Gregor840943d2009-12-21 20:18:30 +00001225 // Unwrap the type as needed for debug information.
1226 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001227
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001228 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001229 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001230 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001231 if (it != TypeCache.end()) {
1232 // Verify that the debug info still exists.
1233 if (&*it->second)
1234 return llvm::DIType(cast<llvm::MDNode>(it->second));
1235 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001236
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001237 // Otherwise create the type.
1238 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001239
1240 // And update the type cache.
1241 TypeCache[Ty.getAsOpaquePtr()] = Res.getNode();
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001242 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001243}
1244
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001245/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001246llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001247 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001248 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001249 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001250 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001251
Douglas Gregor2101a822009-12-21 19:57:21 +00001252 const char *Diag = 0;
1253
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001254 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001255 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001256#define TYPE(Class, Base)
1257#define ABSTRACT_TYPE(Class, Base)
1258#define NON_CANONICAL_TYPE(Class, Base)
1259#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1260#include "clang/AST/TypeNodes.def"
1261 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001262
Anders Carlssonbfe69952009-11-06 18:24:04 +00001263 // FIXME: Handle these.
1264 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001265 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001266
1267 case Type::Vector:
1268 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001269 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001270 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001271 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001272 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1273 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1274 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1275 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001276 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001277 return CreateType(cast<BlockPointerType>(Ty), Unit);
1278 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001279 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001280 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001281 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001282 case Type::FunctionProto:
1283 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001284 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001285 case Type::ConstantArray:
1286 case Type::VariableArray:
1287 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001288 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001289
1290 case Type::LValueReference:
1291 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1292
Anders Carlsson20f12a22009-12-06 18:00:51 +00001293 case Type::MemberPointer:
1294 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001295
John McCall3cb0ebd2010-03-10 03:28:59 +00001296 case Type::InjectedClassName:
Douglas Gregor2101a822009-12-21 19:57:21 +00001297 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001298 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001299 case Type::QualifiedName:
Douglas Gregor2101a822009-12-21 19:57:21 +00001300 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001301 case Type::TypeOfExpr:
1302 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001303 case Type::Decltype:
1304 llvm_unreachable("type should have been unwrapped!");
1305 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001306
1307 case Type::RValueReference:
1308 // FIXME: Implement!
1309 Diag = "rvalue references";
1310 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001311 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001312
1313 assert(Diag && "Fall through without a diagnostic?");
1314 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1315 "debug information for %0 is not yet supported");
1316 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1317 << Diag;
1318 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001319}
1320
1321/// EmitFunctionStart - Constructs the debug code for entering a function -
1322/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001323void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001324 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001325 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001326
Devang Patel9c6c3a02010-01-14 00:36:21 +00001327 llvm::StringRef Name;
John McCallf746aa62010-03-19 23:29:14 +00001328 MangleBuffer LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001329
1330 const Decl *D = GD.getDecl();
1331 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001332 // If there is a DISubprogram for this function available then use it.
1333 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1334 FI = SPCache.find(FD);
1335 if (FI != SPCache.end()) {
Devang Patel0804e6e2010-03-08 20:53:17 +00001336 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
1337 if (SP.isSubprogram() && llvm::DISubprogram(SP.getNode()).isDefinition()) {
Devang Patel4125fd22010-01-19 01:54:44 +00001338 RegionStack.push_back(SP.getNode());
Devang Patel3dd96a12010-01-29 18:11:03 +00001339 RegionMap[D] = llvm::WeakVH(SP.getNode());
Devang Patel4125fd22010-01-19 01:54:44 +00001340 return;
1341 }
1342 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001343 Name = getFunctionName(FD);
Eli Friedman3364e622010-01-16 00:43:13 +00001344 if (!Name.empty() && Name[0] == '\01')
Devang Patelaa97d702010-01-14 21:46:57 +00001345 Name = Name.substr(1);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001346 // Use mangled name as linkage name for c/c++ functions.
John McCallf746aa62010-03-19 23:29:14 +00001347 CGM.getMangledName(LinkageName, GD);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001348 } else {
1349 // Use llvm function name as linkage name.
1350 Name = Fn->getName();
John McCallf746aa62010-03-19 23:29:14 +00001351 LinkageName.setString(Name);
Devang Patel17584202010-01-19 00:25:12 +00001352 if (!Name.empty() && Name[0] == '\01')
1353 Name = Name.substr(1);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001354 }
Mike Stump1eb44332009-09-09 15:08:12 +00001355
Devang Patel98a200b2010-01-14 18:06:13 +00001356 // It is expected that CurLoc is set before using EmitFunctionStart.
1357 // Usually, CurLoc points to the left bracket location of compound
1358 // statement representing function body.
Devang Patel17800552010-03-09 00:44:50 +00001359 llvm::DIFile Unit = getOrCreateFile(CurLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001360 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +00001361 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Mike Stump1eb44332009-09-09 15:08:12 +00001362
Chris Lattner9c85ba32008-11-10 06:08:34 +00001363 llvm::DISubprogram SP =
Devang Patel6dba4322009-07-14 21:31:22 +00001364 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001365 getOrCreateType(FnType, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001366 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump1eb44332009-09-09 15:08:12 +00001367
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001368 // Push function on region stack.
Devang Patel8fae0602009-11-13 19:10:24 +00001369 RegionStack.push_back(SP.getNode());
Devang Patel3dd96a12010-01-29 18:11:03 +00001370 RegionMap[D] = llvm::WeakVH(SP.getNode());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001371}
1372
1373
Chris Lattner9c85ba32008-11-10 06:08:34 +00001374void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001375 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001376
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001377 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001378 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001379 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001380 || (SM.getInstantiationLineNumber(CurLoc) ==
1381 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001382 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patel4800ea62010-04-05 21:09:15 +00001383 // New Builder may not be in sync with CGDebugInfo.
1384 if (!Builder.getCurrentDebugLocation().isUnknown())
1385 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001386
1387 // Update last state.
1388 PrevLoc = CurLoc;
1389
1390 // Get the appropriate compile unit.
Devang Patel17800552010-03-09 00:44:50 +00001391 llvm::DIFile Unit = getOrCreateFile(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +00001392 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patelbbd9fa42009-10-06 18:36:08 +00001393
Chris Lattnerc6034632010-04-01 06:31:43 +00001394 llvm::MDNode *Scope = RegionStack.back();
Chris Lattnere541d012010-04-02 20:21:43 +00001395 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(PLoc.getLine(),
1396 PLoc.getColumn(),
1397 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001398}
1399
1400/// EmitRegionStart- Constructs the debug code for entering a declarative
1401/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +00001402void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
Devang Pateld19429f2010-02-16 21:41:20 +00001403 SourceManager &SM = CGM.getContext().getSourceManager();
1404 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patel8fae0602009-11-13 19:10:24 +00001405 llvm::DIDescriptor D =
1406 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1407 llvm::DIDescriptor() :
Devang Pateld19429f2010-02-16 21:41:20 +00001408 llvm::DIDescriptor(RegionStack.back()),
1409 PLoc.getLine(), PLoc.getColumn());
Devang Patel8fae0602009-11-13 19:10:24 +00001410 RegionStack.push_back(D.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001411}
1412
1413/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1414/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +00001415void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001416 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1417
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001418 // Provide an region stop point.
1419 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001420
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001421 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001422}
1423
Devang Patel809b9bb2010-02-10 18:49:08 +00001424// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1425// See BuildByRefType.
1426llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1427 uint64_t *XOffset) {
1428
1429 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1430
1431 QualType FType;
1432 uint64_t FieldSize, FieldOffset;
1433 unsigned FieldAlign;
1434
Devang Patel17800552010-03-09 00:44:50 +00001435 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001436 QualType Type = VD->getType();
1437
1438 FieldOffset = 0;
1439 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
1440 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1441 FieldSize = CGM.getContext().getTypeSize(FType);
1442 FieldAlign = CGM.getContext().getTypeAlign(FType);
1443 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001444 "__isa", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001445 0, FieldSize, FieldAlign,
1446 FieldOffset, 0, FieldTy);
1447 EltTys.push_back(FieldTy);
1448 FieldOffset += FieldSize;
1449
1450 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
1451 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1452 FieldSize = CGM.getContext().getTypeSize(FType);
1453 FieldAlign = CGM.getContext().getTypeAlign(FType);
1454 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001455 "__forwarding", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001456 0, FieldSize, FieldAlign,
1457 FieldOffset, 0, FieldTy);
1458 EltTys.push_back(FieldTy);
1459 FieldOffset += FieldSize;
1460
1461 FType = CGM.getContext().IntTy;
1462 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1463 FieldSize = CGM.getContext().getTypeSize(FType);
1464 FieldAlign = CGM.getContext().getTypeAlign(FType);
1465 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001466 "__flags", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001467 0, FieldSize, FieldAlign,
1468 FieldOffset, 0, FieldTy);
1469 EltTys.push_back(FieldTy);
1470 FieldOffset += FieldSize;
1471
1472 FType = CGM.getContext().IntTy;
1473 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1474 FieldSize = CGM.getContext().getTypeSize(FType);
1475 FieldAlign = CGM.getContext().getTypeAlign(FType);
1476 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001477 "__size", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001478 0, FieldSize, FieldAlign,
1479 FieldOffset, 0, FieldTy);
1480 EltTys.push_back(FieldTy);
1481 FieldOffset += FieldSize;
1482
1483 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1484 if (HasCopyAndDispose) {
1485 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
1486 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1487 FieldSize = CGM.getContext().getTypeSize(FType);
1488 FieldAlign = CGM.getContext().getTypeAlign(FType);
1489 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001490 "__copy_helper", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001491 0, FieldSize, FieldAlign,
1492 FieldOffset, 0, FieldTy);
1493 EltTys.push_back(FieldTy);
1494 FieldOffset += FieldSize;
1495
1496 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
1497 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1498 FieldSize = CGM.getContext().getTypeSize(FType);
1499 FieldAlign = CGM.getContext().getTypeAlign(FType);
1500 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001501 "__destroy_helper", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001502 0, FieldSize, FieldAlign,
1503 FieldOffset, 0, FieldTy);
1504 EltTys.push_back(FieldTy);
1505 FieldOffset += FieldSize;
1506 }
1507
1508 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1509 if (Align > CharUnits::fromQuantity(
1510 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1511 unsigned AlignedOffsetInBytes
1512 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1513 unsigned NumPaddingBytes
1514 = AlignedOffsetInBytes - FieldOffset/8;
1515
1516 if (NumPaddingBytes > 0) {
1517 llvm::APInt pad(32, NumPaddingBytes);
1518 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1519 pad, ArrayType::Normal, 0);
1520 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1521 FieldSize = CGM.getContext().getTypeSize(FType);
1522 FieldAlign = CGM.getContext().getTypeAlign(FType);
1523 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
Devang Pateld58562e2010-03-09 22:49:11 +00001524 Unit, "", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001525 0, FieldSize, FieldAlign,
1526 FieldOffset, 0, FieldTy);
1527 EltTys.push_back(FieldTy);
1528 FieldOffset += FieldSize;
1529 }
1530 }
1531
1532 FType = Type;
1533 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1534 FieldSize = CGM.getContext().getTypeSize(FType);
1535 FieldAlign = Align.getQuantity()*8;
1536
1537 *XOffset = FieldOffset;
1538 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001539 VD->getName(), Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001540 0, FieldSize, FieldAlign,
1541 FieldOffset, 0, FieldTy);
1542 EltTys.push_back(FieldTy);
1543 FieldOffset += FieldSize;
1544
1545 llvm::DIArray Elements =
1546 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1547
1548 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1549
1550 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001551 Unit, "", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001552 0, FieldOffset, 0, 0, Flags,
1553 llvm::DIType(), Elements);
1554
1555}
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001556/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001557void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001558 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001559 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1560
Devang Patel07739032009-03-27 23:16:32 +00001561 // Do not emit variable debug information while generating optimized code.
1562 // The llvm optimizer and code generator are not yet ready to support
1563 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001564 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001565 if (CGO.OptimizationLevel)
Devang Patel07739032009-03-27 23:16:32 +00001566 return;
1567
Devang Patel17800552010-03-09 00:44:50 +00001568 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001569 llvm::DIType Ty;
1570 uint64_t XOffset = 0;
1571 if (VD->hasAttr<BlocksAttr>())
1572 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1573 else
1574 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001575
Chris Lattner9c85ba32008-11-10 06:08:34 +00001576 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001577 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel239cec62010-02-01 21:39:52 +00001578 PresumedLoc PLoc = SM.getPresumedLoc(VD->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +00001579 unsigned Line = 0;
Eli Friedman1468ac72009-11-16 20:33:31 +00001580 unsigned Column = 0;
Devang Pateld263e7b2010-02-10 01:09:50 +00001581 if (PLoc.isInvalid())
1582 PLoc = SM.getPresumedLoc(CurLoc);
1583 if (PLoc.isValid()) {
Chris Lattner650cea92009-05-05 04:57:08 +00001584 Line = PLoc.getLine();
Eli Friedman1468ac72009-11-16 20:33:31 +00001585 Column = PLoc.getColumn();
Devang Patel17800552010-03-09 00:44:50 +00001586 Unit = getOrCreateFile(CurLoc);
Eli Friedman1468ac72009-11-16 20:33:31 +00001587 } else {
Devang Patel17800552010-03-09 00:44:50 +00001588 Unit = llvm::DIFile();
Eli Friedman1468ac72009-11-16 20:33:31 +00001589 }
Mike Stump1eb44332009-09-09 15:08:12 +00001590
Chris Lattner9c85ba32008-11-10 06:08:34 +00001591 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001592 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001593 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel239cec62010-02-01 21:39:52 +00001594 VD->getName(),
Chris Lattner650cea92009-05-05 04:57:08 +00001595 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001596 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001597 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001598 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001599
Chris Lattnerc6034632010-04-01 06:31:43 +00001600 llvm::MDNode *Scope = RegionStack.back();
Chris Lattnere541d012010-04-02 20:21:43 +00001601 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001602}
1603
Mike Stumpb1a6e682009-09-30 02:43:10 +00001604/// EmitDeclare - Emit local variable declaration debug info.
1605void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1606 llvm::Value *Storage, CGBuilderTy &Builder,
1607 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001608 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001609 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1610
1611 // Do not emit variable debug information while generating optimized code.
1612 // The llvm optimizer and code generator are not yet ready to support
1613 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001614 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001615 if (CGO.OptimizationLevel || Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001616 return;
1617
1618 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001619 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001620 llvm::DIType Ty;
1621 if (VD->hasAttr<BlocksAttr>())
1622 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1623 else
1624 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001625
1626 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001627 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateld6c5a262010-02-01 21:52:22 +00001628 PresumedLoc PLoc = SM.getPresumedLoc(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001629 unsigned Line = 0;
1630 if (!PLoc.isInvalid())
1631 Line = PLoc.getLine();
1632 else
Devang Patel17800552010-03-09 00:44:50 +00001633 Unit = llvm::DIFile();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001634
Devang Pateld6c5a262010-02-01 21:52:22 +00001635 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001636 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001637 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1638 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1639 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1640 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001641 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001642 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1643 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001644 // offset of __forwarding field
1645 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001646 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1647 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1648 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001649 // offset of x field
1650 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001651 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001652 }
1653
1654 // Create the descriptor for the variable.
1655 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001656 DebugFactory.CreateComplexVariable(Tag,
1657 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001658 VD->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001659 addr);
1660 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001661 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001662 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattnerd5b89022009-12-28 21:44:41 +00001663
Chris Lattnerc6034632010-04-01 06:31:43 +00001664 llvm::MDNode *Scope = RegionStack.back();
Chris Lattnere541d012010-04-02 20:21:43 +00001665 Call->setDebugLoc(llvm::DebugLoc::get(Line, PLoc.getColumn(), Scope));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001666}
1667
Devang Pateld6c5a262010-02-01 21:52:22 +00001668void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001669 llvm::Value *Storage,
1670 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001671 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001672}
1673
Mike Stumpb1a6e682009-09-30 02:43:10 +00001674void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1675 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1676 CodeGenFunction *CGF) {
1677 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1678}
1679
Chris Lattner9c85ba32008-11-10 06:08:34 +00001680/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1681/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001682void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001683 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001684 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001685}
1686
1687
1688
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001689/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001690void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001691 const VarDecl *D) {
1692
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001693 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001694 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001695 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001696 PresumedLoc PLoc = SM.getPresumedLoc(D->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +00001697 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001698
Devang Pateleb6d79b2010-02-01 21:34:11 +00001699 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001700 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001701
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001702 // CodeGen turns int[] into int[1] so we'll do the same here.
1703 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001704
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001705 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001706 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001707
Anders Carlsson20f12a22009-12-06 18:00:51 +00001708 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001709 ArrayType::Normal, 0);
1710 }
Sanjiv Gupta67b14f02010-02-25 05:20:44 +00001711 llvm::StringRef DeclName = Var->getName();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001712 llvm::DIDescriptor DContext =
1713 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
1714 DebugFactory.CreateGlobalVariable(DContext, DeclName,
Devang Patel33583052010-01-28 23:15:27 +00001715 DeclName, llvm::StringRef(), Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001716 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001717 Var->hasInternalLinkage(),
1718 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001719}
1720
Devang Patel9ca36b62009-02-26 21:10:26 +00001721/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001722void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001723 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001724 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001725 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001726 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateld6c5a262010-02-01 21:52:22 +00001727 PresumedLoc PLoc = SM.getPresumedLoc(ID->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +00001728 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +00001729
Devang Pateld6c5a262010-02-01 21:52:22 +00001730 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001731
Devang Pateld6c5a262010-02-01 21:52:22 +00001732 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001733 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001734
Devang Patel9ca36b62009-02-26 21:10:26 +00001735 // CodeGen turns int[] into int[1] so we'll do the same here.
1736 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001737
Devang Patel9ca36b62009-02-26 21:10:26 +00001738 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001739 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001740
Anders Carlsson20f12a22009-12-06 18:00:51 +00001741 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001742 ArrayType::Normal, 0);
1743 }
1744
Devang Patelf6a39b72009-10-20 18:26:30 +00001745 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001746 getOrCreateType(T, Unit),
1747 Var->hasInternalLinkage(),
1748 true/*definition*/, Var);
1749}
Devang Patelabb485f2010-02-01 19:16:32 +00001750
1751/// getOrCreateNamesSpace - Return namespace descriptor for the given
1752/// namespace decl.
1753llvm::DINameSpace
1754CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1755 llvm::DIDescriptor Unit) {
1756 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1757 NameSpaceCache.find(NSDecl);
1758 if (I != NameSpaceCache.end())
1759 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1760
1761 SourceManager &SM = CGM.getContext().getSourceManager();
1762 PresumedLoc PLoc = SM.getPresumedLoc(NSDecl->getLocation());
1763 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
1764
1765 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00001766 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00001767 llvm::DINameSpace NS =
1768 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
Devang Patel17800552010-03-09 00:44:50 +00001769 llvm::DIFile(Unit.getNode()), LineNo);
Devang Patelabb485f2010-02-01 19:16:32 +00001770 NameSpaceCache[NSDecl] = llvm::WeakVH(NS.getNode());
1771 return NS;
1772}