blob: a2d7b023d4a67d7f9137594bdd783aecc7821e2b [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"
Devang Patel9ca36b62009-02-26 21:10:26 +000017#include "clang/AST/DeclObjC.h"
Chris Lattner3cc5c402008-11-11 07:01:36 +000018#include "clang/AST/Expr.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000019#include "clang/AST/RecordLayout.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000020#include "clang/Basic/SourceManager.h"
21#include "clang/Basic/FileManager.h"
Devang Patel07739032009-03-27 23:16:32 +000022#include "clang/Frontend/CompileOptions.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000023#include "llvm/Constants.h"
24#include "llvm/DerivedTypes.h"
25#include "llvm/Instructions.h"
26#include "llvm/Intrinsics.h"
27#include "llvm/Module.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000028#include "llvm/ADT/StringExtras.h"
29#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000030#include "llvm/Support/Dwarf.h"
Devang Patel446c6192009-04-17 21:06:59 +000031#include "llvm/System/Path.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000032#include "llvm/Target/TargetMachine.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000033using namespace clang;
34using namespace clang::CodeGen;
35
Devang Patel6dba4322009-07-14 21:31:22 +000036CGDebugInfo::CGDebugInfo(CodeGenModule *m)
Mike Stump9bc093c2009-05-14 02:03:51 +000037 : M(m), isMainCompileUnitCreated(false), DebugFactory(M->getModule()),
38 BlockLiteralGenericSet(false) {
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000039}
40
Chris Lattner9c85ba32008-11-10 06:08:34 +000041CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar66031a52008-10-17 16:15:48 +000042 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000043}
44
Chris Lattner9c85ba32008-11-10 06:08:34 +000045void CGDebugInfo::setLocation(SourceLocation Loc) {
46 if (Loc.isValid())
Chris Lattnerf7cf85b2009-01-16 07:36:28 +000047 CurLoc = M->getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000048}
49
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000050/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
Daniel Dunbar25f51dd2008-10-24 08:38:36 +000051/// one if necessary. This returns null for invalid source locations.
Chris Lattner9c85ba32008-11-10 06:08:34 +000052llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) {
Devang Patel446c6192009-04-17 21:06:59 +000053 // Get source file information.
54 const char *FileName = "<unknown>";
Devang Patel77820222009-02-24 23:16:03 +000055 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattneradb1a6f2009-04-19 06:50:29 +000056 unsigned FID = 0;
Daniel Dunbar831570c2009-01-22 00:09:25 +000057 if (Loc.isValid()) {
Devang Patel446c6192009-04-17 21:06:59 +000058 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
59 FileName = PLoc.getFilename();
60 FID = PLoc.getIncludeLoc().getRawEncoding();
Daniel Dunbar831570c2009-01-22 00:09:25 +000061 }
62
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000063 // See if this compile unit has been used before.
Devang Patel446c6192009-04-17 21:06:59 +000064 llvm::DICompileUnit &Unit = CompileUnitCache[FID];
Chris Lattner9c85ba32008-11-10 06:08:34 +000065 if (!Unit.isNull()) return Unit;
Daniel Dunbarc9abc042009-04-08 05:11:16 +000066
Devang Patel446c6192009-04-17 21:06:59 +000067 // Get absolute path name.
68 llvm::sys::Path AbsFileName(FileName);
69 if (!AbsFileName.isAbsolute()) {
70 llvm::sys::Path tmp = llvm::sys::Path::GetCurrentDirectory();
71 tmp.appendComponent(FileName);
72 AbsFileName = tmp;
73 }
74
Devang Patel72240d72009-06-26 18:32:22 +000075 // See if thie compile unit is representing main source file. Each source
76 // file has corresponding compile unit. There is only one main source
77 // file at a time.
78 bool isMain = false;
79 const LangOptions &LO = M->getLangOptions();
80 const char *MainFileName = LO.getMainFileName();
81 if (isMainCompileUnitCreated == false) {
82 if (MainFileName) {
83 if (!strcmp(AbsFileName.getLast().c_str(), MainFileName))
84 isMain = true;
85 } else {
86 if (Loc.isValid() && SM.isFromMainFile(Loc))
87 isMain = true;
88 }
89 if (isMain)
90 isMainCompileUnitCreated = true;
Devang Patel446c6192009-04-17 21:06:59 +000091 }
Daniel Dunbarc9abc042009-04-08 05:11:16 +000092
Chris Lattner515455a2009-03-25 03:28:08 +000093 unsigned LangTag;
94 if (LO.CPlusPlus) {
95 if (LO.ObjC1)
96 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
97 else
98 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
99 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000100 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000101 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000102 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000103 } else {
104 LangTag = llvm::dwarf::DW_LANG_C89;
105 }
Devang Patel446c6192009-04-17 21:06:59 +0000106
Chris Lattnerb95ee582009-05-02 01:04:13 +0000107 std::string Producer = "clang 1.0";// FIXME: clang version.
108 bool isOptimized = LO.Optimize;
Chris Lattner4c2577a2009-05-02 01:00:04 +0000109 const char *Flags = ""; // FIXME: Encode command line options.
110
111 // Figure out which version of the ObjC runtime we have.
112 unsigned RuntimeVers = 0;
113 if (LO.ObjC1)
114 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
115
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000116 // Create new compile unit.
Devang Patel72240d72009-06-26 18:32:22 +0000117 return Unit = DebugFactory.CreateCompileUnit(LangTag, AbsFileName.getLast(),
118 AbsFileName.getDirname(),
119 Producer, isMain, isOptimized,
120 Flags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000121}
122
Devang Patel65e99f22009-02-25 01:36:11 +0000123/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000124/// one if necessary.
125llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel65e99f22009-02-25 01:36:11 +0000126 llvm::DICompileUnit Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000127 unsigned Encoding = 0;
128 switch (BT->getKind()) {
129 default:
130 case BuiltinType::Void:
131 return llvm::DIType();
132 case BuiltinType::UChar:
133 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
134 case BuiltinType::Char_S:
135 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
136 case BuiltinType::UShort:
137 case BuiltinType::UInt:
138 case BuiltinType::ULong:
139 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
140 case BuiltinType::Short:
141 case BuiltinType::Int:
142 case BuiltinType::Long:
143 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
144 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
145 case BuiltinType::Float:
146 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
147 }
148 // Bit size, align and offset of the type.
149 uint64_t Size = M->getContext().getTypeSize(BT);
150 uint64_t Align = M->getContext().getTypeAlign(BT);
151 uint64_t Offset = 0;
152
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000153 return DebugFactory.CreateBasicType(Unit,
Chris Lattnere4f21422009-06-30 01:26:17 +0000154 BT->getName(M->getContext().getLangOptions()),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000155 Unit, 0, Size, Align,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000156 Offset, /*flags*/ 0, Encoding);
157}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000158
Chris Lattnerb7003772009-04-23 06:13:01 +0000159llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
160 llvm::DICompileUnit Unit) {
161 // Bit size, align and offset of the type.
162 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
163 if (Ty->isComplexIntegerType())
164 Encoding = llvm::dwarf::DW_ATE_lo_user;
165
166 uint64_t Size = M->getContext().getTypeSize(Ty);
167 uint64_t Align = M->getContext().getTypeAlign(Ty);
168 uint64_t Offset = 0;
169
170 return DebugFactory.CreateBasicType(Unit, "complex",
171 Unit, 0, Size, Align,
172 Offset, /*flags*/ 0, Encoding);
173}
174
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000175/// getOrCreateCVRType - Get the CVR qualified type from the cache or create
176/// a new one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000177llvm::DIType CGDebugInfo::CreateCVRType(QualType Ty, llvm::DICompileUnit Unit) {
178 // We will create one Derived type for one qualifier and recurse to handle any
179 // additional ones.
180 llvm::DIType FromTy;
181 unsigned Tag;
182 if (Ty.isConstQualified()) {
183 Tag = llvm::dwarf::DW_TAG_const_type;
184 Ty.removeConst();
185 FromTy = getOrCreateType(Ty, Unit);
186 } else if (Ty.isVolatileQualified()) {
187 Tag = llvm::dwarf::DW_TAG_volatile_type;
188 Ty.removeVolatile();
189 FromTy = getOrCreateType(Ty, Unit);
190 } else {
191 assert(Ty.isRestrictQualified() && "Unknown type qualifier for debug info");
192 Tag = llvm::dwarf::DW_TAG_restrict_type;
193 Ty.removeRestrict();
194 FromTy = getOrCreateType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000195 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000196
Daniel Dunbar3845f862008-10-31 03:54:29 +0000197 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
198 // CVR derived types.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000199 return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
200 0, 0, 0, 0, 0, FromTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000201}
202
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000203llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
204 llvm::DICompileUnit Unit) {
205 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
206
207 // Bit size, align and offset of the type.
208 uint64_t Size = M->getContext().getTypeSize(Ty);
209 uint64_t Align = M->getContext().getTypeAlign(Ty);
210
211 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
212 "", llvm::DICompileUnit(),
213 0, Size, Align, 0, 0, EltTy);
214}
215
Chris Lattner9c85ba32008-11-10 06:08:34 +0000216llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
217 llvm::DICompileUnit Unit) {
218 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000219
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000220 // Bit size, align and offset of the type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000221 uint64_t Size = M->getContext().getTypeSize(Ty);
222 uint64_t Align = M->getContext().getTypeAlign(Ty);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000223
Chris Lattner9c85ba32008-11-10 06:08:34 +0000224 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
225 "", llvm::DICompileUnit(),
226 0, Size, Align, 0, 0, EltTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000227}
228
Mike Stump9bc093c2009-05-14 02:03:51 +0000229llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
230 llvm::DICompileUnit Unit) {
231 if (BlockLiteralGenericSet)
232 return BlockLiteralGeneric;
233
234 llvm::DICompileUnit DefUnit;
235 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
236
237 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
238
239 llvm::DIType FieldTy;
240
241 QualType FType;
242 uint64_t FieldSize, FieldOffset;
243 unsigned FieldAlign;
244
245 llvm::DIArray Elements;
246 llvm::DIType EltTy, DescTy;
247
248 FieldOffset = 0;
249 FType = M->getContext().UnsignedLongTy;
250 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
251 FieldSize = M->getContext().getTypeSize(FType);
252 FieldAlign = M->getContext().getTypeAlign(FType);
253 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
254 "reserved", DefUnit,
255 0, FieldSize, FieldAlign,
256 FieldOffset, 0, FieldTy);
257 EltTys.push_back(FieldTy);
258
259 FieldOffset += FieldSize;
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 "Size", DefUnit,
266 0, FieldSize, FieldAlign,
267 FieldOffset, 0, FieldTy);
268 EltTys.push_back(FieldTy);
269
270 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000271 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000272 EltTys.clear();
273
274 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
275 DefUnit, 0, FieldOffset, 0, 0, 0,
276 llvm::DIType(), Elements);
277
278 // Bit size, align and offset of the type.
279 uint64_t Size = M->getContext().getTypeSize(Ty);
280 uint64_t Align = M->getContext().getTypeAlign(Ty);
281
282 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
283 Unit, "", llvm::DICompileUnit(),
284 0, Size, Align, 0, 0, EltTy);
285
286 FieldOffset = 0;
287 FType = M->getContext().getPointerType(M->getContext().VoidTy);
288 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
289 FieldSize = M->getContext().getTypeSize(FType);
290 FieldAlign = M->getContext().getTypeAlign(FType);
291 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
292 "__isa", DefUnit,
293 0, FieldSize, FieldAlign,
294 FieldOffset, 0, FieldTy);
295 EltTys.push_back(FieldTy);
296
297 FieldOffset += FieldSize;
298 FType = M->getContext().IntTy;
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 "__flags", 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 "__reserved", DefUnit,
315 0, FieldSize, FieldAlign,
316 FieldOffset, 0, FieldTy);
317 EltTys.push_back(FieldTy);
318
319 FieldOffset += FieldSize;
320 FType = M->getContext().getPointerType(M->getContext().VoidTy);
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 "__FuncPtr", 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 = DescTy;
333 FieldSize = M->getContext().getTypeSize(Ty);
334 FieldAlign = M->getContext().getTypeAlign(Ty);
335 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
336 "__descriptor", DefUnit,
337 0, FieldSize, FieldAlign,
338 FieldOffset, 0, FieldTy);
339 EltTys.push_back(FieldTy);
340
341 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000342 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000343
344 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
345 DefUnit, 0, FieldOffset, 0, 0, 0,
346 llvm::DIType(), Elements);
347
348 BlockLiteralGenericSet = true;
349 BlockLiteralGeneric
350 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
351 "", llvm::DICompileUnit(),
352 0, Size, Align, 0, 0, EltTy);
353 return BlockLiteralGeneric;
354}
355
Chris Lattner9c85ba32008-11-10 06:08:34 +0000356llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
357 llvm::DICompileUnit Unit) {
358 // Typedefs are derived from some other type. If we have a typedef of a
359 // typedef, make sure to emit the whole chain.
360 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
361
362 // We don't set size information, but do specify where the typedef was
363 // declared.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000364 std::string TyName = Ty->getDecl()->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000365 SourceLocation DefLoc = Ty->getDecl()->getLocation();
366 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000367
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000368 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000369 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
370 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000371
Chris Lattner9c85ba32008-11-10 06:08:34 +0000372 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
373 TyName, DefUnit, Line, 0, 0, 0, 0, Src);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000374}
375
Chris Lattner9c85ba32008-11-10 06:08:34 +0000376llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
377 llvm::DICompileUnit Unit) {
378 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000379
Chris Lattner9c85ba32008-11-10 06:08:34 +0000380 // Add the result type at least.
381 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
382
383 // Set up remainder of arguments if there is a prototype.
384 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000385 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000386 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
387 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
388 } else {
389 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000390 }
391
Chris Lattner9c85ba32008-11-10 06:08:34 +0000392 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000393 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000394
395 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
396 Unit, "", llvm::DICompileUnit(),
397 0, 0, 0, 0, 0,
398 llvm::DIType(), EltTypeArray);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000399}
400
Devang Patel65e99f22009-02-25 01:36:11 +0000401/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000402llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
403 llvm::DICompileUnit Unit) {
Douglas Gregora4c46df2008-12-11 17:59:21 +0000404 RecordDecl *Decl = Ty->getDecl();
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000405
Chris Lattner9c85ba32008-11-10 06:08:34 +0000406 unsigned Tag;
407 if (Decl->isStruct())
408 Tag = llvm::dwarf::DW_TAG_structure_type;
409 else if (Decl->isUnion())
410 Tag = llvm::dwarf::DW_TAG_union_type;
411 else {
412 assert(Decl->isClass() && "Unknown RecordType!");
413 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000414 }
415
Sanjiv Gupta507de852008-06-09 10:47:41 +0000416 SourceManager &SM = M->getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000417
Chris Lattner9c85ba32008-11-10 06:08:34 +0000418 // Get overall information about the record type for the debug info.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000419 std::string Name = Decl->getNameAsString();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000420
Devang Patel4f6fa232009-04-17 21:35:15 +0000421 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000422 llvm::DICompileUnit DefUnit;
423 unsigned Line = 0;
424 if (!PLoc.isInvalid()) {
425 DefUnit = getOrCreateCompileUnit(Decl->getLocation());
426 Line = PLoc.getLine();
427 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000428
429 // Records and classes and unions can all be recursive. To handle them, we
430 // first generate a debug descriptor for the struct as a forward declaration.
431 // Then (if it is a definition) we go through and get debug info for all of
432 // its members. Finally, we create a descriptor for the complete type (which
433 // may refer to the forward decl if the struct is recursive) and replace all
434 // uses of the forward declaration with the final definition.
435 llvm::DIType FwdDecl =
436 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
437 llvm::DIType(), llvm::DIArray());
438
439 // If this is just a forward declaration, return it.
440 if (!Decl->getDefinition(M->getContext()))
441 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000442
Chris Lattner9c85ba32008-11-10 06:08:34 +0000443 // Otherwise, insert it into the TypeCache so that recursive uses will find
444 // it.
445 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
446
447 // Convert all the elements.
448 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
449
450 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl);
451
452 unsigned FieldNo = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000453 for (RecordDecl::field_iterator I = Decl->field_begin(),
454 E = Decl->field_end();
Douglas Gregora4c46df2008-12-11 17:59:21 +0000455 I != E; ++I, ++FieldNo) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000456 FieldDecl *Field = *I;
457 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattner8ec03f52008-11-24 03:54:41 +0000458
459 std::string FieldName = Field->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000460
Devang Patelde135022009-04-27 22:40:36 +0000461 // Ignore unnamed fields.
462 if (FieldName.empty())
463 continue;
464
Chris Lattner9c85ba32008-11-10 06:08:34 +0000465 // Get the location for the field.
466 SourceLocation FieldDefLoc = Field->getLocation();
Devang Patel4f6fa232009-04-17 21:35:15 +0000467 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000468 llvm::DICompileUnit FieldDefUnit;
469 unsigned FieldLine = 0;
470
471 if (!PLoc.isInvalid()) {
472 FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
473 FieldLine = PLoc.getLine();
474 }
Devang Patelec9b5d52009-03-16 23:47:53 +0000475
476 QualType FType = Field->getType();
477 uint64_t FieldSize = 0;
478 unsigned FieldAlign = 0;
479 if (!FType->isIncompleteArrayType()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000480
Devang Patelec9b5d52009-03-16 23:47:53 +0000481 // Bit size, align and offset of the type.
482 FieldSize = M->getContext().getTypeSize(FType);
483 Expr *BitWidth = Field->getBitWidth();
484 if (BitWidth)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000485 FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue();
Devang Patelec9b5d52009-03-16 23:47:53 +0000486
487 FieldAlign = M->getContext().getTypeAlign(FType);
488 }
489
Chris Lattner9c85ba32008-11-10 06:08:34 +0000490 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
491
492 // Create a DW_TAG_member node to remember the offset of this field in the
493 // struct. FIXME: This is an absolutely insane way to capture this
494 // information. When we gut debug info, this should be fixed.
495 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
496 FieldName, FieldDefUnit,
497 FieldLine, FieldSize, FieldAlign,
498 FieldOffset, 0, FieldTy);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000499 EltTys.push_back(FieldTy);
500 }
501
502 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000503 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000504
505 // Bit size, align and offset of the type.
506 uint64_t Size = M->getContext().getTypeSize(Ty);
507 uint64_t Align = M->getContext().getTypeAlign(Ty);
508
509 llvm::DIType RealDecl =
510 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
511 Align, 0, 0, llvm::DIType(), Elements);
512
513 // Now that we have a real decl for the struct, replace anything using the
514 // old decl with the new one. This will recursively update the debug info.
515 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
516 FwdDecl.getGV()->eraseFromParent();
Devang Patelfe09eab2009-07-13 17:03:14 +0000517
518 // Update TypeCache.
519 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = RealDecl;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000520 return RealDecl;
521}
522
Devang Patel9ca36b62009-02-26 21:10:26 +0000523/// CreateType - get objective-c interface type.
524llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
525 llvm::DICompileUnit Unit) {
526 ObjCInterfaceDecl *Decl = Ty->getDecl();
527
528 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
529 SourceManager &SM = M->getContext().getSourceManager();
530
531 // Get overall information about the record type for the debug info.
532 std::string Name = Decl->getNameAsString();
533
534 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000535 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
536 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
537
Devang Patel9ca36b62009-02-26 21:10:26 +0000538
Daniel Dunbard86d3362009-05-18 20:51:58 +0000539 unsigned RuntimeLang = DefUnit.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000540
Devang Patel9ca36b62009-02-26 21:10:26 +0000541 // To handle recursive interface, we
542 // first generate a debug descriptor for the struct as a forward declaration.
543 // Then (if it is a definition) we go through and get debug info for all of
544 // its members. Finally, we create a descriptor for the complete type (which
545 // may refer to the forward decl if the struct is recursive) and replace all
546 // uses of the forward declaration with the final definition.
547 llvm::DIType FwdDecl =
548 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000549 llvm::DIType(), llvm::DIArray(),
550 RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000551
552 // If this is just a forward declaration, return it.
553 if (Decl->isForwardDecl())
554 return FwdDecl;
555
556 // Otherwise, insert it into the TypeCache so that recursive uses will find
557 // it.
558 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
559
560 // Convert all the elements.
561 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
562
Devang Patelfbe899f2009-03-10 21:30:26 +0000563 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
564 if (SClass) {
565 llvm::DIType SClassTy =
566 getOrCreateType(M->getContext().getObjCInterfaceType(SClass), Unit);
567 llvm::DIType InhTag =
568 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Chris Lattner9e55b8a2009-05-05 05:05:36 +0000569 Unit, "", llvm::DICompileUnit(), 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000570 0 /* offset */, 0, SClassTy);
571 EltTys.push_back(InhTag);
572 }
573
Devang Patel9ca36b62009-02-26 21:10:26 +0000574 const ASTRecordLayout &RL = M->getContext().getASTObjCInterfaceLayout(Decl);
575
576 unsigned FieldNo = 0;
577 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
578 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
579 ObjCIvarDecl *Field = *I;
580 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
581
582 std::string FieldName = Field->getNameAsString();
583
Devang Patelde135022009-04-27 22:40:36 +0000584 // Ignore unnamed fields.
585 if (FieldName.empty())
586 continue;
587
Devang Patel9ca36b62009-02-26 21:10:26 +0000588 // Get the location for the field.
589 SourceLocation FieldDefLoc = Field->getLocation();
590 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000591 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
592 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
593
Devang Patel99c20eb2009-03-20 18:24:39 +0000594
595 QualType FType = Field->getType();
596 uint64_t FieldSize = 0;
597 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000598
Devang Patel99c20eb2009-03-20 18:24:39 +0000599 if (!FType->isIncompleteArrayType()) {
600
601 // Bit size, align and offset of the type.
602 FieldSize = M->getContext().getTypeSize(FType);
603 Expr *BitWidth = Field->getBitWidth();
604 if (BitWidth)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000605 FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue();
606
Devang Patel99c20eb2009-03-20 18:24:39 +0000607 FieldAlign = M->getContext().getTypeAlign(FType);
608 }
609
610 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
611
Devang Patelc20482b2009-03-19 00:23:53 +0000612 unsigned Flags = 0;
613 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
614 Flags = llvm::DIType::FlagProtected;
615 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
616 Flags = llvm::DIType::FlagPrivate;
617
Devang Patel9ca36b62009-02-26 21:10:26 +0000618 // Create a DW_TAG_member node to remember the offset of this field in the
619 // struct. FIXME: This is an absolutely insane way to capture this
620 // information. When we gut debug info, this should be fixed.
621 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
622 FieldName, FieldDefUnit,
623 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000624 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000625 EltTys.push_back(FieldTy);
626 }
627
628 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000629 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +0000630
631 // Bit size, align and offset of the type.
632 uint64_t Size = M->getContext().getTypeSize(Ty);
633 uint64_t Align = M->getContext().getTypeAlign(Ty);
634
635 llvm::DIType RealDecl =
636 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000637 Align, 0, 0, llvm::DIType(), Elements,
638 RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000639
640 // Now that we have a real decl for the struct, replace anything using the
641 // old decl with the new one. This will recursively update the debug info.
642 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
643 FwdDecl.getGV()->eraseFromParent();
Devang Patelfe09eab2009-07-13 17:03:14 +0000644
645 // Update TypeCache.
646 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = RealDecl;
Devang Patel9ca36b62009-02-26 21:10:26 +0000647 return RealDecl;
648}
649
Chris Lattner9c85ba32008-11-10 06:08:34 +0000650llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
651 llvm::DICompileUnit Unit) {
652 EnumDecl *Decl = Ty->getDecl();
653
654 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
655
656 // Create DIEnumerator elements for each enumerator.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000657 for (EnumDecl::enumerator_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000658 Enum = Decl->enumerator_begin(), EnumEnd = Decl->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000659 Enum != EnumEnd; ++Enum) {
660 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(),
661 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000662 }
663
664 // Return a CompositeType for the enum itself.
665 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000666 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000667
Chris Lattner8ec03f52008-11-24 03:54:41 +0000668 std::string EnumName = Decl->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000669 SourceLocation DefLoc = Decl->getLocation();
670 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
671 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000672 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
673 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
674
Chris Lattner9c85ba32008-11-10 06:08:34 +0000675
676 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +0000677 uint64_t Size = 0;
678 unsigned Align = 0;
679 if (!Ty->isIncompleteType()) {
680 Size = M->getContext().getTypeSize(Ty);
681 Align = M->getContext().getTypeAlign(Ty);
682 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000683
684 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
685 Unit, EnumName, DefUnit, Line,
686 Size, Align, 0, 0,
687 llvm::DIType(), EltArray);
688}
689
690llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
691 llvm::DICompileUnit Unit) {
692 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
693 return CreateType(RT, Unit);
694 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
695 return CreateType(ET, Unit);
696
697 return llvm::DIType();
698}
699
700llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
701 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000702 uint64_t Size;
703 uint64_t Align;
704
705
Nuno Lopes010d5142009-01-28 00:35:17 +0000706 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +0000707 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000708 Size = 0;
709 Align =
Nuno Lopes010d5142009-01-28 00:35:17 +0000710 M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT));
711 } else if (Ty->isIncompleteArrayType()) {
712 Size = 0;
713 Align = M->getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +0000714 } else {
715 // Size and align of the whole array, not the element type.
716 Size = M->getContext().getTypeSize(Ty);
717 Align = M->getContext().getTypeAlign(Ty);
718 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000719
720 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
721 // interior arrays, do we care? Why aren't nested arrays represented the
722 // obvious/recursive way?
723 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
724 QualType EltTy(Ty, 0);
725 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000726 uint64_t Upper = 0;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000727 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
728 Upper = CAT->getSize().getZExtValue() - 1;
729 // FIXME: Verify this is right for VLAs.
730 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
731 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000732 }
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000733
Chris Lattner9c85ba32008-11-10 06:08:34 +0000734 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000735 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000736
737 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
738 Unit, "", llvm::DICompileUnit(),
739 0, Size, Align, 0, 0,
740 getOrCreateType(EltTy, Unit),
741 SubscriptArray);
742}
743
744
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000745/// getOrCreateType - Get the type from the cache or create a new
746/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000747llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
748 llvm::DICompileUnit Unit) {
749 if (Ty.isNull())
750 return llvm::DIType();
751
Devang Patel0ae3d602009-07-13 16:15:54 +0000752 // Check TypeCache first.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000753 llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()];
754 if (!Slot.isNull()) return Slot;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000755
Chris Lattner9c85ba32008-11-10 06:08:34 +0000756 // Handle CVR qualifiers, which recursively handles what they refer to.
757 if (Ty.getCVRQualifiers())
758 return Slot = CreateCVRType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000759
760 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000761 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000762#define TYPE(Class, Base)
763#define ABSTRACT_TYPE(Class, Base)
764#define NON_CANONICAL_TYPE(Class, Base)
765#define DEPENDENT_TYPE(Class, Base) case Type::Class:
766#include "clang/AST/TypeNodes.def"
767 assert(false && "Dependent types cannot show up in debug information");
768
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000769 case Type::LValueReference:
770 case Type::RValueReference:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000771 case Type::Vector:
772 case Type::ExtVector:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000773 case Type::ExtQual:
Eli Friedman00524e32009-02-27 23:15:07 +0000774 case Type::FixedWidthInt:
Eli Friedman00524e32009-02-27 23:15:07 +0000775 case Type::MemberPointer:
Douglas Gregor7532dc62009-03-30 22:58:21 +0000776 case Type::TemplateSpecialization:
Douglas Gregore4e5b052009-03-19 00:18:19 +0000777 case Type::QualifiedName:
Eli Friedman00524e32009-02-27 23:15:07 +0000778 // Unsupported types
Chris Lattner9c85ba32008-11-10 06:08:34 +0000779 return llvm::DIType();
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000780 case Type::ObjCObjectPointer:
781 return Slot = CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Devang Patele7987062009-03-02 17:58:28 +0000782 case Type::ObjCInterface:
Chris Lattneraa2b5792009-04-22 06:58:56 +0000783 return Slot = CreateType(cast<ObjCInterfaceType>(Ty), Unit);
784 case Type::Builtin: return Slot = CreateType(cast<BuiltinType>(Ty), Unit);
Chris Lattnerb7003772009-04-23 06:13:01 +0000785 case Type::Complex: return Slot = CreateType(cast<ComplexType>(Ty), Unit);
Chris Lattneraa2b5792009-04-22 06:58:56 +0000786 case Type::Pointer: return Slot = CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +0000787 case Type::BlockPointer:
788 return Slot = CreateType(cast<BlockPointerType>(Ty), Unit);
Chris Lattneraa2b5792009-04-22 06:58:56 +0000789 case Type::Typedef: return Slot = CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000790 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000791 case Type::Enum:
Chris Lattneraa2b5792009-04-22 06:58:56 +0000792 return Slot = CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000793 case Type::FunctionProto:
794 case Type::FunctionNoProto:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000795 return Slot = CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000796
797 case Type::ConstantArray:
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000798 case Type::ConstantArrayWithExpr:
799 case Type::ConstantArrayWithoutExpr:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000800 case Type::VariableArray:
801 case Type::IncompleteArray:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000802 return Slot = CreateType(cast<ArrayType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000803 case Type::TypeOfExpr:
804 return Slot = getOrCreateType(cast<TypeOfExprType>(Ty)->getUnderlyingExpr()
Chris Lattner3cc5c402008-11-11 07:01:36 +0000805 ->getType(), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000806 case Type::TypeOf:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000807 return Slot = getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(),
808 Unit);
Anders Carlsson395b4752009-06-24 19:06:50 +0000809 case Type::Decltype:
Anders Carlsson563a03b2009-07-10 19:20:26 +0000810 return Slot = getOrCreateType(cast<DecltypeType>(Ty)->getUnderlyingType(),
811 Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000812 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000813
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000814 return Slot;
815}
816
817/// EmitFunctionStart - Constructs the debug code for entering a function -
818/// "llvm.dbg.func.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000819void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000820 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000821 CGBuilderTy &Builder) {
Devang Patel6dba4322009-07-14 21:31:22 +0000822 const char *LinkageName = Name;
823
Daniel Dunbara2893932009-05-13 23:08:57 +0000824 // Skip the asm prefix if it exists.
Daniel Dunbarbbd53af2009-05-14 01:45:24 +0000825 //
826 // FIXME: This should probably be the unmangled name?
Daniel Dunbara2893932009-05-13 23:08:57 +0000827 if (Name[0] == '\01')
828 ++Name;
829
Chris Lattner9c85ba32008-11-10 06:08:34 +0000830 // FIXME: Why is this using CurLoc???
831 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000832 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +0000833 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000834
835 llvm::DISubprogram SP =
Devang Patel6dba4322009-07-14 21:31:22 +0000836 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000837 getOrCreateType(ReturnType, Unit),
838 Fn->hasInternalLinkage(), true/*definition*/);
839
840 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
841
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000842 // Push function on region stack.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000843 RegionStack.push_back(SP);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000844}
845
846
Chris Lattner9c85ba32008-11-10 06:08:34 +0000847void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000848 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
849
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000850 // Don't bother if things are the same as last time.
851 SourceManager &SM = M->getContext().getSourceManager();
852 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +0000853 || (SM.getInstantiationLineNumber(CurLoc) ==
854 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000855 && SM.isFromSameFile(CurLoc, PrevLoc)))
856 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000857
858 // Update last state.
859 PrevLoc = CurLoc;
860
861 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000862 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +0000863 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
864 DebugFactory.InsertStopPoint(Unit, PLoc.getLine(), PLoc.getColumn(),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000865 Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000866}
867
868/// EmitRegionStart- Constructs the debug code for entering a declarative
869/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000870void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
871 llvm::DIDescriptor D;
Daniel Dunbar5273f512008-10-17 01:07:56 +0000872 if (!RegionStack.empty())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000873 D = RegionStack.back();
874 D = DebugFactory.CreateBlock(D);
875 RegionStack.push_back(D);
876 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000877}
878
879/// EmitRegionEnd - Constructs the debug code for exiting a declarative
880/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +0000881void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000882 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
883
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000884 // Provide an region stop point.
885 EmitStopPoint(Fn, Builder);
886
Chris Lattner9c85ba32008-11-10 06:08:34 +0000887 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000888 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000889}
890
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000891/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000892void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
893 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000894 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
895
Devang Patel07739032009-03-27 23:16:32 +0000896 // Do not emit variable debug information while generating optimized code.
897 // The llvm optimizer and code generator are not yet ready to support
898 // optimized code debugging.
899 const CompileOptions &CO = M->getCompileOpts();
900 if (CO.OptimizationLevel)
901 return;
902
Chris Lattner650cea92009-05-05 04:57:08 +0000903 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
904 llvm::DIType Ty = getOrCreateType(Decl->getType(), Unit);
905
Chris Lattner9c85ba32008-11-10 06:08:34 +0000906 // Get location information.
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000907 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000908 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +0000909 unsigned Line = 0;
910 if (!PLoc.isInvalid())
911 Line = PLoc.getLine();
912 else
913 Unit = llvm::DICompileUnit();
914
Chris Lattner9c85ba32008-11-10 06:08:34 +0000915
916 // Create the descriptor for the variable.
917 llvm::DIVariable D =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000918 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner650cea92009-05-05 04:57:08 +0000919 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000920 // Insert an llvm.dbg.declare into the current block.
921 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000922}
923
Chris Lattner9c85ba32008-11-10 06:08:34 +0000924void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
925 llvm::Value *Storage,
926 CGBuilderTy &Builder) {
927 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
928}
929
930/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
931/// variable declaration.
932void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
933 CGBuilderTy &Builder) {
934 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
935}
936
937
938
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000939/// EmitGlobalVariable - Emit information about a global variable.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000940void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
941 const VarDecl *Decl) {
Devang Patel07739032009-03-27 23:16:32 +0000942
943 // Do not emit variable debug information while generating optimized code.
944 // The llvm optimizer and code generator are not yet ready to support
945 // optimized code debugging.
946 const CompileOptions &CO = M->getCompileOpts();
947 if (CO.OptimizationLevel)
948 return;
949
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000950 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000951 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000952 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000953 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
954 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000955
956 std::string Name = Decl->getNameAsString();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000957
958 QualType T = Decl->getType();
959 if (T->isIncompleteArrayType()) {
960
961 // CodeGen turns int[] into int[1] so we'll do the same here.
962 llvm::APSInt ConstVal(32);
963
964 ConstVal = 1;
965 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
966
967 T = M->getContext().getConstantArrayType(ET, ConstVal,
968 ArrayType::Normal, 0);
969 }
970
Devang Patel6dba4322009-07-14 21:31:22 +0000971 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000972 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000973 Var->hasInternalLinkage(),
974 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000975}
976
Devang Patel9ca36b62009-02-26 21:10:26 +0000977/// EmitGlobalVariable - Emit information about an objective-c interface.
978void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
979 ObjCInterfaceDecl *Decl) {
980 // Create global variable debug descriptor.
981 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
982 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000983 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
984 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +0000985
986 std::string Name = Decl->getNameAsString();
987
Chris Lattner03d9f342009-04-01 06:23:52 +0000988 QualType T = M->getContext().getObjCInterfaceType(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000989 if (T->isIncompleteArrayType()) {
990
991 // CodeGen turns int[] into int[1] so we'll do the same here.
992 llvm::APSInt ConstVal(32);
993
994 ConstVal = 1;
995 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
996
997 T = M->getContext().getConstantArrayType(ET, ConstVal,
998 ArrayType::Normal, 0);
999 }
1000
Devang Patel6dba4322009-07-14 21:31:22 +00001001 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001002 getOrCreateType(T, Unit),
1003 Var->hasInternalLinkage(),
1004 true/*definition*/, Var);
1005}
1006