blob: f2fc90c6cfb398b740bfe934d00e66d6c0f5adb0 [file] [log] [blame]
Sanjiv Gupta40e56a12008-05-08 08:54:20 +00001//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the debug information generation while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGDebugInfo.h"
15#include "CodeGenModule.h"
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000016#include "clang/AST/ASTContext.h"
Devang Patel3fc13e32009-02-26 21:10:26 +000017#include "clang/AST/DeclObjC.h"
Chris Lattnere3afa462008-11-11 07:01:36 +000018#include "clang/AST/Expr.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000019#include "clang/AST/RecordLayout.h"
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000020#include "clang/Basic/SourceManager.h"
21#include "clang/Basic/FileManager.h"
Devang Patelbcded192009-03-27 23:16:32 +000022#include "clang/Frontend/CompileOptions.h"
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000023#include "llvm/Constants.h"
24#include "llvm/DerivedTypes.h"
25#include "llvm/Instructions.h"
26#include "llvm/Intrinsics.h"
27#include "llvm/Module.h"
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000028#include "llvm/ADT/StringExtras.h"
29#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000030#include "llvm/Support/Dwarf.h"
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000031#include "llvm/Target/TargetMachine.h"
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000032using namespace clang;
33using namespace clang::CodeGen;
34
35CGDebugInfo::CGDebugInfo(CodeGenModule *m)
Chris Lattner562ce0a2008-11-10 06:08:34 +000036 : M(m), DebugFactory(M->getModule()) {
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000037}
38
Chris Lattner562ce0a2008-11-10 06:08:34 +000039CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar6fc1f972008-10-17 16:15:48 +000040 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000041}
42
Chris Lattner562ce0a2008-11-10 06:08:34 +000043void CGDebugInfo::setLocation(SourceLocation Loc) {
44 if (Loc.isValid())
Chris Lattner18c8dc02009-01-16 07:36:28 +000045 CurLoc = M->getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000046}
47
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000048/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
Daniel Dunbar4a66ef12008-10-24 08:38:36 +000049/// one if necessary. This returns null for invalid source locations.
Chris Lattner562ce0a2008-11-10 06:08:34 +000050llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) {
Daniel Dunbar67bb1fa2009-01-22 00:09:25 +000051 // FIXME: Until we do a complete job of emitting debug information,
52 // we need to support making dummy compile units so that we generate
53 // "well formed" debug info.
54 const FileEntry *FE = 0;
Daniel Dunbar4a66ef12008-10-24 08:38:36 +000055
Devang Patel61910072009-02-24 23:16:03 +000056 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnera9aae3e2009-03-25 03:28:08 +000057 bool isMain;
Daniel Dunbar67bb1fa2009-01-22 00:09:25 +000058 if (Loc.isValid()) {
Daniel Dunbar67bb1fa2009-01-22 00:09:25 +000059 Loc = SM.getInstantiationLoc(Loc);
60 FE = SM.getFileEntryForID(SM.getFileID(Loc));
Chris Lattnera9aae3e2009-03-25 03:28:08 +000061 isMain = SM.getFileID(Loc) == SM.getMainFileID();
Devang Patel61910072009-02-24 23:16:03 +000062 } else {
63 // If Loc is not valid then use main file id.
64 FE = SM.getFileEntryForID(SM.getMainFileID());
Chris Lattnera9aae3e2009-03-25 03:28:08 +000065 isMain = true;
Daniel Dunbar67bb1fa2009-01-22 00:09:25 +000066 }
67
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000068 // See if this compile unit has been used before.
Chris Lattner562ce0a2008-11-10 06:08:34 +000069 llvm::DICompileUnit &Unit = CompileUnitCache[FE];
70 if (!Unit.isNull()) return Unit;
71
72 // Get source file information.
Daniel Dunbar67bb1fa2009-01-22 00:09:25 +000073 const char *FileName = FE ? FE->getName() : "<unknown>";
Daniel Dunbar2863cae2009-02-07 00:40:41 +000074 const char *DirName = FE ? FE->getDir()->getName() : "<unknown>";
Daniel Dunbar9865e6f2008-10-24 00:46:51 +000075
Chris Lattnera9aae3e2009-03-25 03:28:08 +000076 const LangOptions &LO = M->getLangOptions();
Daniel Dunbardedc94c2009-04-08 05:11:16 +000077
78 // If this is the main file, use the user provided main file name if
79 // specified.
80 if (isMain && LO.getMainFileName())
81 FileName = LO.getMainFileName();
82
Chris Lattnera9aae3e2009-03-25 03:28:08 +000083 unsigned LangTag;
84 if (LO.CPlusPlus) {
85 if (LO.ObjC1)
86 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
87 else
88 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
89 } else if (LO.ObjC1) {
Devang Patel07461fa2009-03-24 20:35:51 +000090 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattnera9aae3e2009-03-25 03:28:08 +000091 } else if (LO.C99) {
Devang Patel07461fa2009-03-24 20:35:51 +000092 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattnera9aae3e2009-03-25 03:28:08 +000093 } else {
94 LangTag = llvm::dwarf::DW_LANG_C89;
95 }
Devang Patel07461fa2009-03-24 20:35:51 +000096
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000097 // Create new compile unit.
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000098 // FIXME: Do not know how to get clang version yet.
Devang Patel000ee2f2009-03-19 00:23:53 +000099 // FIXME: Encode command line options.
100 // FIXME: Encode optimization level.
Devang Patel07461fa2009-03-24 20:35:51 +0000101 return Unit = DebugFactory.CreateCompileUnit(LangTag, FileName, DirName,
102 "clang", isMain);
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000103}
104
Devang Patela4cc0c42009-02-25 01:36:11 +0000105/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner562ce0a2008-11-10 06:08:34 +0000106/// one if necessary.
107llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patela4cc0c42009-02-25 01:36:11 +0000108 llvm::DICompileUnit Unit) {
Chris Lattner562ce0a2008-11-10 06:08:34 +0000109 unsigned Encoding = 0;
110 switch (BT->getKind()) {
111 default:
112 case BuiltinType::Void:
113 return llvm::DIType();
114 case BuiltinType::UChar:
115 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
116 case BuiltinType::Char_S:
117 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
118 case BuiltinType::UShort:
119 case BuiltinType::UInt:
120 case BuiltinType::ULong:
121 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
122 case BuiltinType::Short:
123 case BuiltinType::Int:
124 case BuiltinType::Long:
125 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
126 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
127 case BuiltinType::Float:
128 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
129 }
130 // Bit size, align and offset of the type.
131 uint64_t Size = M->getContext().getTypeSize(BT);
132 uint64_t Align = M->getContext().getTypeAlign(BT);
133 uint64_t Offset = 0;
134
135 return DebugFactory.CreateBasicType(Unit, BT->getName(), Unit, 0, Size, Align,
136 Offset, /*flags*/ 0, Encoding);
137}
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000138
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000139/// getOrCreateCVRType - Get the CVR qualified type from the cache or create
140/// a new one if necessary.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000141llvm::DIType CGDebugInfo::CreateCVRType(QualType Ty, llvm::DICompileUnit Unit) {
142 // We will create one Derived type for one qualifier and recurse to handle any
143 // additional ones.
144 llvm::DIType FromTy;
145 unsigned Tag;
146 if (Ty.isConstQualified()) {
147 Tag = llvm::dwarf::DW_TAG_const_type;
148 Ty.removeConst();
149 FromTy = getOrCreateType(Ty, Unit);
150 } else if (Ty.isVolatileQualified()) {
151 Tag = llvm::dwarf::DW_TAG_volatile_type;
152 Ty.removeVolatile();
153 FromTy = getOrCreateType(Ty, Unit);
154 } else {
155 assert(Ty.isRestrictQualified() && "Unknown type qualifier for debug info");
156 Tag = llvm::dwarf::DW_TAG_restrict_type;
157 Ty.removeRestrict();
158 FromTy = getOrCreateType(Ty, Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000159 }
Chris Lattner562ce0a2008-11-10 06:08:34 +0000160
Daniel Dunbar44252b42008-10-31 03:54:29 +0000161 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
162 // CVR derived types.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000163 return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
164 0, 0, 0, 0, 0, FromTy);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000165}
166
Chris Lattner562ce0a2008-11-10 06:08:34 +0000167llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
168 llvm::DICompileUnit Unit) {
169 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000170
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000171 // Bit size, align and offset of the type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000172 uint64_t Size = M->getContext().getTypeSize(Ty);
173 uint64_t Align = M->getContext().getTypeAlign(Ty);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000174
Chris Lattner562ce0a2008-11-10 06:08:34 +0000175 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
176 "", llvm::DICompileUnit(),
177 0, Size, Align, 0, 0, EltTy);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000178}
179
Chris Lattner562ce0a2008-11-10 06:08:34 +0000180llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
181 llvm::DICompileUnit Unit) {
182 // Typedefs are derived from some other type. If we have a typedef of a
183 // typedef, make sure to emit the whole chain.
184 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
185
186 // We don't set size information, but do specify where the typedef was
187 // declared.
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000188 std::string TyName = Ty->getDecl()->getNameAsString();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000189 SourceLocation DefLoc = Ty->getDecl()->getLocation();
190 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000191
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000192 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner18c8dc02009-01-16 07:36:28 +0000193 uint64_t Line = SM.getInstantiationLineNumber(DefLoc);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000194
Chris Lattner562ce0a2008-11-10 06:08:34 +0000195 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
196 TyName, DefUnit, Line, 0, 0, 0, 0, Src);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000197}
198
Chris Lattner562ce0a2008-11-10 06:08:34 +0000199llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
200 llvm::DICompileUnit Unit) {
201 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000202
Chris Lattner562ce0a2008-11-10 06:08:34 +0000203 // Add the result type at least.
204 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
205
206 // Set up remainder of arguments if there is a prototype.
207 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor4fa58902009-02-26 23:50:07 +0000208 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner562ce0a2008-11-10 06:08:34 +0000209 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
210 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
211 } else {
212 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000213 }
214
Chris Lattner562ce0a2008-11-10 06:08:34 +0000215 llvm::DIArray EltTypeArray =
216 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
217
218 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
219 Unit, "", llvm::DICompileUnit(),
220 0, 0, 0, 0, 0,
221 llvm::DIType(), EltTypeArray);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000222}
223
Devang Patela4cc0c42009-02-25 01:36:11 +0000224/// CreateType - get structure or union type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000225llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
226 llvm::DICompileUnit Unit) {
Douglas Gregor640a04b2008-12-11 17:59:21 +0000227 RecordDecl *Decl = Ty->getDecl();
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000228
Chris Lattner562ce0a2008-11-10 06:08:34 +0000229 unsigned Tag;
230 if (Decl->isStruct())
231 Tag = llvm::dwarf::DW_TAG_structure_type;
232 else if (Decl->isUnion())
233 Tag = llvm::dwarf::DW_TAG_union_type;
234 else {
235 assert(Decl->isClass() && "Unknown RecordType!");
236 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000237 }
238
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000239 SourceManager &SM = M->getContext().getSourceManager();
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000240
Chris Lattner562ce0a2008-11-10 06:08:34 +0000241 // Get overall information about the record type for the debug info.
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000242 std::string Name = Decl->getNameAsString();
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000243
Chris Lattner562ce0a2008-11-10 06:08:34 +0000244 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Chris Lattner18c8dc02009-01-16 07:36:28 +0000245 unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation());
Chris Lattner562ce0a2008-11-10 06:08:34 +0000246
247
248 // Records and classes and unions can all be recursive. To handle them, we
249 // first generate a debug descriptor for the struct as a forward declaration.
250 // Then (if it is a definition) we go through and get debug info for all of
251 // its members. Finally, we create a descriptor for the complete type (which
252 // may refer to the forward decl if the struct is recursive) and replace all
253 // uses of the forward declaration with the final definition.
254 llvm::DIType FwdDecl =
255 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
256 llvm::DIType(), llvm::DIArray());
257
258 // If this is just a forward declaration, return it.
259 if (!Decl->getDefinition(M->getContext()))
260 return FwdDecl;
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000261
Chris Lattner562ce0a2008-11-10 06:08:34 +0000262 // Otherwise, insert it into the TypeCache so that recursive uses will find
263 // it.
264 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
265
266 // Convert all the elements.
267 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
268
269 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl);
270
271 unsigned FieldNo = 0;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000272 for (RecordDecl::field_iterator I = Decl->field_begin(M->getContext()),
273 E = Decl->field_end(M->getContext());
Douglas Gregor640a04b2008-12-11 17:59:21 +0000274 I != E; ++I, ++FieldNo) {
Chris Lattner562ce0a2008-11-10 06:08:34 +0000275 FieldDecl *Field = *I;
276 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000277
278 std::string FieldName = Field->getNameAsString();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000279
280 // Get the location for the field.
281 SourceLocation FieldDefLoc = Field->getLocation();
282 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Chris Lattner18c8dc02009-01-16 07:36:28 +0000283 unsigned FieldLine = SM.getInstantiationLineNumber(FieldDefLoc);
Devang Patelb3a08ba2009-03-16 23:47:53 +0000284
285 QualType FType = Field->getType();
286 uint64_t FieldSize = 0;
287 unsigned FieldAlign = 0;
288 if (!FType->isIncompleteArrayType()) {
Chris Lattner562ce0a2008-11-10 06:08:34 +0000289
Devang Patelb3a08ba2009-03-16 23:47:53 +0000290 // Bit size, align and offset of the type.
291 FieldSize = M->getContext().getTypeSize(FType);
292 Expr *BitWidth = Field->getBitWidth();
293 if (BitWidth)
294 FieldSize =
295 BitWidth->getIntegerConstantExprValue(M->getContext()).getZExtValue();
296
297 FieldAlign = M->getContext().getTypeAlign(FType);
298 }
299
Chris Lattner562ce0a2008-11-10 06:08:34 +0000300 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
301
302 // Create a DW_TAG_member node to remember the offset of this field in the
303 // struct. FIXME: This is an absolutely insane way to capture this
304 // information. When we gut debug info, this should be fixed.
305 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
306 FieldName, FieldDefUnit,
307 FieldLine, FieldSize, FieldAlign,
308 FieldOffset, 0, FieldTy);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000309 EltTys.push_back(FieldTy);
310 }
311
312 llvm::DIArray Elements =
313 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
314
315 // Bit size, align and offset of the type.
316 uint64_t Size = M->getContext().getTypeSize(Ty);
317 uint64_t Align = M->getContext().getTypeAlign(Ty);
318
319 llvm::DIType RealDecl =
320 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
321 Align, 0, 0, llvm::DIType(), Elements);
322
323 // Now that we have a real decl for the struct, replace anything using the
324 // old decl with the new one. This will recursively update the debug info.
325 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
326 FwdDecl.getGV()->eraseFromParent();
327
328 return RealDecl;
329}
330
Devang Patel3fc13e32009-02-26 21:10:26 +0000331/// CreateType - get objective-c interface type.
332llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
333 llvm::DICompileUnit Unit) {
334 ObjCInterfaceDecl *Decl = Ty->getDecl();
335
336 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
337 SourceManager &SM = M->getContext().getSourceManager();
338
339 // Get overall information about the record type for the debug info.
340 std::string Name = Decl->getNameAsString();
341
342 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
343 unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation());
344
345
346 // To handle recursive interface, we
347 // first generate a debug descriptor for the struct as a forward declaration.
348 // Then (if it is a definition) we go through and get debug info for all of
349 // its members. Finally, we create a descriptor for the complete type (which
350 // may refer to the forward decl if the struct is recursive) and replace all
351 // uses of the forward declaration with the final definition.
352 llvm::DIType FwdDecl =
353 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
354 llvm::DIType(), llvm::DIArray());
355
356 // If this is just a forward declaration, return it.
357 if (Decl->isForwardDecl())
358 return FwdDecl;
359
360 // Otherwise, insert it into the TypeCache so that recursive uses will find
361 // it.
362 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
363
364 // Convert all the elements.
365 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
366
Devang Patele6178df2009-03-10 21:30:26 +0000367 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
368 if (SClass) {
369 llvm::DIType SClassTy =
370 getOrCreateType(M->getContext().getObjCInterfaceType(SClass), Unit);
371 llvm::DIType InhTag =
372 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
373 Unit, "", Unit, 0, 0, 0,
374 0 /* offset */, 0, SClassTy);
375 EltTys.push_back(InhTag);
376 }
377
Devang Patel3fc13e32009-02-26 21:10:26 +0000378 const ASTRecordLayout &RL = M->getContext().getASTObjCInterfaceLayout(Decl);
379
380 unsigned FieldNo = 0;
381 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
382 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
383 ObjCIvarDecl *Field = *I;
384 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
385
386 std::string FieldName = Field->getNameAsString();
387
388 // Get the location for the field.
389 SourceLocation FieldDefLoc = Field->getLocation();
390 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
391 unsigned FieldLine = SM.getInstantiationLineNumber(FieldDefLoc);
Devang Patel806af7d2009-03-20 18:24:39 +0000392
393 QualType FType = Field->getType();
394 uint64_t FieldSize = 0;
395 unsigned FieldAlign = 0;
Devang Patel000ee2f2009-03-19 00:23:53 +0000396
Devang Patel806af7d2009-03-20 18:24:39 +0000397 if (!FType->isIncompleteArrayType()) {
398
399 // Bit size, align and offset of the type.
400 FieldSize = M->getContext().getTypeSize(FType);
401 Expr *BitWidth = Field->getBitWidth();
402 if (BitWidth)
403 FieldSize =
404 BitWidth->getIntegerConstantExprValue(M->getContext()).getZExtValue();
405
406 FieldAlign = M->getContext().getTypeAlign(FType);
407 }
408
409 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
410
Devang Patel000ee2f2009-03-19 00:23:53 +0000411 unsigned Flags = 0;
412 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
413 Flags = llvm::DIType::FlagProtected;
414 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
415 Flags = llvm::DIType::FlagPrivate;
416
Devang Patel3fc13e32009-02-26 21:10:26 +0000417 // Create a DW_TAG_member node to remember the offset of this field in the
418 // struct. FIXME: This is an absolutely insane way to capture this
419 // information. When we gut debug info, this should be fixed.
420 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
421 FieldName, FieldDefUnit,
422 FieldLine, FieldSize, FieldAlign,
Devang Patel000ee2f2009-03-19 00:23:53 +0000423 FieldOffset, Flags, FieldTy);
Devang Patel3fc13e32009-02-26 21:10:26 +0000424 EltTys.push_back(FieldTy);
425 }
426
427 llvm::DIArray Elements =
428 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
429
430 // Bit size, align and offset of the type.
431 uint64_t Size = M->getContext().getTypeSize(Ty);
432 uint64_t Align = M->getContext().getTypeAlign(Ty);
433
434 llvm::DIType RealDecl =
435 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
436 Align, 0, 0, llvm::DIType(), Elements);
437
438 // Now that we have a real decl for the struct, replace anything using the
439 // old decl with the new one. This will recursively update the debug info.
440 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
441 FwdDecl.getGV()->eraseFromParent();
442
443 return RealDecl;
444}
445
Chris Lattner562ce0a2008-11-10 06:08:34 +0000446llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
447 llvm::DICompileUnit Unit) {
448 EnumDecl *Decl = Ty->getDecl();
449
450 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
451
452 // Create DIEnumerator elements for each enumerator.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000453 for (EnumDecl::enumerator_iterator
454 Enum = Decl->enumerator_begin(M->getContext()),
455 EnumEnd = Decl->enumerator_end(M->getContext());
Douglas Gregor8acb7272008-12-11 16:49:14 +0000456 Enum != EnumEnd; ++Enum) {
457 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(),
458 Enum->getInitVal().getZExtValue()));
Chris Lattner562ce0a2008-11-10 06:08:34 +0000459 }
460
461 // Return a CompositeType for the enum itself.
462 llvm::DIArray EltArray =
463 DebugFactory.GetOrCreateArray(&Enumerators[0], Enumerators.size());
464
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000465 std::string EnumName = Decl->getNameAsString();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000466 SourceLocation DefLoc = Decl->getLocation();
467 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
468 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner18c8dc02009-01-16 07:36:28 +0000469 unsigned Line = SM.getInstantiationLineNumber(DefLoc);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000470
471 // Size and align of the type.
472 uint64_t Size = M->getContext().getTypeSize(Ty);
Chris Lattner18c8dc02009-01-16 07:36:28 +0000473 unsigned Align = M->getContext().getTypeAlign(Ty);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000474
475 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
476 Unit, EnumName, DefUnit, Line,
477 Size, Align, 0, 0,
478 llvm::DIType(), EltArray);
479}
480
481llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
482 llvm::DICompileUnit Unit) {
483 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
484 return CreateType(RT, Unit);
485 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
486 return CreateType(ET, Unit);
487
488 return llvm::DIType();
489}
490
491llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
492 llvm::DICompileUnit Unit) {
Anders Carlsson86b360e2009-01-05 01:23:29 +0000493 uint64_t Size;
494 uint64_t Align;
495
496
Nuno Lopes15bc9c32009-01-28 00:35:17 +0000497 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson86b360e2009-01-05 01:23:29 +0000498 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson86b360e2009-01-05 01:23:29 +0000499 Size = 0;
500 Align =
Nuno Lopes15bc9c32009-01-28 00:35:17 +0000501 M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT));
502 } else if (Ty->isIncompleteArrayType()) {
503 Size = 0;
504 Align = M->getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson86b360e2009-01-05 01:23:29 +0000505 } else {
506 // Size and align of the whole array, not the element type.
507 Size = M->getContext().getTypeSize(Ty);
508 Align = M->getContext().getTypeAlign(Ty);
509 }
Chris Lattner562ce0a2008-11-10 06:08:34 +0000510
511 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
512 // interior arrays, do we care? Why aren't nested arrays represented the
513 // obvious/recursive way?
514 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
515 QualType EltTy(Ty, 0);
516 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000517 uint64_t Upper = 0;
Chris Lattner562ce0a2008-11-10 06:08:34 +0000518 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
519 Upper = CAT->getSize().getZExtValue() - 1;
520 // FIXME: Verify this is right for VLAs.
521 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
522 EltTy = Ty->getElementType();
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000523 }
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000524
Chris Lattner562ce0a2008-11-10 06:08:34 +0000525 llvm::DIArray SubscriptArray =
526 DebugFactory.GetOrCreateArray(&Subscripts[0], Subscripts.size());
527
528 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
529 Unit, "", llvm::DICompileUnit(),
530 0, Size, Align, 0, 0,
531 getOrCreateType(EltTy, Unit),
532 SubscriptArray);
533}
534
535
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000536/// getOrCreateType - Get the type from the cache or create a new
537/// one if necessary.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000538llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
539 llvm::DICompileUnit Unit) {
540 if (Ty.isNull())
541 return llvm::DIType();
542
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000543 // Check to see if the compile unit already has created this type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000544 llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()];
545 if (!Slot.isNull()) return Slot;
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000546
Chris Lattner562ce0a2008-11-10 06:08:34 +0000547 // Handle CVR qualifiers, which recursively handles what they refer to.
548 if (Ty.getCVRQualifiers())
549 return Slot = CreateCVRType(Ty, Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000550
551 // Work out details of type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000552 switch (Ty->getTypeClass()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +0000553#define TYPE(Class, Base)
554#define ABSTRACT_TYPE(Class, Base)
555#define NON_CANONICAL_TYPE(Class, Base)
556#define DEPENDENT_TYPE(Class, Base) case Type::Class:
557#include "clang/AST/TypeNodes.def"
558 assert(false && "Dependent types cannot show up in debug information");
559
Chris Lattner562ce0a2008-11-10 06:08:34 +0000560 case Type::Complex:
Sebastian Redlce6fff02009-03-16 23:22:08 +0000561 case Type::LValueReference:
562 case Type::RValueReference:
Chris Lattner562ce0a2008-11-10 06:08:34 +0000563 case Type::Vector:
564 case Type::ExtVector:
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000565 case Type::ExtQual:
Chris Lattner562ce0a2008-11-10 06:08:34 +0000566 case Type::ObjCQualifiedInterface:
567 case Type::ObjCQualifiedId:
Eli Friedman673e91b2009-02-27 23:15:07 +0000568 case Type::FixedWidthInt:
569 case Type::BlockPointer:
570 case Type::MemberPointer:
Douglas Gregordd13e842009-03-30 22:58:21 +0000571 case Type::TemplateSpecialization:
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000572 case Type::QualifiedName:
Eli Friedman673e91b2009-02-27 23:15:07 +0000573 case Type::ObjCQualifiedClass:
574 // Unsupported types
Chris Lattner562ce0a2008-11-10 06:08:34 +0000575 return llvm::DIType();
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000576
Devang Patel5a1ff3b2009-03-02 17:58:28 +0000577 case Type::ObjCInterface:
578 Slot = CreateType(cast<ObjCInterfaceType>(Ty), Unit); break;
Chris Lattner562ce0a2008-11-10 06:08:34 +0000579 case Type::Builtin: Slot = CreateType(cast<BuiltinType>(Ty), Unit); break;
580 case Type::Pointer: Slot = CreateType(cast<PointerType>(Ty), Unit); break;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000581 case Type::Typedef: Slot = CreateType(cast<TypedefType>(Ty), Unit); break;
582 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000583 case Type::Enum:
584 Slot = CreateType(cast<TagType>(Ty), Unit);
585 break;
Chris Lattner562ce0a2008-11-10 06:08:34 +0000586 case Type::FunctionProto:
587 case Type::FunctionNoProto:
Chris Lattnere3afa462008-11-11 07:01:36 +0000588 return Slot = CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000589
590 case Type::ConstantArray:
591 case Type::VariableArray:
592 case Type::IncompleteArray:
Chris Lattnere3afa462008-11-11 07:01:36 +0000593 return Slot = CreateType(cast<ArrayType>(Ty), Unit);
Douglas Gregor4fa58902009-02-26 23:50:07 +0000594 case Type::TypeOfExpr:
595 return Slot = getOrCreateType(cast<TypeOfExprType>(Ty)->getUnderlyingExpr()
Chris Lattnere3afa462008-11-11 07:01:36 +0000596 ->getType(), Unit);
Douglas Gregor4fa58902009-02-26 23:50:07 +0000597 case Type::TypeOf:
Chris Lattnere3afa462008-11-11 07:01:36 +0000598 return Slot = getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(),
599 Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000600 }
Chris Lattner562ce0a2008-11-10 06:08:34 +0000601
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000602 return Slot;
603}
604
605/// EmitFunctionStart - Constructs the debug code for entering a function -
606/// "llvm.dbg.func.start.".
Chris Lattner562ce0a2008-11-10 06:08:34 +0000607void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000608 llvm::Function *Fn,
Chris Lattner562ce0a2008-11-10 06:08:34 +0000609 CGBuilderTy &Builder) {
610 // FIXME: Why is this using CurLoc???
611 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000612 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel7f9e2092009-04-08 19:47:04 +0000613 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000614
615 llvm::DISubprogram SP =
616 DebugFactory.CreateSubprogram(Unit, Name, Name, "", Unit, LineNo,
617 getOrCreateType(ReturnType, Unit),
618 Fn->hasInternalLinkage(), true/*definition*/);
619
620 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
621
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000622 // Push function on region stack.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000623 RegionStack.push_back(SP);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000624}
625
626
Chris Lattner562ce0a2008-11-10 06:08:34 +0000627void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000628 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
629
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000630 // Don't bother if things are the same as last time.
631 SourceManager &SM = M->getContext().getSourceManager();
632 if (CurLoc == PrevLoc
Chris Lattner2d89c562009-02-04 01:06:56 +0000633 || (SM.getInstantiationLineNumber(CurLoc) ==
634 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000635 && SM.isFromSameFile(CurLoc, PrevLoc)))
636 return;
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000637
638 // Update last state.
639 PrevLoc = CurLoc;
640
641 // Get the appropriate compile unit.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000642 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel7f9e2092009-04-08 19:47:04 +0000643 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
644 DebugFactory.InsertStopPoint(Unit, PLoc.getLine(), PLoc.getColumn(),
Chris Lattner562ce0a2008-11-10 06:08:34 +0000645 Builder.GetInsertBlock());
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000646}
647
648/// EmitRegionStart- Constructs the debug code for entering a declarative
649/// region - "llvm.dbg.region.start.".
Chris Lattner562ce0a2008-11-10 06:08:34 +0000650void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
651 llvm::DIDescriptor D;
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000652 if (!RegionStack.empty())
Chris Lattner562ce0a2008-11-10 06:08:34 +0000653 D = RegionStack.back();
654 D = DebugFactory.CreateBlock(D);
655 RegionStack.push_back(D);
656 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000657}
658
659/// EmitRegionEnd - Constructs the debug code for exiting a declarative
660/// region - "llvm.dbg.region.end."
Chris Lattner562ce0a2008-11-10 06:08:34 +0000661void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000662 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
663
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000664 // Provide an region stop point.
665 EmitStopPoint(Fn, Builder);
666
Chris Lattner562ce0a2008-11-10 06:08:34 +0000667 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000668 RegionStack.pop_back();
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000669}
670
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000671/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000672void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
673 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000674 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
675
Devang Patelbcded192009-03-27 23:16:32 +0000676 // Do not emit variable debug information while generating optimized code.
677 // The llvm optimizer and code generator are not yet ready to support
678 // optimized code debugging.
679 const CompileOptions &CO = M->getCompileOpts();
680 if (CO.OptimizationLevel)
681 return;
682
Chris Lattner562ce0a2008-11-10 06:08:34 +0000683 // Get location information.
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000684 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner18c8dc02009-01-16 07:36:28 +0000685 unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation());
Chris Lattner562ce0a2008-11-10 06:08:34 +0000686 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
687
688 // Create the descriptor for the variable.
689 llvm::DIVariable D =
Chris Lattner271d4c22008-11-24 05:29:24 +0000690 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner562ce0a2008-11-10 06:08:34 +0000691 Unit, Line,
692 getOrCreateType(Decl->getType(), Unit));
693 // Insert an llvm.dbg.declare into the current block.
694 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000695}
696
Chris Lattner562ce0a2008-11-10 06:08:34 +0000697void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
698 llvm::Value *Storage,
699 CGBuilderTy &Builder) {
700 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
701}
702
703/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
704/// variable declaration.
705void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
706 CGBuilderTy &Builder) {
707 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
708}
709
710
711
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000712/// EmitGlobalVariable - Emit information about a global variable.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000713void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
714 const VarDecl *Decl) {
Devang Patelbcded192009-03-27 23:16:32 +0000715
716 // Do not emit variable debug information while generating optimized code.
717 // The llvm optimizer and code generator are not yet ready to support
718 // optimized code debugging.
719 const CompileOptions &CO = M->getCompileOpts();
720 if (CO.OptimizationLevel)
721 return;
722
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000723 // Create global variable debug descriptor.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000724 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000725 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner18c8dc02009-01-16 07:36:28 +0000726 unsigned LineNo = SM.getInstantiationLineNumber(Decl->getLocation());
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000727
728 std::string Name = Decl->getNameAsString();
Anders Carlsson123485c2008-11-26 17:40:42 +0000729
730 QualType T = Decl->getType();
731 if (T->isIncompleteArrayType()) {
732
733 // CodeGen turns int[] into int[1] so we'll do the same here.
734 llvm::APSInt ConstVal(32);
735
736 ConstVal = 1;
737 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
738
739 T = M->getContext().getConstantArrayType(ET, ConstVal,
740 ArrayType::Normal, 0);
741 }
742
Chris Lattner562ce0a2008-11-10 06:08:34 +0000743 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Anders Carlsson123485c2008-11-26 17:40:42 +0000744 getOrCreateType(T, Unit),
Chris Lattner562ce0a2008-11-10 06:08:34 +0000745 Var->hasInternalLinkage(),
746 true/*definition*/, Var);
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000747}
748
Devang Patel3fc13e32009-02-26 21:10:26 +0000749/// EmitGlobalVariable - Emit information about an objective-c interface.
750void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
751 ObjCInterfaceDecl *Decl) {
752 // Create global variable debug descriptor.
753 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
754 SourceManager &SM = M->getContext().getSourceManager();
755 unsigned LineNo = SM.getInstantiationLineNumber(Decl->getLocation());
756
757 std::string Name = Decl->getNameAsString();
758
Chris Lattner46ee0f32009-04-01 06:23:52 +0000759 QualType T = M->getContext().getObjCInterfaceType(Decl);
Devang Patel3fc13e32009-02-26 21:10:26 +0000760 if (T->isIncompleteArrayType()) {
761
762 // CodeGen turns int[] into int[1] so we'll do the same here.
763 llvm::APSInt ConstVal(32);
764
765 ConstVal = 1;
766 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
767
768 T = M->getContext().getConstantArrayType(ET, ConstVal,
769 ArrayType::Normal, 0);
770 }
771
772 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
773 getOrCreateType(T, Unit),
774 Var->hasInternalLinkage(),
775 true/*definition*/, Var);
776}
777