blob: 3d7548e293dd29399900713a69a0417f78bac764 [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"
15#include "CodeGenModule.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000016#include "clang/AST/ASTContext.h"
Anders Carlsson3d598a52009-07-14 17:29:11 +000017#include "clang/AST/ASTRecordLayout.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"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000020#include "clang/Basic/SourceManager.h"
21#include "clang/Basic/FileManager.h"
Devang Patel61b5f3e2009-07-14 02:47:58 +000022#include "clang/Basic/TargetInfo.h"
Devang Patel07739032009-03-27 23:16:32 +000023#include "clang/Frontend/CompileOptions.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000024#include "llvm/Constants.h"
25#include "llvm/DerivedTypes.h"
26#include "llvm/Instructions.h"
27#include "llvm/Intrinsics.h"
28#include "llvm/Module.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000029#include "llvm/ADT/StringExtras.h"
30#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000031#include "llvm/Support/Dwarf.h"
Devang Patel446c6192009-04-17 21:06:59 +000032#include "llvm/System/Path.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000033#include "llvm/Target/TargetMachine.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000034using namespace clang;
35using namespace clang::CodeGen;
36
Devang Patel61b5f3e2009-07-14 02:47:58 +000037CGDebugInfo::CGDebugInfo(CodeGenModule *m, TargetInfo *t)
Mike Stump9bc093c2009-05-14 02:03:51 +000038 : M(m), isMainCompileUnitCreated(false), DebugFactory(M->getModule()),
39 BlockLiteralGenericSet(false) {
Devang Patel61b5f3e2009-07-14 02:47:58 +000040 LLVMMangler = new llvm::Mangler(m->getModule(), t->getUserLabelPrefix(), ".");
41 // add chars used in ObjC method names so method names aren't mangled
42 LLVMMangler->markCharAcceptable('[');
43 LLVMMangler->markCharAcceptable(']');
44 LLVMMangler->markCharAcceptable('(');
45 LLVMMangler->markCharAcceptable(')');
46 LLVMMangler->markCharAcceptable('-');
47 LLVMMangler->markCharAcceptable('+');
48 LLVMMangler->markCharAcceptable(' ');
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000049}
50
Chris Lattner9c85ba32008-11-10 06:08:34 +000051CGDebugInfo::~CGDebugInfo() {
Devang Patel61b5f3e2009-07-14 02:47:58 +000052 delete LLVMMangler;
Daniel Dunbar66031a52008-10-17 16:15:48 +000053 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000054}
55
Chris Lattner9c85ba32008-11-10 06:08:34 +000056void CGDebugInfo::setLocation(SourceLocation Loc) {
57 if (Loc.isValid())
Chris Lattnerf7cf85b2009-01-16 07:36:28 +000058 CurLoc = M->getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000059}
60
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000061/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
Daniel Dunbar25f51dd2008-10-24 08:38:36 +000062/// one if necessary. This returns null for invalid source locations.
Chris Lattner9c85ba32008-11-10 06:08:34 +000063llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) {
Devang Patel446c6192009-04-17 21:06:59 +000064 // Get source file information.
65 const char *FileName = "<unknown>";
Devang Patel77820222009-02-24 23:16:03 +000066 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattneradb1a6f2009-04-19 06:50:29 +000067 unsigned FID = 0;
Daniel Dunbar831570c2009-01-22 00:09:25 +000068 if (Loc.isValid()) {
Devang Patel446c6192009-04-17 21:06:59 +000069 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
70 FileName = PLoc.getFilename();
71 FID = PLoc.getIncludeLoc().getRawEncoding();
Daniel Dunbar831570c2009-01-22 00:09:25 +000072 }
73
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000074 // See if this compile unit has been used before.
Devang Patel446c6192009-04-17 21:06:59 +000075 llvm::DICompileUnit &Unit = CompileUnitCache[FID];
Chris Lattner9c85ba32008-11-10 06:08:34 +000076 if (!Unit.isNull()) return Unit;
Daniel Dunbarc9abc042009-04-08 05:11:16 +000077
Devang Patel446c6192009-04-17 21:06:59 +000078 // Get absolute path name.
79 llvm::sys::Path AbsFileName(FileName);
80 if (!AbsFileName.isAbsolute()) {
81 llvm::sys::Path tmp = llvm::sys::Path::GetCurrentDirectory();
82 tmp.appendComponent(FileName);
83 AbsFileName = tmp;
84 }
85
Devang Patel72240d72009-06-26 18:32:22 +000086 // See if thie compile unit is representing main source file. Each source
87 // file has corresponding compile unit. There is only one main source
88 // file at a time.
89 bool isMain = false;
90 const LangOptions &LO = M->getLangOptions();
91 const char *MainFileName = LO.getMainFileName();
92 if (isMainCompileUnitCreated == false) {
93 if (MainFileName) {
94 if (!strcmp(AbsFileName.getLast().c_str(), MainFileName))
95 isMain = true;
96 } else {
97 if (Loc.isValid() && SM.isFromMainFile(Loc))
98 isMain = true;
99 }
100 if (isMain)
101 isMainCompileUnitCreated = true;
Devang Patel446c6192009-04-17 21:06:59 +0000102 }
Daniel Dunbarc9abc042009-04-08 05:11:16 +0000103
Chris Lattner515455a2009-03-25 03:28:08 +0000104 unsigned LangTag;
105 if (LO.CPlusPlus) {
106 if (LO.ObjC1)
107 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
108 else
109 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
110 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000111 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000112 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000113 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000114 } else {
115 LangTag = llvm::dwarf::DW_LANG_C89;
116 }
Devang Patel446c6192009-04-17 21:06:59 +0000117
Chris Lattnerb95ee582009-05-02 01:04:13 +0000118 std::string Producer = "clang 1.0";// FIXME: clang version.
119 bool isOptimized = LO.Optimize;
Chris Lattner4c2577a2009-05-02 01:00:04 +0000120 const char *Flags = ""; // FIXME: Encode command line options.
121
122 // Figure out which version of the ObjC runtime we have.
123 unsigned RuntimeVers = 0;
124 if (LO.ObjC1)
125 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
126
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000127 // Create new compile unit.
Devang Patel72240d72009-06-26 18:32:22 +0000128 return Unit = DebugFactory.CreateCompileUnit(LangTag, AbsFileName.getLast(),
129 AbsFileName.getDirname(),
130 Producer, isMain, isOptimized,
131 Flags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000132}
133
Devang Patel65e99f22009-02-25 01:36:11 +0000134/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000135/// one if necessary.
136llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel65e99f22009-02-25 01:36:11 +0000137 llvm::DICompileUnit Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000138 unsigned Encoding = 0;
139 switch (BT->getKind()) {
140 default:
141 case BuiltinType::Void:
142 return llvm::DIType();
143 case BuiltinType::UChar:
144 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
145 case BuiltinType::Char_S:
146 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
147 case BuiltinType::UShort:
148 case BuiltinType::UInt:
149 case BuiltinType::ULong:
150 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
151 case BuiltinType::Short:
152 case BuiltinType::Int:
153 case BuiltinType::Long:
154 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
155 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
156 case BuiltinType::Float:
157 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
158 }
159 // Bit size, align and offset of the type.
160 uint64_t Size = M->getContext().getTypeSize(BT);
161 uint64_t Align = M->getContext().getTypeAlign(BT);
162 uint64_t Offset = 0;
163
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000164 return DebugFactory.CreateBasicType(Unit,
Chris Lattnere4f21422009-06-30 01:26:17 +0000165 BT->getName(M->getContext().getLangOptions()),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000166 Unit, 0, Size, Align,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000167 Offset, /*flags*/ 0, Encoding);
168}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000169
Chris Lattnerb7003772009-04-23 06:13:01 +0000170llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
171 llvm::DICompileUnit Unit) {
172 // Bit size, align and offset of the type.
173 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
174 if (Ty->isComplexIntegerType())
175 Encoding = llvm::dwarf::DW_ATE_lo_user;
176
177 uint64_t Size = M->getContext().getTypeSize(Ty);
178 uint64_t Align = M->getContext().getTypeAlign(Ty);
179 uint64_t Offset = 0;
180
181 return DebugFactory.CreateBasicType(Unit, "complex",
182 Unit, 0, Size, Align,
183 Offset, /*flags*/ 0, Encoding);
184}
185
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000186/// getOrCreateCVRType - Get the CVR qualified type from the cache or create
187/// a new one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000188llvm::DIType CGDebugInfo::CreateCVRType(QualType Ty, llvm::DICompileUnit Unit) {
189 // We will create one Derived type for one qualifier and recurse to handle any
190 // additional ones.
191 llvm::DIType FromTy;
192 unsigned Tag;
193 if (Ty.isConstQualified()) {
194 Tag = llvm::dwarf::DW_TAG_const_type;
195 Ty.removeConst();
196 FromTy = getOrCreateType(Ty, Unit);
197 } else if (Ty.isVolatileQualified()) {
198 Tag = llvm::dwarf::DW_TAG_volatile_type;
199 Ty.removeVolatile();
200 FromTy = getOrCreateType(Ty, Unit);
201 } else {
202 assert(Ty.isRestrictQualified() && "Unknown type qualifier for debug info");
203 Tag = llvm::dwarf::DW_TAG_restrict_type;
204 Ty.removeRestrict();
205 FromTy = getOrCreateType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000206 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000207
Daniel Dunbar3845f862008-10-31 03:54:29 +0000208 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
209 // CVR derived types.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000210 return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
211 0, 0, 0, 0, 0, FromTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000212}
213
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000214llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
215 llvm::DICompileUnit Unit) {
216 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
217
218 // Bit size, align and offset of the type.
219 uint64_t Size = M->getContext().getTypeSize(Ty);
220 uint64_t Align = M->getContext().getTypeAlign(Ty);
221
222 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
223 "", llvm::DICompileUnit(),
224 0, Size, Align, 0, 0, EltTy);
225}
226
Chris Lattner9c85ba32008-11-10 06:08:34 +0000227llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
228 llvm::DICompileUnit Unit) {
229 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000230
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000231 // Bit size, align and offset of the type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000232 uint64_t Size = M->getContext().getTypeSize(Ty);
233 uint64_t Align = M->getContext().getTypeAlign(Ty);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000234
Chris Lattner9c85ba32008-11-10 06:08:34 +0000235 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
236 "", llvm::DICompileUnit(),
237 0, Size, Align, 0, 0, EltTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000238}
239
Mike Stump9bc093c2009-05-14 02:03:51 +0000240llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
241 llvm::DICompileUnit Unit) {
242 if (BlockLiteralGenericSet)
243 return BlockLiteralGeneric;
244
245 llvm::DICompileUnit DefUnit;
246 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
247
248 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
249
250 llvm::DIType FieldTy;
251
252 QualType FType;
253 uint64_t FieldSize, FieldOffset;
254 unsigned FieldAlign;
255
256 llvm::DIArray Elements;
257 llvm::DIType EltTy, DescTy;
258
259 FieldOffset = 0;
260 FType = M->getContext().UnsignedLongTy;
261 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
262 FieldSize = M->getContext().getTypeSize(FType);
263 FieldAlign = M->getContext().getTypeAlign(FType);
264 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
265 "reserved", DefUnit,
266 0, FieldSize, FieldAlign,
267 FieldOffset, 0, FieldTy);
268 EltTys.push_back(FieldTy);
269
270 FieldOffset += FieldSize;
271 FType = M->getContext().UnsignedLongTy;
272 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
273 FieldSize = M->getContext().getTypeSize(FType);
274 FieldAlign = M->getContext().getTypeAlign(FType);
275 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
276 "Size", DefUnit,
277 0, FieldSize, FieldAlign,
278 FieldOffset, 0, FieldTy);
279 EltTys.push_back(FieldTy);
280
281 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000282 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000283 EltTys.clear();
284
285 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
286 DefUnit, 0, FieldOffset, 0, 0, 0,
287 llvm::DIType(), Elements);
288
289 // Bit size, align and offset of the type.
290 uint64_t Size = M->getContext().getTypeSize(Ty);
291 uint64_t Align = M->getContext().getTypeAlign(Ty);
292
293 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
294 Unit, "", llvm::DICompileUnit(),
295 0, Size, Align, 0, 0, EltTy);
296
297 FieldOffset = 0;
298 FType = M->getContext().getPointerType(M->getContext().VoidTy);
299 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
300 FieldSize = M->getContext().getTypeSize(FType);
301 FieldAlign = M->getContext().getTypeAlign(FType);
302 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
303 "__isa", DefUnit,
304 0, FieldSize, FieldAlign,
305 FieldOffset, 0, FieldTy);
306 EltTys.push_back(FieldTy);
307
308 FieldOffset += FieldSize;
309 FType = M->getContext().IntTy;
310 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
311 FieldSize = M->getContext().getTypeSize(FType);
312 FieldAlign = M->getContext().getTypeAlign(FType);
313 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
314 "__flags", DefUnit,
315 0, FieldSize, FieldAlign,
316 FieldOffset, 0, FieldTy);
317 EltTys.push_back(FieldTy);
318
319 FieldOffset += FieldSize;
320 FType = M->getContext().IntTy;
321 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
322 FieldSize = M->getContext().getTypeSize(FType);
323 FieldAlign = M->getContext().getTypeAlign(FType);
324 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
325 "__reserved", DefUnit,
326 0, FieldSize, FieldAlign,
327 FieldOffset, 0, FieldTy);
328 EltTys.push_back(FieldTy);
329
330 FieldOffset += FieldSize;
331 FType = M->getContext().getPointerType(M->getContext().VoidTy);
332 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
333 FieldSize = M->getContext().getTypeSize(FType);
334 FieldAlign = M->getContext().getTypeAlign(FType);
335 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
336 "__FuncPtr", DefUnit,
337 0, FieldSize, FieldAlign,
338 FieldOffset, 0, FieldTy);
339 EltTys.push_back(FieldTy);
340
341 FieldOffset += FieldSize;
342 FType = M->getContext().getPointerType(M->getContext().VoidTy);
343 FieldTy = DescTy;
344 FieldSize = M->getContext().getTypeSize(Ty);
345 FieldAlign = M->getContext().getTypeAlign(Ty);
346 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
347 "__descriptor", DefUnit,
348 0, FieldSize, FieldAlign,
349 FieldOffset, 0, FieldTy);
350 EltTys.push_back(FieldTy);
351
352 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000353 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000354
355 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
356 DefUnit, 0, FieldOffset, 0, 0, 0,
357 llvm::DIType(), Elements);
358
359 BlockLiteralGenericSet = true;
360 BlockLiteralGeneric
361 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
362 "", llvm::DICompileUnit(),
363 0, Size, Align, 0, 0, EltTy);
364 return BlockLiteralGeneric;
365}
366
Chris Lattner9c85ba32008-11-10 06:08:34 +0000367llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
368 llvm::DICompileUnit Unit) {
369 // Typedefs are derived from some other type. If we have a typedef of a
370 // typedef, make sure to emit the whole chain.
371 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
372
373 // We don't set size information, but do specify where the typedef was
374 // declared.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000375 std::string TyName = Ty->getDecl()->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000376 SourceLocation DefLoc = Ty->getDecl()->getLocation();
377 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000378
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000379 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000380 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
381 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000382
Chris Lattner9c85ba32008-11-10 06:08:34 +0000383 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
384 TyName, DefUnit, Line, 0, 0, 0, 0, Src);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000385}
386
Chris Lattner9c85ba32008-11-10 06:08:34 +0000387llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
388 llvm::DICompileUnit Unit) {
389 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000390
Chris Lattner9c85ba32008-11-10 06:08:34 +0000391 // Add the result type at least.
392 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
393
394 // Set up remainder of arguments if there is a prototype.
395 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000396 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000397 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
398 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
399 } else {
400 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000401 }
402
Chris Lattner9c85ba32008-11-10 06:08:34 +0000403 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000404 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000405
406 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
407 Unit, "", llvm::DICompileUnit(),
408 0, 0, 0, 0, 0,
409 llvm::DIType(), EltTypeArray);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000410}
411
Devang Patel65e99f22009-02-25 01:36:11 +0000412/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000413llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
414 llvm::DICompileUnit Unit) {
Douglas Gregora4c46df2008-12-11 17:59:21 +0000415 RecordDecl *Decl = Ty->getDecl();
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000416
Chris Lattner9c85ba32008-11-10 06:08:34 +0000417 unsigned Tag;
418 if (Decl->isStruct())
419 Tag = llvm::dwarf::DW_TAG_structure_type;
420 else if (Decl->isUnion())
421 Tag = llvm::dwarf::DW_TAG_union_type;
422 else {
423 assert(Decl->isClass() && "Unknown RecordType!");
424 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000425 }
426
Sanjiv Gupta507de852008-06-09 10:47:41 +0000427 SourceManager &SM = M->getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000428
Chris Lattner9c85ba32008-11-10 06:08:34 +0000429 // Get overall information about the record type for the debug info.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000430 std::string Name = Decl->getNameAsString();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000431
Devang Patel4f6fa232009-04-17 21:35:15 +0000432 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000433 llvm::DICompileUnit DefUnit;
434 unsigned Line = 0;
435 if (!PLoc.isInvalid()) {
436 DefUnit = getOrCreateCompileUnit(Decl->getLocation());
437 Line = PLoc.getLine();
438 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000439
440 // Records and classes and unions can all be recursive. To handle them, we
441 // first generate a debug descriptor for the struct as a forward declaration.
442 // Then (if it is a definition) we go through and get debug info for all of
443 // its members. Finally, we create a descriptor for the complete type (which
444 // may refer to the forward decl if the struct is recursive) and replace all
445 // uses of the forward declaration with the final definition.
446 llvm::DIType FwdDecl =
447 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
448 llvm::DIType(), llvm::DIArray());
449
450 // If this is just a forward declaration, return it.
451 if (!Decl->getDefinition(M->getContext()))
452 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000453
Chris Lattner9c85ba32008-11-10 06:08:34 +0000454 // Otherwise, insert it into the TypeCache so that recursive uses will find
455 // it.
456 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
457
458 // Convert all the elements.
459 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
460
461 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl);
462
463 unsigned FieldNo = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000464 for (RecordDecl::field_iterator I = Decl->field_begin(),
465 E = Decl->field_end();
Douglas Gregora4c46df2008-12-11 17:59:21 +0000466 I != E; ++I, ++FieldNo) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000467 FieldDecl *Field = *I;
468 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattner8ec03f52008-11-24 03:54:41 +0000469
470 std::string FieldName = Field->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000471
Devang Patelde135022009-04-27 22:40:36 +0000472 // Ignore unnamed fields.
473 if (FieldName.empty())
474 continue;
475
Chris Lattner9c85ba32008-11-10 06:08:34 +0000476 // Get the location for the field.
477 SourceLocation FieldDefLoc = Field->getLocation();
Devang Patel4f6fa232009-04-17 21:35:15 +0000478 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000479 llvm::DICompileUnit FieldDefUnit;
480 unsigned FieldLine = 0;
481
482 if (!PLoc.isInvalid()) {
483 FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
484 FieldLine = PLoc.getLine();
485 }
Devang Patelec9b5d52009-03-16 23:47:53 +0000486
487 QualType FType = Field->getType();
488 uint64_t FieldSize = 0;
489 unsigned FieldAlign = 0;
490 if (!FType->isIncompleteArrayType()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000491
Devang Patelec9b5d52009-03-16 23:47:53 +0000492 // Bit size, align and offset of the type.
493 FieldSize = M->getContext().getTypeSize(FType);
494 Expr *BitWidth = Field->getBitWidth();
495 if (BitWidth)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000496 FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue();
Devang Patelec9b5d52009-03-16 23:47:53 +0000497
498 FieldAlign = M->getContext().getTypeAlign(FType);
499 }
500
Chris Lattner9c85ba32008-11-10 06:08:34 +0000501 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
502
503 // Create a DW_TAG_member node to remember the offset of this field in the
504 // struct. FIXME: This is an absolutely insane way to capture this
505 // information. When we gut debug info, this should be fixed.
506 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
507 FieldName, FieldDefUnit,
508 FieldLine, FieldSize, FieldAlign,
509 FieldOffset, 0, FieldTy);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000510 EltTys.push_back(FieldTy);
511 }
512
513 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000514 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000515
516 // Bit size, align and offset of the type.
517 uint64_t Size = M->getContext().getTypeSize(Ty);
518 uint64_t Align = M->getContext().getTypeAlign(Ty);
519
520 llvm::DIType RealDecl =
521 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
522 Align, 0, 0, llvm::DIType(), Elements);
523
524 // Now that we have a real decl for the struct, replace anything using the
525 // old decl with the new one. This will recursively update the debug info.
526 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
527 FwdDecl.getGV()->eraseFromParent();
Devang Patelfe09eab2009-07-13 17:03:14 +0000528
529 // Update TypeCache.
530 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = RealDecl;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000531 return RealDecl;
532}
533
Devang Patel9ca36b62009-02-26 21:10:26 +0000534/// CreateType - get objective-c interface type.
535llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
536 llvm::DICompileUnit Unit) {
537 ObjCInterfaceDecl *Decl = Ty->getDecl();
538
539 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
540 SourceManager &SM = M->getContext().getSourceManager();
541
542 // Get overall information about the record type for the debug info.
543 std::string Name = Decl->getNameAsString();
544
545 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000546 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
547 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
548
Devang Patel9ca36b62009-02-26 21:10:26 +0000549
Daniel Dunbard86d3362009-05-18 20:51:58 +0000550 unsigned RuntimeLang = DefUnit.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000551
Devang Patel9ca36b62009-02-26 21:10:26 +0000552 // To handle recursive interface, we
553 // first generate a debug descriptor for the struct as a forward declaration.
554 // Then (if it is a definition) we go through and get debug info for all of
555 // its members. Finally, we create a descriptor for the complete type (which
556 // may refer to the forward decl if the struct is recursive) and replace all
557 // uses of the forward declaration with the final definition.
558 llvm::DIType FwdDecl =
559 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000560 llvm::DIType(), llvm::DIArray(),
561 RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000562
563 // If this is just a forward declaration, return it.
564 if (Decl->isForwardDecl())
565 return FwdDecl;
566
567 // Otherwise, insert it into the TypeCache so that recursive uses will find
568 // it.
569 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
570
571 // Convert all the elements.
572 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
573
Devang Patelfbe899f2009-03-10 21:30:26 +0000574 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
575 if (SClass) {
576 llvm::DIType SClassTy =
577 getOrCreateType(M->getContext().getObjCInterfaceType(SClass), Unit);
578 llvm::DIType InhTag =
579 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Chris Lattner9e55b8a2009-05-05 05:05:36 +0000580 Unit, "", llvm::DICompileUnit(), 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000581 0 /* offset */, 0, SClassTy);
582 EltTys.push_back(InhTag);
583 }
584
Devang Patel9ca36b62009-02-26 21:10:26 +0000585 const ASTRecordLayout &RL = M->getContext().getASTObjCInterfaceLayout(Decl);
586
587 unsigned FieldNo = 0;
588 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
589 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
590 ObjCIvarDecl *Field = *I;
591 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
592
593 std::string FieldName = Field->getNameAsString();
594
Devang Patelde135022009-04-27 22:40:36 +0000595 // Ignore unnamed fields.
596 if (FieldName.empty())
597 continue;
598
Devang Patel9ca36b62009-02-26 21:10:26 +0000599 // Get the location for the field.
600 SourceLocation FieldDefLoc = Field->getLocation();
601 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000602 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
603 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
604
Devang Patel99c20eb2009-03-20 18:24:39 +0000605
606 QualType FType = Field->getType();
607 uint64_t FieldSize = 0;
608 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000609
Devang Patel99c20eb2009-03-20 18:24:39 +0000610 if (!FType->isIncompleteArrayType()) {
611
612 // Bit size, align and offset of the type.
613 FieldSize = M->getContext().getTypeSize(FType);
614 Expr *BitWidth = Field->getBitWidth();
615 if (BitWidth)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000616 FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue();
617
Devang Patel99c20eb2009-03-20 18:24:39 +0000618 FieldAlign = M->getContext().getTypeAlign(FType);
619 }
620
621 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
622
Devang Patelc20482b2009-03-19 00:23:53 +0000623 unsigned Flags = 0;
624 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
625 Flags = llvm::DIType::FlagProtected;
626 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
627 Flags = llvm::DIType::FlagPrivate;
628
Devang Patel9ca36b62009-02-26 21:10:26 +0000629 // Create a DW_TAG_member node to remember the offset of this field in the
630 // struct. FIXME: This is an absolutely insane way to capture this
631 // information. When we gut debug info, this should be fixed.
632 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
633 FieldName, FieldDefUnit,
634 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000635 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000636 EltTys.push_back(FieldTy);
637 }
638
639 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000640 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +0000641
642 // Bit size, align and offset of the type.
643 uint64_t Size = M->getContext().getTypeSize(Ty);
644 uint64_t Align = M->getContext().getTypeAlign(Ty);
645
646 llvm::DIType RealDecl =
647 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000648 Align, 0, 0, llvm::DIType(), Elements,
649 RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000650
651 // Now that we have a real decl for the struct, replace anything using the
652 // old decl with the new one. This will recursively update the debug info.
653 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
654 FwdDecl.getGV()->eraseFromParent();
Devang Patelfe09eab2009-07-13 17:03:14 +0000655
656 // Update TypeCache.
657 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = RealDecl;
Devang Patel9ca36b62009-02-26 21:10:26 +0000658 return RealDecl;
659}
660
Chris Lattner9c85ba32008-11-10 06:08:34 +0000661llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
662 llvm::DICompileUnit Unit) {
663 EnumDecl *Decl = Ty->getDecl();
664
665 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
666
667 // Create DIEnumerator elements for each enumerator.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000668 for (EnumDecl::enumerator_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000669 Enum = Decl->enumerator_begin(), EnumEnd = Decl->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000670 Enum != EnumEnd; ++Enum) {
671 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(),
672 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000673 }
674
675 // Return a CompositeType for the enum itself.
676 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000677 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000678
Chris Lattner8ec03f52008-11-24 03:54:41 +0000679 std::string EnumName = Decl->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000680 SourceLocation DefLoc = Decl->getLocation();
681 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
682 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000683 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
684 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
685
Chris Lattner9c85ba32008-11-10 06:08:34 +0000686
687 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +0000688 uint64_t Size = 0;
689 unsigned Align = 0;
690 if (!Ty->isIncompleteType()) {
691 Size = M->getContext().getTypeSize(Ty);
692 Align = M->getContext().getTypeAlign(Ty);
693 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000694
695 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
696 Unit, EnumName, DefUnit, Line,
697 Size, Align, 0, 0,
698 llvm::DIType(), EltArray);
699}
700
701llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
702 llvm::DICompileUnit Unit) {
703 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
704 return CreateType(RT, Unit);
705 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
706 return CreateType(ET, Unit);
707
708 return llvm::DIType();
709}
710
711llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
712 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000713 uint64_t Size;
714 uint64_t Align;
715
716
Nuno Lopes010d5142009-01-28 00:35:17 +0000717 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +0000718 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000719 Size = 0;
720 Align =
Nuno Lopes010d5142009-01-28 00:35:17 +0000721 M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT));
722 } else if (Ty->isIncompleteArrayType()) {
723 Size = 0;
724 Align = M->getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +0000725 } else {
726 // Size and align of the whole array, not the element type.
727 Size = M->getContext().getTypeSize(Ty);
728 Align = M->getContext().getTypeAlign(Ty);
729 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000730
731 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
732 // interior arrays, do we care? Why aren't nested arrays represented the
733 // obvious/recursive way?
734 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
735 QualType EltTy(Ty, 0);
736 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000737 uint64_t Upper = 0;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000738 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
739 Upper = CAT->getSize().getZExtValue() - 1;
740 // FIXME: Verify this is right for VLAs.
741 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
742 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000743 }
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000744
Chris Lattner9c85ba32008-11-10 06:08:34 +0000745 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000746 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000747
748 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
749 Unit, "", llvm::DICompileUnit(),
750 0, Size, Align, 0, 0,
751 getOrCreateType(EltTy, Unit),
752 SubscriptArray);
753}
754
755
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000756/// getOrCreateType - Get the type from the cache or create a new
757/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000758llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
759 llvm::DICompileUnit Unit) {
760 if (Ty.isNull())
761 return llvm::DIType();
762
Devang Patel0ae3d602009-07-13 16:15:54 +0000763 // Check TypeCache first.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000764 llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()];
765 if (!Slot.isNull()) return Slot;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000766
Chris Lattner9c85ba32008-11-10 06:08:34 +0000767 // Handle CVR qualifiers, which recursively handles what they refer to.
768 if (Ty.getCVRQualifiers())
769 return Slot = CreateCVRType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000770
771 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000772 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000773#define TYPE(Class, Base)
774#define ABSTRACT_TYPE(Class, Base)
775#define NON_CANONICAL_TYPE(Class, Base)
776#define DEPENDENT_TYPE(Class, Base) case Type::Class:
777#include "clang/AST/TypeNodes.def"
778 assert(false && "Dependent types cannot show up in debug information");
779
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000780 case Type::LValueReference:
781 case Type::RValueReference:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000782 case Type::Vector:
783 case Type::ExtVector:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000784 case Type::ExtQual:
Eli Friedman00524e32009-02-27 23:15:07 +0000785 case Type::FixedWidthInt:
Eli Friedman00524e32009-02-27 23:15:07 +0000786 case Type::MemberPointer:
Douglas Gregor7532dc62009-03-30 22:58:21 +0000787 case Type::TemplateSpecialization:
Douglas Gregore4e5b052009-03-19 00:18:19 +0000788 case Type::QualifiedName:
Eli Friedman00524e32009-02-27 23:15:07 +0000789 // Unsupported types
Chris Lattner9c85ba32008-11-10 06:08:34 +0000790 return llvm::DIType();
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000791 case Type::ObjCObjectPointer:
792 return Slot = CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Chris Lattneraa2b5792009-04-22 06:58:56 +0000793 case Type::ObjCQualifiedInterface: // Drop protocols from interface.
Devang Patele7987062009-03-02 17:58:28 +0000794 case Type::ObjCInterface:
Chris Lattneraa2b5792009-04-22 06:58:56 +0000795 return Slot = CreateType(cast<ObjCInterfaceType>(Ty), Unit);
796 case Type::Builtin: return Slot = CreateType(cast<BuiltinType>(Ty), Unit);
Chris Lattnerb7003772009-04-23 06:13:01 +0000797 case Type::Complex: return Slot = CreateType(cast<ComplexType>(Ty), Unit);
Chris Lattneraa2b5792009-04-22 06:58:56 +0000798 case Type::Pointer: return Slot = CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +0000799 case Type::BlockPointer:
800 return Slot = CreateType(cast<BlockPointerType>(Ty), Unit);
Chris Lattneraa2b5792009-04-22 06:58:56 +0000801 case Type::Typedef: return Slot = CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000802 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000803 case Type::Enum:
Chris Lattneraa2b5792009-04-22 06:58:56 +0000804 return Slot = CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000805 case Type::FunctionProto:
806 case Type::FunctionNoProto:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000807 return Slot = CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000808
809 case Type::ConstantArray:
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000810 case Type::ConstantArrayWithExpr:
811 case Type::ConstantArrayWithoutExpr:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000812 case Type::VariableArray:
813 case Type::IncompleteArray:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000814 return Slot = CreateType(cast<ArrayType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000815 case Type::TypeOfExpr:
816 return Slot = getOrCreateType(cast<TypeOfExprType>(Ty)->getUnderlyingExpr()
Chris Lattner3cc5c402008-11-11 07:01:36 +0000817 ->getType(), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000818 case Type::TypeOf:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000819 return Slot = getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(),
820 Unit);
Anders Carlsson395b4752009-06-24 19:06:50 +0000821 case Type::Decltype:
Anders Carlsson563a03b2009-07-10 19:20:26 +0000822 return Slot = getOrCreateType(cast<DecltypeType>(Ty)->getUnderlyingType(),
823 Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000824 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000825
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000826 return Slot;
827}
828
829/// EmitFunctionStart - Constructs the debug code for entering a function -
830/// "llvm.dbg.func.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000831void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000832 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000833 CGBuilderTy &Builder) {
Daniel Dunbara2893932009-05-13 23:08:57 +0000834 // Skip the asm prefix if it exists.
Daniel Dunbarbbd53af2009-05-14 01:45:24 +0000835 //
836 // FIXME: This should probably be the unmangled name?
Daniel Dunbara2893932009-05-13 23:08:57 +0000837 if (Name[0] == '\01')
838 ++Name;
839
Chris Lattner9c85ba32008-11-10 06:08:34 +0000840 // FIXME: Why is this using CurLoc???
841 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000842 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +0000843 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000844
845 llvm::DISubprogram SP =
Chris Lattner991b5152009-07-14 18:18:16 +0000846 DebugFactory.CreateSubprogram(Unit, Name, Name,
847 LLVMMangler->getMangledName(Fn),
Devang Patel61b5f3e2009-07-14 02:47:58 +0000848 Unit, LineNo,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000849 getOrCreateType(ReturnType, Unit),
850 Fn->hasInternalLinkage(), true/*definition*/);
851
852 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
853
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000854 // Push function on region stack.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000855 RegionStack.push_back(SP);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000856}
857
858
Chris Lattner9c85ba32008-11-10 06:08:34 +0000859void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000860 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
861
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000862 // Don't bother if things are the same as last time.
863 SourceManager &SM = M->getContext().getSourceManager();
864 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +0000865 || (SM.getInstantiationLineNumber(CurLoc) ==
866 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000867 && SM.isFromSameFile(CurLoc, PrevLoc)))
868 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000869
870 // Update last state.
871 PrevLoc = CurLoc;
872
873 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000874 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +0000875 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
876 DebugFactory.InsertStopPoint(Unit, PLoc.getLine(), PLoc.getColumn(),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000877 Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000878}
879
880/// EmitRegionStart- Constructs the debug code for entering a declarative
881/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000882void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
883 llvm::DIDescriptor D;
Daniel Dunbar5273f512008-10-17 01:07:56 +0000884 if (!RegionStack.empty())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000885 D = RegionStack.back();
886 D = DebugFactory.CreateBlock(D);
887 RegionStack.push_back(D);
888 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000889}
890
891/// EmitRegionEnd - Constructs the debug code for exiting a declarative
892/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +0000893void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000894 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
895
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000896 // Provide an region stop point.
897 EmitStopPoint(Fn, Builder);
898
Chris Lattner9c85ba32008-11-10 06:08:34 +0000899 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000900 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000901}
902
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000903/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000904void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
905 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000906 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
907
Devang Patel07739032009-03-27 23:16:32 +0000908 // Do not emit variable debug information while generating optimized code.
909 // The llvm optimizer and code generator are not yet ready to support
910 // optimized code debugging.
911 const CompileOptions &CO = M->getCompileOpts();
912 if (CO.OptimizationLevel)
913 return;
914
Chris Lattner650cea92009-05-05 04:57:08 +0000915 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
916 llvm::DIType Ty = getOrCreateType(Decl->getType(), Unit);
917
Chris Lattner9c85ba32008-11-10 06:08:34 +0000918 // Get location information.
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000919 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000920 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +0000921 unsigned Line = 0;
922 if (!PLoc.isInvalid())
923 Line = PLoc.getLine();
924 else
925 Unit = llvm::DICompileUnit();
926
Chris Lattner9c85ba32008-11-10 06:08:34 +0000927
928 // Create the descriptor for the variable.
929 llvm::DIVariable D =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000930 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner650cea92009-05-05 04:57:08 +0000931 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000932 // Insert an llvm.dbg.declare into the current block.
933 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000934}
935
Chris Lattner9c85ba32008-11-10 06:08:34 +0000936void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
937 llvm::Value *Storage,
938 CGBuilderTy &Builder) {
939 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
940}
941
942/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
943/// variable declaration.
944void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
945 CGBuilderTy &Builder) {
946 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
947}
948
949
950
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000951/// EmitGlobalVariable - Emit information about a global variable.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000952void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
953 const VarDecl *Decl) {
Devang Patel07739032009-03-27 23:16:32 +0000954
955 // Do not emit variable debug information while generating optimized code.
956 // The llvm optimizer and code generator are not yet ready to support
957 // optimized code debugging.
958 const CompileOptions &CO = M->getCompileOpts();
959 if (CO.OptimizationLevel)
960 return;
961
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000962 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000963 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000964 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000965 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
966 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000967
968 std::string Name = Decl->getNameAsString();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000969
970 QualType T = Decl->getType();
971 if (T->isIncompleteArrayType()) {
972
973 // CodeGen turns int[] into int[1] so we'll do the same here.
974 llvm::APSInt ConstVal(32);
975
976 ConstVal = 1;
977 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
978
979 T = M->getContext().getConstantArrayType(ET, ConstVal,
980 ArrayType::Normal, 0);
981 }
982
Devang Patel61b5f3e2009-07-14 02:47:58 +0000983 DebugFactory.CreateGlobalVariable(Unit, Name, Name,
Chris Lattner991b5152009-07-14 18:18:16 +0000984 LLVMMangler->getMangledName(Var),
Devang Patel61b5f3e2009-07-14 02:47:58 +0000985 Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000986 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000987 Var->hasInternalLinkage(),
988 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000989}
990
Devang Patel9ca36b62009-02-26 21:10:26 +0000991/// EmitGlobalVariable - Emit information about an objective-c interface.
992void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
993 ObjCInterfaceDecl *Decl) {
994 // Create global variable debug descriptor.
995 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
996 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000997 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
998 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +0000999
1000 std::string Name = Decl->getNameAsString();
1001
Chris Lattner03d9f342009-04-01 06:23:52 +00001002 QualType T = M->getContext().getObjCInterfaceType(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001003 if (T->isIncompleteArrayType()) {
1004
1005 // CodeGen turns int[] into int[1] so we'll do the same here.
1006 llvm::APSInt ConstVal(32);
1007
1008 ConstVal = 1;
1009 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
1010
1011 T = M->getContext().getConstantArrayType(ET, ConstVal,
1012 ArrayType::Normal, 0);
1013 }
1014
Devang Patel61b5f3e2009-07-14 02:47:58 +00001015 DebugFactory.CreateGlobalVariable(Unit, Name, Name,
Chris Lattner991b5152009-07-14 18:18:16 +00001016 LLVMMangler->getMangledName(Var),
Devang Patel61b5f3e2009-07-14 02:47:58 +00001017 Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001018 getOrCreateType(T, Unit),
1019 Var->hasInternalLinkage(),
1020 true/*definition*/, Var);
1021}
1022