blob: dcf34bbd2e989d6613f3dc3d27b40a8e2f712f0e [file] [log] [blame]
Sanjiv Gupta40e56a12008-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 Gupta93eb8252008-05-25 05:15:42 +000016#include "clang/AST/ASTContext.h"
Devang Patel3fc13e32009-02-26 21:10:26 +000017#include "clang/AST/DeclObjC.h"
Chris Lattnere3afa462008-11-11 07:01:36 +000018#include "clang/AST/Expr.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000019#include "clang/AST/RecordLayout.h"
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000020#include "clang/Basic/SourceManager.h"
21#include "clang/Basic/FileManager.h"
Sanjiv Gupta40e56a12008-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 Gupta40e56a12008-05-08 08:54:20 +000027#include "llvm/ADT/StringExtras.h"
28#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000029#include "llvm/Support/Dwarf.h"
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000030#include "llvm/Target/TargetMachine.h"
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000031using namespace clang;
32using namespace clang::CodeGen;
33
34CGDebugInfo::CGDebugInfo(CodeGenModule *m)
Chris Lattner562ce0a2008-11-10 06:08:34 +000035 : M(m), DebugFactory(M->getModule()) {
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000036}
37
Chris Lattner562ce0a2008-11-10 06:08:34 +000038CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar6fc1f972008-10-17 16:15:48 +000039 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000040}
41
Chris Lattner562ce0a2008-11-10 06:08:34 +000042void CGDebugInfo::setLocation(SourceLocation Loc) {
43 if (Loc.isValid())
Chris Lattner18c8dc02009-01-16 07:36:28 +000044 CurLoc = M->getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000045}
46
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000047/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
Daniel Dunbar4a66ef12008-10-24 08:38:36 +000048/// one if necessary. This returns null for invalid source locations.
Chris Lattner562ce0a2008-11-10 06:08:34 +000049llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) {
Daniel Dunbar67bb1fa2009-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 Dunbar4a66ef12008-10-24 08:38:36 +000054
Devang Patel61910072009-02-24 23:16:03 +000055 SourceManager &SM = M->getContext().getSourceManager();
Daniel Dunbar67bb1fa2009-01-22 00:09:25 +000056 if (Loc.isValid()) {
Daniel Dunbar67bb1fa2009-01-22 00:09:25 +000057 Loc = SM.getInstantiationLoc(Loc);
58 FE = SM.getFileEntryForID(SM.getFileID(Loc));
Devang Patel61910072009-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 Dunbar67bb1fa2009-01-22 00:09:25 +000062 }
63
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000064 // See if this compile unit has been used before.
Chris Lattner562ce0a2008-11-10 06:08:34 +000065 llvm::DICompileUnit &Unit = CompileUnitCache[FE];
66 if (!Unit.isNull()) return Unit;
67
68 // Get source file information.
Daniel Dunbar67bb1fa2009-01-22 00:09:25 +000069 const char *FileName = FE ? FE->getName() : "<unknown>";
Daniel Dunbar2863cae2009-02-07 00:40:41 +000070 const char *DirName = FE ? FE->getDir()->getName() : "<unknown>";
Daniel Dunbar9865e6f2008-10-24 00:46:51 +000071
Devang Patel2b34d8f2009-03-05 01:55:07 +000072 bool isMain = (FE == SM.getFileEntryForID(SM.getMainFileID()));
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000073 // Create new compile unit.
Chris Lattner562ce0a2008-11-10 06:08:34 +000074 // FIXME: Handle other language IDs as well.
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000075 // FIXME: Do not know how to get clang version yet.
Chris Lattner562ce0a2008-11-10 06:08:34 +000076 return Unit = DebugFactory.CreateCompileUnit(llvm::dwarf::DW_LANG_C89,
Devang Patel2b34d8f2009-03-05 01:55:07 +000077 FileName, DirName, "clang",
78 isMain);
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000079}
80
Devang Patela4cc0c42009-02-25 01:36:11 +000081/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner562ce0a2008-11-10 06:08:34 +000082/// one if necessary.
83llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patela4cc0c42009-02-25 01:36:11 +000084 llvm::DICompileUnit Unit) {
Chris Lattner562ce0a2008-11-10 06:08:34 +000085 unsigned Encoding = 0;
86 switch (BT->getKind()) {
87 default:
88 case BuiltinType::Void:
89 return llvm::DIType();
90 case BuiltinType::UChar:
91 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
92 case BuiltinType::Char_S:
93 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
94 case BuiltinType::UShort:
95 case BuiltinType::UInt:
96 case BuiltinType::ULong:
97 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
98 case BuiltinType::Short:
99 case BuiltinType::Int:
100 case BuiltinType::Long:
101 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
102 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
103 case BuiltinType::Float:
104 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
105 }
106 // Bit size, align and offset of the type.
107 uint64_t Size = M->getContext().getTypeSize(BT);
108 uint64_t Align = M->getContext().getTypeAlign(BT);
109 uint64_t Offset = 0;
110
111 return DebugFactory.CreateBasicType(Unit, BT->getName(), Unit, 0, Size, Align,
112 Offset, /*flags*/ 0, Encoding);
113}
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000114
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000115/// getOrCreateCVRType - Get the CVR qualified type from the cache or create
116/// a new one if necessary.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000117llvm::DIType CGDebugInfo::CreateCVRType(QualType Ty, llvm::DICompileUnit Unit) {
118 // We will create one Derived type for one qualifier and recurse to handle any
119 // additional ones.
120 llvm::DIType FromTy;
121 unsigned Tag;
122 if (Ty.isConstQualified()) {
123 Tag = llvm::dwarf::DW_TAG_const_type;
124 Ty.removeConst();
125 FromTy = getOrCreateType(Ty, Unit);
126 } else if (Ty.isVolatileQualified()) {
127 Tag = llvm::dwarf::DW_TAG_volatile_type;
128 Ty.removeVolatile();
129 FromTy = getOrCreateType(Ty, Unit);
130 } else {
131 assert(Ty.isRestrictQualified() && "Unknown type qualifier for debug info");
132 Tag = llvm::dwarf::DW_TAG_restrict_type;
133 Ty.removeRestrict();
134 FromTy = getOrCreateType(Ty, Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000135 }
Chris Lattner562ce0a2008-11-10 06:08:34 +0000136
Daniel Dunbar44252b42008-10-31 03:54:29 +0000137 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
138 // CVR derived types.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000139 return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
140 0, 0, 0, 0, 0, FromTy);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000141}
142
Chris Lattner562ce0a2008-11-10 06:08:34 +0000143llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
144 llvm::DICompileUnit Unit) {
145 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000146
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000147 // Bit size, align and offset of the type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000148 uint64_t Size = M->getContext().getTypeSize(Ty);
149 uint64_t Align = M->getContext().getTypeAlign(Ty);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000150
Chris Lattner562ce0a2008-11-10 06:08:34 +0000151 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
152 "", llvm::DICompileUnit(),
153 0, Size, Align, 0, 0, EltTy);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000154}
155
Chris Lattner562ce0a2008-11-10 06:08:34 +0000156llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
157 llvm::DICompileUnit Unit) {
158 // Typedefs are derived from some other type. If we have a typedef of a
159 // typedef, make sure to emit the whole chain.
160 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
161
162 // We don't set size information, but do specify where the typedef was
163 // declared.
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000164 std::string TyName = Ty->getDecl()->getNameAsString();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000165 SourceLocation DefLoc = Ty->getDecl()->getLocation();
166 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000167
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000168 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner18c8dc02009-01-16 07:36:28 +0000169 uint64_t Line = SM.getInstantiationLineNumber(DefLoc);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000170
Chris Lattner562ce0a2008-11-10 06:08:34 +0000171 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
172 TyName, DefUnit, Line, 0, 0, 0, 0, Src);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000173}
174
Chris Lattner562ce0a2008-11-10 06:08:34 +0000175llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
176 llvm::DICompileUnit Unit) {
177 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000178
Chris Lattner562ce0a2008-11-10 06:08:34 +0000179 // Add the result type at least.
180 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
181
182 // Set up remainder of arguments if there is a prototype.
183 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor4fa58902009-02-26 23:50:07 +0000184 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner562ce0a2008-11-10 06:08:34 +0000185 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
186 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
187 } else {
188 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000189 }
190
Chris Lattner562ce0a2008-11-10 06:08:34 +0000191 llvm::DIArray EltTypeArray =
192 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
193
194 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
195 Unit, "", llvm::DICompileUnit(),
196 0, 0, 0, 0, 0,
197 llvm::DIType(), EltTypeArray);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000198}
199
Devang Patela4cc0c42009-02-25 01:36:11 +0000200/// CreateType - get structure or union type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000201llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
202 llvm::DICompileUnit Unit) {
Douglas Gregor640a04b2008-12-11 17:59:21 +0000203 RecordDecl *Decl = Ty->getDecl();
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000204
Chris Lattner562ce0a2008-11-10 06:08:34 +0000205 unsigned Tag;
206 if (Decl->isStruct())
207 Tag = llvm::dwarf::DW_TAG_structure_type;
208 else if (Decl->isUnion())
209 Tag = llvm::dwarf::DW_TAG_union_type;
210 else {
211 assert(Decl->isClass() && "Unknown RecordType!");
212 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000213 }
214
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000215 SourceManager &SM = M->getContext().getSourceManager();
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000216
Chris Lattner562ce0a2008-11-10 06:08:34 +0000217 // Get overall information about the record type for the debug info.
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000218 std::string Name = Decl->getNameAsString();
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000219
Chris Lattner562ce0a2008-11-10 06:08:34 +0000220 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Chris Lattner18c8dc02009-01-16 07:36:28 +0000221 unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation());
Chris Lattner562ce0a2008-11-10 06:08:34 +0000222
223
224 // Records and classes and unions can all be recursive. To handle them, we
225 // first generate a debug descriptor for the struct as a forward declaration.
226 // Then (if it is a definition) we go through and get debug info for all of
227 // its members. Finally, we create a descriptor for the complete type (which
228 // may refer to the forward decl if the struct is recursive) and replace all
229 // uses of the forward declaration with the final definition.
230 llvm::DIType FwdDecl =
231 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
232 llvm::DIType(), llvm::DIArray());
233
234 // If this is just a forward declaration, return it.
235 if (!Decl->getDefinition(M->getContext()))
236 return FwdDecl;
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000237
Chris Lattner562ce0a2008-11-10 06:08:34 +0000238 // Otherwise, insert it into the TypeCache so that recursive uses will find
239 // it.
240 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
241
242 // Convert all the elements.
243 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
244
245 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl);
246
247 unsigned FieldNo = 0;
Douglas Gregor640a04b2008-12-11 17:59:21 +0000248 for (RecordDecl::field_iterator I = Decl->field_begin(),
249 E = Decl->field_end();
250 I != E; ++I, ++FieldNo) {
Chris Lattner562ce0a2008-11-10 06:08:34 +0000251 FieldDecl *Field = *I;
252 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000253
254 std::string FieldName = Field->getNameAsString();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000255
256 // Get the location for the field.
257 SourceLocation FieldDefLoc = Field->getLocation();
258 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Chris Lattner18c8dc02009-01-16 07:36:28 +0000259 unsigned FieldLine = SM.getInstantiationLineNumber(FieldDefLoc);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000260
261 // Bit size, align and offset of the type.
262 uint64_t FieldSize = M->getContext().getTypeSize(Ty);
Chris Lattner18c8dc02009-01-16 07:36:28 +0000263 unsigned FieldAlign = M->getContext().getTypeAlign(Ty);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000264 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
265
266 // Create a DW_TAG_member node to remember the offset of this field in the
267 // struct. FIXME: This is an absolutely insane way to capture this
268 // information. When we gut debug info, this should be fixed.
269 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
270 FieldName, FieldDefUnit,
271 FieldLine, FieldSize, FieldAlign,
272 FieldOffset, 0, FieldTy);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000273 EltTys.push_back(FieldTy);
274 }
275
276 llvm::DIArray Elements =
277 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
278
279 // Bit size, align and offset of the type.
280 uint64_t Size = M->getContext().getTypeSize(Ty);
281 uint64_t Align = M->getContext().getTypeAlign(Ty);
282
283 llvm::DIType RealDecl =
284 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
285 Align, 0, 0, llvm::DIType(), Elements);
286
287 // Now that we have a real decl for the struct, replace anything using the
288 // old decl with the new one. This will recursively update the debug info.
289 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
290 FwdDecl.getGV()->eraseFromParent();
291
292 return RealDecl;
293}
294
Devang Patel3fc13e32009-02-26 21:10:26 +0000295/// CreateType - get objective-c interface type.
296llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
297 llvm::DICompileUnit Unit) {
298 ObjCInterfaceDecl *Decl = Ty->getDecl();
299
300 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
301 SourceManager &SM = M->getContext().getSourceManager();
302
303 // Get overall information about the record type for the debug info.
304 std::string Name = Decl->getNameAsString();
305
306 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
307 unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation());
308
309
310 // To handle recursive interface, we
311 // first generate a debug descriptor for the struct as a forward declaration.
312 // Then (if it is a definition) we go through and get debug info for all of
313 // its members. Finally, we create a descriptor for the complete type (which
314 // may refer to the forward decl if the struct is recursive) and replace all
315 // uses of the forward declaration with the final definition.
316 llvm::DIType FwdDecl =
317 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
318 llvm::DIType(), llvm::DIArray());
319
320 // If this is just a forward declaration, return it.
321 if (Decl->isForwardDecl())
322 return FwdDecl;
323
324 // Otherwise, insert it into the TypeCache so that recursive uses will find
325 // it.
326 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
327
328 // Convert all the elements.
329 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
330
331 const ASTRecordLayout &RL = M->getContext().getASTObjCInterfaceLayout(Decl);
332
333 unsigned FieldNo = 0;
334 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
335 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
336 ObjCIvarDecl *Field = *I;
337 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
338
339 std::string FieldName = Field->getNameAsString();
340
341 // Get the location for the field.
342 SourceLocation FieldDefLoc = Field->getLocation();
343 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
344 unsigned FieldLine = SM.getInstantiationLineNumber(FieldDefLoc);
345
346 // Bit size, align and offset of the type.
347 uint64_t FieldSize = M->getContext().getTypeSize(Ty);
348 unsigned FieldAlign = M->getContext().getTypeAlign(Ty);
349 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
350
351 // Create a DW_TAG_member node to remember the offset of this field in the
352 // struct. FIXME: This is an absolutely insane way to capture this
353 // information. When we gut debug info, this should be fixed.
354 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
355 FieldName, FieldDefUnit,
356 FieldLine, FieldSize, FieldAlign,
357 FieldOffset, 0, FieldTy);
358 EltTys.push_back(FieldTy);
359 }
360
361 llvm::DIArray Elements =
362 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
363
364 // Bit size, align and offset of the type.
365 uint64_t Size = M->getContext().getTypeSize(Ty);
366 uint64_t Align = M->getContext().getTypeAlign(Ty);
367
368 llvm::DIType RealDecl =
369 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
370 Align, 0, 0, llvm::DIType(), Elements);
371
372 // Now that we have a real decl for the struct, replace anything using the
373 // old decl with the new one. This will recursively update the debug info.
374 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
375 FwdDecl.getGV()->eraseFromParent();
376
377 return RealDecl;
378}
379
Chris Lattner562ce0a2008-11-10 06:08:34 +0000380llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
381 llvm::DICompileUnit Unit) {
382 EnumDecl *Decl = Ty->getDecl();
383
384 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
385
386 // Create DIEnumerator elements for each enumerator.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000387 for (EnumDecl::enumerator_iterator Enum = Decl->enumerator_begin(),
388 EnumEnd = Decl->enumerator_end();
389 Enum != EnumEnd; ++Enum) {
390 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(),
391 Enum->getInitVal().getZExtValue()));
Chris Lattner562ce0a2008-11-10 06:08:34 +0000392 }
393
394 // Return a CompositeType for the enum itself.
395 llvm::DIArray EltArray =
396 DebugFactory.GetOrCreateArray(&Enumerators[0], Enumerators.size());
397
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000398 std::string EnumName = Decl->getNameAsString();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000399 SourceLocation DefLoc = Decl->getLocation();
400 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
401 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner18c8dc02009-01-16 07:36:28 +0000402 unsigned Line = SM.getInstantiationLineNumber(DefLoc);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000403
404 // Size and align of the type.
405 uint64_t Size = M->getContext().getTypeSize(Ty);
Chris Lattner18c8dc02009-01-16 07:36:28 +0000406 unsigned Align = M->getContext().getTypeAlign(Ty);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000407
408 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
409 Unit, EnumName, DefUnit, Line,
410 Size, Align, 0, 0,
411 llvm::DIType(), EltArray);
412}
413
414llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
415 llvm::DICompileUnit Unit) {
416 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
417 return CreateType(RT, Unit);
418 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
419 return CreateType(ET, Unit);
420
421 return llvm::DIType();
422}
423
424llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
425 llvm::DICompileUnit Unit) {
Anders Carlsson86b360e2009-01-05 01:23:29 +0000426 uint64_t Size;
427 uint64_t Align;
428
429
Nuno Lopes15bc9c32009-01-28 00:35:17 +0000430 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson86b360e2009-01-05 01:23:29 +0000431 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson86b360e2009-01-05 01:23:29 +0000432 Size = 0;
433 Align =
Nuno Lopes15bc9c32009-01-28 00:35:17 +0000434 M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT));
435 } else if (Ty->isIncompleteArrayType()) {
436 Size = 0;
437 Align = M->getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson86b360e2009-01-05 01:23:29 +0000438 } else {
439 // Size and align of the whole array, not the element type.
440 Size = M->getContext().getTypeSize(Ty);
441 Align = M->getContext().getTypeAlign(Ty);
442 }
Chris Lattner562ce0a2008-11-10 06:08:34 +0000443
444 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
445 // interior arrays, do we care? Why aren't nested arrays represented the
446 // obvious/recursive way?
447 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
448 QualType EltTy(Ty, 0);
449 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000450 uint64_t Upper = 0;
Chris Lattner562ce0a2008-11-10 06:08:34 +0000451 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
452 Upper = CAT->getSize().getZExtValue() - 1;
453 // FIXME: Verify this is right for VLAs.
454 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
455 EltTy = Ty->getElementType();
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000456 }
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000457
Chris Lattner562ce0a2008-11-10 06:08:34 +0000458 llvm::DIArray SubscriptArray =
459 DebugFactory.GetOrCreateArray(&Subscripts[0], Subscripts.size());
460
461 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
462 Unit, "", llvm::DICompileUnit(),
463 0, Size, Align, 0, 0,
464 getOrCreateType(EltTy, Unit),
465 SubscriptArray);
466}
467
468
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000469/// getOrCreateType - Get the type from the cache or create a new
470/// one if necessary.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000471llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
472 llvm::DICompileUnit Unit) {
473 if (Ty.isNull())
474 return llvm::DIType();
475
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000476 // Check to see if the compile unit already has created this type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000477 llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()];
478 if (!Slot.isNull()) return Slot;
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000479
Chris Lattner562ce0a2008-11-10 06:08:34 +0000480 // Handle CVR qualifiers, which recursively handles what they refer to.
481 if (Ty.getCVRQualifiers())
482 return Slot = CreateCVRType(Ty, Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000483
484 // Work out details of type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000485 switch (Ty->getTypeClass()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +0000486#define TYPE(Class, Base)
487#define ABSTRACT_TYPE(Class, Base)
488#define NON_CANONICAL_TYPE(Class, Base)
489#define DEPENDENT_TYPE(Class, Base) case Type::Class:
490#include "clang/AST/TypeNodes.def"
491 assert(false && "Dependent types cannot show up in debug information");
492
Chris Lattner562ce0a2008-11-10 06:08:34 +0000493 case Type::Complex:
494 case Type::Reference:
495 case Type::Vector:
496 case Type::ExtVector:
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000497 case Type::ExtQual:
Chris Lattner562ce0a2008-11-10 06:08:34 +0000498 case Type::ObjCQualifiedInterface:
499 case Type::ObjCQualifiedId:
Eli Friedman673e91b2009-02-27 23:15:07 +0000500 case Type::FixedWidthInt:
501 case Type::BlockPointer:
502 case Type::MemberPointer:
503 case Type::ClassTemplateSpecialization:
504 case Type::ObjCQualifiedClass:
505 // Unsupported types
Chris Lattner562ce0a2008-11-10 06:08:34 +0000506 return llvm::DIType();
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000507
Devang Patel5a1ff3b2009-03-02 17:58:28 +0000508 case Type::ObjCInterface:
509 Slot = CreateType(cast<ObjCInterfaceType>(Ty), Unit); break;
Chris Lattner562ce0a2008-11-10 06:08:34 +0000510 case Type::Builtin: Slot = CreateType(cast<BuiltinType>(Ty), Unit); break;
511 case Type::Pointer: Slot = CreateType(cast<PointerType>(Ty), Unit); break;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000512 case Type::Typedef: Slot = CreateType(cast<TypedefType>(Ty), Unit); break;
513 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000514 case Type::Enum:
515 Slot = CreateType(cast<TagType>(Ty), Unit);
516 break;
Chris Lattner562ce0a2008-11-10 06:08:34 +0000517 case Type::FunctionProto:
518 case Type::FunctionNoProto:
Chris Lattnere3afa462008-11-11 07:01:36 +0000519 return Slot = CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000520
521 case Type::ConstantArray:
522 case Type::VariableArray:
523 case Type::IncompleteArray:
Chris Lattnere3afa462008-11-11 07:01:36 +0000524 return Slot = CreateType(cast<ArrayType>(Ty), Unit);
Douglas Gregor4fa58902009-02-26 23:50:07 +0000525 case Type::TypeOfExpr:
526 return Slot = getOrCreateType(cast<TypeOfExprType>(Ty)->getUnderlyingExpr()
Chris Lattnere3afa462008-11-11 07:01:36 +0000527 ->getType(), Unit);
Douglas Gregor4fa58902009-02-26 23:50:07 +0000528 case Type::TypeOf:
Chris Lattnere3afa462008-11-11 07:01:36 +0000529 return Slot = getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(),
530 Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000531 }
Chris Lattner562ce0a2008-11-10 06:08:34 +0000532
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000533 return Slot;
534}
535
536/// EmitFunctionStart - Constructs the debug code for entering a function -
537/// "llvm.dbg.func.start.".
Chris Lattner562ce0a2008-11-10 06:08:34 +0000538void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000539 llvm::Function *Fn,
Chris Lattner562ce0a2008-11-10 06:08:34 +0000540 CGBuilderTy &Builder) {
541 // FIXME: Why is this using CurLoc???
542 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000543 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner18c8dc02009-01-16 07:36:28 +0000544 unsigned LineNo = SM.getInstantiationLineNumber(CurLoc);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000545
546 llvm::DISubprogram SP =
547 DebugFactory.CreateSubprogram(Unit, Name, Name, "", Unit, LineNo,
548 getOrCreateType(ReturnType, Unit),
549 Fn->hasInternalLinkage(), true/*definition*/);
550
551 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
552
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000553 // Push function on region stack.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000554 RegionStack.push_back(SP);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000555}
556
557
Chris Lattner562ce0a2008-11-10 06:08:34 +0000558void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000559 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
560
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000561 // Don't bother if things are the same as last time.
562 SourceManager &SM = M->getContext().getSourceManager();
563 if (CurLoc == PrevLoc
Chris Lattner2d89c562009-02-04 01:06:56 +0000564 || (SM.getInstantiationLineNumber(CurLoc) ==
565 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000566 && SM.isFromSameFile(CurLoc, PrevLoc)))
567 return;
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000568
569 // Update last state.
570 PrevLoc = CurLoc;
571
572 // Get the appropriate compile unit.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000573 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Chris Lattner18c8dc02009-01-16 07:36:28 +0000574 DebugFactory.InsertStopPoint(Unit, SM.getInstantiationLineNumber(CurLoc),
575 SM.getInstantiationColumnNumber(CurLoc),
Chris Lattner562ce0a2008-11-10 06:08:34 +0000576 Builder.GetInsertBlock());
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000577}
578
579/// EmitRegionStart- Constructs the debug code for entering a declarative
580/// region - "llvm.dbg.region.start.".
Chris Lattner562ce0a2008-11-10 06:08:34 +0000581void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
582 llvm::DIDescriptor D;
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000583 if (!RegionStack.empty())
Chris Lattner562ce0a2008-11-10 06:08:34 +0000584 D = RegionStack.back();
585 D = DebugFactory.CreateBlock(D);
586 RegionStack.push_back(D);
587 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000588}
589
590/// EmitRegionEnd - Constructs the debug code for exiting a declarative
591/// region - "llvm.dbg.region.end."
Chris Lattner562ce0a2008-11-10 06:08:34 +0000592void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000593 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
594
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000595 // Provide an region stop point.
596 EmitStopPoint(Fn, Builder);
597
Chris Lattner562ce0a2008-11-10 06:08:34 +0000598 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000599 RegionStack.pop_back();
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000600}
601
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000602/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000603void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
604 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000605 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
606
Chris Lattner562ce0a2008-11-10 06:08:34 +0000607 // Get location information.
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000608 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner18c8dc02009-01-16 07:36:28 +0000609 unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation());
Chris Lattner562ce0a2008-11-10 06:08:34 +0000610 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
611
612 // Create the descriptor for the variable.
613 llvm::DIVariable D =
Chris Lattner271d4c22008-11-24 05:29:24 +0000614 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner562ce0a2008-11-10 06:08:34 +0000615 Unit, Line,
616 getOrCreateType(Decl->getType(), Unit));
617 // Insert an llvm.dbg.declare into the current block.
618 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000619}
620
Chris Lattner562ce0a2008-11-10 06:08:34 +0000621void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
622 llvm::Value *Storage,
623 CGBuilderTy &Builder) {
624 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
625}
626
627/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
628/// variable declaration.
629void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
630 CGBuilderTy &Builder) {
631 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
632}
633
634
635
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000636/// EmitGlobalVariable - Emit information about a global variable.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000637void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
638 const VarDecl *Decl) {
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000639 // Create global variable debug descriptor.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000640 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000641 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner18c8dc02009-01-16 07:36:28 +0000642 unsigned LineNo = SM.getInstantiationLineNumber(Decl->getLocation());
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000643
644 std::string Name = Decl->getNameAsString();
Anders Carlsson123485c2008-11-26 17:40:42 +0000645
646 QualType T = Decl->getType();
647 if (T->isIncompleteArrayType()) {
648
649 // CodeGen turns int[] into int[1] so we'll do the same here.
650 llvm::APSInt ConstVal(32);
651
652 ConstVal = 1;
653 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
654
655 T = M->getContext().getConstantArrayType(ET, ConstVal,
656 ArrayType::Normal, 0);
657 }
658
Chris Lattner562ce0a2008-11-10 06:08:34 +0000659 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Anders Carlsson123485c2008-11-26 17:40:42 +0000660 getOrCreateType(T, Unit),
Chris Lattner562ce0a2008-11-10 06:08:34 +0000661 Var->hasInternalLinkage(),
662 true/*definition*/, Var);
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000663}
664
Devang Patel3fc13e32009-02-26 21:10:26 +0000665/// EmitGlobalVariable - Emit information about an objective-c interface.
666void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
667 ObjCInterfaceDecl *Decl) {
668 // Create global variable debug descriptor.
669 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
670 SourceManager &SM = M->getContext().getSourceManager();
671 unsigned LineNo = SM.getInstantiationLineNumber(Decl->getLocation());
672
673 std::string Name = Decl->getNameAsString();
674
675 QualType T = M->getContext().buildObjCInterfaceType(Decl);
676 if (T->isIncompleteArrayType()) {
677
678 // CodeGen turns int[] into int[1] so we'll do the same here.
679 llvm::APSInt ConstVal(32);
680
681 ConstVal = 1;
682 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
683
684 T = M->getContext().getConstantArrayType(ET, ConstVal,
685 ArrayType::Normal, 0);
686 }
687
688 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
689 getOrCreateType(T, Unit),
690 Var->hasInternalLinkage(),
691 true/*definition*/, Var);
692}
693