blob: 444ee7c4005afef4fc7678792bcafd7561da901c [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"
Daniel Dunbare91593e2008-08-11 04:54:23 +000017#include "clang/AST/Decl.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())
44 CurLoc = M->getContext().getSourceManager().getLogicalLoc(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 Dunbar25f51dd2008-10-24 08:38:36 +000050 if (Loc.isInvalid())
Chris Lattner9c85ba32008-11-10 06:08:34 +000051 return llvm::DICompileUnit();
Daniel Dunbar25f51dd2008-10-24 08:38:36 +000052
Daniel Dunbar2104bf92008-10-24 00:46:51 +000053 SourceManager &SM = M->getContext().getSourceManager();
54 const FileEntry *FE = SM.getFileEntryForLoc(Loc);
Chris Lattner9c85ba32008-11-10 06:08:34 +000055 if (FE == 0) return llvm::DICompileUnit();
56
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000057 // See if this compile unit has been used before.
Chris Lattner9c85ba32008-11-10 06:08:34 +000058 llvm::DICompileUnit &Unit = CompileUnitCache[FE];
59 if (!Unit.isNull()) return Unit;
60
61 // Get source file information.
62 const char *FileName = FE->getName();
63 const char *DirName = FE->getDir()->getName();
Daniel Dunbar2104bf92008-10-24 00:46:51 +000064
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000065 // Create new compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +000066 // FIXME: Handle other language IDs as well.
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000067 // FIXME: Do not know how to get clang version yet.
Chris Lattner9c85ba32008-11-10 06:08:34 +000068 return Unit = DebugFactory.CreateCompileUnit(llvm::dwarf::DW_LANG_C89,
69 FileName, DirName, "clang");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000070}
71
Chris Lattner9c85ba32008-11-10 06:08:34 +000072/// getOrCreateBuiltinType - Get the Basic type from the cache or create a new
73/// one if necessary.
74llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
75 llvm::DICompileUnit Unit){
76 unsigned Encoding = 0;
77 switch (BT->getKind()) {
78 default:
79 case BuiltinType::Void:
80 return llvm::DIType();
81 case BuiltinType::UChar:
82 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
83 case BuiltinType::Char_S:
84 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
85 case BuiltinType::UShort:
86 case BuiltinType::UInt:
87 case BuiltinType::ULong:
88 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
89 case BuiltinType::Short:
90 case BuiltinType::Int:
91 case BuiltinType::Long:
92 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
93 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
94 case BuiltinType::Float:
95 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
96 }
97 // Bit size, align and offset of the type.
98 uint64_t Size = M->getContext().getTypeSize(BT);
99 uint64_t Align = M->getContext().getTypeAlign(BT);
100 uint64_t Offset = 0;
101
102 return DebugFactory.CreateBasicType(Unit, BT->getName(), Unit, 0, Size, Align,
103 Offset, /*flags*/ 0, Encoding);
104}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000105
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000106/// getOrCreateCVRType - Get the CVR qualified type from the cache or create
107/// a new one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000108llvm::DIType CGDebugInfo::CreateCVRType(QualType Ty, llvm::DICompileUnit Unit) {
109 // We will create one Derived type for one qualifier and recurse to handle any
110 // additional ones.
111 llvm::DIType FromTy;
112 unsigned Tag;
113 if (Ty.isConstQualified()) {
114 Tag = llvm::dwarf::DW_TAG_const_type;
115 Ty.removeConst();
116 FromTy = getOrCreateType(Ty, Unit);
117 } else if (Ty.isVolatileQualified()) {
118 Tag = llvm::dwarf::DW_TAG_volatile_type;
119 Ty.removeVolatile();
120 FromTy = getOrCreateType(Ty, Unit);
121 } else {
122 assert(Ty.isRestrictQualified() && "Unknown type qualifier for debug info");
123 Tag = llvm::dwarf::DW_TAG_restrict_type;
124 Ty.removeRestrict();
125 FromTy = getOrCreateType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000126 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000127
Daniel Dunbar3845f862008-10-31 03:54:29 +0000128 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
129 // CVR derived types.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000130 return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
131 0, 0, 0, 0, 0, FromTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000132}
133
Chris Lattner9c85ba32008-11-10 06:08:34 +0000134llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
135 llvm::DICompileUnit Unit) {
136 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000137
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000138 // Bit size, align and offset of the type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000139 uint64_t Size = M->getContext().getTypeSize(Ty);
140 uint64_t Align = M->getContext().getTypeAlign(Ty);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000141
Chris Lattner9c85ba32008-11-10 06:08:34 +0000142 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
143 "", llvm::DICompileUnit(),
144 0, Size, Align, 0, 0, EltTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000145}
146
Chris Lattner9c85ba32008-11-10 06:08:34 +0000147llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
148 llvm::DICompileUnit Unit) {
149 // Typedefs are derived from some other type. If we have a typedef of a
150 // typedef, make sure to emit the whole chain.
151 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
152
153 // We don't set size information, but do specify where the typedef was
154 // declared.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000155 std::string TyName = Ty->getDecl()->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000156 SourceLocation DefLoc = Ty->getDecl()->getLocation();
157 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000158
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000159 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000160 uint64_t Line = SM.getLogicalLineNumber(DefLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000161
Chris Lattner9c85ba32008-11-10 06:08:34 +0000162 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
163 TyName, DefUnit, Line, 0, 0, 0, 0, Src);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000164}
165
Chris Lattner9c85ba32008-11-10 06:08:34 +0000166llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
167 llvm::DICompileUnit Unit) {
168 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000169
Chris Lattner9c85ba32008-11-10 06:08:34 +0000170 // Add the result type at least.
171 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
172
173 // Set up remainder of arguments if there is a prototype.
174 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
175 if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(Ty)) {
176 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
177 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
178 } else {
179 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000180 }
181
Chris Lattner9c85ba32008-11-10 06:08:34 +0000182 llvm::DIArray EltTypeArray =
183 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
184
185 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
186 Unit, "", llvm::DICompileUnit(),
187 0, 0, 0, 0, 0,
188 llvm::DIType(), EltTypeArray);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000189}
190
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000191/// getOrCreateRecordType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000192llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
193 llvm::DICompileUnit Unit) {
194 const RecordDecl *Decl = Ty->getDecl();
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000195
Chris Lattner9c85ba32008-11-10 06:08:34 +0000196 unsigned Tag;
197 if (Decl->isStruct())
198 Tag = llvm::dwarf::DW_TAG_structure_type;
199 else if (Decl->isUnion())
200 Tag = llvm::dwarf::DW_TAG_union_type;
201 else {
202 assert(Decl->isClass() && "Unknown RecordType!");
203 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000204 }
205
Sanjiv Gupta507de852008-06-09 10:47:41 +0000206 SourceManager &SM = M->getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000207
Chris Lattner9c85ba32008-11-10 06:08:34 +0000208 // Get overall information about the record type for the debug info.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000209 std::string Name = Decl->getNameAsString();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000210
Chris Lattner9c85ba32008-11-10 06:08:34 +0000211 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
212 uint64_t Line = SM.getLogicalLineNumber(Decl->getLocation());
213
214
215 // Records and classes and unions can all be recursive. To handle them, we
216 // first generate a debug descriptor for the struct as a forward declaration.
217 // Then (if it is a definition) we go through and get debug info for all of
218 // its members. Finally, we create a descriptor for the complete type (which
219 // may refer to the forward decl if the struct is recursive) and replace all
220 // uses of the forward declaration with the final definition.
221 llvm::DIType FwdDecl =
222 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
223 llvm::DIType(), llvm::DIArray());
224
225 // If this is just a forward declaration, return it.
226 if (!Decl->getDefinition(M->getContext()))
227 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000228
Chris Lattner9c85ba32008-11-10 06:08:34 +0000229 // Otherwise, insert it into the TypeCache so that recursive uses will find
230 // it.
231 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
232
233 // Convert all the elements.
234 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
235
236 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl);
237
238 unsigned FieldNo = 0;
239 for (RecordDecl::field_const_iterator I = Decl->field_begin(),
240 E = Decl->field_end(); I != E; ++I, ++FieldNo) {
241 FieldDecl *Field = *I;
242 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattner8ec03f52008-11-24 03:54:41 +0000243
244 std::string FieldName = Field->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000245
246 // Get the location for the field.
247 SourceLocation FieldDefLoc = Field->getLocation();
248 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
249 uint64_t FieldLine = SM.getLogicalLineNumber(FieldDefLoc);
250
251 // Bit size, align and offset of the type.
252 uint64_t FieldSize = M->getContext().getTypeSize(Ty);
253 uint64_t FieldAlign = M->getContext().getTypeAlign(Ty);
254 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
255
256 // Create a DW_TAG_member node to remember the offset of this field in the
257 // struct. FIXME: This is an absolutely insane way to capture this
258 // information. When we gut debug info, this should be fixed.
259 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
260 FieldName, FieldDefUnit,
261 FieldLine, FieldSize, FieldAlign,
262 FieldOffset, 0, FieldTy);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000263 EltTys.push_back(FieldTy);
264 }
265
266 llvm::DIArray Elements =
267 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
268
269 // Bit size, align and offset of the type.
270 uint64_t Size = M->getContext().getTypeSize(Ty);
271 uint64_t Align = M->getContext().getTypeAlign(Ty);
272
273 llvm::DIType RealDecl =
274 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
275 Align, 0, 0, llvm::DIType(), Elements);
276
277 // Now that we have a real decl for the struct, replace anything using the
278 // old decl with the new one. This will recursively update the debug info.
279 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
280 FwdDecl.getGV()->eraseFromParent();
281
282 return RealDecl;
283}
284
285llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
286 llvm::DICompileUnit Unit) {
287 EnumDecl *Decl = Ty->getDecl();
288
289 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
290
291 // Create DIEnumerator elements for each enumerator.
292 for (EnumConstantDecl *Elt = Decl->getEnumConstantList(); Elt;
293 Elt = dyn_cast_or_null<EnumConstantDecl>(Elt->getNextDeclarator())) {
294 Enumerators.push_back(DebugFactory.CreateEnumerator(Elt->getName(),
295 Elt->getInitVal().getZExtValue()));
296 }
297
298 // Return a CompositeType for the enum itself.
299 llvm::DIArray EltArray =
300 DebugFactory.GetOrCreateArray(&Enumerators[0], Enumerators.size());
301
Chris Lattner8ec03f52008-11-24 03:54:41 +0000302 std::string EnumName = Decl->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000303 SourceLocation DefLoc = Decl->getLocation();
304 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
305 SourceManager &SM = M->getContext().getSourceManager();
306 uint64_t Line = SM.getLogicalLineNumber(DefLoc);
307
308 // Size and align of the type.
309 uint64_t Size = M->getContext().getTypeSize(Ty);
310 uint64_t Align = M->getContext().getTypeAlign(Ty);
311
312 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
313 Unit, EnumName, DefUnit, Line,
314 Size, Align, 0, 0,
315 llvm::DIType(), EltArray);
316}
317
318llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
319 llvm::DICompileUnit Unit) {
320 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
321 return CreateType(RT, Unit);
322 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
323 return CreateType(ET, Unit);
324
325 return llvm::DIType();
326}
327
328llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
329 llvm::DICompileUnit Unit) {
330 // Size and align of the whole array, not the element type.
331 uint64_t Size = M->getContext().getTypeSize(Ty);
332 uint64_t Align = M->getContext().getTypeAlign(Ty);
333
334 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
335 // interior arrays, do we care? Why aren't nested arrays represented the
336 // obvious/recursive way?
337 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
338 QualType EltTy(Ty, 0);
339 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000340 uint64_t Upper = 0;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000341 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
342 Upper = CAT->getSize().getZExtValue() - 1;
343 // FIXME: Verify this is right for VLAs.
344 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
345 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000346 }
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000347
Chris Lattner9c85ba32008-11-10 06:08:34 +0000348 llvm::DIArray SubscriptArray =
349 DebugFactory.GetOrCreateArray(&Subscripts[0], Subscripts.size());
350
351 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
352 Unit, "", llvm::DICompileUnit(),
353 0, Size, Align, 0, 0,
354 getOrCreateType(EltTy, Unit),
355 SubscriptArray);
356}
357
358
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000359/// getOrCreateType - Get the type from the cache or create a new
360/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000361llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
362 llvm::DICompileUnit Unit) {
363 if (Ty.isNull())
364 return llvm::DIType();
365
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000366 // Check to see if the compile unit already has created this type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000367 llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()];
368 if (!Slot.isNull()) return Slot;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000369
Chris Lattner9c85ba32008-11-10 06:08:34 +0000370 // Handle CVR qualifiers, which recursively handles what they refer to.
371 if (Ty.getCVRQualifiers())
372 return Slot = CreateCVRType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000373
374 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000375 switch (Ty->getTypeClass()) {
376 case Type::Complex:
377 case Type::Reference:
378 case Type::Vector:
379 case Type::ExtVector:
380 case Type::ASQual:
381 case Type::ObjCInterface:
382 case Type::ObjCQualifiedInterface:
383 case Type::ObjCQualifiedId:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000384 default:
385 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000386
Chris Lattner9c85ba32008-11-10 06:08:34 +0000387 case Type::Builtin: Slot = CreateType(cast<BuiltinType>(Ty), Unit); break;
388 case Type::Pointer: Slot = CreateType(cast<PointerType>(Ty), Unit); break;
389 case Type::TypeName: Slot = CreateType(cast<TypedefType>(Ty), Unit); break;
390 case Type::Tagged: Slot = CreateType(cast<TagType>(Ty), Unit); break;
391 case Type::FunctionProto:
392 case Type::FunctionNoProto:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000393 return Slot = CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000394
395 case Type::ConstantArray:
396 case Type::VariableArray:
397 case Type::IncompleteArray:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000398 return Slot = CreateType(cast<ArrayType>(Ty), Unit);
399 case Type::TypeOfExp:
400 return Slot = getOrCreateType(cast<TypeOfExpr>(Ty)->getUnderlyingExpr()
401 ->getType(), Unit);
402 case Type::TypeOfTyp:
403 return Slot = getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(),
404 Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000405 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000406
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000407 return Slot;
408}
409
410/// EmitFunctionStart - Constructs the debug code for entering a function -
411/// "llvm.dbg.func.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000412void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000413 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000414 CGBuilderTy &Builder) {
415 // FIXME: Why is this using CurLoc???
416 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000417 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000418 uint64_t LineNo = SM.getLogicalLineNumber(CurLoc);
419
420 llvm::DISubprogram SP =
421 DebugFactory.CreateSubprogram(Unit, Name, Name, "", Unit, LineNo,
422 getOrCreateType(ReturnType, Unit),
423 Fn->hasInternalLinkage(), true/*definition*/);
424
425 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
426
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000427 // Push function on region stack.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000428 RegionStack.push_back(SP);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000429}
430
431
Chris Lattner9c85ba32008-11-10 06:08:34 +0000432void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000433 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
434
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000435 // Don't bother if things are the same as last time.
436 SourceManager &SM = M->getContext().getSourceManager();
437 if (CurLoc == PrevLoc
438 || (SM.getLineNumber(CurLoc) == SM.getLineNumber(PrevLoc)
439 && SM.isFromSameFile(CurLoc, PrevLoc)))
440 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000441
442 // Update last state.
443 PrevLoc = CurLoc;
444
445 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000446 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
447 DebugFactory.InsertStopPoint(Unit, SM.getLogicalLineNumber(CurLoc),
448 SM.getLogicalColumnNumber(CurLoc),
449 Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000450}
451
452/// EmitRegionStart- Constructs the debug code for entering a declarative
453/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000454void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
455 llvm::DIDescriptor D;
Daniel Dunbar5273f512008-10-17 01:07:56 +0000456 if (!RegionStack.empty())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000457 D = RegionStack.back();
458 D = DebugFactory.CreateBlock(D);
459 RegionStack.push_back(D);
460 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000461}
462
463/// EmitRegionEnd - Constructs the debug code for exiting a declarative
464/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +0000465void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000466 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
467
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000468 // Provide an region stop point.
469 EmitStopPoint(Fn, Builder);
470
Chris Lattner9c85ba32008-11-10 06:08:34 +0000471 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000472 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000473}
474
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000475/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000476void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
477 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000478 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
479
Chris Lattner9c85ba32008-11-10 06:08:34 +0000480 // Get location information.
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000481 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000482 uint64_t Line = SM.getLogicalLineNumber(Decl->getLocation());
483 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
484
485 // Create the descriptor for the variable.
486 llvm::DIVariable D =
487 DebugFactory.CreateVariable(Tag, RegionStack.back(), Decl->getName(),
488 Unit, Line,
489 getOrCreateType(Decl->getType(), Unit));
490 // Insert an llvm.dbg.declare into the current block.
491 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000492}
493
Chris Lattner9c85ba32008-11-10 06:08:34 +0000494void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
495 llvm::Value *Storage,
496 CGBuilderTy &Builder) {
497 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
498}
499
500/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
501/// variable declaration.
502void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
503 CGBuilderTy &Builder) {
504 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
505}
506
507
508
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000509/// EmitGlobalVariable - Emit information about a global variable.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000510void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
511 const VarDecl *Decl) {
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000512 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000513 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000514 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000515 uint64_t LineNo = SM.getLogicalLineNumber(Decl->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +0000516
517 std::string Name = Decl->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000518
519 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
520 getOrCreateType(Decl->getType(), Unit),
521 Var->hasInternalLinkage(),
522 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000523}
524