blob: 4b3ef0ebd08eb82a32265804812b5702538eb552 [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"
18#include "clang/AST/RecordLayout.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000019#include "clang/Basic/SourceManager.h"
20#include "clang/Basic/FileManager.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000021#include "llvm/Constants.h"
22#include "llvm/DerivedTypes.h"
23#include "llvm/Instructions.h"
24#include "llvm/Intrinsics.h"
25#include "llvm/Module.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000026#include "llvm/ADT/StringExtras.h"
27#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000028#include "llvm/Support/Dwarf.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000029#include "llvm/Target/TargetMachine.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000030using namespace clang;
31using namespace clang::CodeGen;
32
33CGDebugInfo::CGDebugInfo(CodeGenModule *m)
Chris Lattner9c85ba32008-11-10 06:08:34 +000034 : M(m), DebugFactory(M->getModule()) {
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000035}
36
Chris Lattner9c85ba32008-11-10 06:08:34 +000037CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar66031a52008-10-17 16:15:48 +000038 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000039}
40
Chris Lattner9c85ba32008-11-10 06:08:34 +000041void CGDebugInfo::setLocation(SourceLocation Loc) {
42 if (Loc.isValid())
43 CurLoc = M->getContext().getSourceManager().getLogicalLoc(Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000044}
45
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000046/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
Daniel Dunbar25f51dd2008-10-24 08:38:36 +000047/// one if necessary. This returns null for invalid source locations.
Chris Lattner9c85ba32008-11-10 06:08:34 +000048llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) {
Daniel Dunbar25f51dd2008-10-24 08:38:36 +000049 if (Loc.isInvalid())
Chris Lattner9c85ba32008-11-10 06:08:34 +000050 return llvm::DICompileUnit();
Daniel Dunbar25f51dd2008-10-24 08:38:36 +000051
Daniel Dunbar2104bf92008-10-24 00:46:51 +000052 SourceManager &SM = M->getContext().getSourceManager();
53 const FileEntry *FE = SM.getFileEntryForLoc(Loc);
Chris Lattner9c85ba32008-11-10 06:08:34 +000054 if (FE == 0) return llvm::DICompileUnit();
55
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000056 // See if this compile unit has been used before.
Chris Lattner9c85ba32008-11-10 06:08:34 +000057 llvm::DICompileUnit &Unit = CompileUnitCache[FE];
58 if (!Unit.isNull()) return Unit;
59
60 // Get source file information.
61 const char *FileName = FE->getName();
62 const char *DirName = FE->getDir()->getName();
Daniel Dunbar2104bf92008-10-24 00:46:51 +000063
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000064 // Create new compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +000065 // FIXME: Handle other language IDs as well.
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000066 // FIXME: Do not know how to get clang version yet.
Chris Lattner9c85ba32008-11-10 06:08:34 +000067 return Unit = DebugFactory.CreateCompileUnit(llvm::dwarf::DW_LANG_C89,
68 FileName, DirName, "clang");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000069}
70
Chris Lattner9c85ba32008-11-10 06:08:34 +000071/// getOrCreateBuiltinType - Get the Basic type from the cache or create a new
72/// one if necessary.
73llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
74 llvm::DICompileUnit Unit){
75 unsigned Encoding = 0;
76 switch (BT->getKind()) {
77 default:
78 case BuiltinType::Void:
79 return llvm::DIType();
80 case BuiltinType::UChar:
81 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
82 case BuiltinType::Char_S:
83 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
84 case BuiltinType::UShort:
85 case BuiltinType::UInt:
86 case BuiltinType::ULong:
87 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
88 case BuiltinType::Short:
89 case BuiltinType::Int:
90 case BuiltinType::Long:
91 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
92 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
93 case BuiltinType::Float:
94 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
95 }
96 // Bit size, align and offset of the type.
97 uint64_t Size = M->getContext().getTypeSize(BT);
98 uint64_t Align = M->getContext().getTypeAlign(BT);
99 uint64_t Offset = 0;
100
101 return DebugFactory.CreateBasicType(Unit, BT->getName(), Unit, 0, Size, Align,
102 Offset, /*flags*/ 0, Encoding);
103}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000104
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000105/// getOrCreateCVRType - Get the CVR qualified type from the cache or create
106/// a new one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000107llvm::DIType CGDebugInfo::CreateCVRType(QualType Ty, llvm::DICompileUnit Unit) {
108 // We will create one Derived type for one qualifier and recurse to handle any
109 // additional ones.
110 llvm::DIType FromTy;
111 unsigned Tag;
112 if (Ty.isConstQualified()) {
113 Tag = llvm::dwarf::DW_TAG_const_type;
114 Ty.removeConst();
115 FromTy = getOrCreateType(Ty, Unit);
116 } else if (Ty.isVolatileQualified()) {
117 Tag = llvm::dwarf::DW_TAG_volatile_type;
118 Ty.removeVolatile();
119 FromTy = getOrCreateType(Ty, Unit);
120 } else {
121 assert(Ty.isRestrictQualified() && "Unknown type qualifier for debug info");
122 Tag = llvm::dwarf::DW_TAG_restrict_type;
123 Ty.removeRestrict();
124 FromTy = getOrCreateType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000125 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000126
Daniel Dunbar3845f862008-10-31 03:54:29 +0000127 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
128 // CVR derived types.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000129 return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
130 0, 0, 0, 0, 0, FromTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000131}
132
Chris Lattner9c85ba32008-11-10 06:08:34 +0000133llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
134 llvm::DICompileUnit Unit) {
135 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000136
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000137 // Bit size, align and offset of the type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000138 uint64_t Size = M->getContext().getTypeSize(Ty);
139 uint64_t Align = M->getContext().getTypeAlign(Ty);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000140
Chris Lattner9c85ba32008-11-10 06:08:34 +0000141 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
142 "", llvm::DICompileUnit(),
143 0, Size, Align, 0, 0, EltTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000144}
145
Chris Lattner9c85ba32008-11-10 06:08:34 +0000146llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
147 llvm::DICompileUnit Unit) {
148 // Typedefs are derived from some other type. If we have a typedef of a
149 // typedef, make sure to emit the whole chain.
150 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
151
152 // We don't set size information, but do specify where the typedef was
153 // declared.
154 const char *TyName = Ty->getDecl()->getName();
155 SourceLocation DefLoc = Ty->getDecl()->getLocation();
156 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000157
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000158 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000159 uint64_t Line = SM.getLogicalLineNumber(DefLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000160
Chris Lattner9c85ba32008-11-10 06:08:34 +0000161 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
162 TyName, DefUnit, Line, 0, 0, 0, 0, Src);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000163}
164
Chris Lattner9c85ba32008-11-10 06:08:34 +0000165llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
166 llvm::DICompileUnit Unit) {
167 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000168
Chris Lattner9c85ba32008-11-10 06:08:34 +0000169 // Add the result type at least.
170 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
171
172 // Set up remainder of arguments if there is a prototype.
173 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
174 if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(Ty)) {
175 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
176 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
177 } else {
178 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000179 }
180
Chris Lattner9c85ba32008-11-10 06:08:34 +0000181 llvm::DIArray EltTypeArray =
182 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
183
184 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
185 Unit, "", llvm::DICompileUnit(),
186 0, 0, 0, 0, 0,
187 llvm::DIType(), EltTypeArray);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000188}
189
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000190/// getOrCreateRecordType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000191llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
192 llvm::DICompileUnit Unit) {
193 const RecordDecl *Decl = Ty->getDecl();
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000194
Chris Lattner9c85ba32008-11-10 06:08:34 +0000195 if (!Decl->getDefinition(M->getContext()))
196 return llvm::DIType();
197
198 unsigned Tag;
199 if (Decl->isStruct())
200 Tag = llvm::dwarf::DW_TAG_structure_type;
201 else if (Decl->isUnion())
202 Tag = llvm::dwarf::DW_TAG_union_type;
203 else {
204 assert(Decl->isClass() && "Unknown RecordType!");
205 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000206 }
207
Sanjiv Gupta507de852008-06-09 10:47:41 +0000208 SourceManager &SM = M->getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000209
Chris Lattner9c85ba32008-11-10 06:08:34 +0000210 // Get overall information about the record type for the debug info.
211 const char *Name = Decl->getName();
212 if (Name == 0) Name = "";
Sanjiv Gupta507de852008-06-09 10:47:41 +0000213
Chris Lattner9c85ba32008-11-10 06:08:34 +0000214 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
215 uint64_t Line = SM.getLogicalLineNumber(Decl->getLocation());
216
217
218 // Records and classes and unions can all be recursive. To handle them, we
219 // first generate a debug descriptor for the struct as a forward declaration.
220 // Then (if it is a definition) we go through and get debug info for all of
221 // its members. Finally, we create a descriptor for the complete type (which
222 // may refer to the forward decl if the struct is recursive) and replace all
223 // uses of the forward declaration with the final definition.
224 llvm::DIType FwdDecl =
225 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
226 llvm::DIType(), llvm::DIArray());
227
228 // If this is just a forward declaration, return it.
229 if (!Decl->getDefinition(M->getContext()))
230 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000231
Chris Lattner9c85ba32008-11-10 06:08:34 +0000232 // Otherwise, insert it into the TypeCache so that recursive uses will find
233 // it.
234 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
235
236 // Convert all the elements.
237 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
238
239 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl);
240
241 unsigned FieldNo = 0;
242 for (RecordDecl::field_const_iterator I = Decl->field_begin(),
243 E = Decl->field_end(); I != E; ++I, ++FieldNo) {
244 FieldDecl *Field = *I;
245 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
246
247#if 0
248 const char *FieldName = Field->getName();
249 if (FieldName == 0) FieldName = "";
250
251 // Get the location for the field.
252 SourceLocation FieldDefLoc = Field->getLocation();
253 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
254 uint64_t FieldLine = SM.getLogicalLineNumber(FieldDefLoc);
255
256 // Bit size, align and offset of the type.
257 uint64_t FieldSize = M->getContext().getTypeSize(Ty);
258 uint64_t FieldAlign = M->getContext().getTypeAlign(Ty);
259 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
260
261 // Create a DW_TAG_member node to remember the offset of this field in the
262 // struct. FIXME: This is an absolutely insane way to capture this
263 // information. When we gut debug info, this should be fixed.
264 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
265 FieldName, FieldDefUnit,
266 FieldLine, FieldSize, FieldAlign,
267 FieldOffset, 0, FieldTy);
268#endif
269 EltTys.push_back(FieldTy);
270 }
271
272 llvm::DIArray Elements =
273 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
274
275 // Bit size, align and offset of the type.
276 uint64_t Size = M->getContext().getTypeSize(Ty);
277 uint64_t Align = M->getContext().getTypeAlign(Ty);
278
279 llvm::DIType RealDecl =
280 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
281 Align, 0, 0, llvm::DIType(), Elements);
282
283 // Now that we have a real decl for the struct, replace anything using the
284 // old decl with the new one. This will recursively update the debug info.
285 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
286 FwdDecl.getGV()->eraseFromParent();
287
288 return RealDecl;
289}
290
291llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
292 llvm::DICompileUnit Unit) {
293 EnumDecl *Decl = Ty->getDecl();
294
295 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
296
297 // Create DIEnumerator elements for each enumerator.
298 for (EnumConstantDecl *Elt = Decl->getEnumConstantList(); Elt;
299 Elt = dyn_cast_or_null<EnumConstantDecl>(Elt->getNextDeclarator())) {
300 Enumerators.push_back(DebugFactory.CreateEnumerator(Elt->getName(),
301 Elt->getInitVal().getZExtValue()));
302 }
303
304 // Return a CompositeType for the enum itself.
305 llvm::DIArray EltArray =
306 DebugFactory.GetOrCreateArray(&Enumerators[0], Enumerators.size());
307
308 const char *EnumName = Decl->getName() ? Decl->getName() : "";
309 SourceLocation DefLoc = Decl->getLocation();
310 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
311 SourceManager &SM = M->getContext().getSourceManager();
312 uint64_t Line = SM.getLogicalLineNumber(DefLoc);
313
314 // Size and align of the type.
315 uint64_t Size = M->getContext().getTypeSize(Ty);
316 uint64_t Align = M->getContext().getTypeAlign(Ty);
317
318 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
319 Unit, EnumName, DefUnit, Line,
320 Size, Align, 0, 0,
321 llvm::DIType(), EltArray);
322}
323
324llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
325 llvm::DICompileUnit Unit) {
326 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
327 return CreateType(RT, Unit);
328 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
329 return CreateType(ET, Unit);
330
331 return llvm::DIType();
332}
333
334llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
335 llvm::DICompileUnit Unit) {
336 // Size and align of the whole array, not the element type.
337 uint64_t Size = M->getContext().getTypeSize(Ty);
338 uint64_t Align = M->getContext().getTypeAlign(Ty);
339
340 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
341 // interior arrays, do we care? Why aren't nested arrays represented the
342 // obvious/recursive way?
343 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
344 QualType EltTy(Ty, 0);
345 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000346 uint64_t Upper = 0;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000347 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
348 Upper = CAT->getSize().getZExtValue() - 1;
349 // FIXME: Verify this is right for VLAs.
350 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
351 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000352 }
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000353
Chris Lattner9c85ba32008-11-10 06:08:34 +0000354 llvm::DIArray SubscriptArray =
355 DebugFactory.GetOrCreateArray(&Subscripts[0], Subscripts.size());
356
357 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
358 Unit, "", llvm::DICompileUnit(),
359 0, Size, Align, 0, 0,
360 getOrCreateType(EltTy, Unit),
361 SubscriptArray);
362}
363
364
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000365/// getOrCreateType - Get the type from the cache or create a new
366/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000367llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
368 llvm::DICompileUnit Unit) {
369 if (Ty.isNull())
370 return llvm::DIType();
371
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000372 // Check to see if the compile unit already has created this type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000373 llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()];
374 if (!Slot.isNull()) return Slot;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000375
Chris Lattner9c85ba32008-11-10 06:08:34 +0000376 // Handle CVR qualifiers, which recursively handles what they refer to.
377 if (Ty.getCVRQualifiers())
378 return Slot = CreateCVRType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000379
380 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000381 switch (Ty->getTypeClass()) {
382 case Type::Complex:
383 case Type::Reference:
384 case Type::Vector:
385 case Type::ExtVector:
386 case Type::ASQual:
387 case Type::ObjCInterface:
388 case Type::ObjCQualifiedInterface:
389 case Type::ObjCQualifiedId:
390 case Type::TypeOfExp:
391 case Type::TypeOfTyp:
392 default:
393 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000394
Chris Lattner9c85ba32008-11-10 06:08:34 +0000395 case Type::Builtin: Slot = CreateType(cast<BuiltinType>(Ty), Unit); break;
396 case Type::Pointer: Slot = CreateType(cast<PointerType>(Ty), Unit); break;
397 case Type::TypeName: Slot = CreateType(cast<TypedefType>(Ty), Unit); break;
398 case Type::Tagged: Slot = CreateType(cast<TagType>(Ty), Unit); break;
399 case Type::FunctionProto:
400 case Type::FunctionNoProto:
401 Slot = CreateType(cast<FunctionType>(Ty), Unit);
402 break;
403
404 case Type::ConstantArray:
405 case Type::VariableArray:
406 case Type::IncompleteArray:
407 Slot = CreateType(cast<ArrayType>(Ty), Unit);
408 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000409 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000410
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000411 return Slot;
412}
413
414/// EmitFunctionStart - Constructs the debug code for entering a function -
415/// "llvm.dbg.func.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000416void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000417 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000418 CGBuilderTy &Builder) {
419 // FIXME: Why is this using CurLoc???
420 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000421 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000422 uint64_t LineNo = SM.getLogicalLineNumber(CurLoc);
423
424 llvm::DISubprogram SP =
425 DebugFactory.CreateSubprogram(Unit, Name, Name, "", Unit, LineNo,
426 getOrCreateType(ReturnType, Unit),
427 Fn->hasInternalLinkage(), true/*definition*/);
428
429 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
430
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000431 // Push function on region stack.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000432 RegionStack.push_back(SP);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000433}
434
435
Chris Lattner9c85ba32008-11-10 06:08:34 +0000436void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000437 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
438
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000439 // Don't bother if things are the same as last time.
440 SourceManager &SM = M->getContext().getSourceManager();
441 if (CurLoc == PrevLoc
442 || (SM.getLineNumber(CurLoc) == SM.getLineNumber(PrevLoc)
443 && SM.isFromSameFile(CurLoc, PrevLoc)))
444 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000445
446 // Update last state.
447 PrevLoc = CurLoc;
448
449 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000450 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
451 DebugFactory.InsertStopPoint(Unit, SM.getLogicalLineNumber(CurLoc),
452 SM.getLogicalColumnNumber(CurLoc),
453 Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000454}
455
456/// EmitRegionStart- Constructs the debug code for entering a declarative
457/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000458void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
459 llvm::DIDescriptor D;
Daniel Dunbar5273f512008-10-17 01:07:56 +0000460 if (!RegionStack.empty())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000461 D = RegionStack.back();
462 D = DebugFactory.CreateBlock(D);
463 RegionStack.push_back(D);
464 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000465}
466
467/// EmitRegionEnd - Constructs the debug code for exiting a declarative
468/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +0000469void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000470 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
471
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000472 // Provide an region stop point.
473 EmitStopPoint(Fn, Builder);
474
Chris Lattner9c85ba32008-11-10 06:08:34 +0000475 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000476 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000477}
478
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000479/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000480void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
481 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000482 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
483
Chris Lattner9c85ba32008-11-10 06:08:34 +0000484 // Get location information.
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000485 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000486 uint64_t Line = SM.getLogicalLineNumber(Decl->getLocation());
487 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
488
489 // Create the descriptor for the variable.
490 llvm::DIVariable D =
491 DebugFactory.CreateVariable(Tag, RegionStack.back(), Decl->getName(),
492 Unit, Line,
493 getOrCreateType(Decl->getType(), Unit));
494 // Insert an llvm.dbg.declare into the current block.
495 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000496}
497
Chris Lattner9c85ba32008-11-10 06:08:34 +0000498void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
499 llvm::Value *Storage,
500 CGBuilderTy &Builder) {
501 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
502}
503
504/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
505/// variable declaration.
506void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
507 CGBuilderTy &Builder) {
508 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
509}
510
511
512
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000513/// EmitGlobalVariable - Emit information about a global variable.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000514void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
515 const VarDecl *Decl) {
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000516 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000517 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000518 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000519 uint64_t LineNo = SM.getLogicalLineNumber(Decl->getLocation());
520 const char *Name = Decl->getName();
521
522 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
523 getOrCreateType(Decl->getType(), Unit),
524 Var->hasInternalLinkage(),
525 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000526}
527