blob: d3fee11e467dcd23c640dd6e82dc02403f0a527a [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"
Daniel Dunbarde300732008-08-11 04:54:23 +000017#include "clang/AST/Decl.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 Dunbar4a66ef12008-10-24 08:38:36 +000050 if (Loc.isInvalid())
Chris Lattner562ce0a2008-11-10 06:08:34 +000051 return llvm::DICompileUnit();
Daniel Dunbar4a66ef12008-10-24 08:38:36 +000052
Daniel Dunbar9865e6f2008-10-24 00:46:51 +000053 SourceManager &SM = M->getContext().getSourceManager();
Daniel Dunbare426ed02009-01-20 01:06:30 +000054 Loc = SM.getInstantiationLoc(Loc);
Chris Lattner1737bd52009-01-19 07:46:45 +000055 const FileEntry *FE = SM.getFileEntryForID(SM.getFileID(Loc));
Chris Lattner562ce0a2008-11-10 06:08:34 +000056 if (FE == 0) return llvm::DICompileUnit();
57
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000058 // See if this compile unit has been used before.
Chris Lattner562ce0a2008-11-10 06:08:34 +000059 llvm::DICompileUnit &Unit = CompileUnitCache[FE];
60 if (!Unit.isNull()) return Unit;
61
62 // Get source file information.
63 const char *FileName = FE->getName();
64 const char *DirName = FE->getDir()->getName();
Daniel Dunbar9865e6f2008-10-24 00:46:51 +000065
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000066 // Create new compile unit.
Chris Lattner562ce0a2008-11-10 06:08:34 +000067 // FIXME: Handle other language IDs as well.
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000068 // FIXME: Do not know how to get clang version yet.
Chris Lattner562ce0a2008-11-10 06:08:34 +000069 return Unit = DebugFactory.CreateCompileUnit(llvm::dwarf::DW_LANG_C89,
70 FileName, DirName, "clang");
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000071}
72
Chris Lattner562ce0a2008-11-10 06:08:34 +000073/// getOrCreateBuiltinType - Get the Basic type from the cache or create a new
74/// one if necessary.
75llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
76 llvm::DICompileUnit Unit){
77 unsigned Encoding = 0;
78 switch (BT->getKind()) {
79 default:
80 case BuiltinType::Void:
81 return llvm::DIType();
82 case BuiltinType::UChar:
83 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
84 case BuiltinType::Char_S:
85 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
86 case BuiltinType::UShort:
87 case BuiltinType::UInt:
88 case BuiltinType::ULong:
89 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
90 case BuiltinType::Short:
91 case BuiltinType::Int:
92 case BuiltinType::Long:
93 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
94 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
95 case BuiltinType::Float:
96 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
97 }
98 // Bit size, align and offset of the type.
99 uint64_t Size = M->getContext().getTypeSize(BT);
100 uint64_t Align = M->getContext().getTypeAlign(BT);
101 uint64_t Offset = 0;
102
103 return DebugFactory.CreateBasicType(Unit, BT->getName(), Unit, 0, Size, Align,
104 Offset, /*flags*/ 0, Encoding);
105}
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000106
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000107/// getOrCreateCVRType - Get the CVR qualified type from the cache or create
108/// a new one if necessary.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000109llvm::DIType CGDebugInfo::CreateCVRType(QualType Ty, llvm::DICompileUnit Unit) {
110 // We will create one Derived type for one qualifier and recurse to handle any
111 // additional ones.
112 llvm::DIType FromTy;
113 unsigned Tag;
114 if (Ty.isConstQualified()) {
115 Tag = llvm::dwarf::DW_TAG_const_type;
116 Ty.removeConst();
117 FromTy = getOrCreateType(Ty, Unit);
118 } else if (Ty.isVolatileQualified()) {
119 Tag = llvm::dwarf::DW_TAG_volatile_type;
120 Ty.removeVolatile();
121 FromTy = getOrCreateType(Ty, Unit);
122 } else {
123 assert(Ty.isRestrictQualified() && "Unknown type qualifier for debug info");
124 Tag = llvm::dwarf::DW_TAG_restrict_type;
125 Ty.removeRestrict();
126 FromTy = getOrCreateType(Ty, Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000127 }
Chris Lattner562ce0a2008-11-10 06:08:34 +0000128
Daniel Dunbar44252b42008-10-31 03:54:29 +0000129 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
130 // CVR derived types.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000131 return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
132 0, 0, 0, 0, 0, FromTy);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000133}
134
Chris Lattner562ce0a2008-11-10 06:08:34 +0000135llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
136 llvm::DICompileUnit Unit) {
137 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000138
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000139 // Bit size, align and offset of the type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000140 uint64_t Size = M->getContext().getTypeSize(Ty);
141 uint64_t Align = M->getContext().getTypeAlign(Ty);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000142
Chris Lattner562ce0a2008-11-10 06:08:34 +0000143 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
144 "", llvm::DICompileUnit(),
145 0, Size, Align, 0, 0, EltTy);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000146}
147
Chris Lattner562ce0a2008-11-10 06:08:34 +0000148llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
149 llvm::DICompileUnit Unit) {
150 // Typedefs are derived from some other type. If we have a typedef of a
151 // typedef, make sure to emit the whole chain.
152 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
153
154 // We don't set size information, but do specify where the typedef was
155 // declared.
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000156 std::string TyName = Ty->getDecl()->getNameAsString();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000157 SourceLocation DefLoc = Ty->getDecl()->getLocation();
158 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000159
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000160 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner18c8dc02009-01-16 07:36:28 +0000161 uint64_t Line = SM.getInstantiationLineNumber(DefLoc);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000162
Chris Lattner562ce0a2008-11-10 06:08:34 +0000163 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
164 TyName, DefUnit, Line, 0, 0, 0, 0, Src);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000165}
166
Chris Lattner562ce0a2008-11-10 06:08:34 +0000167llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
168 llvm::DICompileUnit Unit) {
169 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000170
Chris Lattner562ce0a2008-11-10 06:08:34 +0000171 // Add the result type at least.
172 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
173
174 // Set up remainder of arguments if there is a prototype.
175 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
176 if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(Ty)) {
177 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
178 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
179 } else {
180 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000181 }
182
Chris Lattner562ce0a2008-11-10 06:08:34 +0000183 llvm::DIArray EltTypeArray =
184 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
185
186 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
187 Unit, "", llvm::DICompileUnit(),
188 0, 0, 0, 0, 0,
189 llvm::DIType(), EltTypeArray);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000190}
191
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000192/// getOrCreateRecordType - get structure or union type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000193llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
194 llvm::DICompileUnit Unit) {
Douglas Gregor640a04b2008-12-11 17:59:21 +0000195 RecordDecl *Decl = Ty->getDecl();
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000196
Chris Lattner562ce0a2008-11-10 06:08:34 +0000197 unsigned Tag;
198 if (Decl->isStruct())
199 Tag = llvm::dwarf::DW_TAG_structure_type;
200 else if (Decl->isUnion())
201 Tag = llvm::dwarf::DW_TAG_union_type;
202 else {
203 assert(Decl->isClass() && "Unknown RecordType!");
204 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000205 }
206
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000207 SourceManager &SM = M->getContext().getSourceManager();
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000208
Chris Lattner562ce0a2008-11-10 06:08:34 +0000209 // Get overall information about the record type for the debug info.
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000210 std::string Name = Decl->getNameAsString();
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000211
Chris Lattner562ce0a2008-11-10 06:08:34 +0000212 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Chris Lattner18c8dc02009-01-16 07:36:28 +0000213 unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation());
Chris Lattner562ce0a2008-11-10 06:08:34 +0000214
215
216 // Records and classes and unions can all be recursive. To handle them, we
217 // first generate a debug descriptor for the struct as a forward declaration.
218 // Then (if it is a definition) we go through and get debug info for all of
219 // its members. Finally, we create a descriptor for the complete type (which
220 // may refer to the forward decl if the struct is recursive) and replace all
221 // uses of the forward declaration with the final definition.
222 llvm::DIType FwdDecl =
223 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
224 llvm::DIType(), llvm::DIArray());
225
226 // If this is just a forward declaration, return it.
227 if (!Decl->getDefinition(M->getContext()))
228 return FwdDecl;
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000229
Chris Lattner562ce0a2008-11-10 06:08:34 +0000230 // Otherwise, insert it into the TypeCache so that recursive uses will find
231 // it.
232 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
233
234 // Convert all the elements.
235 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
236
237 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl);
238
239 unsigned FieldNo = 0;
Douglas Gregor640a04b2008-12-11 17:59:21 +0000240 for (RecordDecl::field_iterator I = Decl->field_begin(),
241 E = Decl->field_end();
242 I != E; ++I, ++FieldNo) {
Chris Lattner562ce0a2008-11-10 06:08:34 +0000243 FieldDecl *Field = *I;
244 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000245
246 std::string FieldName = Field->getNameAsString();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000247
248 // Get the location for the field.
249 SourceLocation FieldDefLoc = Field->getLocation();
250 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Chris Lattner18c8dc02009-01-16 07:36:28 +0000251 unsigned FieldLine = SM.getInstantiationLineNumber(FieldDefLoc);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000252
253 // Bit size, align and offset of the type.
254 uint64_t FieldSize = M->getContext().getTypeSize(Ty);
Chris Lattner18c8dc02009-01-16 07:36:28 +0000255 unsigned FieldAlign = M->getContext().getTypeAlign(Ty);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000256 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
257
258 // Create a DW_TAG_member node to remember the offset of this field in the
259 // struct. FIXME: This is an absolutely insane way to capture this
260 // information. When we gut debug info, this should be fixed.
261 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
262 FieldName, FieldDefUnit,
263 FieldLine, FieldSize, FieldAlign,
264 FieldOffset, 0, FieldTy);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000265 EltTys.push_back(FieldTy);
266 }
267
268 llvm::DIArray Elements =
269 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
270
271 // Bit size, align and offset of the type.
272 uint64_t Size = M->getContext().getTypeSize(Ty);
273 uint64_t Align = M->getContext().getTypeAlign(Ty);
274
275 llvm::DIType RealDecl =
276 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
277 Align, 0, 0, llvm::DIType(), Elements);
278
279 // Now that we have a real decl for the struct, replace anything using the
280 // old decl with the new one. This will recursively update the debug info.
281 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
282 FwdDecl.getGV()->eraseFromParent();
283
284 return RealDecl;
285}
286
287llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
288 llvm::DICompileUnit Unit) {
289 EnumDecl *Decl = Ty->getDecl();
290
291 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
292
293 // Create DIEnumerator elements for each enumerator.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000294 for (EnumDecl::enumerator_iterator Enum = Decl->enumerator_begin(),
295 EnumEnd = Decl->enumerator_end();
296 Enum != EnumEnd; ++Enum) {
297 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(),
298 Enum->getInitVal().getZExtValue()));
Chris Lattner562ce0a2008-11-10 06:08:34 +0000299 }
300
301 // Return a CompositeType for the enum itself.
302 llvm::DIArray EltArray =
303 DebugFactory.GetOrCreateArray(&Enumerators[0], Enumerators.size());
304
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000305 std::string EnumName = Decl->getNameAsString();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000306 SourceLocation DefLoc = Decl->getLocation();
307 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
308 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner18c8dc02009-01-16 07:36:28 +0000309 unsigned Line = SM.getInstantiationLineNumber(DefLoc);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000310
311 // Size and align of the type.
312 uint64_t Size = M->getContext().getTypeSize(Ty);
Chris Lattner18c8dc02009-01-16 07:36:28 +0000313 unsigned Align = M->getContext().getTypeAlign(Ty);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000314
315 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
316 Unit, EnumName, DefUnit, Line,
317 Size, Align, 0, 0,
318 llvm::DIType(), EltArray);
319}
320
321llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
322 llvm::DICompileUnit Unit) {
323 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
324 return CreateType(RT, Unit);
325 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
326 return CreateType(ET, Unit);
327
328 return llvm::DIType();
329}
330
331llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
332 llvm::DICompileUnit Unit) {
Anders Carlsson86b360e2009-01-05 01:23:29 +0000333 uint64_t Size;
334 uint64_t Align;
335
336
337 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
338
339 Size = 0;
340 Align =
341 M->getContext().getTypeSize(M->getContext().getBaseElementType(VAT));
342 } else {
343 // Size and align of the whole array, not the element type.
344 Size = M->getContext().getTypeSize(Ty);
345 Align = M->getContext().getTypeAlign(Ty);
346 }
Chris Lattner562ce0a2008-11-10 06:08:34 +0000347
348 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
349 // interior arrays, do we care? Why aren't nested arrays represented the
350 // obvious/recursive way?
351 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
352 QualType EltTy(Ty, 0);
353 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000354 uint64_t Upper = 0;
Chris Lattner562ce0a2008-11-10 06:08:34 +0000355 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
356 Upper = CAT->getSize().getZExtValue() - 1;
357 // FIXME: Verify this is right for VLAs.
358 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
359 EltTy = Ty->getElementType();
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000360 }
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000361
Chris Lattner562ce0a2008-11-10 06:08:34 +0000362 llvm::DIArray SubscriptArray =
363 DebugFactory.GetOrCreateArray(&Subscripts[0], Subscripts.size());
364
365 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
366 Unit, "", llvm::DICompileUnit(),
367 0, Size, Align, 0, 0,
368 getOrCreateType(EltTy, Unit),
369 SubscriptArray);
370}
371
372
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000373/// getOrCreateType - Get the type from the cache or create a new
374/// one if necessary.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000375llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
376 llvm::DICompileUnit Unit) {
377 if (Ty.isNull())
378 return llvm::DIType();
379
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000380 // Check to see if the compile unit already has created this type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000381 llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()];
382 if (!Slot.isNull()) return Slot;
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000383
Chris Lattner562ce0a2008-11-10 06:08:34 +0000384 // Handle CVR qualifiers, which recursively handles what they refer to.
385 if (Ty.getCVRQualifiers())
386 return Slot = CreateCVRType(Ty, Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000387
388 // Work out details of type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000389 switch (Ty->getTypeClass()) {
390 case Type::Complex:
391 case Type::Reference:
392 case Type::Vector:
393 case Type::ExtVector:
394 case Type::ASQual:
395 case Type::ObjCInterface:
396 case Type::ObjCQualifiedInterface:
397 case Type::ObjCQualifiedId:
Chris Lattner562ce0a2008-11-10 06:08:34 +0000398 default:
399 return llvm::DIType();
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000400
Chris Lattner562ce0a2008-11-10 06:08:34 +0000401 case Type::Builtin: Slot = CreateType(cast<BuiltinType>(Ty), Unit); break;
402 case Type::Pointer: Slot = CreateType(cast<PointerType>(Ty), Unit); break;
403 case Type::TypeName: Slot = CreateType(cast<TypedefType>(Ty), Unit); break;
404 case Type::Tagged: Slot = CreateType(cast<TagType>(Ty), Unit); break;
405 case Type::FunctionProto:
406 case Type::FunctionNoProto:
Chris Lattnere3afa462008-11-11 07:01:36 +0000407 return Slot = CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000408
409 case Type::ConstantArray:
410 case Type::VariableArray:
411 case Type::IncompleteArray:
Chris Lattnere3afa462008-11-11 07:01:36 +0000412 return Slot = CreateType(cast<ArrayType>(Ty), Unit);
413 case Type::TypeOfExp:
414 return Slot = getOrCreateType(cast<TypeOfExpr>(Ty)->getUnderlyingExpr()
415 ->getType(), Unit);
416 case Type::TypeOfTyp:
417 return Slot = getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(),
418 Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000419 }
Chris Lattner562ce0a2008-11-10 06:08:34 +0000420
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000421 return Slot;
422}
423
424/// EmitFunctionStart - Constructs the debug code for entering a function -
425/// "llvm.dbg.func.start.".
Chris Lattner562ce0a2008-11-10 06:08:34 +0000426void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000427 llvm::Function *Fn,
Chris Lattner562ce0a2008-11-10 06:08:34 +0000428 CGBuilderTy &Builder) {
429 // FIXME: Why is this using CurLoc???
430 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000431 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner18c8dc02009-01-16 07:36:28 +0000432 unsigned LineNo = SM.getInstantiationLineNumber(CurLoc);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000433
434 llvm::DISubprogram SP =
435 DebugFactory.CreateSubprogram(Unit, Name, Name, "", Unit, LineNo,
436 getOrCreateType(ReturnType, Unit),
437 Fn->hasInternalLinkage(), true/*definition*/);
438
439 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
440
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000441 // Push function on region stack.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000442 RegionStack.push_back(SP);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000443}
444
445
Chris Lattner562ce0a2008-11-10 06:08:34 +0000446void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000447 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
448
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000449 // Don't bother if things are the same as last time.
450 SourceManager &SM = M->getContext().getSourceManager();
451 if (CurLoc == PrevLoc
452 || (SM.getLineNumber(CurLoc) == SM.getLineNumber(PrevLoc)
453 && SM.isFromSameFile(CurLoc, PrevLoc)))
454 return;
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000455
456 // Update last state.
457 PrevLoc = CurLoc;
458
459 // Get the appropriate compile unit.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000460 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Chris Lattner18c8dc02009-01-16 07:36:28 +0000461 DebugFactory.InsertStopPoint(Unit, SM.getInstantiationLineNumber(CurLoc),
462 SM.getInstantiationColumnNumber(CurLoc),
Chris Lattner562ce0a2008-11-10 06:08:34 +0000463 Builder.GetInsertBlock());
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000464}
465
466/// EmitRegionStart- Constructs the debug code for entering a declarative
467/// region - "llvm.dbg.region.start.".
Chris Lattner562ce0a2008-11-10 06:08:34 +0000468void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
469 llvm::DIDescriptor D;
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000470 if (!RegionStack.empty())
Chris Lattner562ce0a2008-11-10 06:08:34 +0000471 D = RegionStack.back();
472 D = DebugFactory.CreateBlock(D);
473 RegionStack.push_back(D);
474 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000475}
476
477/// EmitRegionEnd - Constructs the debug code for exiting a declarative
478/// region - "llvm.dbg.region.end."
Chris Lattner562ce0a2008-11-10 06:08:34 +0000479void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000480 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
481
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000482 // Provide an region stop point.
483 EmitStopPoint(Fn, Builder);
484
Chris Lattner562ce0a2008-11-10 06:08:34 +0000485 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000486 RegionStack.pop_back();
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000487}
488
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000489/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000490void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
491 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000492 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
493
Chris Lattner562ce0a2008-11-10 06:08:34 +0000494 // Get location information.
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000495 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner18c8dc02009-01-16 07:36:28 +0000496 unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation());
Chris Lattner562ce0a2008-11-10 06:08:34 +0000497 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
498
499 // Create the descriptor for the variable.
500 llvm::DIVariable D =
Chris Lattner271d4c22008-11-24 05:29:24 +0000501 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner562ce0a2008-11-10 06:08:34 +0000502 Unit, Line,
503 getOrCreateType(Decl->getType(), Unit));
504 // Insert an llvm.dbg.declare into the current block.
505 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000506}
507
Chris Lattner562ce0a2008-11-10 06:08:34 +0000508void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
509 llvm::Value *Storage,
510 CGBuilderTy &Builder) {
511 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
512}
513
514/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
515/// variable declaration.
516void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
517 CGBuilderTy &Builder) {
518 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
519}
520
521
522
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000523/// EmitGlobalVariable - Emit information about a global variable.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000524void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
525 const VarDecl *Decl) {
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000526 // Create global variable debug descriptor.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000527 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000528 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner18c8dc02009-01-16 07:36:28 +0000529 unsigned LineNo = SM.getInstantiationLineNumber(Decl->getLocation());
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000530
531 std::string Name = Decl->getNameAsString();
Anders Carlsson123485c2008-11-26 17:40:42 +0000532
533 QualType T = Decl->getType();
534 if (T->isIncompleteArrayType()) {
535
536 // CodeGen turns int[] into int[1] so we'll do the same here.
537 llvm::APSInt ConstVal(32);
538
539 ConstVal = 1;
540 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
541
542 T = M->getContext().getConstantArrayType(ET, ConstVal,
543 ArrayType::Normal, 0);
544 }
545
Chris Lattner562ce0a2008-11-10 06:08:34 +0000546 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Anders Carlsson123485c2008-11-26 17:40:42 +0000547 getOrCreateType(T, Unit),
Chris Lattner562ce0a2008-11-10 06:08:34 +0000548 Var->hasInternalLinkage(),
549 true/*definition*/, Var);
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000550}
551