blob: fb1fefe6ff6745e02c2a372c690c5ebc44c1e160 [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"
Daniel Dunbare91593e2008-08-11 04:54:23 +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
36CGDebugInfo::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 Patel99831692009-06-16 18:02:02 +000053
54 // Each input file is encoded as a separate compile unit in LLVM
55 // debugging information output. However, many target specific tool chains
56 // prefer to encode only one compile unit in an object file. In this
57 // situation, the LLVM code generator will include debugging information
58 // entities in the compile unit that is marked as main compile unit. The
59 // code generator accepts maximum one main compile unit per module. If a
60 // module does not contain any main compile unit then the code generator
61 // will emit multiple compile units in the output object file. Create main
62 // compile unit if there is not one available.
63 const LangOptions &LO = M->getLangOptions();
64 if (isMainCompileUnitCreated == false) {
65 if (LO.getMainFileName()) {
66 createCompileUnit(LO.getMainFileName(), true /* isMain */);
67 isMainCompileUnitCreated = true;
68 }
69 }
70
Devang Patel446c6192009-04-17 21:06:59 +000071 // Get source file information.
72 const char *FileName = "<unknown>";
Devang Patel77820222009-02-24 23:16:03 +000073 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattneradb1a6f2009-04-19 06:50:29 +000074 unsigned FID = 0;
Daniel Dunbar831570c2009-01-22 00:09:25 +000075 if (Loc.isValid()) {
Devang Patel446c6192009-04-17 21:06:59 +000076 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
77 FileName = PLoc.getFilename();
78 FID = PLoc.getIncludeLoc().getRawEncoding();
Daniel Dunbar831570c2009-01-22 00:09:25 +000079 }
80
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000081 // See if this compile unit has been used before.
Devang Patel446c6192009-04-17 21:06:59 +000082 llvm::DICompileUnit &Unit = CompileUnitCache[FID];
Chris Lattner9c85ba32008-11-10 06:08:34 +000083 if (!Unit.isNull()) return Unit;
Daniel Dunbarc9abc042009-04-08 05:11:16 +000084
Devang Patel446c6192009-04-17 21:06:59 +000085 // Get absolute path name.
86 llvm::sys::Path AbsFileName(FileName);
87 if (!AbsFileName.isAbsolute()) {
88 llvm::sys::Path tmp = llvm::sys::Path::GetCurrentDirectory();
89 tmp.appendComponent(FileName);
90 AbsFileName = tmp;
91 }
92
Devang Patel99831692009-06-16 18:02:02 +000093 // There is only one main source file at a time whose compile unit
94 // is already created.
95 Unit = createCompileUnit(FileName, false /* isMain */);
96 return Unit;
97}
98
99/// createCompileUnit - Create a new unit for the given file.
100llvm::DICompileUnit CGDebugInfo::createCompileUnit(const char *FileName,
101 bool isMain) {
102
103 // Get absolute path name.
104 llvm::sys::Path AbsFileName(FileName);
105 if (!AbsFileName.isAbsolute()) {
106 llvm::sys::Path tmp = llvm::sys::Path::GetCurrentDirectory();
107 tmp.appendComponent(FileName);
108 AbsFileName = tmp;
Devang Patel446c6192009-04-17 21:06:59 +0000109 }
Daniel Dunbarc9abc042009-04-08 05:11:16 +0000110
Chris Lattner515455a2009-03-25 03:28:08 +0000111 unsigned LangTag;
Devang Patel99831692009-06-16 18:02:02 +0000112 const LangOptions &LO = M->getLangOptions();
Chris Lattner515455a2009-03-25 03:28:08 +0000113 if (LO.CPlusPlus) {
114 if (LO.ObjC1)
115 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
116 else
117 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
118 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000119 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000120 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000121 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000122 } else {
123 LangTag = llvm::dwarf::DW_LANG_C89;
124 }
Devang Patel446c6192009-04-17 21:06:59 +0000125
Chris Lattnerb95ee582009-05-02 01:04:13 +0000126 std::string Producer = "clang 1.0";// FIXME: clang version.
127 bool isOptimized = LO.Optimize;
Chris Lattner4c2577a2009-05-02 01:00:04 +0000128 const char *Flags = ""; // FIXME: Encode command line options.
129
130 // Figure out which version of the ObjC runtime we have.
131 unsigned RuntimeVers = 0;
132 if (LO.ObjC1)
133 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
134
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000135 // Create new compile unit.
Devang Patel99831692009-06-16 18:02:02 +0000136 return DebugFactory.CreateCompileUnit(LangTag, AbsFileName.getLast(),
137 AbsFileName.getDirname(),
138 Producer, isMain, isOptimized,
139 Flags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000140}
141
Devang Patel99831692009-06-16 18:02:02 +0000142
Devang Patel65e99f22009-02-25 01:36:11 +0000143/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000144/// one if necessary.
145llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel65e99f22009-02-25 01:36:11 +0000146 llvm::DICompileUnit Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000147 unsigned Encoding = 0;
148 switch (BT->getKind()) {
149 default:
150 case BuiltinType::Void:
151 return llvm::DIType();
152 case BuiltinType::UChar:
153 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
154 case BuiltinType::Char_S:
155 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
156 case BuiltinType::UShort:
157 case BuiltinType::UInt:
158 case BuiltinType::ULong:
159 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
160 case BuiltinType::Short:
161 case BuiltinType::Int:
162 case BuiltinType::Long:
163 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
164 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
165 case BuiltinType::Float:
166 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
167 }
168 // Bit size, align and offset of the type.
169 uint64_t Size = M->getContext().getTypeSize(BT);
170 uint64_t Align = M->getContext().getTypeAlign(BT);
171 uint64_t Offset = 0;
172
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000173 return DebugFactory.CreateBasicType(Unit,
174 BT->getName(M->getContext().getLangOptions().CPlusPlus),
175 Unit, 0, Size, Align,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000176 Offset, /*flags*/ 0, Encoding);
177}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000178
Chris Lattnerb7003772009-04-23 06:13:01 +0000179llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
180 llvm::DICompileUnit Unit) {
181 // Bit size, align and offset of the type.
182 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
183 if (Ty->isComplexIntegerType())
184 Encoding = llvm::dwarf::DW_ATE_lo_user;
185
186 uint64_t Size = M->getContext().getTypeSize(Ty);
187 uint64_t Align = M->getContext().getTypeAlign(Ty);
188 uint64_t Offset = 0;
189
190 return DebugFactory.CreateBasicType(Unit, "complex",
191 Unit, 0, Size, Align,
192 Offset, /*flags*/ 0, Encoding);
193}
194
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000195/// getOrCreateCVRType - Get the CVR qualified type from the cache or create
196/// a new one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000197llvm::DIType CGDebugInfo::CreateCVRType(QualType Ty, llvm::DICompileUnit Unit) {
198 // We will create one Derived type for one qualifier and recurse to handle any
199 // additional ones.
200 llvm::DIType FromTy;
201 unsigned Tag;
202 if (Ty.isConstQualified()) {
203 Tag = llvm::dwarf::DW_TAG_const_type;
204 Ty.removeConst();
205 FromTy = getOrCreateType(Ty, Unit);
206 } else if (Ty.isVolatileQualified()) {
207 Tag = llvm::dwarf::DW_TAG_volatile_type;
208 Ty.removeVolatile();
209 FromTy = getOrCreateType(Ty, Unit);
210 } else {
211 assert(Ty.isRestrictQualified() && "Unknown type qualifier for debug info");
212 Tag = llvm::dwarf::DW_TAG_restrict_type;
213 Ty.removeRestrict();
214 FromTy = getOrCreateType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000215 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000216
Daniel Dunbar3845f862008-10-31 03:54:29 +0000217 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
218 // CVR derived types.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000219 return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
220 0, 0, 0, 0, 0, FromTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000221}
222
Chris Lattner9c85ba32008-11-10 06:08:34 +0000223llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
224 llvm::DICompileUnit Unit) {
225 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000226
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000227 // Bit size, align and offset of the type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000228 uint64_t Size = M->getContext().getTypeSize(Ty);
229 uint64_t Align = M->getContext().getTypeAlign(Ty);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000230
Chris Lattner9c85ba32008-11-10 06:08:34 +0000231 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
232 "", llvm::DICompileUnit(),
233 0, Size, Align, 0, 0, EltTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000234}
235
Mike Stump9bc093c2009-05-14 02:03:51 +0000236llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
237 llvm::DICompileUnit Unit) {
238 if (BlockLiteralGenericSet)
239 return BlockLiteralGeneric;
240
241 llvm::DICompileUnit DefUnit;
242 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
243
244 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
245
246 llvm::DIType FieldTy;
247
248 QualType FType;
249 uint64_t FieldSize, FieldOffset;
250 unsigned FieldAlign;
251
252 llvm::DIArray Elements;
253 llvm::DIType EltTy, DescTy;
254
255 FieldOffset = 0;
256 FType = M->getContext().UnsignedLongTy;
257 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
258 FieldSize = M->getContext().getTypeSize(FType);
259 FieldAlign = M->getContext().getTypeAlign(FType);
260 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
261 "reserved", DefUnit,
262 0, FieldSize, FieldAlign,
263 FieldOffset, 0, FieldTy);
264 EltTys.push_back(FieldTy);
265
266 FieldOffset += FieldSize;
267 FType = M->getContext().UnsignedLongTy;
268 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
269 FieldSize = M->getContext().getTypeSize(FType);
270 FieldAlign = M->getContext().getTypeAlign(FType);
271 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
272 "Size", DefUnit,
273 0, FieldSize, FieldAlign,
274 FieldOffset, 0, FieldTy);
275 EltTys.push_back(FieldTy);
276
277 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000278 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000279 EltTys.clear();
280
281 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
282 DefUnit, 0, FieldOffset, 0, 0, 0,
283 llvm::DIType(), Elements);
284
285 // Bit size, align and offset of the type.
286 uint64_t Size = M->getContext().getTypeSize(Ty);
287 uint64_t Align = M->getContext().getTypeAlign(Ty);
288
289 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
290 Unit, "", llvm::DICompileUnit(),
291 0, Size, Align, 0, 0, EltTy);
292
293 FieldOffset = 0;
294 FType = M->getContext().getPointerType(M->getContext().VoidTy);
295 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
296 FieldSize = M->getContext().getTypeSize(FType);
297 FieldAlign = M->getContext().getTypeAlign(FType);
298 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
299 "__isa", DefUnit,
300 0, FieldSize, FieldAlign,
301 FieldOffset, 0, FieldTy);
302 EltTys.push_back(FieldTy);
303
304 FieldOffset += FieldSize;
305 FType = M->getContext().IntTy;
306 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
307 FieldSize = M->getContext().getTypeSize(FType);
308 FieldAlign = M->getContext().getTypeAlign(FType);
309 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
310 "__flags", DefUnit,
311 0, FieldSize, FieldAlign,
312 FieldOffset, 0, FieldTy);
313 EltTys.push_back(FieldTy);
314
315 FieldOffset += FieldSize;
316 FType = M->getContext().IntTy;
317 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
318 FieldSize = M->getContext().getTypeSize(FType);
319 FieldAlign = M->getContext().getTypeAlign(FType);
320 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
321 "__reserved", DefUnit,
322 0, FieldSize, FieldAlign,
323 FieldOffset, 0, FieldTy);
324 EltTys.push_back(FieldTy);
325
326 FieldOffset += FieldSize;
327 FType = M->getContext().getPointerType(M->getContext().VoidTy);
328 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
329 FieldSize = M->getContext().getTypeSize(FType);
330 FieldAlign = M->getContext().getTypeAlign(FType);
331 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
332 "__FuncPtr", DefUnit,
333 0, FieldSize, FieldAlign,
334 FieldOffset, 0, FieldTy);
335 EltTys.push_back(FieldTy);
336
337 FieldOffset += FieldSize;
338 FType = M->getContext().getPointerType(M->getContext().VoidTy);
339 FieldTy = DescTy;
340 FieldSize = M->getContext().getTypeSize(Ty);
341 FieldAlign = M->getContext().getTypeAlign(Ty);
342 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
343 "__descriptor", DefUnit,
344 0, FieldSize, FieldAlign,
345 FieldOffset, 0, FieldTy);
346 EltTys.push_back(FieldTy);
347
348 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000349 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000350
351 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
352 DefUnit, 0, FieldOffset, 0, 0, 0,
353 llvm::DIType(), Elements);
354
355 BlockLiteralGenericSet = true;
356 BlockLiteralGeneric
357 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
358 "", llvm::DICompileUnit(),
359 0, Size, Align, 0, 0, EltTy);
360 return BlockLiteralGeneric;
361}
362
Chris Lattner9c85ba32008-11-10 06:08:34 +0000363llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
364 llvm::DICompileUnit Unit) {
365 // Typedefs are derived from some other type. If we have a typedef of a
366 // typedef, make sure to emit the whole chain.
367 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
368
369 // We don't set size information, but do specify where the typedef was
370 // declared.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000371 std::string TyName = Ty->getDecl()->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000372 SourceLocation DefLoc = Ty->getDecl()->getLocation();
373 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000374
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000375 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000376 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
377 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000378
Chris Lattner9c85ba32008-11-10 06:08:34 +0000379 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
380 TyName, DefUnit, Line, 0, 0, 0, 0, Src);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000381}
382
Chris Lattner9c85ba32008-11-10 06:08:34 +0000383llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
384 llvm::DICompileUnit Unit) {
385 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000386
Chris Lattner9c85ba32008-11-10 06:08:34 +0000387 // Add the result type at least.
388 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
389
390 // Set up remainder of arguments if there is a prototype.
391 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000392 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000393 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
394 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
395 } else {
396 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000397 }
398
Chris Lattner9c85ba32008-11-10 06:08:34 +0000399 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000400 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000401
402 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
403 Unit, "", llvm::DICompileUnit(),
404 0, 0, 0, 0, 0,
405 llvm::DIType(), EltTypeArray);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000406}
407
Devang Patel65e99f22009-02-25 01:36:11 +0000408/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000409llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
410 llvm::DICompileUnit Unit) {
Douglas Gregora4c46df2008-12-11 17:59:21 +0000411 RecordDecl *Decl = Ty->getDecl();
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000412
Chris Lattner9c85ba32008-11-10 06:08:34 +0000413 unsigned Tag;
414 if (Decl->isStruct())
415 Tag = llvm::dwarf::DW_TAG_structure_type;
416 else if (Decl->isUnion())
417 Tag = llvm::dwarf::DW_TAG_union_type;
418 else {
419 assert(Decl->isClass() && "Unknown RecordType!");
420 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000421 }
422
Sanjiv Gupta507de852008-06-09 10:47:41 +0000423 SourceManager &SM = M->getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000424
Chris Lattner9c85ba32008-11-10 06:08:34 +0000425 // Get overall information about the record type for the debug info.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000426 std::string Name = Decl->getNameAsString();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000427
Devang Patel4f6fa232009-04-17 21:35:15 +0000428 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000429 llvm::DICompileUnit DefUnit;
430 unsigned Line = 0;
431 if (!PLoc.isInvalid()) {
432 DefUnit = getOrCreateCompileUnit(Decl->getLocation());
433 Line = PLoc.getLine();
434 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000435
436 // Records and classes and unions can all be recursive. To handle them, we
437 // first generate a debug descriptor for the struct as a forward declaration.
438 // Then (if it is a definition) we go through and get debug info for all of
439 // its members. Finally, we create a descriptor for the complete type (which
440 // may refer to the forward decl if the struct is recursive) and replace all
441 // uses of the forward declaration with the final definition.
442 llvm::DIType FwdDecl =
443 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
444 llvm::DIType(), llvm::DIArray());
445
446 // If this is just a forward declaration, return it.
447 if (!Decl->getDefinition(M->getContext()))
448 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000449
Chris Lattner9c85ba32008-11-10 06:08:34 +0000450 // Otherwise, insert it into the TypeCache so that recursive uses will find
451 // it.
452 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
453
454 // Convert all the elements.
455 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
456
457 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl);
458
459 unsigned FieldNo = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000460 for (RecordDecl::field_iterator I = Decl->field_begin(M->getContext()),
461 E = Decl->field_end(M->getContext());
Douglas Gregora4c46df2008-12-11 17:59:21 +0000462 I != E; ++I, ++FieldNo) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000463 FieldDecl *Field = *I;
464 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattner8ec03f52008-11-24 03:54:41 +0000465
466 std::string FieldName = Field->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000467
Devang Patelde135022009-04-27 22:40:36 +0000468 // Ignore unnamed fields.
469 if (FieldName.empty())
470 continue;
471
Chris Lattner9c85ba32008-11-10 06:08:34 +0000472 // Get the location for the field.
473 SourceLocation FieldDefLoc = Field->getLocation();
Devang Patel4f6fa232009-04-17 21:35:15 +0000474 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000475 llvm::DICompileUnit FieldDefUnit;
476 unsigned FieldLine = 0;
477
478 if (!PLoc.isInvalid()) {
479 FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
480 FieldLine = PLoc.getLine();
481 }
Devang Patelec9b5d52009-03-16 23:47:53 +0000482
483 QualType FType = Field->getType();
484 uint64_t FieldSize = 0;
485 unsigned FieldAlign = 0;
486 if (!FType->isIncompleteArrayType()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000487
Devang Patelec9b5d52009-03-16 23:47:53 +0000488 // Bit size, align and offset of the type.
489 FieldSize = M->getContext().getTypeSize(FType);
490 Expr *BitWidth = Field->getBitWidth();
491 if (BitWidth)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000492 FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue();
Devang Patelec9b5d52009-03-16 23:47:53 +0000493
494 FieldAlign = M->getContext().getTypeAlign(FType);
495 }
496
Chris Lattner9c85ba32008-11-10 06:08:34 +0000497 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
498
499 // Create a DW_TAG_member node to remember the offset of this field in the
500 // struct. FIXME: This is an absolutely insane way to capture this
501 // information. When we gut debug info, this should be fixed.
502 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
503 FieldName, FieldDefUnit,
504 FieldLine, FieldSize, FieldAlign,
505 FieldOffset, 0, FieldTy);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000506 EltTys.push_back(FieldTy);
507 }
508
509 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000510 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000511
512 // Bit size, align and offset of the type.
513 uint64_t Size = M->getContext().getTypeSize(Ty);
514 uint64_t Align = M->getContext().getTypeAlign(Ty);
515
516 llvm::DIType RealDecl =
517 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
518 Align, 0, 0, llvm::DIType(), Elements);
519
520 // Now that we have a real decl for the struct, replace anything using the
521 // old decl with the new one. This will recursively update the debug info.
522 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
523 FwdDecl.getGV()->eraseFromParent();
524
525 return RealDecl;
526}
527
Devang Patel9ca36b62009-02-26 21:10:26 +0000528/// CreateType - get objective-c interface type.
529llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
530 llvm::DICompileUnit Unit) {
531 ObjCInterfaceDecl *Decl = Ty->getDecl();
532
533 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
534 SourceManager &SM = M->getContext().getSourceManager();
535
536 // Get overall information about the record type for the debug info.
537 std::string Name = Decl->getNameAsString();
538
539 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000540 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
541 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
542
Devang Patel9ca36b62009-02-26 21:10:26 +0000543
Daniel Dunbard86d3362009-05-18 20:51:58 +0000544 unsigned RuntimeLang = DefUnit.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000545
Devang Patel9ca36b62009-02-26 21:10:26 +0000546 // To handle recursive interface, we
547 // first generate a debug descriptor for the struct as a forward declaration.
548 // Then (if it is a definition) we go through and get debug info for all of
549 // its members. Finally, we create a descriptor for the complete type (which
550 // may refer to the forward decl if the struct is recursive) and replace all
551 // uses of the forward declaration with the final definition.
552 llvm::DIType FwdDecl =
553 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000554 llvm::DIType(), llvm::DIArray(),
555 RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000556
557 // If this is just a forward declaration, return it.
558 if (Decl->isForwardDecl())
559 return FwdDecl;
560
561 // Otherwise, insert it into the TypeCache so that recursive uses will find
562 // it.
563 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
564
565 // Convert all the elements.
566 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
567
Devang Patelfbe899f2009-03-10 21:30:26 +0000568 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
569 if (SClass) {
570 llvm::DIType SClassTy =
571 getOrCreateType(M->getContext().getObjCInterfaceType(SClass), Unit);
572 llvm::DIType InhTag =
573 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Chris Lattner9e55b8a2009-05-05 05:05:36 +0000574 Unit, "", llvm::DICompileUnit(), 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000575 0 /* offset */, 0, SClassTy);
576 EltTys.push_back(InhTag);
577 }
578
Devang Patel9ca36b62009-02-26 21:10:26 +0000579 const ASTRecordLayout &RL = M->getContext().getASTObjCInterfaceLayout(Decl);
580
581 unsigned FieldNo = 0;
582 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
583 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
584 ObjCIvarDecl *Field = *I;
585 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
586
587 std::string FieldName = Field->getNameAsString();
588
Devang Patelde135022009-04-27 22:40:36 +0000589 // Ignore unnamed fields.
590 if (FieldName.empty())
591 continue;
592
Devang Patel9ca36b62009-02-26 21:10:26 +0000593 // Get the location for the field.
594 SourceLocation FieldDefLoc = Field->getLocation();
595 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000596 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
597 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
598
Devang Patel99c20eb2009-03-20 18:24:39 +0000599
600 QualType FType = Field->getType();
601 uint64_t FieldSize = 0;
602 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000603
Devang Patel99c20eb2009-03-20 18:24:39 +0000604 if (!FType->isIncompleteArrayType()) {
605
606 // Bit size, align and offset of the type.
607 FieldSize = M->getContext().getTypeSize(FType);
608 Expr *BitWidth = Field->getBitWidth();
609 if (BitWidth)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000610 FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue();
611
Devang Patel99c20eb2009-03-20 18:24:39 +0000612 FieldAlign = M->getContext().getTypeAlign(FType);
613 }
614
615 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
616
Devang Patelc20482b2009-03-19 00:23:53 +0000617 unsigned Flags = 0;
618 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
619 Flags = llvm::DIType::FlagProtected;
620 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
621 Flags = llvm::DIType::FlagPrivate;
622
Devang Patel9ca36b62009-02-26 21:10:26 +0000623 // Create a DW_TAG_member node to remember the offset of this field in the
624 // struct. FIXME: This is an absolutely insane way to capture this
625 // information. When we gut debug info, this should be fixed.
626 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
627 FieldName, FieldDefUnit,
628 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000629 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000630 EltTys.push_back(FieldTy);
631 }
632
633 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000634 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +0000635
636 // Bit size, align and offset of the type.
637 uint64_t Size = M->getContext().getTypeSize(Ty);
638 uint64_t Align = M->getContext().getTypeAlign(Ty);
639
640 llvm::DIType RealDecl =
641 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000642 Align, 0, 0, llvm::DIType(), Elements,
643 RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000644
645 // Now that we have a real decl for the struct, replace anything using the
646 // old decl with the new one. This will recursively update the debug info.
647 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
648 FwdDecl.getGV()->eraseFromParent();
649
650 return RealDecl;
651}
652
Chris Lattner9c85ba32008-11-10 06:08:34 +0000653llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
654 llvm::DICompileUnit Unit) {
655 EnumDecl *Decl = Ty->getDecl();
656
657 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
658
659 // Create DIEnumerator elements for each enumerator.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000660 for (EnumDecl::enumerator_iterator
661 Enum = Decl->enumerator_begin(M->getContext()),
662 EnumEnd = Decl->enumerator_end(M->getContext());
Douglas Gregor44b43212008-12-11 16:49:14 +0000663 Enum != EnumEnd; ++Enum) {
664 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(),
665 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000666 }
667
668 // Return a CompositeType for the enum itself.
669 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000670 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000671
Chris Lattner8ec03f52008-11-24 03:54:41 +0000672 std::string EnumName = Decl->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000673 SourceLocation DefLoc = Decl->getLocation();
674 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
675 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000676 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
677 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
678
Chris Lattner9c85ba32008-11-10 06:08:34 +0000679
680 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +0000681 uint64_t Size = 0;
682 unsigned Align = 0;
683 if (!Ty->isIncompleteType()) {
684 Size = M->getContext().getTypeSize(Ty);
685 Align = M->getContext().getTypeAlign(Ty);
686 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000687
688 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
689 Unit, EnumName, DefUnit, Line,
690 Size, Align, 0, 0,
691 llvm::DIType(), EltArray);
692}
693
694llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
695 llvm::DICompileUnit Unit) {
696 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
697 return CreateType(RT, Unit);
698 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
699 return CreateType(ET, Unit);
700
701 return llvm::DIType();
702}
703
704llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
705 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000706 uint64_t Size;
707 uint64_t Align;
708
709
Nuno Lopes010d5142009-01-28 00:35:17 +0000710 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +0000711 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000712 Size = 0;
713 Align =
Nuno Lopes010d5142009-01-28 00:35:17 +0000714 M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT));
715 } else if (Ty->isIncompleteArrayType()) {
716 Size = 0;
717 Align = M->getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +0000718 } else {
719 // Size and align of the whole array, not the element type.
720 Size = M->getContext().getTypeSize(Ty);
721 Align = M->getContext().getTypeAlign(Ty);
722 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000723
724 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
725 // interior arrays, do we care? Why aren't nested arrays represented the
726 // obvious/recursive way?
727 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
728 QualType EltTy(Ty, 0);
729 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000730 uint64_t Upper = 0;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000731 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
732 Upper = CAT->getSize().getZExtValue() - 1;
733 // FIXME: Verify this is right for VLAs.
734 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
735 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000736 }
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000737
Chris Lattner9c85ba32008-11-10 06:08:34 +0000738 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000739 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000740
741 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
742 Unit, "", llvm::DICompileUnit(),
743 0, Size, Align, 0, 0,
744 getOrCreateType(EltTy, Unit),
745 SubscriptArray);
746}
747
748
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000749/// getOrCreateType - Get the type from the cache or create a new
750/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000751llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
752 llvm::DICompileUnit Unit) {
753 if (Ty.isNull())
754 return llvm::DIType();
755
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000756 // Check to see if the compile unit already has created this type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000757 llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()];
758 if (!Slot.isNull()) return Slot;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000759
Chris Lattner9c85ba32008-11-10 06:08:34 +0000760 // Handle CVR qualifiers, which recursively handles what they refer to.
761 if (Ty.getCVRQualifiers())
762 return Slot = CreateCVRType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000763
764 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000765 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000766#define TYPE(Class, Base)
767#define ABSTRACT_TYPE(Class, Base)
768#define NON_CANONICAL_TYPE(Class, Base)
769#define DEPENDENT_TYPE(Class, Base) case Type::Class:
770#include "clang/AST/TypeNodes.def"
771 assert(false && "Dependent types cannot show up in debug information");
772
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000773 case Type::LValueReference:
774 case Type::RValueReference:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000775 case Type::Vector:
776 case Type::ExtVector:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000777 case Type::ExtQual:
Eli Friedman00524e32009-02-27 23:15:07 +0000778 case Type::FixedWidthInt:
Eli Friedman00524e32009-02-27 23:15:07 +0000779 case Type::MemberPointer:
Douglas Gregor7532dc62009-03-30 22:58:21 +0000780 case Type::TemplateSpecialization:
Douglas Gregore4e5b052009-03-19 00:18:19 +0000781 case Type::QualifiedName:
Eli Friedman00524e32009-02-27 23:15:07 +0000782 // Unsupported types
Chris Lattner9c85ba32008-11-10 06:08:34 +0000783 return llvm::DIType();
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000784 case Type::ObjCObjectPointer: // Encode id<p> in debug info just like id.
Chris Lattneraa2b5792009-04-22 06:58:56 +0000785 return Slot = getOrCreateType(M->getContext().getObjCIdType(), Unit);
786
787 case Type::ObjCQualifiedInterface: // Drop protocols from interface.
Devang Patele7987062009-03-02 17:58:28 +0000788 case Type::ObjCInterface:
Chris Lattneraa2b5792009-04-22 06:58:56 +0000789 return Slot = CreateType(cast<ObjCInterfaceType>(Ty), Unit);
790 case Type::Builtin: return Slot = CreateType(cast<BuiltinType>(Ty), Unit);
Chris Lattnerb7003772009-04-23 06:13:01 +0000791 case Type::Complex: return Slot = CreateType(cast<ComplexType>(Ty), Unit);
Chris Lattneraa2b5792009-04-22 06:58:56 +0000792 case Type::Pointer: return Slot = CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +0000793 case Type::BlockPointer:
794 return Slot = CreateType(cast<BlockPointerType>(Ty), Unit);
Chris Lattneraa2b5792009-04-22 06:58:56 +0000795 case Type::Typedef: return Slot = CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000796 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000797 case Type::Enum:
Chris Lattneraa2b5792009-04-22 06:58:56 +0000798 return Slot = CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000799 case Type::FunctionProto:
800 case Type::FunctionNoProto:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000801 return Slot = CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000802
803 case Type::ConstantArray:
804 case Type::VariableArray:
805 case Type::IncompleteArray:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000806 return Slot = CreateType(cast<ArrayType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000807 case Type::TypeOfExpr:
808 return Slot = getOrCreateType(cast<TypeOfExprType>(Ty)->getUnderlyingExpr()
Chris Lattner3cc5c402008-11-11 07:01:36 +0000809 ->getType(), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000810 case Type::TypeOf:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000811 return Slot = getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(),
812 Unit);
Anders Carlsson395b4752009-06-24 19:06:50 +0000813 case Type::Decltype:
814 return Slot = getOrCreateType(cast<DecltypeType>(Ty)->getUnderlyingExpr()
815 ->getType(), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000816 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000817
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000818 return Slot;
819}
820
821/// EmitFunctionStart - Constructs the debug code for entering a function -
822/// "llvm.dbg.func.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000823void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000824 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000825 CGBuilderTy &Builder) {
Daniel Dunbarbbd53af2009-05-14 01:45:24 +0000826 const char *LinkageName = Name;
827
Daniel Dunbara2893932009-05-13 23:08:57 +0000828 // Skip the asm prefix if it exists.
Daniel Dunbarbbd53af2009-05-14 01:45:24 +0000829 //
830 // FIXME: This should probably be the unmangled name?
Daniel Dunbara2893932009-05-13 23:08:57 +0000831 if (Name[0] == '\01')
832 ++Name;
833
Chris Lattner9c85ba32008-11-10 06:08:34 +0000834 // FIXME: Why is this using CurLoc???
835 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000836 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +0000837 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000838
839 llvm::DISubprogram SP =
Daniel Dunbarbbd53af2009-05-14 01:45:24 +0000840 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000841 getOrCreateType(ReturnType, Unit),
842 Fn->hasInternalLinkage(), true/*definition*/);
843
844 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
845
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000846 // Push function on region stack.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000847 RegionStack.push_back(SP);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000848}
849
850
Chris Lattner9c85ba32008-11-10 06:08:34 +0000851void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000852 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
853
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000854 // Don't bother if things are the same as last time.
855 SourceManager &SM = M->getContext().getSourceManager();
856 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +0000857 || (SM.getInstantiationLineNumber(CurLoc) ==
858 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000859 && SM.isFromSameFile(CurLoc, PrevLoc)))
860 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000861
862 // Update last state.
863 PrevLoc = CurLoc;
864
865 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000866 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +0000867 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
868 DebugFactory.InsertStopPoint(Unit, PLoc.getLine(), PLoc.getColumn(),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000869 Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000870}
871
872/// EmitRegionStart- Constructs the debug code for entering a declarative
873/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000874void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
875 llvm::DIDescriptor D;
Daniel Dunbar5273f512008-10-17 01:07:56 +0000876 if (!RegionStack.empty())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000877 D = RegionStack.back();
878 D = DebugFactory.CreateBlock(D);
879 RegionStack.push_back(D);
880 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000881}
882
883/// EmitRegionEnd - Constructs the debug code for exiting a declarative
884/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +0000885void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000886 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
887
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000888 // Provide an region stop point.
889 EmitStopPoint(Fn, Builder);
890
Chris Lattner9c85ba32008-11-10 06:08:34 +0000891 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000892 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000893}
894
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000895/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000896void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
897 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000898 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
899
Devang Patel07739032009-03-27 23:16:32 +0000900 // Do not emit variable debug information while generating optimized code.
901 // The llvm optimizer and code generator are not yet ready to support
902 // optimized code debugging.
903 const CompileOptions &CO = M->getCompileOpts();
904 if (CO.OptimizationLevel)
905 return;
906
Chris Lattner650cea92009-05-05 04:57:08 +0000907 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
908 llvm::DIType Ty = getOrCreateType(Decl->getType(), Unit);
909
Chris Lattner9c85ba32008-11-10 06:08:34 +0000910 // Get location information.
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000911 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000912 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +0000913 unsigned Line = 0;
914 if (!PLoc.isInvalid())
915 Line = PLoc.getLine();
916 else
917 Unit = llvm::DICompileUnit();
918
Chris Lattner9c85ba32008-11-10 06:08:34 +0000919
920 // Create the descriptor for the variable.
921 llvm::DIVariable D =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000922 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner650cea92009-05-05 04:57:08 +0000923 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000924 // Insert an llvm.dbg.declare into the current block.
925 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000926}
927
Chris Lattner9c85ba32008-11-10 06:08:34 +0000928void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
929 llvm::Value *Storage,
930 CGBuilderTy &Builder) {
931 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
932}
933
934/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
935/// variable declaration.
936void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
937 CGBuilderTy &Builder) {
938 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
939}
940
941
942
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000943/// EmitGlobalVariable - Emit information about a global variable.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000944void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
945 const VarDecl *Decl) {
Devang Patel07739032009-03-27 23:16:32 +0000946
947 // Do not emit variable debug information while generating optimized code.
948 // The llvm optimizer and code generator are not yet ready to support
949 // optimized code debugging.
950 const CompileOptions &CO = M->getCompileOpts();
951 if (CO.OptimizationLevel)
952 return;
953
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000954 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000955 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000956 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000957 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
958 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000959
960 std::string Name = Decl->getNameAsString();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000961
962 QualType T = Decl->getType();
963 if (T->isIncompleteArrayType()) {
964
965 // CodeGen turns int[] into int[1] so we'll do the same here.
966 llvm::APSInt ConstVal(32);
967
968 ConstVal = 1;
969 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
970
971 T = M->getContext().getConstantArrayType(ET, ConstVal,
972 ArrayType::Normal, 0);
973 }
974
Chris Lattner9c85ba32008-11-10 06:08:34 +0000975 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000976 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000977 Var->hasInternalLinkage(),
978 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000979}
980
Devang Patel9ca36b62009-02-26 21:10:26 +0000981/// EmitGlobalVariable - Emit information about an objective-c interface.
982void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
983 ObjCInterfaceDecl *Decl) {
984 // Create global variable debug descriptor.
985 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
986 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000987 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
988 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +0000989
990 std::string Name = Decl->getNameAsString();
991
Chris Lattner03d9f342009-04-01 06:23:52 +0000992 QualType T = M->getContext().getObjCInterfaceType(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000993 if (T->isIncompleteArrayType()) {
994
995 // CodeGen turns int[] into int[1] so we'll do the same here.
996 llvm::APSInt ConstVal(32);
997
998 ConstVal = 1;
999 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
1000
1001 T = M->getContext().getConstantArrayType(ET, ConstVal,
1002 ArrayType::Normal, 0);
1003 }
1004
1005 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
1006 getOrCreateType(T, Unit),
1007 Var->hasInternalLinkage(),
1008 true/*definition*/, Var);
1009}
1010