blob: 7014fa839efe101878abc61222d798ad9b2ff83c [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()));
Devang Patel8d9aefc2009-03-24 20:35:51 +000073 unsigned LangTag = llvm::dwarf::DW_LANG_C89;
74
75 LangOptions LO = M->getLangOptions();
76 if (LO.CPlusPlus
77 && (LO.ObjC1 || LO.ObjC2 || LO.ObjCNonFragileABI || LO.NeXTRuntime))
78 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
79 else if (LO.CPlusPlus)
80 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
81 else if (LO.ObjC1 || LO.ObjC2 || LO.ObjCNonFragileABI || LO.NeXTRuntime)
82 LangTag = llvm::dwarf::DW_LANG_ObjC;
83 else if (LO.C99)
84 LangTag = llvm::dwarf::DW_LANG_C99;
85
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000086 // Create new compile unit.
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000087 // FIXME: Do not know how to get clang version yet.
Devang Patelc20482b2009-03-19 00:23:53 +000088 // FIXME: Encode command line options.
89 // FIXME: Encode optimization level.
Devang Patel8d9aefc2009-03-24 20:35:51 +000090 return Unit = DebugFactory.CreateCompileUnit(LangTag, FileName, DirName,
91 "clang", isMain);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000092}
93
Devang Patel65e99f22009-02-25 01:36:11 +000094/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +000095/// one if necessary.
96llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel65e99f22009-02-25 01:36:11 +000097 llvm::DICompileUnit Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +000098 unsigned Encoding = 0;
99 switch (BT->getKind()) {
100 default:
101 case BuiltinType::Void:
102 return llvm::DIType();
103 case BuiltinType::UChar:
104 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
105 case BuiltinType::Char_S:
106 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
107 case BuiltinType::UShort:
108 case BuiltinType::UInt:
109 case BuiltinType::ULong:
110 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
111 case BuiltinType::Short:
112 case BuiltinType::Int:
113 case BuiltinType::Long:
114 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
115 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
116 case BuiltinType::Float:
117 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
118 }
119 // Bit size, align and offset of the type.
120 uint64_t Size = M->getContext().getTypeSize(BT);
121 uint64_t Align = M->getContext().getTypeAlign(BT);
122 uint64_t Offset = 0;
123
124 return DebugFactory.CreateBasicType(Unit, BT->getName(), Unit, 0, Size, Align,
125 Offset, /*flags*/ 0, Encoding);
126}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000127
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000128/// getOrCreateCVRType - Get the CVR qualified type from the cache or create
129/// a new one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000130llvm::DIType CGDebugInfo::CreateCVRType(QualType Ty, llvm::DICompileUnit Unit) {
131 // We will create one Derived type for one qualifier and recurse to handle any
132 // additional ones.
133 llvm::DIType FromTy;
134 unsigned Tag;
135 if (Ty.isConstQualified()) {
136 Tag = llvm::dwarf::DW_TAG_const_type;
137 Ty.removeConst();
138 FromTy = getOrCreateType(Ty, Unit);
139 } else if (Ty.isVolatileQualified()) {
140 Tag = llvm::dwarf::DW_TAG_volatile_type;
141 Ty.removeVolatile();
142 FromTy = getOrCreateType(Ty, Unit);
143 } else {
144 assert(Ty.isRestrictQualified() && "Unknown type qualifier for debug info");
145 Tag = llvm::dwarf::DW_TAG_restrict_type;
146 Ty.removeRestrict();
147 FromTy = getOrCreateType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000148 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000149
Daniel Dunbar3845f862008-10-31 03:54:29 +0000150 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
151 // CVR derived types.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000152 return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
153 0, 0, 0, 0, 0, FromTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000154}
155
Chris Lattner9c85ba32008-11-10 06:08:34 +0000156llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
157 llvm::DICompileUnit Unit) {
158 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000159
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000160 // Bit size, align and offset of the type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000161 uint64_t Size = M->getContext().getTypeSize(Ty);
162 uint64_t Align = M->getContext().getTypeAlign(Ty);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000163
Chris Lattner9c85ba32008-11-10 06:08:34 +0000164 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
165 "", llvm::DICompileUnit(),
166 0, Size, Align, 0, 0, EltTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000167}
168
Chris Lattner9c85ba32008-11-10 06:08:34 +0000169llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
170 llvm::DICompileUnit Unit) {
171 // Typedefs are derived from some other type. If we have a typedef of a
172 // typedef, make sure to emit the whole chain.
173 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
174
175 // We don't set size information, but do specify where the typedef was
176 // declared.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000177 std::string TyName = Ty->getDecl()->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000178 SourceLocation DefLoc = Ty->getDecl()->getLocation();
179 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000180
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000181 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000182 uint64_t Line = SM.getInstantiationLineNumber(DefLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000183
Chris Lattner9c85ba32008-11-10 06:08:34 +0000184 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
185 TyName, DefUnit, Line, 0, 0, 0, 0, Src);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000186}
187
Chris Lattner9c85ba32008-11-10 06:08:34 +0000188llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
189 llvm::DICompileUnit Unit) {
190 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000191
Chris Lattner9c85ba32008-11-10 06:08:34 +0000192 // Add the result type at least.
193 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
194
195 // Set up remainder of arguments if there is a prototype.
196 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000197 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000198 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
199 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
200 } else {
201 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000202 }
203
Chris Lattner9c85ba32008-11-10 06:08:34 +0000204 llvm::DIArray EltTypeArray =
205 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
206
207 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
208 Unit, "", llvm::DICompileUnit(),
209 0, 0, 0, 0, 0,
210 llvm::DIType(), EltTypeArray);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000211}
212
Devang Patel65e99f22009-02-25 01:36:11 +0000213/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000214llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
215 llvm::DICompileUnit Unit) {
Douglas Gregora4c46df2008-12-11 17:59:21 +0000216 RecordDecl *Decl = Ty->getDecl();
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000217
Chris Lattner9c85ba32008-11-10 06:08:34 +0000218 unsigned Tag;
219 if (Decl->isStruct())
220 Tag = llvm::dwarf::DW_TAG_structure_type;
221 else if (Decl->isUnion())
222 Tag = llvm::dwarf::DW_TAG_union_type;
223 else {
224 assert(Decl->isClass() && "Unknown RecordType!");
225 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000226 }
227
Sanjiv Gupta507de852008-06-09 10:47:41 +0000228 SourceManager &SM = M->getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000229
Chris Lattner9c85ba32008-11-10 06:08:34 +0000230 // Get overall information about the record type for the debug info.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000231 std::string Name = Decl->getNameAsString();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000232
Chris Lattner9c85ba32008-11-10 06:08:34 +0000233 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000234 unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000235
236
237 // Records and classes and unions can all be recursive. To handle them, we
238 // first generate a debug descriptor for the struct as a forward declaration.
239 // Then (if it is a definition) we go through and get debug info for all of
240 // its members. Finally, we create a descriptor for the complete type (which
241 // may refer to the forward decl if the struct is recursive) and replace all
242 // uses of the forward declaration with the final definition.
243 llvm::DIType FwdDecl =
244 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
245 llvm::DIType(), llvm::DIArray());
246
247 // If this is just a forward declaration, return it.
248 if (!Decl->getDefinition(M->getContext()))
249 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000250
Chris Lattner9c85ba32008-11-10 06:08:34 +0000251 // Otherwise, insert it into the TypeCache so that recursive uses will find
252 // it.
253 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
254
255 // Convert all the elements.
256 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
257
258 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl);
259
260 unsigned FieldNo = 0;
Douglas Gregora4c46df2008-12-11 17:59:21 +0000261 for (RecordDecl::field_iterator I = Decl->field_begin(),
262 E = Decl->field_end();
263 I != E; ++I, ++FieldNo) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000264 FieldDecl *Field = *I;
265 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattner8ec03f52008-11-24 03:54:41 +0000266
267 std::string FieldName = Field->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000268
269 // Get the location for the field.
270 SourceLocation FieldDefLoc = Field->getLocation();
271 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000272 unsigned FieldLine = SM.getInstantiationLineNumber(FieldDefLoc);
Devang Patelec9b5d52009-03-16 23:47:53 +0000273
274 QualType FType = Field->getType();
275 uint64_t FieldSize = 0;
276 unsigned FieldAlign = 0;
277 if (!FType->isIncompleteArrayType()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000278
Devang Patelec9b5d52009-03-16 23:47:53 +0000279 // Bit size, align and offset of the type.
280 FieldSize = M->getContext().getTypeSize(FType);
281 Expr *BitWidth = Field->getBitWidth();
282 if (BitWidth)
283 FieldSize =
284 BitWidth->getIntegerConstantExprValue(M->getContext()).getZExtValue();
285
286 FieldAlign = M->getContext().getTypeAlign(FType);
287 }
288
Chris Lattner9c85ba32008-11-10 06:08:34 +0000289 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
290
291 // Create a DW_TAG_member node to remember the offset of this field in the
292 // struct. FIXME: This is an absolutely insane way to capture this
293 // information. When we gut debug info, this should be fixed.
294 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
295 FieldName, FieldDefUnit,
296 FieldLine, FieldSize, FieldAlign,
297 FieldOffset, 0, FieldTy);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000298 EltTys.push_back(FieldTy);
299 }
300
301 llvm::DIArray Elements =
302 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
303
304 // Bit size, align and offset of the type.
305 uint64_t Size = M->getContext().getTypeSize(Ty);
306 uint64_t Align = M->getContext().getTypeAlign(Ty);
307
308 llvm::DIType RealDecl =
309 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
310 Align, 0, 0, llvm::DIType(), Elements);
311
312 // Now that we have a real decl for the struct, replace anything using the
313 // old decl with the new one. This will recursively update the debug info.
314 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
315 FwdDecl.getGV()->eraseFromParent();
316
317 return RealDecl;
318}
319
Devang Patel9ca36b62009-02-26 21:10:26 +0000320/// CreateType - get objective-c interface type.
321llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
322 llvm::DICompileUnit Unit) {
323 ObjCInterfaceDecl *Decl = Ty->getDecl();
324
325 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
326 SourceManager &SM = M->getContext().getSourceManager();
327
328 // Get overall information about the record type for the debug info.
329 std::string Name = Decl->getNameAsString();
330
331 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
332 unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation());
333
334
335 // To handle recursive interface, we
336 // first generate a debug descriptor for the struct as a forward declaration.
337 // Then (if it is a definition) we go through and get debug info for all of
338 // its members. Finally, we create a descriptor for the complete type (which
339 // may refer to the forward decl if the struct is recursive) and replace all
340 // uses of the forward declaration with the final definition.
341 llvm::DIType FwdDecl =
342 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
343 llvm::DIType(), llvm::DIArray());
344
345 // If this is just a forward declaration, return it.
346 if (Decl->isForwardDecl())
347 return FwdDecl;
348
349 // Otherwise, insert it into the TypeCache so that recursive uses will find
350 // it.
351 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
352
353 // Convert all the elements.
354 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
355
Devang Patelfbe899f2009-03-10 21:30:26 +0000356 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
357 if (SClass) {
358 llvm::DIType SClassTy =
359 getOrCreateType(M->getContext().getObjCInterfaceType(SClass), Unit);
360 llvm::DIType InhTag =
361 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
362 Unit, "", Unit, 0, 0, 0,
363 0 /* offset */, 0, SClassTy);
364 EltTys.push_back(InhTag);
365 }
366
Devang Patel9ca36b62009-02-26 21:10:26 +0000367 const ASTRecordLayout &RL = M->getContext().getASTObjCInterfaceLayout(Decl);
368
369 unsigned FieldNo = 0;
370 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
371 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
372 ObjCIvarDecl *Field = *I;
373 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
374
375 std::string FieldName = Field->getNameAsString();
376
377 // Get the location for the field.
378 SourceLocation FieldDefLoc = Field->getLocation();
379 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
380 unsigned FieldLine = SM.getInstantiationLineNumber(FieldDefLoc);
Devang Patel99c20eb2009-03-20 18:24:39 +0000381
382 QualType FType = Field->getType();
383 uint64_t FieldSize = 0;
384 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000385
Devang Patel99c20eb2009-03-20 18:24:39 +0000386 if (!FType->isIncompleteArrayType()) {
387
388 // Bit size, align and offset of the type.
389 FieldSize = M->getContext().getTypeSize(FType);
390 Expr *BitWidth = Field->getBitWidth();
391 if (BitWidth)
392 FieldSize =
393 BitWidth->getIntegerConstantExprValue(M->getContext()).getZExtValue();
394
395 FieldAlign = M->getContext().getTypeAlign(FType);
396 }
397
398 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
399
Devang Patelc20482b2009-03-19 00:23:53 +0000400 unsigned Flags = 0;
401 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
402 Flags = llvm::DIType::FlagProtected;
403 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
404 Flags = llvm::DIType::FlagPrivate;
405
Devang Patel9ca36b62009-02-26 21:10:26 +0000406 // Create a DW_TAG_member node to remember the offset of this field in the
407 // struct. FIXME: This is an absolutely insane way to capture this
408 // information. When we gut debug info, this should be fixed.
409 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
410 FieldName, FieldDefUnit,
411 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000412 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000413 EltTys.push_back(FieldTy);
414 }
415
416 llvm::DIArray Elements =
417 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
418
419 // Bit size, align and offset of the type.
420 uint64_t Size = M->getContext().getTypeSize(Ty);
421 uint64_t Align = M->getContext().getTypeAlign(Ty);
422
423 llvm::DIType RealDecl =
424 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
425 Align, 0, 0, llvm::DIType(), Elements);
426
427 // Now that we have a real decl for the struct, replace anything using the
428 // old decl with the new one. This will recursively update the debug info.
429 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
430 FwdDecl.getGV()->eraseFromParent();
431
432 return RealDecl;
433}
434
Chris Lattner9c85ba32008-11-10 06:08:34 +0000435llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
436 llvm::DICompileUnit Unit) {
437 EnumDecl *Decl = Ty->getDecl();
438
439 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
440
441 // Create DIEnumerator elements for each enumerator.
Douglas Gregor44b43212008-12-11 16:49:14 +0000442 for (EnumDecl::enumerator_iterator Enum = Decl->enumerator_begin(),
443 EnumEnd = Decl->enumerator_end();
444 Enum != EnumEnd; ++Enum) {
445 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(),
446 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000447 }
448
449 // Return a CompositeType for the enum itself.
450 llvm::DIArray EltArray =
451 DebugFactory.GetOrCreateArray(&Enumerators[0], Enumerators.size());
452
Chris Lattner8ec03f52008-11-24 03:54:41 +0000453 std::string EnumName = Decl->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000454 SourceLocation DefLoc = Decl->getLocation();
455 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
456 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000457 unsigned Line = SM.getInstantiationLineNumber(DefLoc);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000458
459 // Size and align of the type.
460 uint64_t Size = M->getContext().getTypeSize(Ty);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000461 unsigned Align = M->getContext().getTypeAlign(Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000462
463 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
464 Unit, EnumName, DefUnit, Line,
465 Size, Align, 0, 0,
466 llvm::DIType(), EltArray);
467}
468
469llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
470 llvm::DICompileUnit Unit) {
471 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
472 return CreateType(RT, Unit);
473 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
474 return CreateType(ET, Unit);
475
476 return llvm::DIType();
477}
478
479llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
480 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000481 uint64_t Size;
482 uint64_t Align;
483
484
Nuno Lopes010d5142009-01-28 00:35:17 +0000485 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +0000486 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000487 Size = 0;
488 Align =
Nuno Lopes010d5142009-01-28 00:35:17 +0000489 M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT));
490 } else if (Ty->isIncompleteArrayType()) {
491 Size = 0;
492 Align = M->getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +0000493 } else {
494 // Size and align of the whole array, not the element type.
495 Size = M->getContext().getTypeSize(Ty);
496 Align = M->getContext().getTypeAlign(Ty);
497 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000498
499 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
500 // interior arrays, do we care? Why aren't nested arrays represented the
501 // obvious/recursive way?
502 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
503 QualType EltTy(Ty, 0);
504 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000505 uint64_t Upper = 0;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000506 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
507 Upper = CAT->getSize().getZExtValue() - 1;
508 // FIXME: Verify this is right for VLAs.
509 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
510 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000511 }
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000512
Chris Lattner9c85ba32008-11-10 06:08:34 +0000513 llvm::DIArray SubscriptArray =
514 DebugFactory.GetOrCreateArray(&Subscripts[0], Subscripts.size());
515
516 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
517 Unit, "", llvm::DICompileUnit(),
518 0, Size, Align, 0, 0,
519 getOrCreateType(EltTy, Unit),
520 SubscriptArray);
521}
522
523
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000524/// getOrCreateType - Get the type from the cache or create a new
525/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000526llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
527 llvm::DICompileUnit Unit) {
528 if (Ty.isNull())
529 return llvm::DIType();
530
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000531 // Check to see if the compile unit already has created this type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000532 llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()];
533 if (!Slot.isNull()) return Slot;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000534
Chris Lattner9c85ba32008-11-10 06:08:34 +0000535 // Handle CVR qualifiers, which recursively handles what they refer to.
536 if (Ty.getCVRQualifiers())
537 return Slot = CreateCVRType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000538
539 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000540 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000541#define TYPE(Class, Base)
542#define ABSTRACT_TYPE(Class, Base)
543#define NON_CANONICAL_TYPE(Class, Base)
544#define DEPENDENT_TYPE(Class, Base) case Type::Class:
545#include "clang/AST/TypeNodes.def"
546 assert(false && "Dependent types cannot show up in debug information");
547
Chris Lattner9c85ba32008-11-10 06:08:34 +0000548 case Type::Complex:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000549 case Type::LValueReference:
550 case Type::RValueReference:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000551 case Type::Vector:
552 case Type::ExtVector:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000553 case Type::ExtQual:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000554 case Type::ObjCQualifiedInterface:
555 case Type::ObjCQualifiedId:
Eli Friedman00524e32009-02-27 23:15:07 +0000556 case Type::FixedWidthInt:
557 case Type::BlockPointer:
558 case Type::MemberPointer:
559 case Type::ClassTemplateSpecialization:
Douglas Gregore4e5b052009-03-19 00:18:19 +0000560 case Type::QualifiedName:
Eli Friedman00524e32009-02-27 23:15:07 +0000561 case Type::ObjCQualifiedClass:
562 // Unsupported types
Chris Lattner9c85ba32008-11-10 06:08:34 +0000563 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000564
Devang Patele7987062009-03-02 17:58:28 +0000565 case Type::ObjCInterface:
566 Slot = CreateType(cast<ObjCInterfaceType>(Ty), Unit); break;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000567 case Type::Builtin: Slot = CreateType(cast<BuiltinType>(Ty), Unit); break;
568 case Type::Pointer: Slot = CreateType(cast<PointerType>(Ty), Unit); break;
Douglas Gregor72564e72009-02-26 23:50:07 +0000569 case Type::Typedef: Slot = CreateType(cast<TypedefType>(Ty), Unit); break;
570 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000571 case Type::Enum:
572 Slot = CreateType(cast<TagType>(Ty), Unit);
573 break;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000574 case Type::FunctionProto:
575 case Type::FunctionNoProto:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000576 return Slot = CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000577
578 case Type::ConstantArray:
579 case Type::VariableArray:
580 case Type::IncompleteArray:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000581 return Slot = CreateType(cast<ArrayType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000582 case Type::TypeOfExpr:
583 return Slot = getOrCreateType(cast<TypeOfExprType>(Ty)->getUnderlyingExpr()
Chris Lattner3cc5c402008-11-11 07:01:36 +0000584 ->getType(), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000585 case Type::TypeOf:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000586 return Slot = getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(),
587 Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000588 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000589
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000590 return Slot;
591}
592
593/// EmitFunctionStart - Constructs the debug code for entering a function -
594/// "llvm.dbg.func.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000595void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000596 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000597 CGBuilderTy &Builder) {
598 // FIXME: Why is this using CurLoc???
599 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000600 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000601 unsigned LineNo = SM.getInstantiationLineNumber(CurLoc);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000602
603 llvm::DISubprogram SP =
604 DebugFactory.CreateSubprogram(Unit, Name, Name, "", Unit, LineNo,
605 getOrCreateType(ReturnType, Unit),
606 Fn->hasInternalLinkage(), true/*definition*/);
607
608 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
609
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000610 // Push function on region stack.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000611 RegionStack.push_back(SP);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000612}
613
614
Chris Lattner9c85ba32008-11-10 06:08:34 +0000615void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000616 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
617
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000618 // Don't bother if things are the same as last time.
619 SourceManager &SM = M->getContext().getSourceManager();
620 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +0000621 || (SM.getInstantiationLineNumber(CurLoc) ==
622 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000623 && SM.isFromSameFile(CurLoc, PrevLoc)))
624 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000625
626 // Update last state.
627 PrevLoc = CurLoc;
628
629 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000630 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000631 DebugFactory.InsertStopPoint(Unit, SM.getInstantiationLineNumber(CurLoc),
632 SM.getInstantiationColumnNumber(CurLoc),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000633 Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000634}
635
636/// EmitRegionStart- Constructs the debug code for entering a declarative
637/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000638void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
639 llvm::DIDescriptor D;
Daniel Dunbar5273f512008-10-17 01:07:56 +0000640 if (!RegionStack.empty())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000641 D = RegionStack.back();
642 D = DebugFactory.CreateBlock(D);
643 RegionStack.push_back(D);
644 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000645}
646
647/// EmitRegionEnd - Constructs the debug code for exiting a declarative
648/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +0000649void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000650 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
651
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000652 // Provide an region stop point.
653 EmitStopPoint(Fn, Builder);
654
Chris Lattner9c85ba32008-11-10 06:08:34 +0000655 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000656 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000657}
658
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000659/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000660void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
661 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000662 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
663
Chris Lattner9c85ba32008-11-10 06:08:34 +0000664 // Get location information.
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000665 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000666 unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000667 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
668
669 // Create the descriptor for the variable.
670 llvm::DIVariable D =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000671 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000672 Unit, Line,
673 getOrCreateType(Decl->getType(), Unit));
674 // Insert an llvm.dbg.declare into the current block.
675 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000676}
677
Chris Lattner9c85ba32008-11-10 06:08:34 +0000678void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
679 llvm::Value *Storage,
680 CGBuilderTy &Builder) {
681 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
682}
683
684/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
685/// variable declaration.
686void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
687 CGBuilderTy &Builder) {
688 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
689}
690
691
692
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000693/// EmitGlobalVariable - Emit information about a global variable.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000694void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
695 const VarDecl *Decl) {
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000696 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000697 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000698 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000699 unsigned LineNo = SM.getInstantiationLineNumber(Decl->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +0000700
701 std::string Name = Decl->getNameAsString();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000702
703 QualType T = Decl->getType();
704 if (T->isIncompleteArrayType()) {
705
706 // CodeGen turns int[] into int[1] so we'll do the same here.
707 llvm::APSInt ConstVal(32);
708
709 ConstVal = 1;
710 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
711
712 T = M->getContext().getConstantArrayType(ET, ConstVal,
713 ArrayType::Normal, 0);
714 }
715
Chris Lattner9c85ba32008-11-10 06:08:34 +0000716 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000717 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000718 Var->hasInternalLinkage(),
719 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000720}
721
Devang Patel9ca36b62009-02-26 21:10:26 +0000722/// EmitGlobalVariable - Emit information about an objective-c interface.
723void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
724 ObjCInterfaceDecl *Decl) {
725 // Create global variable debug descriptor.
726 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
727 SourceManager &SM = M->getContext().getSourceManager();
728 unsigned LineNo = SM.getInstantiationLineNumber(Decl->getLocation());
729
730 std::string Name = Decl->getNameAsString();
731
732 QualType T = M->getContext().buildObjCInterfaceType(Decl);
733 if (T->isIncompleteArrayType()) {
734
735 // CodeGen turns int[] into int[1] so we'll do the same here.
736 llvm::APSInt ConstVal(32);
737
738 ConstVal = 1;
739 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
740
741 T = M->getContext().getConstantArrayType(ET, ConstVal,
742 ArrayType::Normal, 0);
743 }
744
745 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
746 getOrCreateType(T, Unit),
747 Var->hasInternalLinkage(),
748 true/*definition*/, Var);
749}
750