blob: 6176b868ea8a06eaf5991c4106926d4f3f6a8664 [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) {
Douglas Gregora4c46df2008-12-11 17:59:21 +0000194 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;
Douglas Gregora4c46df2008-12-11 17:59:21 +0000239 for (RecordDecl::field_iterator I = Decl->field_begin(),
240 E = Decl->field_end();
241 I != E; ++I, ++FieldNo) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000242 FieldDecl *Field = *I;
243 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattner8ec03f52008-11-24 03:54:41 +0000244
245 std::string FieldName = Field->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000246
247 // Get the location for the field.
248 SourceLocation FieldDefLoc = Field->getLocation();
249 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
250 uint64_t FieldLine = SM.getLogicalLineNumber(FieldDefLoc);
251
252 // Bit size, align and offset of the type.
253 uint64_t FieldSize = M->getContext().getTypeSize(Ty);
254 uint64_t FieldAlign = M->getContext().getTypeAlign(Ty);
255 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
256
257 // Create a DW_TAG_member node to remember the offset of this field in the
258 // struct. FIXME: This is an absolutely insane way to capture this
259 // information. When we gut debug info, this should be fixed.
260 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
261 FieldName, FieldDefUnit,
262 FieldLine, FieldSize, FieldAlign,
263 FieldOffset, 0, FieldTy);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000264 EltTys.push_back(FieldTy);
265 }
266
267 llvm::DIArray Elements =
268 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
269
270 // Bit size, align and offset of the type.
271 uint64_t Size = M->getContext().getTypeSize(Ty);
272 uint64_t Align = M->getContext().getTypeAlign(Ty);
273
274 llvm::DIType RealDecl =
275 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
276 Align, 0, 0, llvm::DIType(), Elements);
277
278 // Now that we have a real decl for the struct, replace anything using the
279 // old decl with the new one. This will recursively update the debug info.
280 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
281 FwdDecl.getGV()->eraseFromParent();
282
283 return RealDecl;
284}
285
286llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
287 llvm::DICompileUnit Unit) {
288 EnumDecl *Decl = Ty->getDecl();
289
290 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
291
292 // Create DIEnumerator elements for each enumerator.
Douglas Gregor44b43212008-12-11 16:49:14 +0000293 for (EnumDecl::enumerator_iterator Enum = Decl->enumerator_begin(),
294 EnumEnd = Decl->enumerator_end();
295 Enum != EnumEnd; ++Enum) {
296 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(),
297 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000298 }
299
300 // Return a CompositeType for the enum itself.
301 llvm::DIArray EltArray =
302 DebugFactory.GetOrCreateArray(&Enumerators[0], Enumerators.size());
303
Chris Lattner8ec03f52008-11-24 03:54:41 +0000304 std::string EnumName = Decl->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000305 SourceLocation DefLoc = Decl->getLocation();
306 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
307 SourceManager &SM = M->getContext().getSourceManager();
308 uint64_t Line = SM.getLogicalLineNumber(DefLoc);
309
310 // Size and align of the type.
311 uint64_t Size = M->getContext().getTypeSize(Ty);
312 uint64_t Align = M->getContext().getTypeAlign(Ty);
313
314 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
315 Unit, EnumName, DefUnit, Line,
316 Size, Align, 0, 0,
317 llvm::DIType(), EltArray);
318}
319
320llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
321 llvm::DICompileUnit Unit) {
322 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
323 return CreateType(RT, Unit);
324 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
325 return CreateType(ET, Unit);
326
327 return llvm::DIType();
328}
329
330llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
331 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000332 uint64_t Size;
333 uint64_t Align;
334
335
336 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
337
338 Size = 0;
339 Align =
340 M->getContext().getTypeSize(M->getContext().getBaseElementType(VAT));
341 } else {
342 // Size and align of the whole array, not the element type.
343 Size = M->getContext().getTypeSize(Ty);
344 Align = M->getContext().getTypeAlign(Ty);
345 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000346
347 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
348 // interior arrays, do we care? Why aren't nested arrays represented the
349 // obvious/recursive way?
350 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
351 QualType EltTy(Ty, 0);
352 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000353 uint64_t Upper = 0;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000354 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
355 Upper = CAT->getSize().getZExtValue() - 1;
356 // FIXME: Verify this is right for VLAs.
357 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
358 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000359 }
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000360
Chris Lattner9c85ba32008-11-10 06:08:34 +0000361 llvm::DIArray SubscriptArray =
362 DebugFactory.GetOrCreateArray(&Subscripts[0], Subscripts.size());
363
364 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
365 Unit, "", llvm::DICompileUnit(),
366 0, Size, Align, 0, 0,
367 getOrCreateType(EltTy, Unit),
368 SubscriptArray);
369}
370
371
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000372/// getOrCreateType - Get the type from the cache or create a new
373/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000374llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
375 llvm::DICompileUnit Unit) {
376 if (Ty.isNull())
377 return llvm::DIType();
378
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000379 // Check to see if the compile unit already has created this type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000380 llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()];
381 if (!Slot.isNull()) return Slot;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000382
Chris Lattner9c85ba32008-11-10 06:08:34 +0000383 // Handle CVR qualifiers, which recursively handles what they refer to.
384 if (Ty.getCVRQualifiers())
385 return Slot = CreateCVRType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000386
387 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000388 switch (Ty->getTypeClass()) {
389 case Type::Complex:
390 case Type::Reference:
391 case Type::Vector:
392 case Type::ExtVector:
393 case Type::ASQual:
394 case Type::ObjCInterface:
395 case Type::ObjCQualifiedInterface:
396 case Type::ObjCQualifiedId:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000397 default:
398 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000399
Chris Lattner9c85ba32008-11-10 06:08:34 +0000400 case Type::Builtin: Slot = CreateType(cast<BuiltinType>(Ty), Unit); break;
401 case Type::Pointer: Slot = CreateType(cast<PointerType>(Ty), Unit); break;
402 case Type::TypeName: Slot = CreateType(cast<TypedefType>(Ty), Unit); break;
403 case Type::Tagged: Slot = CreateType(cast<TagType>(Ty), Unit); break;
404 case Type::FunctionProto:
405 case Type::FunctionNoProto:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000406 return Slot = CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000407
408 case Type::ConstantArray:
409 case Type::VariableArray:
410 case Type::IncompleteArray:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000411 return Slot = CreateType(cast<ArrayType>(Ty), Unit);
412 case Type::TypeOfExp:
413 return Slot = getOrCreateType(cast<TypeOfExpr>(Ty)->getUnderlyingExpr()
414 ->getType(), Unit);
415 case Type::TypeOfTyp:
416 return Slot = getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(),
417 Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000418 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000419
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000420 return Slot;
421}
422
423/// EmitFunctionStart - Constructs the debug code for entering a function -
424/// "llvm.dbg.func.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000425void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000426 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000427 CGBuilderTy &Builder) {
428 // FIXME: Why is this using CurLoc???
429 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000430 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000431 uint64_t LineNo = SM.getLogicalLineNumber(CurLoc);
432
433 llvm::DISubprogram SP =
434 DebugFactory.CreateSubprogram(Unit, Name, Name, "", Unit, LineNo,
435 getOrCreateType(ReturnType, Unit),
436 Fn->hasInternalLinkage(), true/*definition*/);
437
438 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
439
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000440 // Push function on region stack.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000441 RegionStack.push_back(SP);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000442}
443
444
Chris Lattner9c85ba32008-11-10 06:08:34 +0000445void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000446 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
447
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000448 // Don't bother if things are the same as last time.
449 SourceManager &SM = M->getContext().getSourceManager();
450 if (CurLoc == PrevLoc
451 || (SM.getLineNumber(CurLoc) == SM.getLineNumber(PrevLoc)
452 && SM.isFromSameFile(CurLoc, PrevLoc)))
453 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000454
455 // Update last state.
456 PrevLoc = CurLoc;
457
458 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000459 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
460 DebugFactory.InsertStopPoint(Unit, SM.getLogicalLineNumber(CurLoc),
461 SM.getLogicalColumnNumber(CurLoc),
462 Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000463}
464
465/// EmitRegionStart- Constructs the debug code for entering a declarative
466/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000467void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
468 llvm::DIDescriptor D;
Daniel Dunbar5273f512008-10-17 01:07:56 +0000469 if (!RegionStack.empty())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000470 D = RegionStack.back();
471 D = DebugFactory.CreateBlock(D);
472 RegionStack.push_back(D);
473 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000474}
475
476/// EmitRegionEnd - Constructs the debug code for exiting a declarative
477/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +0000478void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000479 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
480
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000481 // Provide an region stop point.
482 EmitStopPoint(Fn, Builder);
483
Chris Lattner9c85ba32008-11-10 06:08:34 +0000484 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000485 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000486}
487
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000488/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000489void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
490 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000491 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
492
Chris Lattner9c85ba32008-11-10 06:08:34 +0000493 // Get location information.
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000494 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000495 uint64_t Line = SM.getLogicalLineNumber(Decl->getLocation());
496 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
497
498 // Create the descriptor for the variable.
499 llvm::DIVariable D =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000500 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000501 Unit, Line,
502 getOrCreateType(Decl->getType(), Unit));
503 // Insert an llvm.dbg.declare into the current block.
504 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000505}
506
Chris Lattner9c85ba32008-11-10 06:08:34 +0000507void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
508 llvm::Value *Storage,
509 CGBuilderTy &Builder) {
510 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
511}
512
513/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
514/// variable declaration.
515void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
516 CGBuilderTy &Builder) {
517 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
518}
519
520
521
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000522/// EmitGlobalVariable - Emit information about a global variable.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000523void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
524 const VarDecl *Decl) {
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000525 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000526 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000527 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000528 uint64_t LineNo = SM.getLogicalLineNumber(Decl->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +0000529
530 std::string Name = Decl->getNameAsString();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000531
532 QualType T = Decl->getType();
533 if (T->isIncompleteArrayType()) {
534
535 // CodeGen turns int[] into int[1] so we'll do the same here.
536 llvm::APSInt ConstVal(32);
537
538 ConstVal = 1;
539 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
540
541 T = M->getContext().getConstantArrayType(ET, ConstVal,
542 ArrayType::Normal, 0);
543 }
544
Chris Lattner9c85ba32008-11-10 06:08:34 +0000545 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000546 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000547 Var->hasInternalLinkage(),
548 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000549}
550