blob: 79abc340644aa803cb812f5cd09e500e0f7bda18 [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())
Chris Lattnerf7cf85b2009-01-16 07:36:28 +000044 CurLoc = M->getContext().getSourceManager().getInstantiationLoc(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 Dunbar831570c2009-01-22 00:09:25 +000050 // FIXME: Until we do a complete job of emitting debug information,
51 // we need to support making dummy compile units so that we generate
52 // "well formed" debug info.
53 const FileEntry *FE = 0;
Daniel Dunbar25f51dd2008-10-24 08:38:36 +000054
Daniel Dunbar831570c2009-01-22 00:09:25 +000055 if (Loc.isValid()) {
56 SourceManager &SM = M->getContext().getSourceManager();
57 Loc = SM.getInstantiationLoc(Loc);
58 FE = SM.getFileEntryForID(SM.getFileID(Loc));
59 }
60
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000061 // See if this compile unit has been used before.
Chris Lattner9c85ba32008-11-10 06:08:34 +000062 llvm::DICompileUnit &Unit = CompileUnitCache[FE];
63 if (!Unit.isNull()) return Unit;
64
65 // Get source file information.
Daniel Dunbar831570c2009-01-22 00:09:25 +000066 const char *FileName = FE ? FE->getName() : "<unknown>";
67 const char *DirName = FE ? FE->getDir()->getName() : "";
Daniel Dunbar2104bf92008-10-24 00:46:51 +000068
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000069 // Create new compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +000070 // FIXME: Handle other language IDs as well.
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000071 // FIXME: Do not know how to get clang version yet.
Chris Lattner9c85ba32008-11-10 06:08:34 +000072 return Unit = DebugFactory.CreateCompileUnit(llvm::dwarf::DW_LANG_C89,
73 FileName, DirName, "clang");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000074}
75
Chris Lattner9c85ba32008-11-10 06:08:34 +000076/// getOrCreateBuiltinType - Get the Basic type from the cache or create a new
77/// one if necessary.
78llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
79 llvm::DICompileUnit Unit){
80 unsigned Encoding = 0;
81 switch (BT->getKind()) {
82 default:
83 case BuiltinType::Void:
84 return llvm::DIType();
85 case BuiltinType::UChar:
86 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
87 case BuiltinType::Char_S:
88 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
89 case BuiltinType::UShort:
90 case BuiltinType::UInt:
91 case BuiltinType::ULong:
92 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
93 case BuiltinType::Short:
94 case BuiltinType::Int:
95 case BuiltinType::Long:
96 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
97 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
98 case BuiltinType::Float:
99 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
100 }
101 // Bit size, align and offset of the type.
102 uint64_t Size = M->getContext().getTypeSize(BT);
103 uint64_t Align = M->getContext().getTypeAlign(BT);
104 uint64_t Offset = 0;
105
106 return DebugFactory.CreateBasicType(Unit, BT->getName(), Unit, 0, Size, Align,
107 Offset, /*flags*/ 0, Encoding);
108}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000109
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000110/// getOrCreateCVRType - Get the CVR qualified type from the cache or create
111/// a new one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000112llvm::DIType CGDebugInfo::CreateCVRType(QualType Ty, llvm::DICompileUnit Unit) {
113 // We will create one Derived type for one qualifier and recurse to handle any
114 // additional ones.
115 llvm::DIType FromTy;
116 unsigned Tag;
117 if (Ty.isConstQualified()) {
118 Tag = llvm::dwarf::DW_TAG_const_type;
119 Ty.removeConst();
120 FromTy = getOrCreateType(Ty, Unit);
121 } else if (Ty.isVolatileQualified()) {
122 Tag = llvm::dwarf::DW_TAG_volatile_type;
123 Ty.removeVolatile();
124 FromTy = getOrCreateType(Ty, Unit);
125 } else {
126 assert(Ty.isRestrictQualified() && "Unknown type qualifier for debug info");
127 Tag = llvm::dwarf::DW_TAG_restrict_type;
128 Ty.removeRestrict();
129 FromTy = getOrCreateType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000130 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000131
Daniel Dunbar3845f862008-10-31 03:54:29 +0000132 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
133 // CVR derived types.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000134 return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
135 0, 0, 0, 0, 0, FromTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000136}
137
Chris Lattner9c85ba32008-11-10 06:08:34 +0000138llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
139 llvm::DICompileUnit Unit) {
140 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000141
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000142 // Bit size, align and offset of the type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000143 uint64_t Size = M->getContext().getTypeSize(Ty);
144 uint64_t Align = M->getContext().getTypeAlign(Ty);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000145
Chris Lattner9c85ba32008-11-10 06:08:34 +0000146 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
147 "", llvm::DICompileUnit(),
148 0, Size, Align, 0, 0, EltTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000149}
150
Chris Lattner9c85ba32008-11-10 06:08:34 +0000151llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
152 llvm::DICompileUnit Unit) {
153 // Typedefs are derived from some other type. If we have a typedef of a
154 // typedef, make sure to emit the whole chain.
155 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
156
157 // We don't set size information, but do specify where the typedef was
158 // declared.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000159 std::string TyName = Ty->getDecl()->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000160 SourceLocation DefLoc = Ty->getDecl()->getLocation();
161 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000162
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000163 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000164 uint64_t Line = SM.getInstantiationLineNumber(DefLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000165
Chris Lattner9c85ba32008-11-10 06:08:34 +0000166 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
167 TyName, DefUnit, Line, 0, 0, 0, 0, Src);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000168}
169
Chris Lattner9c85ba32008-11-10 06:08:34 +0000170llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
171 llvm::DICompileUnit Unit) {
172 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000173
Chris Lattner9c85ba32008-11-10 06:08:34 +0000174 // Add the result type at least.
175 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
176
177 // Set up remainder of arguments if there is a prototype.
178 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
179 if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(Ty)) {
180 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
181 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
182 } else {
183 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000184 }
185
Chris Lattner9c85ba32008-11-10 06:08:34 +0000186 llvm::DIArray EltTypeArray =
187 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
188
189 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
190 Unit, "", llvm::DICompileUnit(),
191 0, 0, 0, 0, 0,
192 llvm::DIType(), EltTypeArray);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000193}
194
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000195/// getOrCreateRecordType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000196llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
197 llvm::DICompileUnit Unit) {
Douglas Gregora4c46df2008-12-11 17:59:21 +0000198 RecordDecl *Decl = Ty->getDecl();
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000199
Chris Lattner9c85ba32008-11-10 06:08:34 +0000200 unsigned Tag;
201 if (Decl->isStruct())
202 Tag = llvm::dwarf::DW_TAG_structure_type;
203 else if (Decl->isUnion())
204 Tag = llvm::dwarf::DW_TAG_union_type;
205 else {
206 assert(Decl->isClass() && "Unknown RecordType!");
207 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000208 }
209
Sanjiv Gupta507de852008-06-09 10:47:41 +0000210 SourceManager &SM = M->getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000211
Chris Lattner9c85ba32008-11-10 06:08:34 +0000212 // Get overall information about the record type for the debug info.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000213 std::string Name = Decl->getNameAsString();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000214
Chris Lattner9c85ba32008-11-10 06:08:34 +0000215 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000216 unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000217
218
219 // Records and classes and unions can all be recursive. To handle them, we
220 // first generate a debug descriptor for the struct as a forward declaration.
221 // Then (if it is a definition) we go through and get debug info for all of
222 // its members. Finally, we create a descriptor for the complete type (which
223 // may refer to the forward decl if the struct is recursive) and replace all
224 // uses of the forward declaration with the final definition.
225 llvm::DIType FwdDecl =
226 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
227 llvm::DIType(), llvm::DIArray());
228
229 // If this is just a forward declaration, return it.
230 if (!Decl->getDefinition(M->getContext()))
231 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000232
Chris Lattner9c85ba32008-11-10 06:08:34 +0000233 // Otherwise, insert it into the TypeCache so that recursive uses will find
234 // it.
235 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
236
237 // Convert all the elements.
238 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
239
240 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl);
241
242 unsigned FieldNo = 0;
Douglas Gregora4c46df2008-12-11 17:59:21 +0000243 for (RecordDecl::field_iterator I = Decl->field_begin(),
244 E = Decl->field_end();
245 I != E; ++I, ++FieldNo) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000246 FieldDecl *Field = *I;
247 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattner8ec03f52008-11-24 03:54:41 +0000248
249 std::string FieldName = Field->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000250
251 // Get the location for the field.
252 SourceLocation FieldDefLoc = Field->getLocation();
253 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000254 unsigned FieldLine = SM.getInstantiationLineNumber(FieldDefLoc);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000255
256 // Bit size, align and offset of the type.
257 uint64_t FieldSize = M->getContext().getTypeSize(Ty);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000258 unsigned FieldAlign = M->getContext().getTypeAlign(Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000259 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);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000268 EltTys.push_back(FieldTy);
269 }
270
271 llvm::DIArray Elements =
272 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
273
274 // Bit size, align and offset of the type.
275 uint64_t Size = M->getContext().getTypeSize(Ty);
276 uint64_t Align = M->getContext().getTypeAlign(Ty);
277
278 llvm::DIType RealDecl =
279 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
280 Align, 0, 0, llvm::DIType(), Elements);
281
282 // Now that we have a real decl for the struct, replace anything using the
283 // old decl with the new one. This will recursively update the debug info.
284 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
285 FwdDecl.getGV()->eraseFromParent();
286
287 return RealDecl;
288}
289
290llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
291 llvm::DICompileUnit Unit) {
292 EnumDecl *Decl = Ty->getDecl();
293
294 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
295
296 // Create DIEnumerator elements for each enumerator.
Douglas Gregor44b43212008-12-11 16:49:14 +0000297 for (EnumDecl::enumerator_iterator Enum = Decl->enumerator_begin(),
298 EnumEnd = Decl->enumerator_end();
299 Enum != EnumEnd; ++Enum) {
300 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(),
301 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000302 }
303
304 // Return a CompositeType for the enum itself.
305 llvm::DIArray EltArray =
306 DebugFactory.GetOrCreateArray(&Enumerators[0], Enumerators.size());
307
Chris Lattner8ec03f52008-11-24 03:54:41 +0000308 std::string EnumName = Decl->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000309 SourceLocation DefLoc = Decl->getLocation();
310 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
311 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000312 unsigned Line = SM.getInstantiationLineNumber(DefLoc);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000313
314 // Size and align of the type.
315 uint64_t Size = M->getContext().getTypeSize(Ty);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000316 unsigned Align = M->getContext().getTypeAlign(Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000317
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) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000336 uint64_t Size;
337 uint64_t Align;
338
339
Nuno Lopes010d5142009-01-28 00:35:17 +0000340 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +0000341 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000342 Size = 0;
343 Align =
Nuno Lopes010d5142009-01-28 00:35:17 +0000344 M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT));
345 } else if (Ty->isIncompleteArrayType()) {
346 Size = 0;
347 Align = M->getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +0000348 } else {
349 // Size and align of the whole array, not the element type.
350 Size = M->getContext().getTypeSize(Ty);
351 Align = M->getContext().getTypeAlign(Ty);
352 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000353
354 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
355 // interior arrays, do we care? Why aren't nested arrays represented the
356 // obvious/recursive way?
357 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
358 QualType EltTy(Ty, 0);
359 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000360 uint64_t Upper = 0;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000361 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
362 Upper = CAT->getSize().getZExtValue() - 1;
363 // FIXME: Verify this is right for VLAs.
364 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
365 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000366 }
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000367
Chris Lattner9c85ba32008-11-10 06:08:34 +0000368 llvm::DIArray SubscriptArray =
369 DebugFactory.GetOrCreateArray(&Subscripts[0], Subscripts.size());
370
371 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
372 Unit, "", llvm::DICompileUnit(),
373 0, Size, Align, 0, 0,
374 getOrCreateType(EltTy, Unit),
375 SubscriptArray);
376}
377
378
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000379/// getOrCreateType - Get the type from the cache or create a new
380/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000381llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
382 llvm::DICompileUnit Unit) {
383 if (Ty.isNull())
384 return llvm::DIType();
385
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000386 // Check to see if the compile unit already has created this type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000387 llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()];
388 if (!Slot.isNull()) return Slot;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000389
Chris Lattner9c85ba32008-11-10 06:08:34 +0000390 // Handle CVR qualifiers, which recursively handles what they refer to.
391 if (Ty.getCVRQualifiers())
392 return Slot = CreateCVRType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000393
394 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000395 switch (Ty->getTypeClass()) {
396 case Type::Complex:
397 case Type::Reference:
398 case Type::Vector:
399 case Type::ExtVector:
400 case Type::ASQual:
401 case Type::ObjCInterface:
402 case Type::ObjCQualifiedInterface:
403 case Type::ObjCQualifiedId:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000404 default:
405 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000406
Chris Lattner9c85ba32008-11-10 06:08:34 +0000407 case Type::Builtin: Slot = CreateType(cast<BuiltinType>(Ty), Unit); break;
408 case Type::Pointer: Slot = CreateType(cast<PointerType>(Ty), Unit); break;
409 case Type::TypeName: Slot = CreateType(cast<TypedefType>(Ty), Unit); break;
410 case Type::Tagged: Slot = CreateType(cast<TagType>(Ty), Unit); break;
411 case Type::FunctionProto:
412 case Type::FunctionNoProto:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000413 return Slot = CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000414
415 case Type::ConstantArray:
416 case Type::VariableArray:
417 case Type::IncompleteArray:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000418 return Slot = CreateType(cast<ArrayType>(Ty), Unit);
419 case Type::TypeOfExp:
420 return Slot = getOrCreateType(cast<TypeOfExpr>(Ty)->getUnderlyingExpr()
421 ->getType(), Unit);
422 case Type::TypeOfTyp:
423 return Slot = getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(),
424 Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000425 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000426
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000427 return Slot;
428}
429
430/// EmitFunctionStart - Constructs the debug code for entering a function -
431/// "llvm.dbg.func.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000432void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000433 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000434 CGBuilderTy &Builder) {
435 // FIXME: Why is this using CurLoc???
436 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000437 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000438 unsigned LineNo = SM.getInstantiationLineNumber(CurLoc);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000439
440 llvm::DISubprogram SP =
441 DebugFactory.CreateSubprogram(Unit, Name, Name, "", Unit, LineNo,
442 getOrCreateType(ReturnType, Unit),
443 Fn->hasInternalLinkage(), true/*definition*/);
444
445 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
446
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000447 // Push function on region stack.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000448 RegionStack.push_back(SP);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000449}
450
451
Chris Lattner9c85ba32008-11-10 06:08:34 +0000452void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000453 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
454
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000455 // Don't bother if things are the same as last time.
456 SourceManager &SM = M->getContext().getSourceManager();
457 if (CurLoc == PrevLoc
458 || (SM.getLineNumber(CurLoc) == SM.getLineNumber(PrevLoc)
459 && SM.isFromSameFile(CurLoc, PrevLoc)))
460 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000461
462 // Update last state.
463 PrevLoc = CurLoc;
464
465 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000466 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000467 DebugFactory.InsertStopPoint(Unit, SM.getInstantiationLineNumber(CurLoc),
468 SM.getInstantiationColumnNumber(CurLoc),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000469 Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000470}
471
472/// EmitRegionStart- Constructs the debug code for entering a declarative
473/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000474void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
475 llvm::DIDescriptor D;
Daniel Dunbar5273f512008-10-17 01:07:56 +0000476 if (!RegionStack.empty())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000477 D = RegionStack.back();
478 D = DebugFactory.CreateBlock(D);
479 RegionStack.push_back(D);
480 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000481}
482
483/// EmitRegionEnd - Constructs the debug code for exiting a declarative
484/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +0000485void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000486 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
487
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000488 // Provide an region stop point.
489 EmitStopPoint(Fn, Builder);
490
Chris Lattner9c85ba32008-11-10 06:08:34 +0000491 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000492 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000493}
494
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000495/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000496void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
497 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000498 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
499
Chris Lattner9c85ba32008-11-10 06:08:34 +0000500 // Get location information.
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000501 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000502 unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000503 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
504
505 // Create the descriptor for the variable.
506 llvm::DIVariable D =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000507 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000508 Unit, Line,
509 getOrCreateType(Decl->getType(), Unit));
510 // Insert an llvm.dbg.declare into the current block.
511 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000512}
513
Chris Lattner9c85ba32008-11-10 06:08:34 +0000514void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
515 llvm::Value *Storage,
516 CGBuilderTy &Builder) {
517 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
518}
519
520/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
521/// variable declaration.
522void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
523 CGBuilderTy &Builder) {
524 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
525}
526
527
528
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000529/// EmitGlobalVariable - Emit information about a global variable.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000530void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
531 const VarDecl *Decl) {
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000532 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000533 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000534 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000535 unsigned LineNo = SM.getInstantiationLineNumber(Decl->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +0000536
537 std::string Name = Decl->getNameAsString();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000538
539 QualType T = Decl->getType();
540 if (T->isIncompleteArrayType()) {
541
542 // CodeGen turns int[] into int[1] so we'll do the same here.
543 llvm::APSInt ConstVal(32);
544
545 ConstVal = 1;
546 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
547
548 T = M->getContext().getConstantArrayType(ET, ConstVal,
549 ArrayType::Normal, 0);
550 }
551
Chris Lattner9c85ba32008-11-10 06:08:34 +0000552 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000553 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000554 Var->hasInternalLinkage(),
555 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000556}
557