blob: 5e78c5817d71f04324f3333b1dfbed495625f280 [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"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000022#include "llvm/Constants.h"
23#include "llvm/DerivedTypes.h"
24#include "llvm/Instructions.h"
25#include "llvm/Intrinsics.h"
26#include "llvm/Module.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000027#include "llvm/ADT/StringExtras.h"
28#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000029#include "llvm/Support/Dwarf.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000030#include "llvm/Target/TargetMachine.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000031using namespace clang;
32using namespace clang::CodeGen;
33
34CGDebugInfo::CGDebugInfo(CodeGenModule *m)
Chris Lattner9c85ba32008-11-10 06:08:34 +000035 : M(m), DebugFactory(M->getModule()) {
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000036}
37
Chris Lattner9c85ba32008-11-10 06:08:34 +000038CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar66031a52008-10-17 16:15:48 +000039 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000040}
41
Chris Lattner9c85ba32008-11-10 06:08:34 +000042void CGDebugInfo::setLocation(SourceLocation Loc) {
43 if (Loc.isValid())
Chris Lattnerf7cf85b2009-01-16 07:36:28 +000044 CurLoc = M->getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000045}
46
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000047/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
Daniel Dunbar25f51dd2008-10-24 08:38:36 +000048/// one if necessary. This returns null for invalid source locations.
Chris Lattner9c85ba32008-11-10 06:08:34 +000049llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) {
Daniel Dunbar831570c2009-01-22 00:09:25 +000050 // FIXME: Until we do a complete job of emitting debug information,
51 // we need to support making dummy compile units so that we generate
52 // "well formed" debug info.
53 const FileEntry *FE = 0;
Daniel Dunbar25f51dd2008-10-24 08:38:36 +000054
Devang Patel77820222009-02-24 23:16:03 +000055 SourceManager &SM = M->getContext().getSourceManager();
Daniel Dunbar831570c2009-01-22 00:09:25 +000056 if (Loc.isValid()) {
Daniel Dunbar831570c2009-01-22 00:09:25 +000057 Loc = SM.getInstantiationLoc(Loc);
58 FE = SM.getFileEntryForID(SM.getFileID(Loc));
Devang Patel77820222009-02-24 23:16:03 +000059 } else {
60 // If Loc is not valid then use main file id.
61 FE = SM.getFileEntryForID(SM.getMainFileID());
Daniel Dunbar831570c2009-01-22 00:09:25 +000062 }
63
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000064 // See if this compile unit has been used before.
Chris Lattner9c85ba32008-11-10 06:08:34 +000065 llvm::DICompileUnit &Unit = CompileUnitCache[FE];
66 if (!Unit.isNull()) return Unit;
67
68 // Get source file information.
Daniel Dunbar831570c2009-01-22 00:09:25 +000069 const char *FileName = FE ? FE->getName() : "<unknown>";
Daniel Dunbarf8e58d02009-02-07 00:40:41 +000070 const char *DirName = FE ? FE->getDir()->getName() : "<unknown>";
Daniel Dunbar2104bf92008-10-24 00:46:51 +000071
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000072 // Create new compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +000073 // FIXME: Handle other language IDs as well.
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000074 // FIXME: Do not know how to get clang version yet.
Chris Lattner9c85ba32008-11-10 06:08:34 +000075 return Unit = DebugFactory.CreateCompileUnit(llvm::dwarf::DW_LANG_C89,
76 FileName, DirName, "clang");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000077}
78
Devang Patel65e99f22009-02-25 01:36:11 +000079/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +000080/// one if necessary.
81llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel65e99f22009-02-25 01:36:11 +000082 llvm::DICompileUnit Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +000083 unsigned Encoding = 0;
84 switch (BT->getKind()) {
85 default:
86 case BuiltinType::Void:
87 return llvm::DIType();
88 case BuiltinType::UChar:
89 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
90 case BuiltinType::Char_S:
91 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
92 case BuiltinType::UShort:
93 case BuiltinType::UInt:
94 case BuiltinType::ULong:
95 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
96 case BuiltinType::Short:
97 case BuiltinType::Int:
98 case BuiltinType::Long:
99 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
100 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
101 case BuiltinType::Float:
102 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
103 }
104 // Bit size, align and offset of the type.
105 uint64_t Size = M->getContext().getTypeSize(BT);
106 uint64_t Align = M->getContext().getTypeAlign(BT);
107 uint64_t Offset = 0;
108
109 return DebugFactory.CreateBasicType(Unit, BT->getName(), Unit, 0, Size, Align,
110 Offset, /*flags*/ 0, Encoding);
111}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000112
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000113/// getOrCreateCVRType - Get the CVR qualified type from the cache or create
114/// a new one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000115llvm::DIType CGDebugInfo::CreateCVRType(QualType Ty, llvm::DICompileUnit Unit) {
116 // We will create one Derived type for one qualifier and recurse to handle any
117 // additional ones.
118 llvm::DIType FromTy;
119 unsigned Tag;
120 if (Ty.isConstQualified()) {
121 Tag = llvm::dwarf::DW_TAG_const_type;
122 Ty.removeConst();
123 FromTy = getOrCreateType(Ty, Unit);
124 } else if (Ty.isVolatileQualified()) {
125 Tag = llvm::dwarf::DW_TAG_volatile_type;
126 Ty.removeVolatile();
127 FromTy = getOrCreateType(Ty, Unit);
128 } else {
129 assert(Ty.isRestrictQualified() && "Unknown type qualifier for debug info");
130 Tag = llvm::dwarf::DW_TAG_restrict_type;
131 Ty.removeRestrict();
132 FromTy = getOrCreateType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000133 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000134
Daniel Dunbar3845f862008-10-31 03:54:29 +0000135 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
136 // CVR derived types.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000137 return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
138 0, 0, 0, 0, 0, FromTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000139}
140
Chris Lattner9c85ba32008-11-10 06:08:34 +0000141llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
142 llvm::DICompileUnit Unit) {
143 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000144
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000145 // Bit size, align and offset of the type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000146 uint64_t Size = M->getContext().getTypeSize(Ty);
147 uint64_t Align = M->getContext().getTypeAlign(Ty);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000148
Chris Lattner9c85ba32008-11-10 06:08:34 +0000149 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
150 "", llvm::DICompileUnit(),
151 0, Size, Align, 0, 0, EltTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000152}
153
Chris Lattner9c85ba32008-11-10 06:08:34 +0000154llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
155 llvm::DICompileUnit Unit) {
156 // Typedefs are derived from some other type. If we have a typedef of a
157 // typedef, make sure to emit the whole chain.
158 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
159
160 // We don't set size information, but do specify where the typedef was
161 // declared.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000162 std::string TyName = Ty->getDecl()->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000163 SourceLocation DefLoc = Ty->getDecl()->getLocation();
164 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000165
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000166 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000167 uint64_t Line = SM.getInstantiationLineNumber(DefLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000168
Chris Lattner9c85ba32008-11-10 06:08:34 +0000169 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
170 TyName, DefUnit, Line, 0, 0, 0, 0, Src);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000171}
172
Chris Lattner9c85ba32008-11-10 06:08:34 +0000173llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
174 llvm::DICompileUnit Unit) {
175 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000176
Chris Lattner9c85ba32008-11-10 06:08:34 +0000177 // Add the result type at least.
178 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
179
180 // Set up remainder of arguments if there is a prototype.
181 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
182 if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(Ty)) {
183 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
184 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
185 } else {
186 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000187 }
188
Chris Lattner9c85ba32008-11-10 06:08:34 +0000189 llvm::DIArray EltTypeArray =
190 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
191
192 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
193 Unit, "", llvm::DICompileUnit(),
194 0, 0, 0, 0, 0,
195 llvm::DIType(), EltTypeArray);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000196}
197
Devang Patel65e99f22009-02-25 01:36:11 +0000198/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000199llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
200 llvm::DICompileUnit Unit) {
Douglas Gregora4c46df2008-12-11 17:59:21 +0000201 RecordDecl *Decl = Ty->getDecl();
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000202
Chris Lattner9c85ba32008-11-10 06:08:34 +0000203 unsigned Tag;
204 if (Decl->isStruct())
205 Tag = llvm::dwarf::DW_TAG_structure_type;
206 else if (Decl->isUnion())
207 Tag = llvm::dwarf::DW_TAG_union_type;
208 else {
209 assert(Decl->isClass() && "Unknown RecordType!");
210 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000211 }
212
Sanjiv Gupta507de852008-06-09 10:47:41 +0000213 SourceManager &SM = M->getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000214
Chris Lattner9c85ba32008-11-10 06:08:34 +0000215 // Get overall information about the record type for the debug info.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000216 std::string Name = Decl->getNameAsString();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000217
Chris Lattner9c85ba32008-11-10 06:08:34 +0000218 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000219 unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000220
221
222 // Records and classes and unions can all be recursive. To handle them, we
223 // first generate a debug descriptor for the struct as a forward declaration.
224 // Then (if it is a definition) we go through and get debug info for all of
225 // its members. Finally, we create a descriptor for the complete type (which
226 // may refer to the forward decl if the struct is recursive) and replace all
227 // uses of the forward declaration with the final definition.
228 llvm::DIType FwdDecl =
229 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
230 llvm::DIType(), llvm::DIArray());
231
232 // If this is just a forward declaration, return it.
233 if (!Decl->getDefinition(M->getContext()))
234 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000235
Chris Lattner9c85ba32008-11-10 06:08:34 +0000236 // Otherwise, insert it into the TypeCache so that recursive uses will find
237 // it.
238 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
239
240 // Convert all the elements.
241 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
242
243 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl);
244
245 unsigned FieldNo = 0;
Douglas Gregora4c46df2008-12-11 17:59:21 +0000246 for (RecordDecl::field_iterator I = Decl->field_begin(),
247 E = Decl->field_end();
248 I != E; ++I, ++FieldNo) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000249 FieldDecl *Field = *I;
250 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattner8ec03f52008-11-24 03:54:41 +0000251
252 std::string FieldName = Field->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000253
254 // Get the location for the field.
255 SourceLocation FieldDefLoc = Field->getLocation();
256 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000257 unsigned FieldLine = SM.getInstantiationLineNumber(FieldDefLoc);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000258
259 // Bit size, align and offset of the type.
260 uint64_t FieldSize = M->getContext().getTypeSize(Ty);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000261 unsigned FieldAlign = M->getContext().getTypeAlign(Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000262 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
263
264 // Create a DW_TAG_member node to remember the offset of this field in the
265 // struct. FIXME: This is an absolutely insane way to capture this
266 // information. When we gut debug info, this should be fixed.
267 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
268 FieldName, FieldDefUnit,
269 FieldLine, FieldSize, FieldAlign,
270 FieldOffset, 0, FieldTy);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000271 EltTys.push_back(FieldTy);
272 }
273
274 llvm::DIArray Elements =
275 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
276
277 // Bit size, align and offset of the type.
278 uint64_t Size = M->getContext().getTypeSize(Ty);
279 uint64_t Align = M->getContext().getTypeAlign(Ty);
280
281 llvm::DIType RealDecl =
282 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
283 Align, 0, 0, llvm::DIType(), Elements);
284
285 // Now that we have a real decl for the struct, replace anything using the
286 // old decl with the new one. This will recursively update the debug info.
287 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
288 FwdDecl.getGV()->eraseFromParent();
289
290 return RealDecl;
291}
292
Devang Patel9ca36b62009-02-26 21:10:26 +0000293/// CreateType - get objective-c interface type.
294llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
295 llvm::DICompileUnit Unit) {
296 ObjCInterfaceDecl *Decl = Ty->getDecl();
297
298 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
299 SourceManager &SM = M->getContext().getSourceManager();
300
301 // Get overall information about the record type for the debug info.
302 std::string Name = Decl->getNameAsString();
303
304 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
305 unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation());
306
307
308 // To handle recursive interface, we
309 // first generate a debug descriptor for the struct as a forward declaration.
310 // Then (if it is a definition) we go through and get debug info for all of
311 // its members. Finally, we create a descriptor for the complete type (which
312 // may refer to the forward decl if the struct is recursive) and replace all
313 // uses of the forward declaration with the final definition.
314 llvm::DIType FwdDecl =
315 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
316 llvm::DIType(), llvm::DIArray());
317
318 // If this is just a forward declaration, return it.
319 if (Decl->isForwardDecl())
320 return FwdDecl;
321
322 // Otherwise, insert it into the TypeCache so that recursive uses will find
323 // it.
324 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
325
326 // Convert all the elements.
327 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
328
329 const ASTRecordLayout &RL = M->getContext().getASTObjCInterfaceLayout(Decl);
330
331 unsigned FieldNo = 0;
332 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
333 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
334 ObjCIvarDecl *Field = *I;
335 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
336
337 std::string FieldName = Field->getNameAsString();
338
339 // Get the location for the field.
340 SourceLocation FieldDefLoc = Field->getLocation();
341 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
342 unsigned FieldLine = SM.getInstantiationLineNumber(FieldDefLoc);
343
344 // Bit size, align and offset of the type.
345 uint64_t FieldSize = M->getContext().getTypeSize(Ty);
346 unsigned FieldAlign = M->getContext().getTypeAlign(Ty);
347 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
348
349 // Create a DW_TAG_member node to remember the offset of this field in the
350 // struct. FIXME: This is an absolutely insane way to capture this
351 // information. When we gut debug info, this should be fixed.
352 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
353 FieldName, FieldDefUnit,
354 FieldLine, FieldSize, FieldAlign,
355 FieldOffset, 0, FieldTy);
356 EltTys.push_back(FieldTy);
357 }
358
359 llvm::DIArray Elements =
360 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
361
362 // Bit size, align and offset of the type.
363 uint64_t Size = M->getContext().getTypeSize(Ty);
364 uint64_t Align = M->getContext().getTypeAlign(Ty);
365
366 llvm::DIType RealDecl =
367 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
368 Align, 0, 0, llvm::DIType(), Elements);
369
370 // Now that we have a real decl for the struct, replace anything using the
371 // old decl with the new one. This will recursively update the debug info.
372 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
373 FwdDecl.getGV()->eraseFromParent();
374
375 return RealDecl;
376}
377
Chris Lattner9c85ba32008-11-10 06:08:34 +0000378llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
379 llvm::DICompileUnit Unit) {
380 EnumDecl *Decl = Ty->getDecl();
381
382 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
383
384 // Create DIEnumerator elements for each enumerator.
Douglas Gregor44b43212008-12-11 16:49:14 +0000385 for (EnumDecl::enumerator_iterator Enum = Decl->enumerator_begin(),
386 EnumEnd = Decl->enumerator_end();
387 Enum != EnumEnd; ++Enum) {
388 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(),
389 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000390 }
391
392 // Return a CompositeType for the enum itself.
393 llvm::DIArray EltArray =
394 DebugFactory.GetOrCreateArray(&Enumerators[0], Enumerators.size());
395
Chris Lattner8ec03f52008-11-24 03:54:41 +0000396 std::string EnumName = Decl->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000397 SourceLocation DefLoc = Decl->getLocation();
398 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
399 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000400 unsigned Line = SM.getInstantiationLineNumber(DefLoc);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000401
402 // Size and align of the type.
403 uint64_t Size = M->getContext().getTypeSize(Ty);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000404 unsigned Align = M->getContext().getTypeAlign(Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000405
406 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
407 Unit, EnumName, DefUnit, Line,
408 Size, Align, 0, 0,
409 llvm::DIType(), EltArray);
410}
411
412llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
413 llvm::DICompileUnit Unit) {
414 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
415 return CreateType(RT, Unit);
416 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
417 return CreateType(ET, Unit);
418
419 return llvm::DIType();
420}
421
422llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
423 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000424 uint64_t Size;
425 uint64_t Align;
426
427
Nuno Lopes010d5142009-01-28 00:35:17 +0000428 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +0000429 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000430 Size = 0;
431 Align =
Nuno Lopes010d5142009-01-28 00:35:17 +0000432 M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT));
433 } else if (Ty->isIncompleteArrayType()) {
434 Size = 0;
435 Align = M->getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +0000436 } else {
437 // Size and align of the whole array, not the element type.
438 Size = M->getContext().getTypeSize(Ty);
439 Align = M->getContext().getTypeAlign(Ty);
440 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000441
442 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
443 // interior arrays, do we care? Why aren't nested arrays represented the
444 // obvious/recursive way?
445 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
446 QualType EltTy(Ty, 0);
447 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000448 uint64_t Upper = 0;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000449 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
450 Upper = CAT->getSize().getZExtValue() - 1;
451 // FIXME: Verify this is right for VLAs.
452 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
453 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000454 }
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000455
Chris Lattner9c85ba32008-11-10 06:08:34 +0000456 llvm::DIArray SubscriptArray =
457 DebugFactory.GetOrCreateArray(&Subscripts[0], Subscripts.size());
458
459 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
460 Unit, "", llvm::DICompileUnit(),
461 0, Size, Align, 0, 0,
462 getOrCreateType(EltTy, Unit),
463 SubscriptArray);
464}
465
466
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000467/// getOrCreateType - Get the type from the cache or create a new
468/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000469llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
470 llvm::DICompileUnit Unit) {
471 if (Ty.isNull())
472 return llvm::DIType();
473
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000474 // Check to see if the compile unit already has created this type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000475 llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()];
476 if (!Slot.isNull()) return Slot;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000477
Chris Lattner9c85ba32008-11-10 06:08:34 +0000478 // Handle CVR qualifiers, which recursively handles what they refer to.
479 if (Ty.getCVRQualifiers())
480 return Slot = CreateCVRType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000481
482 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000483 switch (Ty->getTypeClass()) {
484 case Type::Complex:
485 case Type::Reference:
486 case Type::Vector:
487 case Type::ExtVector:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000488 case Type::ExtQual:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000489 case Type::ObjCInterface:
490 case Type::ObjCQualifiedInterface:
491 case Type::ObjCQualifiedId:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000492 default:
493 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000494
Chris Lattner9c85ba32008-11-10 06:08:34 +0000495 case Type::Builtin: Slot = CreateType(cast<BuiltinType>(Ty), Unit); break;
496 case Type::Pointer: Slot = CreateType(cast<PointerType>(Ty), Unit); break;
497 case Type::TypeName: Slot = CreateType(cast<TypedefType>(Ty), Unit); break;
498 case Type::Tagged: Slot = CreateType(cast<TagType>(Ty), Unit); break;
499 case Type::FunctionProto:
500 case Type::FunctionNoProto:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000501 return Slot = CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000502
503 case Type::ConstantArray:
504 case Type::VariableArray:
505 case Type::IncompleteArray:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000506 return Slot = CreateType(cast<ArrayType>(Ty), Unit);
507 case Type::TypeOfExp:
508 return Slot = getOrCreateType(cast<TypeOfExpr>(Ty)->getUnderlyingExpr()
509 ->getType(), Unit);
510 case Type::TypeOfTyp:
511 return Slot = getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(),
512 Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000513 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000514
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000515 return Slot;
516}
517
518/// EmitFunctionStart - Constructs the debug code for entering a function -
519/// "llvm.dbg.func.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000520void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000521 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000522 CGBuilderTy &Builder) {
523 // FIXME: Why is this using CurLoc???
524 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000525 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000526 unsigned LineNo = SM.getInstantiationLineNumber(CurLoc);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000527
528 llvm::DISubprogram SP =
529 DebugFactory.CreateSubprogram(Unit, Name, Name, "", Unit, LineNo,
530 getOrCreateType(ReturnType, Unit),
531 Fn->hasInternalLinkage(), true/*definition*/);
532
533 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
534
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000535 // Push function on region stack.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000536 RegionStack.push_back(SP);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000537}
538
539
Chris Lattner9c85ba32008-11-10 06:08:34 +0000540void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000541 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
542
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000543 // Don't bother if things are the same as last time.
544 SourceManager &SM = M->getContext().getSourceManager();
545 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +0000546 || (SM.getInstantiationLineNumber(CurLoc) ==
547 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000548 && SM.isFromSameFile(CurLoc, PrevLoc)))
549 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000550
551 // Update last state.
552 PrevLoc = CurLoc;
553
554 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000555 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000556 DebugFactory.InsertStopPoint(Unit, SM.getInstantiationLineNumber(CurLoc),
557 SM.getInstantiationColumnNumber(CurLoc),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000558 Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000559}
560
561/// EmitRegionStart- Constructs the debug code for entering a declarative
562/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000563void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
564 llvm::DIDescriptor D;
Daniel Dunbar5273f512008-10-17 01:07:56 +0000565 if (!RegionStack.empty())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000566 D = RegionStack.back();
567 D = DebugFactory.CreateBlock(D);
568 RegionStack.push_back(D);
569 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000570}
571
572/// EmitRegionEnd - Constructs the debug code for exiting a declarative
573/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +0000574void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000575 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
576
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000577 // Provide an region stop point.
578 EmitStopPoint(Fn, Builder);
579
Chris Lattner9c85ba32008-11-10 06:08:34 +0000580 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000581 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000582}
583
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000584/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000585void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
586 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000587 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
588
Chris Lattner9c85ba32008-11-10 06:08:34 +0000589 // Get location information.
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000590 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000591 unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000592 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
593
594 // Create the descriptor for the variable.
595 llvm::DIVariable D =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000596 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000597 Unit, Line,
598 getOrCreateType(Decl->getType(), Unit));
599 // Insert an llvm.dbg.declare into the current block.
600 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000601}
602
Chris Lattner9c85ba32008-11-10 06:08:34 +0000603void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
604 llvm::Value *Storage,
605 CGBuilderTy &Builder) {
606 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
607}
608
609/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
610/// variable declaration.
611void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
612 CGBuilderTy &Builder) {
613 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
614}
615
616
617
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000618/// EmitGlobalVariable - Emit information about a global variable.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000619void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
620 const VarDecl *Decl) {
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000621 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000622 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000623 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000624 unsigned LineNo = SM.getInstantiationLineNumber(Decl->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +0000625
626 std::string Name = Decl->getNameAsString();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000627
628 QualType T = Decl->getType();
629 if (T->isIncompleteArrayType()) {
630
631 // CodeGen turns int[] into int[1] so we'll do the same here.
632 llvm::APSInt ConstVal(32);
633
634 ConstVal = 1;
635 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
636
637 T = M->getContext().getConstantArrayType(ET, ConstVal,
638 ArrayType::Normal, 0);
639 }
640
Chris Lattner9c85ba32008-11-10 06:08:34 +0000641 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000642 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000643 Var->hasInternalLinkage(),
644 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000645}
646
Devang Patel9ca36b62009-02-26 21:10:26 +0000647/// EmitGlobalVariable - Emit information about an objective-c interface.
648void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
649 ObjCInterfaceDecl *Decl) {
650 // Create global variable debug descriptor.
651 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
652 SourceManager &SM = M->getContext().getSourceManager();
653 unsigned LineNo = SM.getInstantiationLineNumber(Decl->getLocation());
654
655 std::string Name = Decl->getNameAsString();
656
657 QualType T = M->getContext().buildObjCInterfaceType(Decl);
658 if (T->isIncompleteArrayType()) {
659
660 // CodeGen turns int[] into int[1] so we'll do the same here.
661 llvm::APSInt ConstVal(32);
662
663 ConstVal = 1;
664 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
665
666 T = M->getContext().getConstantArrayType(ET, ConstVal,
667 ArrayType::Normal, 0);
668 }
669
670 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
671 getOrCreateType(T, Unit),
672 Var->hasInternalLinkage(),
673 true/*definition*/, Var);
674}
675