blob: ebf403038e07c36ac86f7d752ad0bebac51cc0da [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"
Devang Patel9ca36b62009-02-26 21:10:26 +000017#include "clang/AST/DeclObjC.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
Devang Patel77820222009-02-24 23:16:03 +000055 SourceManager &SM = M->getContext().getSourceManager();
Daniel Dunbar831570c2009-01-22 00:09:25 +000056 if (Loc.isValid()) {
Daniel Dunbar831570c2009-01-22 00:09:25 +000057 Loc = SM.getInstantiationLoc(Loc);
58 FE = SM.getFileEntryForID(SM.getFileID(Loc));
Devang Patel77820222009-02-24 23:16:03 +000059 } else {
60 // If Loc is not valid then use main file id.
61 FE = SM.getFileEntryForID(SM.getMainFileID());
Daniel Dunbar831570c2009-01-22 00:09:25 +000062 }
63
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000064 // See if this compile unit has been used before.
Chris Lattner9c85ba32008-11-10 06:08:34 +000065 llvm::DICompileUnit &Unit = CompileUnitCache[FE];
66 if (!Unit.isNull()) return Unit;
67
68 // Get source file information.
Daniel Dunbar831570c2009-01-22 00:09:25 +000069 const char *FileName = FE ? FE->getName() : "<unknown>";
Daniel Dunbarf8e58d02009-02-07 00:40:41 +000070 const char *DirName = FE ? FE->getDir()->getName() : "<unknown>";
Daniel Dunbar2104bf92008-10-24 00:46:51 +000071
Devang Patel4db4c9c2009-03-05 01:55:07 +000072 bool isMain = (FE == SM.getFileEntryForID(SM.getMainFileID()));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000073 // Create new compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +000074 // FIXME: Handle other language IDs as well.
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000075 // FIXME: Do not know how to get clang version yet.
Devang Patelc20482b2009-03-19 00:23:53 +000076 // FIXME: Encode command line options.
77 // FIXME: Encode optimization level.
Chris Lattner9c85ba32008-11-10 06:08:34 +000078 return Unit = DebugFactory.CreateCompileUnit(llvm::dwarf::DW_LANG_C89,
Devang Patel4db4c9c2009-03-05 01:55:07 +000079 FileName, DirName, "clang",
80 isMain);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000081}
82
Devang Patel65e99f22009-02-25 01:36:11 +000083/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +000084/// one if necessary.
85llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel65e99f22009-02-25 01:36:11 +000086 llvm::DICompileUnit Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +000087 unsigned Encoding = 0;
88 switch (BT->getKind()) {
89 default:
90 case BuiltinType::Void:
91 return llvm::DIType();
92 case BuiltinType::UChar:
93 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
94 case BuiltinType::Char_S:
95 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
96 case BuiltinType::UShort:
97 case BuiltinType::UInt:
98 case BuiltinType::ULong:
99 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
100 case BuiltinType::Short:
101 case BuiltinType::Int:
102 case BuiltinType::Long:
103 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
104 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
105 case BuiltinType::Float:
106 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
107 }
108 // Bit size, align and offset of the type.
109 uint64_t Size = M->getContext().getTypeSize(BT);
110 uint64_t Align = M->getContext().getTypeAlign(BT);
111 uint64_t Offset = 0;
112
113 return DebugFactory.CreateBasicType(Unit, BT->getName(), Unit, 0, Size, Align,
114 Offset, /*flags*/ 0, Encoding);
115}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000116
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000117/// getOrCreateCVRType - Get the CVR qualified type from the cache or create
118/// a new one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000119llvm::DIType CGDebugInfo::CreateCVRType(QualType Ty, llvm::DICompileUnit Unit) {
120 // We will create one Derived type for one qualifier and recurse to handle any
121 // additional ones.
122 llvm::DIType FromTy;
123 unsigned Tag;
124 if (Ty.isConstQualified()) {
125 Tag = llvm::dwarf::DW_TAG_const_type;
126 Ty.removeConst();
127 FromTy = getOrCreateType(Ty, Unit);
128 } else if (Ty.isVolatileQualified()) {
129 Tag = llvm::dwarf::DW_TAG_volatile_type;
130 Ty.removeVolatile();
131 FromTy = getOrCreateType(Ty, Unit);
132 } else {
133 assert(Ty.isRestrictQualified() && "Unknown type qualifier for debug info");
134 Tag = llvm::dwarf::DW_TAG_restrict_type;
135 Ty.removeRestrict();
136 FromTy = getOrCreateType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000137 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000138
Daniel Dunbar3845f862008-10-31 03:54:29 +0000139 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
140 // CVR derived types.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000141 return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
142 0, 0, 0, 0, 0, FromTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000143}
144
Chris Lattner9c85ba32008-11-10 06:08:34 +0000145llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
146 llvm::DICompileUnit Unit) {
147 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000148
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000149 // Bit size, align and offset of the type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000150 uint64_t Size = M->getContext().getTypeSize(Ty);
151 uint64_t Align = M->getContext().getTypeAlign(Ty);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000152
Chris Lattner9c85ba32008-11-10 06:08:34 +0000153 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
154 "", llvm::DICompileUnit(),
155 0, Size, Align, 0, 0, EltTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000156}
157
Chris Lattner9c85ba32008-11-10 06:08:34 +0000158llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
159 llvm::DICompileUnit Unit) {
160 // Typedefs are derived from some other type. If we have a typedef of a
161 // typedef, make sure to emit the whole chain.
162 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
163
164 // We don't set size information, but do specify where the typedef was
165 // declared.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000166 std::string TyName = Ty->getDecl()->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000167 SourceLocation DefLoc = Ty->getDecl()->getLocation();
168 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000169
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000170 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000171 uint64_t Line = SM.getInstantiationLineNumber(DefLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000172
Chris Lattner9c85ba32008-11-10 06:08:34 +0000173 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
174 TyName, DefUnit, Line, 0, 0, 0, 0, Src);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000175}
176
Chris Lattner9c85ba32008-11-10 06:08:34 +0000177llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
178 llvm::DICompileUnit Unit) {
179 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000180
Chris Lattner9c85ba32008-11-10 06:08:34 +0000181 // Add the result type at least.
182 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
183
184 // Set up remainder of arguments if there is a prototype.
185 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000186 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000187 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
188 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
189 } else {
190 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000191 }
192
Chris Lattner9c85ba32008-11-10 06:08:34 +0000193 llvm::DIArray EltTypeArray =
194 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
195
196 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
197 Unit, "", llvm::DICompileUnit(),
198 0, 0, 0, 0, 0,
199 llvm::DIType(), EltTypeArray);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000200}
201
Devang Patel65e99f22009-02-25 01:36:11 +0000202/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000203llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
204 llvm::DICompileUnit Unit) {
Douglas Gregora4c46df2008-12-11 17:59:21 +0000205 RecordDecl *Decl = Ty->getDecl();
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000206
Chris Lattner9c85ba32008-11-10 06:08:34 +0000207 unsigned Tag;
208 if (Decl->isStruct())
209 Tag = llvm::dwarf::DW_TAG_structure_type;
210 else if (Decl->isUnion())
211 Tag = llvm::dwarf::DW_TAG_union_type;
212 else {
213 assert(Decl->isClass() && "Unknown RecordType!");
214 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000215 }
216
Sanjiv Gupta507de852008-06-09 10:47:41 +0000217 SourceManager &SM = M->getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000218
Chris Lattner9c85ba32008-11-10 06:08:34 +0000219 // Get overall information about the record type for the debug info.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000220 std::string Name = Decl->getNameAsString();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000221
Chris Lattner9c85ba32008-11-10 06:08:34 +0000222 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000223 unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000224
225
226 // Records and classes and unions can all be recursive. To handle them, we
227 // first generate a debug descriptor for the struct as a forward declaration.
228 // Then (if it is a definition) we go through and get debug info for all of
229 // its members. Finally, we create a descriptor for the complete type (which
230 // may refer to the forward decl if the struct is recursive) and replace all
231 // uses of the forward declaration with the final definition.
232 llvm::DIType FwdDecl =
233 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
234 llvm::DIType(), llvm::DIArray());
235
236 // If this is just a forward declaration, return it.
237 if (!Decl->getDefinition(M->getContext()))
238 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000239
Chris Lattner9c85ba32008-11-10 06:08:34 +0000240 // Otherwise, insert it into the TypeCache so that recursive uses will find
241 // it.
242 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
243
244 // Convert all the elements.
245 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
246
247 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl);
248
249 unsigned FieldNo = 0;
Douglas Gregora4c46df2008-12-11 17:59:21 +0000250 for (RecordDecl::field_iterator I = Decl->field_begin(),
251 E = Decl->field_end();
252 I != E; ++I, ++FieldNo) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000253 FieldDecl *Field = *I;
254 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattner8ec03f52008-11-24 03:54:41 +0000255
256 std::string FieldName = Field->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000257
258 // Get the location for the field.
259 SourceLocation FieldDefLoc = Field->getLocation();
260 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000261 unsigned FieldLine = SM.getInstantiationLineNumber(FieldDefLoc);
Devang Patelec9b5d52009-03-16 23:47:53 +0000262
263 QualType FType = Field->getType();
264 uint64_t FieldSize = 0;
265 unsigned FieldAlign = 0;
266 if (!FType->isIncompleteArrayType()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000267
Devang Patelec9b5d52009-03-16 23:47:53 +0000268 // Bit size, align and offset of the type.
269 FieldSize = M->getContext().getTypeSize(FType);
270 Expr *BitWidth = Field->getBitWidth();
271 if (BitWidth)
272 FieldSize =
273 BitWidth->getIntegerConstantExprValue(M->getContext()).getZExtValue();
274
275 FieldAlign = M->getContext().getTypeAlign(FType);
276 }
277
Chris Lattner9c85ba32008-11-10 06:08:34 +0000278 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
279
280 // Create a DW_TAG_member node to remember the offset of this field in the
281 // struct. FIXME: This is an absolutely insane way to capture this
282 // information. When we gut debug info, this should be fixed.
283 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
284 FieldName, FieldDefUnit,
285 FieldLine, FieldSize, FieldAlign,
286 FieldOffset, 0, FieldTy);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000287 EltTys.push_back(FieldTy);
288 }
289
290 llvm::DIArray Elements =
291 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
292
293 // Bit size, align and offset of the type.
294 uint64_t Size = M->getContext().getTypeSize(Ty);
295 uint64_t Align = M->getContext().getTypeAlign(Ty);
296
297 llvm::DIType RealDecl =
298 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
299 Align, 0, 0, llvm::DIType(), Elements);
300
301 // Now that we have a real decl for the struct, replace anything using the
302 // old decl with the new one. This will recursively update the debug info.
303 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
304 FwdDecl.getGV()->eraseFromParent();
305
306 return RealDecl;
307}
308
Devang Patel9ca36b62009-02-26 21:10:26 +0000309/// CreateType - get objective-c interface type.
310llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
311 llvm::DICompileUnit Unit) {
312 ObjCInterfaceDecl *Decl = Ty->getDecl();
313
314 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
315 SourceManager &SM = M->getContext().getSourceManager();
316
317 // Get overall information about the record type for the debug info.
318 std::string Name = Decl->getNameAsString();
319
320 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
321 unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation());
322
323
324 // To handle recursive interface, we
325 // first generate a debug descriptor for the struct as a forward declaration.
326 // Then (if it is a definition) we go through and get debug info for all of
327 // its members. Finally, we create a descriptor for the complete type (which
328 // may refer to the forward decl if the struct is recursive) and replace all
329 // uses of the forward declaration with the final definition.
330 llvm::DIType FwdDecl =
331 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
332 llvm::DIType(), llvm::DIArray());
333
334 // If this is just a forward declaration, return it.
335 if (Decl->isForwardDecl())
336 return FwdDecl;
337
338 // Otherwise, insert it into the TypeCache so that recursive uses will find
339 // it.
340 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
341
342 // Convert all the elements.
343 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
344
Devang Patelfbe899f2009-03-10 21:30:26 +0000345 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
346 if (SClass) {
347 llvm::DIType SClassTy =
348 getOrCreateType(M->getContext().getObjCInterfaceType(SClass), Unit);
349 llvm::DIType InhTag =
350 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
351 Unit, "", Unit, 0, 0, 0,
352 0 /* offset */, 0, SClassTy);
353 EltTys.push_back(InhTag);
354 }
355
Devang Patel9ca36b62009-02-26 21:10:26 +0000356 const ASTRecordLayout &RL = M->getContext().getASTObjCInterfaceLayout(Decl);
357
358 unsigned FieldNo = 0;
359 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
360 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
361 ObjCIvarDecl *Field = *I;
362 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
363
364 std::string FieldName = Field->getNameAsString();
365
366 // Get the location for the field.
367 SourceLocation FieldDefLoc = Field->getLocation();
368 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
369 unsigned FieldLine = SM.getInstantiationLineNumber(FieldDefLoc);
370
371 // Bit size, align and offset of the type.
372 uint64_t FieldSize = M->getContext().getTypeSize(Ty);
373 unsigned FieldAlign = M->getContext().getTypeAlign(Ty);
374 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
Devang Patelc20482b2009-03-19 00:23:53 +0000375
376 unsigned Flags = 0;
377 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
378 Flags = llvm::DIType::FlagProtected;
379 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
380 Flags = llvm::DIType::FlagPrivate;
381
Devang Patel9ca36b62009-02-26 21:10:26 +0000382 // Create a DW_TAG_member node to remember the offset of this field in the
383 // struct. FIXME: This is an absolutely insane way to capture this
384 // information. When we gut debug info, this should be fixed.
385 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
386 FieldName, FieldDefUnit,
387 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000388 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000389 EltTys.push_back(FieldTy);
390 }
391
392 llvm::DIArray Elements =
393 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
394
395 // Bit size, align and offset of the type.
396 uint64_t Size = M->getContext().getTypeSize(Ty);
397 uint64_t Align = M->getContext().getTypeAlign(Ty);
398
399 llvm::DIType RealDecl =
400 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
401 Align, 0, 0, llvm::DIType(), Elements);
402
403 // Now that we have a real decl for the struct, replace anything using the
404 // old decl with the new one. This will recursively update the debug info.
405 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
406 FwdDecl.getGV()->eraseFromParent();
407
408 return RealDecl;
409}
410
Chris Lattner9c85ba32008-11-10 06:08:34 +0000411llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
412 llvm::DICompileUnit Unit) {
413 EnumDecl *Decl = Ty->getDecl();
414
415 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
416
417 // Create DIEnumerator elements for each enumerator.
Douglas Gregor44b43212008-12-11 16:49:14 +0000418 for (EnumDecl::enumerator_iterator Enum = Decl->enumerator_begin(),
419 EnumEnd = Decl->enumerator_end();
420 Enum != EnumEnd; ++Enum) {
421 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(),
422 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000423 }
424
425 // Return a CompositeType for the enum itself.
426 llvm::DIArray EltArray =
427 DebugFactory.GetOrCreateArray(&Enumerators[0], Enumerators.size());
428
Chris Lattner8ec03f52008-11-24 03:54:41 +0000429 std::string EnumName = Decl->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000430 SourceLocation DefLoc = Decl->getLocation();
431 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
432 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000433 unsigned Line = SM.getInstantiationLineNumber(DefLoc);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000434
435 // Size and align of the type.
436 uint64_t Size = M->getContext().getTypeSize(Ty);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000437 unsigned Align = M->getContext().getTypeAlign(Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000438
439 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
440 Unit, EnumName, DefUnit, Line,
441 Size, Align, 0, 0,
442 llvm::DIType(), EltArray);
443}
444
445llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
446 llvm::DICompileUnit Unit) {
447 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
448 return CreateType(RT, Unit);
449 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
450 return CreateType(ET, Unit);
451
452 return llvm::DIType();
453}
454
455llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
456 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000457 uint64_t Size;
458 uint64_t Align;
459
460
Nuno Lopes010d5142009-01-28 00:35:17 +0000461 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +0000462 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000463 Size = 0;
464 Align =
Nuno Lopes010d5142009-01-28 00:35:17 +0000465 M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT));
466 } else if (Ty->isIncompleteArrayType()) {
467 Size = 0;
468 Align = M->getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +0000469 } else {
470 // Size and align of the whole array, not the element type.
471 Size = M->getContext().getTypeSize(Ty);
472 Align = M->getContext().getTypeAlign(Ty);
473 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000474
475 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
476 // interior arrays, do we care? Why aren't nested arrays represented the
477 // obvious/recursive way?
478 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
479 QualType EltTy(Ty, 0);
480 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000481 uint64_t Upper = 0;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000482 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
483 Upper = CAT->getSize().getZExtValue() - 1;
484 // FIXME: Verify this is right for VLAs.
485 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
486 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000487 }
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000488
Chris Lattner9c85ba32008-11-10 06:08:34 +0000489 llvm::DIArray SubscriptArray =
490 DebugFactory.GetOrCreateArray(&Subscripts[0], Subscripts.size());
491
492 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
493 Unit, "", llvm::DICompileUnit(),
494 0, Size, Align, 0, 0,
495 getOrCreateType(EltTy, Unit),
496 SubscriptArray);
497}
498
499
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000500/// getOrCreateType - Get the type from the cache or create a new
501/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000502llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
503 llvm::DICompileUnit Unit) {
504 if (Ty.isNull())
505 return llvm::DIType();
506
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000507 // Check to see if the compile unit already has created this type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000508 llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()];
509 if (!Slot.isNull()) return Slot;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000510
Chris Lattner9c85ba32008-11-10 06:08:34 +0000511 // Handle CVR qualifiers, which recursively handles what they refer to.
512 if (Ty.getCVRQualifiers())
513 return Slot = CreateCVRType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000514
515 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000516 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000517#define TYPE(Class, Base)
518#define ABSTRACT_TYPE(Class, Base)
519#define NON_CANONICAL_TYPE(Class, Base)
520#define DEPENDENT_TYPE(Class, Base) case Type::Class:
521#include "clang/AST/TypeNodes.def"
522 assert(false && "Dependent types cannot show up in debug information");
523
Chris Lattner9c85ba32008-11-10 06:08:34 +0000524 case Type::Complex:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000525 case Type::LValueReference:
526 case Type::RValueReference:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000527 case Type::Vector:
528 case Type::ExtVector:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000529 case Type::ExtQual:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000530 case Type::ObjCQualifiedInterface:
531 case Type::ObjCQualifiedId:
Eli Friedman00524e32009-02-27 23:15:07 +0000532 case Type::FixedWidthInt:
533 case Type::BlockPointer:
534 case Type::MemberPointer:
535 case Type::ClassTemplateSpecialization:
Douglas Gregore4e5b052009-03-19 00:18:19 +0000536 case Type::QualifiedName:
Eli Friedman00524e32009-02-27 23:15:07 +0000537 case Type::ObjCQualifiedClass:
538 // Unsupported types
Chris Lattner9c85ba32008-11-10 06:08:34 +0000539 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000540
Devang Patele7987062009-03-02 17:58:28 +0000541 case Type::ObjCInterface:
542 Slot = CreateType(cast<ObjCInterfaceType>(Ty), Unit); break;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000543 case Type::Builtin: Slot = CreateType(cast<BuiltinType>(Ty), Unit); break;
544 case Type::Pointer: Slot = CreateType(cast<PointerType>(Ty), Unit); break;
Douglas Gregor72564e72009-02-26 23:50:07 +0000545 case Type::Typedef: Slot = CreateType(cast<TypedefType>(Ty), Unit); break;
546 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000547 case Type::Enum:
548 Slot = CreateType(cast<TagType>(Ty), Unit);
549 break;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000550 case Type::FunctionProto:
551 case Type::FunctionNoProto:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000552 return Slot = CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000553
554 case Type::ConstantArray:
555 case Type::VariableArray:
556 case Type::IncompleteArray:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000557 return Slot = CreateType(cast<ArrayType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000558 case Type::TypeOfExpr:
559 return Slot = getOrCreateType(cast<TypeOfExprType>(Ty)->getUnderlyingExpr()
Chris Lattner3cc5c402008-11-11 07:01:36 +0000560 ->getType(), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000561 case Type::TypeOf:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000562 return Slot = getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(),
563 Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000564 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000565
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000566 return Slot;
567}
568
569/// EmitFunctionStart - Constructs the debug code for entering a function -
570/// "llvm.dbg.func.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000571void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000572 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000573 CGBuilderTy &Builder) {
574 // FIXME: Why is this using CurLoc???
575 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000576 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000577 unsigned LineNo = SM.getInstantiationLineNumber(CurLoc);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000578
579 llvm::DISubprogram SP =
580 DebugFactory.CreateSubprogram(Unit, Name, Name, "", Unit, LineNo,
581 getOrCreateType(ReturnType, Unit),
582 Fn->hasInternalLinkage(), true/*definition*/);
583
584 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
585
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000586 // Push function on region stack.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000587 RegionStack.push_back(SP);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000588}
589
590
Chris Lattner9c85ba32008-11-10 06:08:34 +0000591void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000592 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
593
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000594 // Don't bother if things are the same as last time.
595 SourceManager &SM = M->getContext().getSourceManager();
596 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +0000597 || (SM.getInstantiationLineNumber(CurLoc) ==
598 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000599 && SM.isFromSameFile(CurLoc, PrevLoc)))
600 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000601
602 // Update last state.
603 PrevLoc = CurLoc;
604
605 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000606 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000607 DebugFactory.InsertStopPoint(Unit, SM.getInstantiationLineNumber(CurLoc),
608 SM.getInstantiationColumnNumber(CurLoc),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000609 Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000610}
611
612/// EmitRegionStart- Constructs the debug code for entering a declarative
613/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000614void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
615 llvm::DIDescriptor D;
Daniel Dunbar5273f512008-10-17 01:07:56 +0000616 if (!RegionStack.empty())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000617 D = RegionStack.back();
618 D = DebugFactory.CreateBlock(D);
619 RegionStack.push_back(D);
620 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000621}
622
623/// EmitRegionEnd - Constructs the debug code for exiting a declarative
624/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +0000625void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000626 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
627
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000628 // Provide an region stop point.
629 EmitStopPoint(Fn, Builder);
630
Chris Lattner9c85ba32008-11-10 06:08:34 +0000631 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000632 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000633}
634
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000635/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000636void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
637 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000638 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
639
Chris Lattner9c85ba32008-11-10 06:08:34 +0000640 // Get location information.
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000641 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000642 unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000643 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
644
645 // Create the descriptor for the variable.
646 llvm::DIVariable D =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000647 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000648 Unit, Line,
649 getOrCreateType(Decl->getType(), Unit));
650 // Insert an llvm.dbg.declare into the current block.
651 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000652}
653
Chris Lattner9c85ba32008-11-10 06:08:34 +0000654void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
655 llvm::Value *Storage,
656 CGBuilderTy &Builder) {
657 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
658}
659
660/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
661/// variable declaration.
662void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
663 CGBuilderTy &Builder) {
664 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
665}
666
667
668
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000669/// EmitGlobalVariable - Emit information about a global variable.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000670void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
671 const VarDecl *Decl) {
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000672 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000673 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000674 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000675 unsigned LineNo = SM.getInstantiationLineNumber(Decl->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +0000676
677 std::string Name = Decl->getNameAsString();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000678
679 QualType T = Decl->getType();
680 if (T->isIncompleteArrayType()) {
681
682 // CodeGen turns int[] into int[1] so we'll do the same here.
683 llvm::APSInt ConstVal(32);
684
685 ConstVal = 1;
686 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
687
688 T = M->getContext().getConstantArrayType(ET, ConstVal,
689 ArrayType::Normal, 0);
690 }
691
Chris Lattner9c85ba32008-11-10 06:08:34 +0000692 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000693 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000694 Var->hasInternalLinkage(),
695 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000696}
697
Devang Patel9ca36b62009-02-26 21:10:26 +0000698/// EmitGlobalVariable - Emit information about an objective-c interface.
699void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
700 ObjCInterfaceDecl *Decl) {
701 // Create global variable debug descriptor.
702 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
703 SourceManager &SM = M->getContext().getSourceManager();
704 unsigned LineNo = SM.getInstantiationLineNumber(Decl->getLocation());
705
706 std::string Name = Decl->getNameAsString();
707
708 QualType T = M->getContext().buildObjCInterfaceType(Decl);
709 if (T->isIncompleteArrayType()) {
710
711 // CodeGen turns int[] into int[1] so we'll do the same here.
712 llvm::APSInt ConstVal(32);
713
714 ConstVal = 1;
715 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
716
717 T = M->getContext().getConstantArrayType(ET, ConstVal,
718 ArrayType::Normal, 0);
719 }
720
721 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
722 getOrCreateType(T, Unit),
723 Var->hasInternalLinkage(),
724 true/*definition*/, Var);
725}
726