blob: c349e4863a69ecd970760bef3aeff813d24772ee [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);
Devang Patel99c20eb2009-03-20 18:24:39 +0000370
371 QualType FType = Field->getType();
372 uint64_t FieldSize = 0;
373 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000374
Devang Patel99c20eb2009-03-20 18:24:39 +0000375 if (!FType->isIncompleteArrayType()) {
376
377 // Bit size, align and offset of the type.
378 FieldSize = M->getContext().getTypeSize(FType);
379 Expr *BitWidth = Field->getBitWidth();
380 if (BitWidth)
381 FieldSize =
382 BitWidth->getIntegerConstantExprValue(M->getContext()).getZExtValue();
383
384 FieldAlign = M->getContext().getTypeAlign(FType);
385 }
386
387 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
388
Devang Patelc20482b2009-03-19 00:23:53 +0000389 unsigned Flags = 0;
390 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
391 Flags = llvm::DIType::FlagProtected;
392 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
393 Flags = llvm::DIType::FlagPrivate;
394
Devang Patel9ca36b62009-02-26 21:10:26 +0000395 // Create a DW_TAG_member node to remember the offset of this field in the
396 // struct. FIXME: This is an absolutely insane way to capture this
397 // information. When we gut debug info, this should be fixed.
398 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
399 FieldName, FieldDefUnit,
400 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000401 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000402 EltTys.push_back(FieldTy);
403 }
404
405 llvm::DIArray Elements =
406 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
407
408 // Bit size, align and offset of the type.
409 uint64_t Size = M->getContext().getTypeSize(Ty);
410 uint64_t Align = M->getContext().getTypeAlign(Ty);
411
412 llvm::DIType RealDecl =
413 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
414 Align, 0, 0, llvm::DIType(), Elements);
415
416 // Now that we have a real decl for the struct, replace anything using the
417 // old decl with the new one. This will recursively update the debug info.
418 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
419 FwdDecl.getGV()->eraseFromParent();
420
421 return RealDecl;
422}
423
Chris Lattner9c85ba32008-11-10 06:08:34 +0000424llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
425 llvm::DICompileUnit Unit) {
426 EnumDecl *Decl = Ty->getDecl();
427
428 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
429
430 // Create DIEnumerator elements for each enumerator.
Douglas Gregor44b43212008-12-11 16:49:14 +0000431 for (EnumDecl::enumerator_iterator Enum = Decl->enumerator_begin(),
432 EnumEnd = Decl->enumerator_end();
433 Enum != EnumEnd; ++Enum) {
434 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(),
435 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000436 }
437
438 // Return a CompositeType for the enum itself.
439 llvm::DIArray EltArray =
440 DebugFactory.GetOrCreateArray(&Enumerators[0], Enumerators.size());
441
Chris Lattner8ec03f52008-11-24 03:54:41 +0000442 std::string EnumName = Decl->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000443 SourceLocation DefLoc = Decl->getLocation();
444 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
445 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000446 unsigned Line = SM.getInstantiationLineNumber(DefLoc);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000447
448 // Size and align of the type.
449 uint64_t Size = M->getContext().getTypeSize(Ty);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000450 unsigned Align = M->getContext().getTypeAlign(Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000451
452 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
453 Unit, EnumName, DefUnit, Line,
454 Size, Align, 0, 0,
455 llvm::DIType(), EltArray);
456}
457
458llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
459 llvm::DICompileUnit Unit) {
460 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
461 return CreateType(RT, Unit);
462 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
463 return CreateType(ET, Unit);
464
465 return llvm::DIType();
466}
467
468llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
469 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000470 uint64_t Size;
471 uint64_t Align;
472
473
Nuno Lopes010d5142009-01-28 00:35:17 +0000474 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +0000475 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000476 Size = 0;
477 Align =
Nuno Lopes010d5142009-01-28 00:35:17 +0000478 M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT));
479 } else if (Ty->isIncompleteArrayType()) {
480 Size = 0;
481 Align = M->getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +0000482 } else {
483 // Size and align of the whole array, not the element type.
484 Size = M->getContext().getTypeSize(Ty);
485 Align = M->getContext().getTypeAlign(Ty);
486 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000487
488 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
489 // interior arrays, do we care? Why aren't nested arrays represented the
490 // obvious/recursive way?
491 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
492 QualType EltTy(Ty, 0);
493 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000494 uint64_t Upper = 0;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000495 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
496 Upper = CAT->getSize().getZExtValue() - 1;
497 // FIXME: Verify this is right for VLAs.
498 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
499 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000500 }
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000501
Chris Lattner9c85ba32008-11-10 06:08:34 +0000502 llvm::DIArray SubscriptArray =
503 DebugFactory.GetOrCreateArray(&Subscripts[0], Subscripts.size());
504
505 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
506 Unit, "", llvm::DICompileUnit(),
507 0, Size, Align, 0, 0,
508 getOrCreateType(EltTy, Unit),
509 SubscriptArray);
510}
511
512
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000513/// getOrCreateType - Get the type from the cache or create a new
514/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000515llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
516 llvm::DICompileUnit Unit) {
517 if (Ty.isNull())
518 return llvm::DIType();
519
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000520 // Check to see if the compile unit already has created this type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000521 llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()];
522 if (!Slot.isNull()) return Slot;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000523
Chris Lattner9c85ba32008-11-10 06:08:34 +0000524 // Handle CVR qualifiers, which recursively handles what they refer to.
525 if (Ty.getCVRQualifiers())
526 return Slot = CreateCVRType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000527
528 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000529 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000530#define TYPE(Class, Base)
531#define ABSTRACT_TYPE(Class, Base)
532#define NON_CANONICAL_TYPE(Class, Base)
533#define DEPENDENT_TYPE(Class, Base) case Type::Class:
534#include "clang/AST/TypeNodes.def"
535 assert(false && "Dependent types cannot show up in debug information");
536
Chris Lattner9c85ba32008-11-10 06:08:34 +0000537 case Type::Complex:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000538 case Type::LValueReference:
539 case Type::RValueReference:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000540 case Type::Vector:
541 case Type::ExtVector:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000542 case Type::ExtQual:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000543 case Type::ObjCQualifiedInterface:
544 case Type::ObjCQualifiedId:
Eli Friedman00524e32009-02-27 23:15:07 +0000545 case Type::FixedWidthInt:
546 case Type::BlockPointer:
547 case Type::MemberPointer:
548 case Type::ClassTemplateSpecialization:
Douglas Gregore4e5b052009-03-19 00:18:19 +0000549 case Type::QualifiedName:
Eli Friedman00524e32009-02-27 23:15:07 +0000550 case Type::ObjCQualifiedClass:
551 // Unsupported types
Chris Lattner9c85ba32008-11-10 06:08:34 +0000552 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000553
Devang Patele7987062009-03-02 17:58:28 +0000554 case Type::ObjCInterface:
555 Slot = CreateType(cast<ObjCInterfaceType>(Ty), Unit); break;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000556 case Type::Builtin: Slot = CreateType(cast<BuiltinType>(Ty), Unit); break;
557 case Type::Pointer: Slot = CreateType(cast<PointerType>(Ty), Unit); break;
Douglas Gregor72564e72009-02-26 23:50:07 +0000558 case Type::Typedef: Slot = CreateType(cast<TypedefType>(Ty), Unit); break;
559 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000560 case Type::Enum:
561 Slot = CreateType(cast<TagType>(Ty), Unit);
562 break;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000563 case Type::FunctionProto:
564 case Type::FunctionNoProto:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000565 return Slot = CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000566
567 case Type::ConstantArray:
568 case Type::VariableArray:
569 case Type::IncompleteArray:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000570 return Slot = CreateType(cast<ArrayType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000571 case Type::TypeOfExpr:
572 return Slot = getOrCreateType(cast<TypeOfExprType>(Ty)->getUnderlyingExpr()
Chris Lattner3cc5c402008-11-11 07:01:36 +0000573 ->getType(), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000574 case Type::TypeOf:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000575 return Slot = getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(),
576 Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000577 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000578
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000579 return Slot;
580}
581
582/// EmitFunctionStart - Constructs the debug code for entering a function -
583/// "llvm.dbg.func.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000584void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000585 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000586 CGBuilderTy &Builder) {
587 // FIXME: Why is this using CurLoc???
588 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000589 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000590 unsigned LineNo = SM.getInstantiationLineNumber(CurLoc);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000591
592 llvm::DISubprogram SP =
593 DebugFactory.CreateSubprogram(Unit, Name, Name, "", Unit, LineNo,
594 getOrCreateType(ReturnType, Unit),
595 Fn->hasInternalLinkage(), true/*definition*/);
596
597 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
598
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000599 // Push function on region stack.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000600 RegionStack.push_back(SP);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000601}
602
603
Chris Lattner9c85ba32008-11-10 06:08:34 +0000604void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000605 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
606
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000607 // Don't bother if things are the same as last time.
608 SourceManager &SM = M->getContext().getSourceManager();
609 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +0000610 || (SM.getInstantiationLineNumber(CurLoc) ==
611 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000612 && SM.isFromSameFile(CurLoc, PrevLoc)))
613 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000614
615 // Update last state.
616 PrevLoc = CurLoc;
617
618 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000619 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000620 DebugFactory.InsertStopPoint(Unit, SM.getInstantiationLineNumber(CurLoc),
621 SM.getInstantiationColumnNumber(CurLoc),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000622 Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000623}
624
625/// EmitRegionStart- Constructs the debug code for entering a declarative
626/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000627void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
628 llvm::DIDescriptor D;
Daniel Dunbar5273f512008-10-17 01:07:56 +0000629 if (!RegionStack.empty())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000630 D = RegionStack.back();
631 D = DebugFactory.CreateBlock(D);
632 RegionStack.push_back(D);
633 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000634}
635
636/// EmitRegionEnd - Constructs the debug code for exiting a declarative
637/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +0000638void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000639 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
640
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000641 // Provide an region stop point.
642 EmitStopPoint(Fn, Builder);
643
Chris Lattner9c85ba32008-11-10 06:08:34 +0000644 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000645 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000646}
647
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000648/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000649void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
650 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000651 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
652
Chris Lattner9c85ba32008-11-10 06:08:34 +0000653 // Get location information.
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000654 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000655 unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000656 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
657
658 // Create the descriptor for the variable.
659 llvm::DIVariable D =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000660 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000661 Unit, Line,
662 getOrCreateType(Decl->getType(), Unit));
663 // Insert an llvm.dbg.declare into the current block.
664 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000665}
666
Chris Lattner9c85ba32008-11-10 06:08:34 +0000667void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
668 llvm::Value *Storage,
669 CGBuilderTy &Builder) {
670 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
671}
672
673/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
674/// variable declaration.
675void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
676 CGBuilderTy &Builder) {
677 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
678}
679
680
681
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000682/// EmitGlobalVariable - Emit information about a global variable.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000683void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
684 const VarDecl *Decl) {
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000685 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000686 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000687 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000688 unsigned LineNo = SM.getInstantiationLineNumber(Decl->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +0000689
690 std::string Name = Decl->getNameAsString();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000691
692 QualType T = Decl->getType();
693 if (T->isIncompleteArrayType()) {
694
695 // CodeGen turns int[] into int[1] so we'll do the same here.
696 llvm::APSInt ConstVal(32);
697
698 ConstVal = 1;
699 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
700
701 T = M->getContext().getConstantArrayType(ET, ConstVal,
702 ArrayType::Normal, 0);
703 }
704
Chris Lattner9c85ba32008-11-10 06:08:34 +0000705 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000706 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000707 Var->hasInternalLinkage(),
708 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000709}
710
Devang Patel9ca36b62009-02-26 21:10:26 +0000711/// EmitGlobalVariable - Emit information about an objective-c interface.
712void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
713 ObjCInterfaceDecl *Decl) {
714 // Create global variable debug descriptor.
715 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
716 SourceManager &SM = M->getContext().getSourceManager();
717 unsigned LineNo = SM.getInstantiationLineNumber(Decl->getLocation());
718
719 std::string Name = Decl->getNameAsString();
720
721 QualType T = M->getContext().buildObjCInterfaceType(Decl);
722 if (T->isIncompleteArrayType()) {
723
724 // CodeGen turns int[] into int[1] so we'll do the same here.
725 llvm::APSInt ConstVal(32);
726
727 ConstVal = 1;
728 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
729
730 T = M->getContext().getConstantArrayType(ET, ConstVal,
731 ArrayType::Normal, 0);
732 }
733
734 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
735 getOrCreateType(T, Unit),
736 Var->hasInternalLinkage(),
737 true/*definition*/, Var);
738}
739