blob: 9b469452a373ed25d1b9916c462f8560107ad765 [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)
39 : CGM(CGM), isMainCompileUnitCreated(false), DebugFactory(CGM.getModule()),
Mike Stump9bc093c2009-05-14 02:03:51 +000040 BlockLiteralGenericSet(false) {
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000041}
42
Chris Lattner9c85ba32008-11-10 06:08:34 +000043CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar66031a52008-10-17 16:15:48 +000044 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000045}
46
Chris Lattner9c85ba32008-11-10 06:08:34 +000047void CGDebugInfo::setLocation(SourceLocation Loc) {
48 if (Loc.isValid())
Anders Carlsson20f12a22009-12-06 18:00:51 +000049 CurLoc = CGM.getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000050}
51
Devang Patel33583052010-01-28 23:15:27 +000052/// getContextDescriptor - Get context info for the decl.
Devang Pateleb6d79b2010-02-01 21:34:11 +000053llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context,
Devang Patel33583052010-01-28 23:15:27 +000054 llvm::DIDescriptor &CompileUnit) {
Devang Pateleb6d79b2010-02-01 21:34:11 +000055 if (!Context)
56 return CompileUnit;
57
58 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
59 I = RegionMap.find(Context);
60 if (I != RegionMap.end())
61 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(I->second));
Devang Patel411894b2010-02-01 22:40:08 +000062
Devang Pateleb6d79b2010-02-01 21:34:11 +000063 // Check namespace.
64 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
65 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl, CompileUnit));
66
Devang Patel979ec2e2009-10-06 00:35:31 +000067 return CompileUnit;
68}
69
Devang Patel9c6c3a02010-01-14 00:36:21 +000070/// getFunctionName - Get function name for the given FunctionDecl. If the
71/// name is constructred on demand (e.g. C++ destructor) then the name
72/// is stored on the side.
73llvm::StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
74 assert (FD && "Invalid FunctionDecl!");
75 IdentifierInfo *FII = FD->getIdentifier();
76 if (FII)
77 return FII->getName();
78
79 // Otherwise construct human readable name for debug info.
80 std::string NS = FD->getNameAsString();
81
82 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +000083 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer1b627dc2010-01-23 18:16:07 +000084 memcpy(StrPtr, NS.data(), NS.length());
85 return llvm::StringRef(StrPtr, NS.length());
Devang Patel9c6c3a02010-01-14 00:36:21 +000086}
87
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000088/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
Daniel Dunbar25f51dd2008-10-24 08:38:36 +000089/// one if necessary. This returns null for invalid source locations.
Chris Lattner9c85ba32008-11-10 06:08:34 +000090llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) {
Devang Patel446c6192009-04-17 21:06:59 +000091 // Get source file information.
92 const char *FileName = "<unknown>";
Anders Carlsson20f12a22009-12-06 18:00:51 +000093 SourceManager &SM = CGM.getContext().getSourceManager();
Daniel Dunbar831570c2009-01-22 00:09:25 +000094 if (Loc.isValid()) {
Devang Patel446c6192009-04-17 21:06:59 +000095 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
96 FileName = PLoc.getFilename();
Devang Pateld263e7b2010-02-10 01:09:50 +000097 unsigned FID = PLoc.getIncludeLoc().getRawEncoding();
Mike Stump1eb44332009-09-09 15:08:12 +000098
Devang Pateld263e7b2010-02-10 01:09:50 +000099 // See if this compile unit has been used before for this valid location.
100 llvm::DICompileUnit &Unit = CompileUnitCache[FID];
Devang Patel0804e6e2010-03-08 20:53:17 +0000101 if (Unit.Verify()) return Unit;
Devang Pateld263e7b2010-02-10 01:09:50 +0000102 }
Daniel Dunbarc9abc042009-04-08 05:11:16 +0000103
Devang Patel446c6192009-04-17 21:06:59 +0000104 // Get absolute path name.
105 llvm::sys::Path AbsFileName(FileName);
Benjamin Kramer47daf682009-12-08 11:02:29 +0000106 AbsFileName.makeAbsolute();
Devang Patel446c6192009-04-17 21:06:59 +0000107
Devang Patel72240d72009-06-26 18:32:22 +0000108 // See if thie compile unit is representing main source file. Each source
109 // file has corresponding compile unit. There is only one main source
110 // file at a time.
111 bool isMain = false;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000112 const LangOptions &LO = CGM.getLangOptions();
113 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Devang Patel72240d72009-06-26 18:32:22 +0000114 if (isMainCompileUnitCreated == false) {
Daniel Dunbar7d065d02009-11-29 02:38:34 +0000115 if (!CGO.MainFileName.empty()) {
116 if (AbsFileName.getLast() == CGO.MainFileName)
Devang Patel72240d72009-06-26 18:32:22 +0000117 isMain = true;
118 } else {
119 if (Loc.isValid() && SM.isFromMainFile(Loc))
120 isMain = true;
121 }
122 if (isMain)
123 isMainCompileUnitCreated = true;
Devang Patel446c6192009-04-17 21:06:59 +0000124 }
Daniel Dunbarc9abc042009-04-08 05:11:16 +0000125
Chris Lattner515455a2009-03-25 03:28:08 +0000126 unsigned LangTag;
127 if (LO.CPlusPlus) {
128 if (LO.ObjC1)
129 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
130 else
131 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
132 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000133 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000134 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000135 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000136 } else {
137 LangTag = llvm::dwarf::DW_LANG_C89;
138 }
Devang Patel446c6192009-04-17 21:06:59 +0000139
Benjamin Kramer47daf682009-12-08 11:02:29 +0000140 const char *Producer =
Mike Stumpd8945d62009-10-09 18:38:12 +0000141#ifdef CLANG_VENDOR
142 CLANG_VENDOR
143#endif
144 "clang " CLANG_VERSION_STRING;
Chris Lattner4c2577a2009-05-02 01:00:04 +0000145
146 // Figure out which version of the ObjC runtime we have.
147 unsigned RuntimeVers = 0;
148 if (LO.ObjC1)
149 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000151 // Create new compile unit.
Devang Patel4cb159c2010-02-12 01:30:31 +0000152 llvm::DICompileUnit Unit = DebugFactory.CreateCompileUnit(
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000153 LangTag, AbsFileName.getLast(), AbsFileName.getDirname(), Producer, isMain,
154 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Devang Patel4cb159c2010-02-12 01:30:31 +0000155
156 if (Loc.isValid()) {
157 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
158 unsigned FID = PLoc.getIncludeLoc().getRawEncoding();
159 CompileUnitCache[FID] = Unit;
160 }
161 return Unit;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000162}
163
Devang Patel65e99f22009-02-25 01:36:11 +0000164/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000165/// one if necessary.
166llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel65e99f22009-02-25 01:36:11 +0000167 llvm::DICompileUnit Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000168 unsigned Encoding = 0;
169 switch (BT->getKind()) {
170 default:
171 case BuiltinType::Void:
172 return llvm::DIType();
173 case BuiltinType::UChar:
174 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
175 case BuiltinType::Char_S:
176 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
177 case BuiltinType::UShort:
178 case BuiltinType::UInt:
179 case BuiltinType::ULong:
180 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
181 case BuiltinType::Short:
182 case BuiltinType::Int:
183 case BuiltinType::Long:
184 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
185 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
186 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000187 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000188 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000189 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000190 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000191 uint64_t Size = CGM.getContext().getTypeSize(BT);
192 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000193 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Devang Patelca80a5f2009-10-20 19:55:01 +0000195 llvm::DIType DbgTy =
196 DebugFactory.CreateBasicType(Unit,
Anders Carlsson20f12a22009-12-06 18:00:51 +0000197 BT->getName(CGM.getContext().getLangOptions()),
Devang Patelca80a5f2009-10-20 19:55:01 +0000198 Unit, 0, Size, Align,
199 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000200 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000201}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000202
Chris Lattnerb7003772009-04-23 06:13:01 +0000203llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
204 llvm::DICompileUnit Unit) {
205 // Bit size, align and offset of the type.
206 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
207 if (Ty->isComplexIntegerType())
208 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Anders Carlsson20f12a22009-12-06 18:00:51 +0000210 uint64_t Size = CGM.getContext().getTypeSize(Ty);
211 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattnerb7003772009-04-23 06:13:01 +0000212 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Devang Patelca80a5f2009-10-20 19:55:01 +0000214 llvm::DIType DbgTy =
215 DebugFactory.CreateBasicType(Unit, "complex",
216 Unit, 0, Size, Align,
217 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000218 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000219}
220
John McCalla1805292009-09-25 01:40:47 +0000221/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000222/// a new one if necessary.
John McCalla1805292009-09-25 01:40:47 +0000223llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DICompileUnit Unit) {
224 QualifierCollector Qc;
225 const Type *T = Qc.strip(Ty);
226
227 // Ignore these qualifiers for now.
228 Qc.removeObjCGCAttr();
229 Qc.removeAddressSpace();
230
Chris Lattner9c85ba32008-11-10 06:08:34 +0000231 // We will create one Derived type for one qualifier and recurse to handle any
232 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000233 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000234 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000235 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000236 Qc.removeConst();
237 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000238 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000239 Qc.removeVolatile();
240 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000241 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000242 Qc.removeRestrict();
243 } else {
244 assert(Qc.empty() && "Unknown type qualifier for debug info");
245 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000246 }
Mike Stump1eb44332009-09-09 15:08:12 +0000247
John McCalla1805292009-09-25 01:40:47 +0000248 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
249
Daniel Dunbar3845f862008-10-31 03:54:29 +0000250 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
251 // CVR derived types.
Devang Patelca80a5f2009-10-20 19:55:01 +0000252 llvm::DIType DbgTy =
253 DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
254 0, 0, 0, 0, 0, FromTy);
Devang Patelca80a5f2009-10-20 19:55:01 +0000255 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000256}
257
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000258llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
259 llvm::DICompileUnit Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000260 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000261 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
262 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000263 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000264}
265
Chris Lattner9c85ba32008-11-10 06:08:34 +0000266llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
267 llvm::DICompileUnit Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000268 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
269 Ty->getPointeeType(), Unit);
270}
271
272llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
273 const Type *Ty,
274 QualType PointeeTy,
275 llvm::DICompileUnit Unit) {
276 llvm::DIType EltTy = getOrCreateType(PointeeTy, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000278 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000279
280 // Size is always the size of a pointer. We can't use getTypeSize here
281 // because that does not return the correct value for references.
282 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000283 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
284 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Devang Patelca80a5f2009-10-20 19:55:01 +0000286 return
Anders Carlssona031b352009-11-06 19:19:55 +0000287 DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
Devang Patelca80a5f2009-10-20 19:55:01 +0000288 0, Size, Align, 0, 0, EltTy);
Anders Carlssona031b352009-11-06 19:19:55 +0000289
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000290}
291
Mike Stump9bc093c2009-05-14 02:03:51 +0000292llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
293 llvm::DICompileUnit Unit) {
294 if (BlockLiteralGenericSet)
295 return BlockLiteralGeneric;
296
297 llvm::DICompileUnit DefUnit;
298 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
299
300 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
301
302 llvm::DIType FieldTy;
303
304 QualType FType;
305 uint64_t FieldSize, FieldOffset;
306 unsigned FieldAlign;
307
308 llvm::DIArray Elements;
309 llvm::DIType EltTy, DescTy;
310
311 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000312 FType = CGM.getContext().UnsignedLongTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000313 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000314 FieldSize = CGM.getContext().getTypeSize(FType);
315 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000316 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
317 "reserved", DefUnit,
318 0, FieldSize, FieldAlign,
319 FieldOffset, 0, FieldTy);
320 EltTys.push_back(FieldTy);
321
322 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000323 FType = CGM.getContext().UnsignedLongTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000324 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000325 FieldSize = CGM.getContext().getTypeSize(FType);
326 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000327 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
328 "Size", DefUnit,
329 0, FieldSize, FieldAlign,
330 FieldOffset, 0, FieldTy);
331 EltTys.push_back(FieldTy);
332
333 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000334 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000335 EltTys.clear();
336
Mike Stump3d363c52009-10-02 02:30:50 +0000337 unsigned Flags = llvm::DIType::FlagAppleBlock;
338
Mike Stump9bc093c2009-05-14 02:03:51 +0000339 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Mike Stump3d363c52009-10-02 02:30:50 +0000340 DefUnit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000341 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Mike Stump9bc093c2009-05-14 02:03:51 +0000343 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000344 uint64_t Size = CGM.getContext().getTypeSize(Ty);
345 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Mike Stump9bc093c2009-05-14 02:03:51 +0000347 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
348 Unit, "", llvm::DICompileUnit(),
349 0, Size, Align, 0, 0, EltTy);
350
351 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000352 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000353 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000354 FieldSize = CGM.getContext().getTypeSize(FType);
355 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000356 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
357 "__isa", DefUnit,
358 0, FieldSize, FieldAlign,
359 FieldOffset, 0, FieldTy);
360 EltTys.push_back(FieldTy);
361
362 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000363 FType = CGM.getContext().IntTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000364 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000365 FieldSize = CGM.getContext().getTypeSize(FType);
366 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000367 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
368 "__flags", DefUnit,
369 0, FieldSize, FieldAlign,
370 FieldOffset, 0, FieldTy);
371 EltTys.push_back(FieldTy);
372
373 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000374 FType = CGM.getContext().IntTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000375 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000376 FieldSize = CGM.getContext().getTypeSize(FType);
377 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000378 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
379 "__reserved", DefUnit,
380 0, FieldSize, FieldAlign,
381 FieldOffset, 0, FieldTy);
382 EltTys.push_back(FieldTy);
383
384 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000385 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000386 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000387 FieldSize = CGM.getContext().getTypeSize(FType);
388 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000389 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
390 "__FuncPtr", DefUnit,
391 0, FieldSize, FieldAlign,
392 FieldOffset, 0, FieldTy);
393 EltTys.push_back(FieldTy);
394
395 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000396 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000397 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000398 FieldSize = CGM.getContext().getTypeSize(Ty);
399 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000400 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
401 "__descriptor", DefUnit,
402 0, FieldSize, FieldAlign,
403 FieldOffset, 0, FieldTy);
404 EltTys.push_back(FieldTy);
405
406 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000407 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000408
409 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Mike Stump944e7052009-10-02 02:23:37 +0000410 DefUnit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000411 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Mike Stump9bc093c2009-05-14 02:03:51 +0000413 BlockLiteralGenericSet = true;
414 BlockLiteralGeneric
415 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
416 "", llvm::DICompileUnit(),
417 0, Size, Align, 0, 0, EltTy);
418 return BlockLiteralGeneric;
419}
420
Chris Lattner9c85ba32008-11-10 06:08:34 +0000421llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
422 llvm::DICompileUnit Unit) {
423 // Typedefs are derived from some other type. If we have a typedef of a
424 // typedef, make sure to emit the whole chain.
425 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Chris Lattner9c85ba32008-11-10 06:08:34 +0000427 // We don't set size information, but do specify where the typedef was
428 // declared.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000429 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateld5289052010-01-29 22:29:31 +0000430 PresumedLoc PLoc = SM.getPresumedLoc(Ty->getDecl()->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000431 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000432
Devang Pateleb6d79b2010-02-01 21:34:11 +0000433 llvm::DIDescriptor TyContext
434 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
435 Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000436 llvm::DIType DbgTy =
Devang Pateld5289052010-01-29 22:29:31 +0000437 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Pateleb6d79b2010-02-01 21:34:11 +0000438 TyContext,
Devang Pateld5289052010-01-29 22:29:31 +0000439 Ty->getDecl()->getName(), Unit,
440 Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000441 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000442}
443
Chris Lattner9c85ba32008-11-10 06:08:34 +0000444llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
445 llvm::DICompileUnit Unit) {
446 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000447
Chris Lattner9c85ba32008-11-10 06:08:34 +0000448 // Add the result type at least.
449 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Chris Lattner9c85ba32008-11-10 06:08:34 +0000451 // Set up remainder of arguments if there is a prototype.
452 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000453 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000454 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
455 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
456 } else {
457 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000458 }
459
Chris Lattner9c85ba32008-11-10 06:08:34 +0000460 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000461 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Devang Patelca80a5f2009-10-20 19:55:01 +0000463 llvm::DIType DbgTy =
464 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
465 Unit, "", llvm::DICompileUnit(),
466 0, 0, 0, 0, 0,
467 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000468 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000469}
470
Devang Patel428deb52010-01-19 00:00:59 +0000471/// CollectRecordFields - A helper function to collect debug info for
472/// record fields. This is used while creating debug info entry for a Record.
473void CGDebugInfo::
Devang Patel239cec62010-02-01 21:39:52 +0000474CollectRecordFields(const RecordDecl *RD, llvm::DICompileUnit Unit,
Devang Patel428deb52010-01-19 00:00:59 +0000475 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
476 unsigned FieldNo = 0;
477 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel239cec62010-02-01 21:39:52 +0000478 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
479 for (RecordDecl::field_iterator I = RD->field_begin(),
480 E = RD->field_end();
Devang Patel428deb52010-01-19 00:00:59 +0000481 I != E; ++I, ++FieldNo) {
482 FieldDecl *Field = *I;
483 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
484
485 llvm::StringRef FieldName = Field->getName();
486
Devang Patel4835fdd2010-02-12 01:31:06 +0000487 // Ignore unnamed fields. Do not ignore unnamed records.
488 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel428deb52010-01-19 00:00:59 +0000489 continue;
490
491 // Get the location for the field.
492 SourceLocation FieldDefLoc = Field->getLocation();
493 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
494 llvm::DICompileUnit FieldDefUnit;
495 unsigned FieldLine = 0;
496
497 if (!PLoc.isInvalid()) {
498 FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
499 FieldLine = PLoc.getLine();
500 }
501
502 QualType FType = Field->getType();
503 uint64_t FieldSize = 0;
504 unsigned FieldAlign = 0;
505 if (!FType->isIncompleteArrayType()) {
506
507 // Bit size, align and offset of the type.
508 FieldSize = CGM.getContext().getTypeSize(FType);
509 Expr *BitWidth = Field->getBitWidth();
510 if (BitWidth)
511 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
512
513 FieldAlign = CGM.getContext().getTypeAlign(FType);
514 }
515
516 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
517
518 // Create a DW_TAG_member node to remember the offset of this field in the
519 // struct. FIXME: This is an absolutely insane way to capture this
520 // information. When we gut debug info, this should be fixed.
521 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
522 FieldName, FieldDefUnit,
523 FieldLine, FieldSize, FieldAlign,
524 FieldOffset, 0, FieldTy);
525 EltTys.push_back(FieldTy);
526 }
527}
528
Devang Patela6da1922010-01-28 00:28:01 +0000529/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
530/// function type is not updated to include implicit "this" pointer. Use this
531/// routine to get a method type which includes "this" pointer.
532llvm::DIType
533CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
534 llvm::DICompileUnit Unit) {
535 llvm::DIType FnTy = getOrCreateType(Method->getType(), Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000536
537 // Static methods do not need "this" pointer argument.
538 if (Method->isStatic())
539 return FnTy;
540
Devang Patela6da1922010-01-28 00:28:01 +0000541 // Add "this" pointer.
542
543 llvm::DIArray Args = llvm::DICompositeType(FnTy.getNode()).getTypeArray();
544 assert (Args.getNumElements() && "Invalid number of arguments!");
545
546 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
547
548 // First element is always return type. For 'void' functions it is NULL.
549 Elts.push_back(Args.getElement(0));
550
551 // "this" pointer is always first argument.
552 ASTContext &Context = CGM.getContext();
553 QualType ThisPtr =
554 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
Devang Patel337472d2010-02-09 17:57:50 +0000555 llvm::DIType ThisPtrType =
556 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
557 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType.getNode();
558 Elts.push_back(ThisPtrType);
Devang Patela6da1922010-01-28 00:28:01 +0000559
560 // Copy rest of the arguments.
561 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
562 Elts.push_back(Args.getElement(i));
563
564 llvm::DIArray EltTypeArray =
565 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
566
567 return
568 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
569 Unit, "", llvm::DICompileUnit(),
570 0, 0, 0, 0, 0,
571 llvm::DIType(), EltTypeArray);
572}
573
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000574/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
575/// a single member function GlobalDecl.
576llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000577CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000578 llvm::DICompileUnit Unit,
579 llvm::DICompositeType &RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000580 bool IsCtorOrDtor =
581 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
582
583 llvm::StringRef MethodName = getFunctionName(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000584 llvm::StringRef MethodLinkageName;
Devang Patela6da1922010-01-28 00:28:01 +0000585 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000586
587 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
588 // make sense to give a single ctor/dtor a linkage name.
589 if (!IsCtorOrDtor)
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000590 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000591
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000592 SourceManager &SM = CGM.getContext().getSourceManager();
593
594 // Get the location for the method.
595 SourceLocation MethodDefLoc = Method->getLocation();
596 PresumedLoc PLoc = SM.getPresumedLoc(MethodDefLoc);
597 llvm::DICompileUnit MethodDefUnit;
598 unsigned MethodLine = 0;
599
600 if (!PLoc.isInvalid()) {
601 MethodDefUnit = getOrCreateCompileUnit(MethodDefLoc);
602 MethodLine = PLoc.getLine();
603 }
604
605 // Collect virtual method info.
606 llvm::DIType ContainingType;
607 unsigned Virtuality = 0;
608 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000609
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000610 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000611 if (Method->isPure())
612 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
613 else
614 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
615
616 // It doesn't make sense to give a virtual destructor a vtable index,
617 // since a single destructor has two entries in the vtable.
618 if (!isa<CXXDestructorDecl>(Method))
619 VIndex = CGM.getVtableInfo().getMethodVtableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000620 ContainingType = RecordTy;
621 }
622
623 llvm::DISubprogram SP =
624 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
625 MethodLinkageName,
626 MethodDefUnit, MethodLine,
627 MethodTy, /*isLocalToUnit=*/false,
628 Method->isThisDeclarationADefinition(),
629 Virtuality, VIndex, ContainingType);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000630
631 // Don't cache ctors or dtors since we have to emit multiple functions for
632 // a single ctor or dtor.
633 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
634 SPCache[Method] = llvm::WeakVH(SP.getNode());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000635
636 return SP;
637}
638
Devang Patel4125fd22010-01-19 01:54:44 +0000639/// CollectCXXMemberFunctions - A helper function to collect debug info for
640/// C++ member functions.This is used while creating debug info entry for
641/// a Record.
642void CGDebugInfo::
Devang Patel239cec62010-02-01 21:39:52 +0000643CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DICompileUnit Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000644 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
645 llvm::DICompositeType &RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000646 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
647 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000648 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000649
Devang Pateld5322da2010-02-09 19:09:28 +0000650 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000651 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000652
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000653 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000654 }
655}
656
Devang Patela245c5b2010-01-25 23:32:18 +0000657/// CollectCXXBases - A helper function to collect debug info for
658/// C++ base classes. This is used while creating debug info entry for
659/// a Record.
660void CGDebugInfo::
Devang Patel239cec62010-02-01 21:39:52 +0000661CollectCXXBases(const CXXRecordDecl *RD, llvm::DICompileUnit Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000662 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
663 llvm::DICompositeType &RecordTy) {
664
Devang Patel239cec62010-02-01 21:39:52 +0000665 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
666 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
667 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000668 unsigned BFlags = 0;
669 uint64_t BaseOffset;
670
671 const CXXRecordDecl *Base =
672 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
673
674 if (BI->isVirtual()) {
Devang Pateld5322da2010-02-09 19:09:28 +0000675 // virtual base offset index is -ve. The code generator emits dwarf
676 // expression where it expects +ve number.
677 BaseOffset = 0 - CGM.getVtableInfo().getVirtualBaseOffsetIndex(RD, Base);
Devang Patelca7daed2010-01-28 21:54:15 +0000678 BFlags = llvm::DIType::FlagVirtual;
679 } else
680 BaseOffset = RL.getBaseClassOffset(Base);
681
682 AccessSpecifier Access = BI->getAccessSpecifier();
683 if (Access == clang::AS_private)
684 BFlags |= llvm::DIType::FlagPrivate;
685 else if (Access == clang::AS_protected)
686 BFlags |= llvm::DIType::FlagProtected;
687
688 llvm::DIType DTy =
689 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
690 RecordTy, llvm::StringRef(),
691 llvm::DICompileUnit(), 0, 0, 0,
692 BaseOffset, BFlags,
693 getOrCreateType(BI->getType(),
694 Unit));
695 EltTys.push_back(DTy);
696 }
Devang Patela245c5b2010-01-25 23:32:18 +0000697}
698
Devang Patel4ce3f202010-01-28 18:11:52 +0000699/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
700llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DICompileUnit Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000701 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000702 return VTablePtrType;
703
704 ASTContext &Context = CGM.getContext();
705
706 /* Function type */
707 llvm::SmallVector<llvm::DIDescriptor, 16> STys;
708 STys.push_back(getOrCreateType(Context.IntTy, Unit));
709 llvm::DIArray SElements =
710 DebugFactory.GetOrCreateArray(STys.data(), STys.size());
711 llvm::DIType SubTy =
712 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
713 Unit, "", llvm::DICompileUnit(),
714 0, 0, 0, 0, 0, llvm::DIType(), SElements);
715
716 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
717 llvm::DIType vtbl_ptr_type
718 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
719 Unit, "__vtbl_ptr_type", llvm::DICompileUnit(),
720 0, Size, 0, 0, 0, SubTy);
721
722 VTablePtrType = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
723 Unit, "", llvm::DICompileUnit(),
724 0, Size, 0, 0, 0, vtbl_ptr_type);
725 return VTablePtrType;
726}
727
728/// getVtableName - Get vtable name for the given Class.
Devang Patel239cec62010-02-01 21:39:52 +0000729llvm::StringRef CGDebugInfo::getVtableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000730 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000731 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000732
733 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000734 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000735 memcpy(StrPtr, Name.data(), Name.length());
736 return llvm::StringRef(StrPtr, Name.length());
737}
738
739
740/// CollectVtableInfo - If the C++ class has vtable info then insert appropriate
741/// debug info entry in EltTys vector.
742void CGDebugInfo::
Devang Patel239cec62010-02-01 21:39:52 +0000743CollectVtableInfo(const CXXRecordDecl *RD, llvm::DICompileUnit Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000744 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000745 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000746
747 // If there is a primary base then it will hold vtable info.
748 if (RL.getPrimaryBase())
749 return;
750
751 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000752 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000753 return;
754
755 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
756 llvm::DIType VPTR
757 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel239cec62010-02-01 21:39:52 +0000758 getVtableName(RD), llvm::DICompileUnit(),
Devang Patel4ce3f202010-01-28 18:11:52 +0000759 0, Size, 0, 0, 0,
760 getOrCreateVTablePtrType(Unit));
761 EltTys.push_back(VPTR);
762}
763
Devang Patel65e99f22009-02-25 01:36:11 +0000764/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000765llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
766 llvm::DICompileUnit Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000767 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Chris Lattner9c85ba32008-11-10 06:08:34 +0000769 unsigned Tag;
Devang Pateld6c5a262010-02-01 21:52:22 +0000770 if (RD->isStruct())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000771 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Pateld6c5a262010-02-01 21:52:22 +0000772 else if (RD->isUnion())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000773 Tag = llvm::dwarf::DW_TAG_union_type;
774 else {
Devang Pateld6c5a262010-02-01 21:52:22 +0000775 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000776 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000777 }
778
Anders Carlsson20f12a22009-12-06 18:00:51 +0000779 SourceManager &SM = CGM.getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000780
Chris Lattner9c85ba32008-11-10 06:08:34 +0000781 // Get overall information about the record type for the debug info.
Devang Pateld6c5a262010-02-01 21:52:22 +0000782 PresumedLoc PLoc = SM.getPresumedLoc(RD->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000783 llvm::DICompileUnit DefUnit;
784 unsigned Line = 0;
785 if (!PLoc.isInvalid()) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000786 DefUnit = getOrCreateCompileUnit(RD->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000787 Line = PLoc.getLine();
788 }
Mike Stump1eb44332009-09-09 15:08:12 +0000789
Chris Lattner9c85ba32008-11-10 06:08:34 +0000790 // Records and classes and unions can all be recursive. To handle them, we
791 // first generate a debug descriptor for the struct as a forward declaration.
792 // Then (if it is a definition) we go through and get debug info for all of
793 // its members. Finally, we create a descriptor for the complete type (which
794 // may refer to the forward decl if the struct is recursive) and replace all
795 // uses of the forward declaration with the final definition.
Devang Pateld0f251b2010-01-20 23:56:40 +0000796
Devang Pateld6c5a262010-02-01 21:52:22 +0000797 // A RD->getName() is not unique. However, the debug info descriptors
Devang Patelce78c972010-02-01 22:51:29 +0000798 // are uniqued so use type name to ensure uniquness.
Devang Pateld0f251b2010-01-20 23:56:40 +0000799 std::string STy = QualType(Ty, 0).getAsString();
Devang Patel411894b2010-02-01 22:40:08 +0000800 llvm::DIDescriptor FDContext =
801 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel0ce73f62009-07-22 18:57:00 +0000802 llvm::DICompositeType FwdDecl =
Devang Patel411894b2010-02-01 22:40:08 +0000803 DebugFactory.CreateCompositeType(Tag, FDContext,
804 STy.c_str(),
Devang Patelab71ff52009-11-12 00:51:46 +0000805 DefUnit, Line, 0, 0, 0, 0,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000806 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000807
Chris Lattner9c85ba32008-11-10 06:08:34 +0000808 // If this is just a forward declaration, return it.
Douglas Gregor952b0172010-02-11 01:04:33 +0000809 if (!RD->getDefinition())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000810 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000811
Eli Friedman14d63652009-11-16 21:04:30 +0000812 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000813 // Otherwise, insert it into the TypeCache so that recursive uses will find
814 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000815 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000816
817 // Convert all the elements.
818 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
819
Devang Pateld6c5a262010-02-01 21:52:22 +0000820 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000821 if (CXXDecl) {
822 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel4ce3f202010-01-28 18:11:52 +0000823 CollectVtableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000824 }
Devang Pateld6c5a262010-02-01 21:52:22 +0000825 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +0000826 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +0000827 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +0000828 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +0000829
830 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +0000831 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel0ac8f312010-01-28 00:54:21 +0000832 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
833 ContainingType =
834 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit).getNode();
835 else if (CXXDecl->isDynamicClass())
836 ContainingType = FwdDecl.getNode();
Devang Patela245c5b2010-01-25 23:32:18 +0000837 }
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Chris Lattner9c85ba32008-11-10 06:08:34 +0000839 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000840 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000841
842 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000843 uint64_t Size = CGM.getContext().getTypeSize(Ty);
844 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000845
Devang Patel411894b2010-02-01 22:40:08 +0000846 llvm::DIDescriptor RDContext =
847 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel0ce73f62009-07-22 18:57:00 +0000848 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +0000849 DebugFactory.CreateCompositeType(Tag, RDContext,
850 RD->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000851 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +0000852 llvm::DIType(), Elements,
853 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000854
855 // Now that we have a real decl for the struct, replace anything using the
856 // old decl with the new one. This will recursively update the debug info.
Eli Friedman14d63652009-11-16 21:04:30 +0000857 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000858
Chris Lattner9c85ba32008-11-10 06:08:34 +0000859 return RealDecl;
860}
861
Devang Patel9ca36b62009-02-26 21:10:26 +0000862/// CreateType - get objective-c interface type.
863llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
864 llvm::DICompileUnit Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000865 ObjCInterfaceDecl *ID = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000866
Devang Patel9ca36b62009-02-26 21:10:26 +0000867 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000868 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel9ca36b62009-02-26 21:10:26 +0000869
870 // Get overall information about the record type for the debug info.
Devang Pateld6c5a262010-02-01 21:52:22 +0000871 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(ID->getLocation());
872 PresumedLoc PLoc = SM.getPresumedLoc(ID->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000873 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
874
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Daniel Dunbard86d3362009-05-18 20:51:58 +0000876 unsigned RuntimeLang = DefUnit.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000877
Devang Patel9ca36b62009-02-26 21:10:26 +0000878 // To handle recursive interface, we
879 // first generate a debug descriptor for the struct as a forward declaration.
880 // Then (if it is a definition) we go through and get debug info for all of
881 // its members. Finally, we create a descriptor for the complete type (which
882 // may refer to the forward decl if the struct is recursive) and replace all
883 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000884 llvm::DICompositeType FwdDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +0000885 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000886 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000887 llvm::DIType(), llvm::DIArray(),
888 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000889
Devang Patel9ca36b62009-02-26 21:10:26 +0000890 // If this is just a forward declaration, return it.
Devang Pateld6c5a262010-02-01 21:52:22 +0000891 if (ID->isForwardDecl())
Devang Patel9ca36b62009-02-26 21:10:26 +0000892 return FwdDecl;
893
Devang Patelffffb032009-11-16 20:09:38 +0000894 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000895 // Otherwise, insert it into the TypeCache so that recursive uses will find
896 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000897 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000898
899 // Convert all the elements.
900 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
901
Devang Pateld6c5a262010-02-01 21:52:22 +0000902 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +0000903 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000904 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000905 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000906 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000907 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Chris Lattner9e55b8a2009-05-05 05:05:36 +0000908 Unit, "", llvm::DICompileUnit(), 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000909 0 /* offset */, 0, SClassTy);
910 EltTys.push_back(InhTag);
911 }
912
Devang Pateld6c5a262010-02-01 21:52:22 +0000913 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +0000914
915 unsigned FieldNo = 0;
Devang Pateld6c5a262010-02-01 21:52:22 +0000916 for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(),
917 E = ID->ivar_end(); I != E; ++I, ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +0000918 ObjCIvarDecl *Field = *I;
919 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
920
Devang Patel73621622009-11-25 17:37:31 +0000921 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +0000922
Devang Patelde135022009-04-27 22:40:36 +0000923 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +0000924 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +0000925 continue;
926
Devang Patel9ca36b62009-02-26 21:10:26 +0000927 // Get the location for the field.
928 SourceLocation FieldDefLoc = Field->getLocation();
929 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000930 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
931 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
932
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Devang Patel99c20eb2009-03-20 18:24:39 +0000934 QualType FType = Field->getType();
935 uint64_t FieldSize = 0;
936 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000937
Devang Patel99c20eb2009-03-20 18:24:39 +0000938 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000939
Devang Patel99c20eb2009-03-20 18:24:39 +0000940 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000941 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000942 Expr *BitWidth = Field->getBitWidth();
943 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +0000944 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000945
Anders Carlsson20f12a22009-12-06 18:00:51 +0000946 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000947 }
948
Mike Stump1eb44332009-09-09 15:08:12 +0000949 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
950
Devang Patelc20482b2009-03-19 00:23:53 +0000951 unsigned Flags = 0;
952 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
953 Flags = llvm::DIType::FlagProtected;
954 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
955 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +0000956
Devang Patel9ca36b62009-02-26 21:10:26 +0000957 // Create a DW_TAG_member node to remember the offset of this field in the
958 // struct. FIXME: This is an absolutely insane way to capture this
959 // information. When we gut debug info, this should be fixed.
960 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
961 FieldName, FieldDefUnit,
962 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000963 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000964 EltTys.push_back(FieldTy);
965 }
Mike Stump1eb44332009-09-09 15:08:12 +0000966
Devang Patel9ca36b62009-02-26 21:10:26 +0000967 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000968 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +0000969
970 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000971 uint64_t Size = CGM.getContext().getTypeSize(Ty);
972 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Devang Patel6c1fddf2009-07-27 18:42:03 +0000974 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +0000975 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +0000976 Line, Size, Align, 0, 0, llvm::DIType(),
977 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000978
979 // Now that we have a real decl for the struct, replace anything using the
980 // old decl with the new one. This will recursively update the debug info.
Devang Patelffffb032009-11-16 20:09:38 +0000981 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000982
Devang Patel9ca36b62009-02-26 21:10:26 +0000983 return RealDecl;
984}
985
Chris Lattner9c85ba32008-11-10 06:08:34 +0000986llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
987 llvm::DICompileUnit Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000988 EnumDecl *ED = Ty->getDecl();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000989
990 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
991
992 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +0000993 for (EnumDecl::enumerator_iterator
Devang Pateld6c5a262010-02-01 21:52:22 +0000994 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000995 Enum != EnumEnd; ++Enum) {
Devang Patel73621622009-11-25 17:37:31 +0000996 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor44b43212008-12-11 16:49:14 +0000997 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000998 }
Mike Stump1eb44332009-09-09 15:08:12 +0000999
Chris Lattner9c85ba32008-11-10 06:08:34 +00001000 // Return a CompositeType for the enum itself.
1001 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +00001002 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001003
Devang Pateld6c5a262010-02-01 21:52:22 +00001004 SourceLocation DefLoc = ED->getLocation();
Chris Lattner9c85ba32008-11-10 06:08:34 +00001005 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001006 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001007 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
1008 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
1009
Mike Stump1eb44332009-09-09 15:08:12 +00001010
Chris Lattner9c85ba32008-11-10 06:08:34 +00001011 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +00001012 uint64_t Size = 0;
1013 unsigned Align = 0;
1014 if (!Ty->isIncompleteType()) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001015 Size = CGM.getContext().getTypeSize(Ty);
1016 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman3189e4b2009-05-04 04:39:55 +00001017 }
Mike Stump1eb44332009-09-09 15:08:12 +00001018
Devang Patelca80a5f2009-10-20 19:55:01 +00001019 llvm::DIType DbgTy =
1020 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Pateld6c5a262010-02-01 21:52:22 +00001021 Unit, ED->getName(), DefUnit, Line,
Devang Patelca80a5f2009-10-20 19:55:01 +00001022 Size, Align, 0, 0,
1023 llvm::DIType(), EltArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001024 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001025}
1026
1027llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
1028 llvm::DICompileUnit Unit) {
1029 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1030 return CreateType(RT, Unit);
1031 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1032 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001033
Chris Lattner9c85ba32008-11-10 06:08:34 +00001034 return llvm::DIType();
1035}
1036
Devang Patel70c23cd2010-02-23 22:59:39 +00001037llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
1038 llvm::DICompileUnit Unit) {
1039 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1040 uint64_t NumElems = Ty->getNumElements();
1041 if (NumElems > 0)
1042 --NumElems;
1043 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1044 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, NumElems));
1045
1046 llvm::DIArray SubscriptArray =
1047 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
1048
1049 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1050 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1051
1052 return
1053 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
1054 Unit, "", llvm::DICompileUnit(),
1055 0, Size, Align, 0, 0,
1056 ElementTy, SubscriptArray);
1057}
1058
Chris Lattner9c85ba32008-11-10 06:08:34 +00001059llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
1060 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001061 uint64_t Size;
1062 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001063
1064
Nuno Lopes010d5142009-01-28 00:35:17 +00001065 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001066 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001067 Size = 0;
1068 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001069 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001070 } else if (Ty->isIncompleteArrayType()) {
1071 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001072 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001073 } else {
1074 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001075 Size = CGM.getContext().getTypeSize(Ty);
1076 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001077 }
Mike Stump1eb44332009-09-09 15:08:12 +00001078
Chris Lattner9c85ba32008-11-10 06:08:34 +00001079 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1080 // interior arrays, do we care? Why aren't nested arrays represented the
1081 // obvious/recursive way?
1082 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1083 QualType EltTy(Ty, 0);
1084 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +00001085 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001086 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +00001087 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +00001088 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001089 // FIXME: Verify this is right for VLAs.
1090 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1091 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +00001092 }
Mike Stump1eb44332009-09-09 15:08:12 +00001093
Chris Lattner9c85ba32008-11-10 06:08:34 +00001094 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001095 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001096
Devang Patelca80a5f2009-10-20 19:55:01 +00001097 llvm::DIType DbgTy =
1098 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
1099 Unit, "", llvm::DICompileUnit(),
1100 0, Size, Align, 0, 0,
1101 getOrCreateType(EltTy, Unit),
1102 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001103 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001104}
1105
Anders Carlssona031b352009-11-06 19:19:55 +00001106llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
1107 llvm::DICompileUnit Unit) {
1108 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1109 Ty, Ty->getPointeeType(), Unit);
1110}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001111
Anders Carlsson20f12a22009-12-06 18:00:51 +00001112llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
1113 llvm::DICompileUnit U) {
1114 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1115 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1116
1117 if (!Ty->getPointeeType()->isFunctionType()) {
1118 // We have a data member pointer type.
1119 return PointerDiffDITy;
1120 }
1121
1122 // We have a member function pointer type. Treat it as a struct with two
1123 // ptrdiff_t members.
1124 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1125
1126 uint64_t FieldOffset = 0;
1127 llvm::DIDescriptor ElementTypes[2];
1128
1129 // FIXME: This should probably be a function type instead.
1130 ElementTypes[0] =
1131 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
1132 "ptr", llvm::DICompileUnit(), 0,
1133 Info.first, Info.second, FieldOffset, 0,
1134 PointerDiffDITy);
1135 FieldOffset += Info.first;
1136
1137 ElementTypes[1] =
1138 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
1139 "ptr", llvm::DICompileUnit(), 0,
1140 Info.first, Info.second, FieldOffset, 0,
1141 PointerDiffDITy);
1142
1143 llvm::DIArray Elements =
1144 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1145 llvm::array_lengthof(ElementTypes));
1146
1147 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1148 U, llvm::StringRef("test"),
1149 llvm::DICompileUnit(), 0, FieldOffset,
1150 0, 0, 0, llvm::DIType(), Elements);
1151}
1152
Douglas Gregor840943d2009-12-21 20:18:30 +00001153static QualType UnwrapTypeForDebugInfo(QualType T) {
1154 do {
1155 QualType LastT = T;
1156 switch (T->getTypeClass()) {
1157 default:
1158 return T;
1159 case Type::TemplateSpecialization:
1160 T = cast<TemplateSpecializationType>(T)->desugar();
1161 break;
1162 case Type::TypeOfExpr: {
1163 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1164 T = Ty->getUnderlyingExpr()->getType();
1165 break;
1166 }
1167 case Type::TypeOf:
1168 T = cast<TypeOfType>(T)->getUnderlyingType();
1169 break;
1170 case Type::Decltype:
1171 T = cast<DecltypeType>(T)->getUnderlyingType();
1172 break;
1173 case Type::QualifiedName:
1174 T = cast<QualifiedNameType>(T)->getNamedType();
1175 break;
1176 case Type::SubstTemplateTypeParm:
1177 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1178 break;
1179 case Type::Elaborated:
1180 T = cast<ElaboratedType>(T)->getUnderlyingType();
1181 break;
1182 }
1183
1184 assert(T != LastT && "Type unwrapping failed to unwrap!");
1185 if (T == LastT)
1186 return T;
1187 } while (true);
1188
1189 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001190}
1191
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001192/// getOrCreateType - Get the type from the cache or create a new
1193/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001194llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
1195 llvm::DICompileUnit Unit) {
1196 if (Ty.isNull())
1197 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001198
Douglas Gregor840943d2009-12-21 20:18:30 +00001199 // Unwrap the type as needed for debug information.
1200 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001201
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001202 // Check for existing entry.
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001203 std::map<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001204 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001205 if (it != TypeCache.end()) {
1206 // Verify that the debug info still exists.
1207 if (&*it->second)
1208 return llvm::DIType(cast<llvm::MDNode>(it->second));
1209 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001210
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001211 // Otherwise create the type.
1212 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001213
1214 // And update the type cache.
1215 TypeCache[Ty.getAsOpaquePtr()] = Res.getNode();
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001216 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001217}
1218
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001219/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001220llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
1221 llvm::DICompileUnit Unit) {
John McCalla1805292009-09-25 01:40:47 +00001222 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001223 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001224 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001225
Douglas Gregor2101a822009-12-21 19:57:21 +00001226 const char *Diag = 0;
1227
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001228 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001229 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001230#define TYPE(Class, Base)
1231#define ABSTRACT_TYPE(Class, Base)
1232#define NON_CANONICAL_TYPE(Class, Base)
1233#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1234#include "clang/AST/TypeNodes.def"
1235 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001236
Anders Carlssonbfe69952009-11-06 18:24:04 +00001237 // FIXME: Handle these.
1238 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001239 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001240
1241 case Type::Vector:
1242 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001243 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001244 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001245 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001246 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1247 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1248 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1249 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001250 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001251 return CreateType(cast<BlockPointerType>(Ty), Unit);
1252 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001253 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001254 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001255 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001256 case Type::FunctionProto:
1257 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001258 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001259 case Type::ConstantArray:
1260 case Type::VariableArray:
1261 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001262 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001263
1264 case Type::LValueReference:
1265 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1266
Anders Carlsson20f12a22009-12-06 18:00:51 +00001267 case Type::MemberPointer:
1268 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001269
1270 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001271 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001272 case Type::QualifiedName:
Douglas Gregor2101a822009-12-21 19:57:21 +00001273 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001274 case Type::TypeOfExpr:
1275 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001276 case Type::Decltype:
1277 llvm_unreachable("type should have been unwrapped!");
1278 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001279
1280 case Type::RValueReference:
1281 // FIXME: Implement!
1282 Diag = "rvalue references";
1283 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001284 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001285
1286 assert(Diag && "Fall through without a diagnostic?");
1287 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1288 "debug information for %0 is not yet supported");
1289 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1290 << Diag;
1291 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001292}
1293
1294/// EmitFunctionStart - Constructs the debug code for entering a function -
1295/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001296void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001297 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001298 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001299
Devang Patel9c6c3a02010-01-14 00:36:21 +00001300 llvm::StringRef Name;
1301 llvm::StringRef LinkageName;
1302
1303 const Decl *D = GD.getDecl();
1304 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001305 // If there is a DISubprogram for this function available then use it.
1306 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1307 FI = SPCache.find(FD);
1308 if (FI != SPCache.end()) {
Devang Patel0804e6e2010-03-08 20:53:17 +00001309 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
1310 if (SP.isSubprogram() && llvm::DISubprogram(SP.getNode()).isDefinition()) {
Devang Patel4125fd22010-01-19 01:54:44 +00001311 RegionStack.push_back(SP.getNode());
Devang Patel3dd96a12010-01-29 18:11:03 +00001312 RegionMap[D] = llvm::WeakVH(SP.getNode());
Devang Patel4125fd22010-01-19 01:54:44 +00001313 return;
1314 }
1315 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001316 Name = getFunctionName(FD);
Eli Friedman3364e622010-01-16 00:43:13 +00001317 if (!Name.empty() && Name[0] == '\01')
Devang Patelaa97d702010-01-14 21:46:57 +00001318 Name = Name.substr(1);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001319 // Use mangled name as linkage name for c/c++ functions.
Devang Patelaa97d702010-01-14 21:46:57 +00001320 LinkageName = CGM.getMangledName(GD);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001321 } else {
1322 // Use llvm function name as linkage name.
1323 Name = Fn->getName();
Devang Patel9c6c3a02010-01-14 00:36:21 +00001324 LinkageName = Name;
Devang Patel17584202010-01-19 00:25:12 +00001325 if (!Name.empty() && Name[0] == '\01')
1326 Name = Name.substr(1);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001327 }
Mike Stump1eb44332009-09-09 15:08:12 +00001328
Devang Patel98a200b2010-01-14 18:06:13 +00001329 // It is expected that CurLoc is set before using EmitFunctionStart.
1330 // Usually, CurLoc points to the left bracket location of compound
1331 // statement representing function body.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001332 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001333 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +00001334 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Mike Stump1eb44332009-09-09 15:08:12 +00001335
Chris Lattner9c85ba32008-11-10 06:08:34 +00001336 llvm::DISubprogram SP =
Devang Patel6dba4322009-07-14 21:31:22 +00001337 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001338 getOrCreateType(FnType, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001339 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump1eb44332009-09-09 15:08:12 +00001340
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001341 // Push function on region stack.
Devang Patel8fae0602009-11-13 19:10:24 +00001342 RegionStack.push_back(SP.getNode());
Devang Patel3dd96a12010-01-29 18:11:03 +00001343 RegionMap[D] = llvm::WeakVH(SP.getNode());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001344}
1345
1346
Chris Lattner9c85ba32008-11-10 06:08:34 +00001347void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001348 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001349
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001350 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001351 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001352 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001353 || (SM.getInstantiationLineNumber(CurLoc) ==
1354 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001355 && SM.isFromSameFile(CurLoc, PrevLoc)))
1356 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001357
1358 // Update last state.
1359 PrevLoc = CurLoc;
1360
1361 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001362 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +00001363 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patelbbd9fa42009-10-06 18:36:08 +00001364
Devang Patel8fae0602009-11-13 19:10:24 +00001365 llvm::DIDescriptor DR(RegionStack.back());
Devang Patelbbd9fa42009-10-06 18:36:08 +00001366 llvm::DIScope DS = llvm::DIScope(DR.getNode());
1367 llvm::DILocation DO(NULL);
1368 llvm::DILocation DL =
1369 DebugFactory.CreateLocation(PLoc.getLine(), PLoc.getColumn(),
1370 DS, DO);
1371 Builder.SetCurrentDebugLocation(DL.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001372}
1373
1374/// EmitRegionStart- Constructs the debug code for entering a declarative
1375/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +00001376void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
Devang Pateld19429f2010-02-16 21:41:20 +00001377 SourceManager &SM = CGM.getContext().getSourceManager();
1378 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patel8fae0602009-11-13 19:10:24 +00001379 llvm::DIDescriptor D =
1380 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1381 llvm::DIDescriptor() :
Devang Pateld19429f2010-02-16 21:41:20 +00001382 llvm::DIDescriptor(RegionStack.back()),
1383 PLoc.getLine(), PLoc.getColumn());
Devang Patel8fae0602009-11-13 19:10:24 +00001384 RegionStack.push_back(D.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001385}
1386
1387/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1388/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +00001389void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001390 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1391
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001392 // Provide an region stop point.
1393 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001394
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001395 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001396}
1397
Devang Patel809b9bb2010-02-10 18:49:08 +00001398// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1399// See BuildByRefType.
1400llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1401 uint64_t *XOffset) {
1402
1403 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1404
1405 QualType FType;
1406 uint64_t FieldSize, FieldOffset;
1407 unsigned FieldAlign;
1408
1409 llvm::DICompileUnit Unit = getOrCreateCompileUnit(VD->getLocation());
1410 QualType Type = VD->getType();
1411
1412 FieldOffset = 0;
1413 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
1414 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1415 FieldSize = CGM.getContext().getTypeSize(FType);
1416 FieldAlign = CGM.getContext().getTypeAlign(FType);
1417 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1418 "__isa", llvm::DICompileUnit(),
1419 0, FieldSize, FieldAlign,
1420 FieldOffset, 0, FieldTy);
1421 EltTys.push_back(FieldTy);
1422 FieldOffset += FieldSize;
1423
1424 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
1425 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1426 FieldSize = CGM.getContext().getTypeSize(FType);
1427 FieldAlign = CGM.getContext().getTypeAlign(FType);
1428 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1429 "__forwarding", llvm::DICompileUnit(),
1430 0, FieldSize, FieldAlign,
1431 FieldOffset, 0, FieldTy);
1432 EltTys.push_back(FieldTy);
1433 FieldOffset += FieldSize;
1434
1435 FType = CGM.getContext().IntTy;
1436 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1437 FieldSize = CGM.getContext().getTypeSize(FType);
1438 FieldAlign = CGM.getContext().getTypeAlign(FType);
1439 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1440 "__flags", llvm::DICompileUnit(),
1441 0, FieldSize, FieldAlign,
1442 FieldOffset, 0, FieldTy);
1443 EltTys.push_back(FieldTy);
1444 FieldOffset += FieldSize;
1445
1446 FType = CGM.getContext().IntTy;
1447 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1448 FieldSize = CGM.getContext().getTypeSize(FType);
1449 FieldAlign = CGM.getContext().getTypeAlign(FType);
1450 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1451 "__size", llvm::DICompileUnit(),
1452 0, FieldSize, FieldAlign,
1453 FieldOffset, 0, FieldTy);
1454 EltTys.push_back(FieldTy);
1455 FieldOffset += FieldSize;
1456
1457 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1458 if (HasCopyAndDispose) {
1459 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
1460 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1461 FieldSize = CGM.getContext().getTypeSize(FType);
1462 FieldAlign = CGM.getContext().getTypeAlign(FType);
1463 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1464 "__copy_helper",
1465 llvm::DICompileUnit(),
1466 0, FieldSize, FieldAlign,
1467 FieldOffset, 0, FieldTy);
1468 EltTys.push_back(FieldTy);
1469 FieldOffset += FieldSize;
1470
1471 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
1472 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1473 FieldSize = CGM.getContext().getTypeSize(FType);
1474 FieldAlign = CGM.getContext().getTypeAlign(FType);
1475 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1476 "__destroy_helper",
1477 llvm::DICompileUnit(),
1478 0, FieldSize, FieldAlign,
1479 FieldOffset, 0, FieldTy);
1480 EltTys.push_back(FieldTy);
1481 FieldOffset += FieldSize;
1482 }
1483
1484 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1485 if (Align > CharUnits::fromQuantity(
1486 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1487 unsigned AlignedOffsetInBytes
1488 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1489 unsigned NumPaddingBytes
1490 = AlignedOffsetInBytes - FieldOffset/8;
1491
1492 if (NumPaddingBytes > 0) {
1493 llvm::APInt pad(32, NumPaddingBytes);
1494 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1495 pad, ArrayType::Normal, 0);
1496 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1497 FieldSize = CGM.getContext().getTypeSize(FType);
1498 FieldAlign = CGM.getContext().getTypeAlign(FType);
1499 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1500 Unit, "", llvm::DICompileUnit(),
1501 0, FieldSize, FieldAlign,
1502 FieldOffset, 0, FieldTy);
1503 EltTys.push_back(FieldTy);
1504 FieldOffset += FieldSize;
1505 }
1506 }
1507
1508 FType = Type;
1509 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1510 FieldSize = CGM.getContext().getTypeSize(FType);
1511 FieldAlign = Align.getQuantity()*8;
1512
1513 *XOffset = FieldOffset;
1514 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1515 VD->getName(), llvm::DICompileUnit(),
1516 0, FieldSize, FieldAlign,
1517 FieldOffset, 0, FieldTy);
1518 EltTys.push_back(FieldTy);
1519 FieldOffset += FieldSize;
1520
1521 llvm::DIArray Elements =
1522 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1523
1524 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1525
1526 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1527 Unit, "",
1528 llvm::DICompileUnit(),
1529 0, FieldOffset, 0, 0, Flags,
1530 llvm::DIType(), Elements);
1531
1532}
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001533/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001534void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001535 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001536 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1537
Devang Patel07739032009-03-27 23:16:32 +00001538 // Do not emit variable debug information while generating optimized code.
1539 // The llvm optimizer and code generator are not yet ready to support
1540 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001541 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001542 if (CGO.OptimizationLevel)
Devang Patel07739032009-03-27 23:16:32 +00001543 return;
1544
Devang Patel239cec62010-02-01 21:39:52 +00001545 llvm::DICompileUnit Unit = getOrCreateCompileUnit(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001546 llvm::DIType Ty;
1547 uint64_t XOffset = 0;
1548 if (VD->hasAttr<BlocksAttr>())
1549 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1550 else
1551 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001552
Chris Lattner9c85ba32008-11-10 06:08:34 +00001553 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001554 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel239cec62010-02-01 21:39:52 +00001555 PresumedLoc PLoc = SM.getPresumedLoc(VD->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +00001556 unsigned Line = 0;
Eli Friedman1468ac72009-11-16 20:33:31 +00001557 unsigned Column = 0;
Devang Pateld263e7b2010-02-10 01:09:50 +00001558 if (PLoc.isInvalid())
1559 PLoc = SM.getPresumedLoc(CurLoc);
1560 if (PLoc.isValid()) {
Chris Lattner650cea92009-05-05 04:57:08 +00001561 Line = PLoc.getLine();
Eli Friedman1468ac72009-11-16 20:33:31 +00001562 Column = PLoc.getColumn();
Devang Pateld263e7b2010-02-10 01:09:50 +00001563 Unit = getOrCreateCompileUnit(CurLoc);
Eli Friedman1468ac72009-11-16 20:33:31 +00001564 } else {
Chris Lattner650cea92009-05-05 04:57:08 +00001565 Unit = llvm::DICompileUnit();
Eli Friedman1468ac72009-11-16 20:33:31 +00001566 }
Mike Stump1eb44332009-09-09 15:08:12 +00001567
Chris Lattner9c85ba32008-11-10 06:08:34 +00001568 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001569 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001570 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel239cec62010-02-01 21:39:52 +00001571 VD->getName(),
Chris Lattner650cea92009-05-05 04:57:08 +00001572 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001573 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001574 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001575 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001576
Devang Patel8fae0602009-11-13 19:10:24 +00001577 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001578 llvm::DILocation DO(NULL);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001579 llvm::DILocation DL = DebugFactory.CreateLocation(Line, Column, DS, DO);
1580
Chris Lattner23e92c02009-12-28 23:41:39 +00001581 Call->setMetadata("dbg", DL.getNode());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001582}
1583
Mike Stumpb1a6e682009-09-30 02:43:10 +00001584/// EmitDeclare - Emit local variable declaration debug info.
1585void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1586 llvm::Value *Storage, CGBuilderTy &Builder,
1587 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001588 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001589 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1590
1591 // Do not emit variable debug information while generating optimized code.
1592 // The llvm optimizer and code generator are not yet ready to support
1593 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001594 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001595 if (CGO.OptimizationLevel || Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001596 return;
1597
1598 uint64_t XOffset = 0;
Devang Pateld6c5a262010-02-01 21:52:22 +00001599 llvm::DICompileUnit Unit = getOrCreateCompileUnit(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001600 llvm::DIType Ty;
1601 if (VD->hasAttr<BlocksAttr>())
1602 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1603 else
1604 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001605
1606 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001607 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateld6c5a262010-02-01 21:52:22 +00001608 PresumedLoc PLoc = SM.getPresumedLoc(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001609 unsigned Line = 0;
1610 if (!PLoc.isInvalid())
1611 Line = PLoc.getLine();
1612 else
1613 Unit = llvm::DICompileUnit();
1614
Devang Pateld6c5a262010-02-01 21:52:22 +00001615 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001616 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001617 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1618 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1619 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1620 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001621 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001622 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1623 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001624 // offset of __forwarding field
1625 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001626 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1627 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1628 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001629 // offset of x field
1630 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001631 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001632 }
1633
1634 // Create the descriptor for the variable.
1635 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001636 DebugFactory.CreateComplexVariable(Tag,
1637 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001638 VD->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001639 addr);
1640 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001641 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001642 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001643
Devang Patel8fae0602009-11-13 19:10:24 +00001644 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001645 llvm::DILocation DO(NULL);
1646 llvm::DILocation DL =
1647 DebugFactory.CreateLocation(Line, PLoc.getColumn(), DS, DO);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001648
Chris Lattner23e92c02009-12-28 23:41:39 +00001649 Call->setMetadata("dbg", DL.getNode());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001650}
1651
Devang Pateld6c5a262010-02-01 21:52:22 +00001652void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001653 llvm::Value *Storage,
1654 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001655 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001656}
1657
Mike Stumpb1a6e682009-09-30 02:43:10 +00001658void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1659 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1660 CodeGenFunction *CGF) {
1661 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1662}
1663
Chris Lattner9c85ba32008-11-10 06:08:34 +00001664/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1665/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001666void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001667 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001668 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001669}
1670
1671
1672
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001673/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001674void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001675 const VarDecl *D) {
1676
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001677 // Create global variable debug descriptor.
Devang Pateleb6d79b2010-02-01 21:34:11 +00001678 llvm::DICompileUnit Unit = getOrCreateCompileUnit(D->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001679 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001680 PresumedLoc PLoc = SM.getPresumedLoc(D->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +00001681 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001682
Devang Pateleb6d79b2010-02-01 21:34:11 +00001683 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001684 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001685
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001686 // CodeGen turns int[] into int[1] so we'll do the same here.
1687 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001688
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001689 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001690 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001691
Anders Carlsson20f12a22009-12-06 18:00:51 +00001692 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001693 ArrayType::Normal, 0);
1694 }
Sanjiv Gupta67b14f02010-02-25 05:20:44 +00001695 llvm::StringRef DeclName = Var->getName();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001696 llvm::DIDescriptor DContext =
1697 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
1698 DebugFactory.CreateGlobalVariable(DContext, DeclName,
Devang Patel33583052010-01-28 23:15:27 +00001699 DeclName, llvm::StringRef(), Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001700 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001701 Var->hasInternalLinkage(),
1702 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001703}
1704
Devang Patel9ca36b62009-02-26 21:10:26 +00001705/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001706void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001707 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001708 // Create global variable debug descriptor.
Devang Pateld6c5a262010-02-01 21:52:22 +00001709 llvm::DICompileUnit Unit = getOrCreateCompileUnit(ID->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001710 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateld6c5a262010-02-01 21:52:22 +00001711 PresumedLoc PLoc = SM.getPresumedLoc(ID->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +00001712 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +00001713
Devang Pateld6c5a262010-02-01 21:52:22 +00001714 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001715
Devang Pateld6c5a262010-02-01 21:52:22 +00001716 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001717 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001718
Devang Patel9ca36b62009-02-26 21:10:26 +00001719 // CodeGen turns int[] into int[1] so we'll do the same here.
1720 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001721
Devang Patel9ca36b62009-02-26 21:10:26 +00001722 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001723 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001724
Anders Carlsson20f12a22009-12-06 18:00:51 +00001725 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001726 ArrayType::Normal, 0);
1727 }
1728
Devang Patelf6a39b72009-10-20 18:26:30 +00001729 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001730 getOrCreateType(T, Unit),
1731 Var->hasInternalLinkage(),
1732 true/*definition*/, Var);
1733}
Devang Patelabb485f2010-02-01 19:16:32 +00001734
1735/// getOrCreateNamesSpace - Return namespace descriptor for the given
1736/// namespace decl.
1737llvm::DINameSpace
1738CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1739 llvm::DIDescriptor Unit) {
1740 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1741 NameSpaceCache.find(NSDecl);
1742 if (I != NameSpaceCache.end())
1743 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1744
1745 SourceManager &SM = CGM.getContext().getSourceManager();
1746 PresumedLoc PLoc = SM.getPresumedLoc(NSDecl->getLocation());
1747 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
1748
1749 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00001750 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00001751 llvm::DINameSpace NS =
1752 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
1753 llvm::DICompileUnit(Unit.getNode()), LineNo);
1754 NameSpaceCache[NSDecl] = llvm::WeakVH(NS.getNode());
1755 return NS;
1756}