blob: 83719e4681a6d51c6cecdd2f95d8511d3ed5a018 [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"
Devang Patel07739032009-03-27 23:16:32 +000022#include "clang/Frontend/CompileOptions.h"
Sanjiv Guptae8b9f5b2008-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 Guptae8b9f5b2008-05-08 08:54:20 +000028#include "llvm/ADT/StringExtras.h"
29#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000030#include "llvm/Support/Dwarf.h"
Devang Patel446c6192009-04-17 21:06:59 +000031#include "llvm/System/Path.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000032#include "llvm/Target/TargetMachine.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000033using namespace clang;
34using namespace clang::CodeGen;
35
36CGDebugInfo::CGDebugInfo(CodeGenModule *m)
Chris Lattner9c85ba32008-11-10 06:08:34 +000037 : M(m), DebugFactory(M->getModule()) {
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000038}
39
Chris Lattner9c85ba32008-11-10 06:08:34 +000040CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar66031a52008-10-17 16:15:48 +000041 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000042}
43
Chris Lattner9c85ba32008-11-10 06:08:34 +000044void CGDebugInfo::setLocation(SourceLocation Loc) {
45 if (Loc.isValid())
Chris Lattnerf7cf85b2009-01-16 07:36:28 +000046 CurLoc = M->getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000047}
48
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000049/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
Daniel Dunbar25f51dd2008-10-24 08:38:36 +000050/// one if necessary. This returns null for invalid source locations.
Chris Lattner9c85ba32008-11-10 06:08:34 +000051llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) {
Devang Patel446c6192009-04-17 21:06:59 +000052 // Get source file information.
53 const char *FileName = "<unknown>";
Devang Patel77820222009-02-24 23:16:03 +000054 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel446c6192009-04-17 21:06:59 +000055 unsigned FID;
Daniel Dunbar831570c2009-01-22 00:09:25 +000056 if (Loc.isValid()) {
Devang Patel446c6192009-04-17 21:06:59 +000057 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
58 FileName = PLoc.getFilename();
59 FID = PLoc.getIncludeLoc().getRawEncoding();
Daniel Dunbar831570c2009-01-22 00:09:25 +000060 }
61
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000062 // See if this compile unit has been used before.
Devang Patel446c6192009-04-17 21:06:59 +000063 llvm::DICompileUnit &Unit = CompileUnitCache[FID];
Chris Lattner9c85ba32008-11-10 06:08:34 +000064 if (!Unit.isNull()) return Unit;
Daniel Dunbarc9abc042009-04-08 05:11:16 +000065
Devang Patel446c6192009-04-17 21:06:59 +000066 // Get absolute path name.
67 llvm::sys::Path AbsFileName(FileName);
68 if (!AbsFileName.isAbsolute()) {
69 llvm::sys::Path tmp = llvm::sys::Path::GetCurrentDirectory();
70 tmp.appendComponent(FileName);
71 AbsFileName = tmp;
72 }
73
74 // See if thie compile unit is represnting main source file.
75 bool isMain = false;
76 const LangOptions &LO = M->getLangOptions();
77 const char *MainFileName = LO.getMainFileName();
78 if (MainFileName) {
79 if (!strcmp(AbsFileName.getLast().c_str(), MainFileName))
80 isMain = true;
81 } else {
82 if (Loc.isValid() && SM.isFromMainFile(Loc))
83 isMain = true;
84 }
Daniel Dunbarc9abc042009-04-08 05:11:16 +000085
Chris Lattner515455a2009-03-25 03:28:08 +000086 unsigned LangTag;
87 if (LO.CPlusPlus) {
88 if (LO.ObjC1)
89 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
90 else
91 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
92 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +000093 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +000094 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +000095 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +000096 } else {
97 LangTag = llvm::dwarf::DW_LANG_C89;
98 }
Devang Patel446c6192009-04-17 21:06:59 +000099
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000100 // Create new compile unit.
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000101 // FIXME: Do not know how to get clang version yet.
Devang Patelc20482b2009-03-19 00:23:53 +0000102 // FIXME: Encode command line options.
103 // FIXME: Encode optimization level.
Devang Patel446c6192009-04-17 21:06:59 +0000104 return Unit = DebugFactory.CreateCompileUnit(LangTag, AbsFileName.getLast(),
105 AbsFileName.getDirname(),
Devang Patel8d9aefc2009-03-24 20:35:51 +0000106 "clang", isMain);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000107}
108
Devang Patel65e99f22009-02-25 01:36:11 +0000109/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000110/// one if necessary.
111llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel65e99f22009-02-25 01:36:11 +0000112 llvm::DICompileUnit Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000113 unsigned Encoding = 0;
114 switch (BT->getKind()) {
115 default:
116 case BuiltinType::Void:
117 return llvm::DIType();
118 case BuiltinType::UChar:
119 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
120 case BuiltinType::Char_S:
121 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
122 case BuiltinType::UShort:
123 case BuiltinType::UInt:
124 case BuiltinType::ULong:
125 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
126 case BuiltinType::Short:
127 case BuiltinType::Int:
128 case BuiltinType::Long:
129 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
130 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
131 case BuiltinType::Float:
132 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
133 }
134 // Bit size, align and offset of the type.
135 uint64_t Size = M->getContext().getTypeSize(BT);
136 uint64_t Align = M->getContext().getTypeAlign(BT);
137 uint64_t Offset = 0;
138
139 return DebugFactory.CreateBasicType(Unit, BT->getName(), Unit, 0, Size, Align,
140 Offset, /*flags*/ 0, Encoding);
141}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000142
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000143/// getOrCreateCVRType - Get the CVR qualified type from the cache or create
144/// a new one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000145llvm::DIType CGDebugInfo::CreateCVRType(QualType Ty, llvm::DICompileUnit Unit) {
146 // We will create one Derived type for one qualifier and recurse to handle any
147 // additional ones.
148 llvm::DIType FromTy;
149 unsigned Tag;
150 if (Ty.isConstQualified()) {
151 Tag = llvm::dwarf::DW_TAG_const_type;
152 Ty.removeConst();
153 FromTy = getOrCreateType(Ty, Unit);
154 } else if (Ty.isVolatileQualified()) {
155 Tag = llvm::dwarf::DW_TAG_volatile_type;
156 Ty.removeVolatile();
157 FromTy = getOrCreateType(Ty, Unit);
158 } else {
159 assert(Ty.isRestrictQualified() && "Unknown type qualifier for debug info");
160 Tag = llvm::dwarf::DW_TAG_restrict_type;
161 Ty.removeRestrict();
162 FromTy = getOrCreateType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000163 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000164
Daniel Dunbar3845f862008-10-31 03:54:29 +0000165 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
166 // CVR derived types.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000167 return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
168 0, 0, 0, 0, 0, FromTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000169}
170
Chris Lattner9c85ba32008-11-10 06:08:34 +0000171llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
172 llvm::DICompileUnit Unit) {
173 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000174
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000175 // Bit size, align and offset of the type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000176 uint64_t Size = M->getContext().getTypeSize(Ty);
177 uint64_t Align = M->getContext().getTypeAlign(Ty);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000178
Chris Lattner9c85ba32008-11-10 06:08:34 +0000179 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
180 "", llvm::DICompileUnit(),
181 0, Size, Align, 0, 0, EltTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000182}
183
Chris Lattner9c85ba32008-11-10 06:08:34 +0000184llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
185 llvm::DICompileUnit Unit) {
186 // Typedefs are derived from some other type. If we have a typedef of a
187 // typedef, make sure to emit the whole chain.
188 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
189
190 // We don't set size information, but do specify where the typedef was
191 // declared.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000192 std::string TyName = Ty->getDecl()->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000193 SourceLocation DefLoc = Ty->getDecl()->getLocation();
194 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000195
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000196 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000197 uint64_t Line = SM.getInstantiationLineNumber(DefLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000198
Chris Lattner9c85ba32008-11-10 06:08:34 +0000199 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
200 TyName, DefUnit, Line, 0, 0, 0, 0, Src);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000201}
202
Chris Lattner9c85ba32008-11-10 06:08:34 +0000203llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
204 llvm::DICompileUnit Unit) {
205 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000206
Chris Lattner9c85ba32008-11-10 06:08:34 +0000207 // Add the result type at least.
208 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
209
210 // Set up remainder of arguments if there is a prototype.
211 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000212 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000213 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
214 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
215 } else {
216 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000217 }
218
Chris Lattner9c85ba32008-11-10 06:08:34 +0000219 llvm::DIArray EltTypeArray =
220 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
221
222 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
223 Unit, "", llvm::DICompileUnit(),
224 0, 0, 0, 0, 0,
225 llvm::DIType(), EltTypeArray);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000226}
227
Devang Patel65e99f22009-02-25 01:36:11 +0000228/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000229llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
230 llvm::DICompileUnit Unit) {
Douglas Gregora4c46df2008-12-11 17:59:21 +0000231 RecordDecl *Decl = Ty->getDecl();
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000232
Chris Lattner9c85ba32008-11-10 06:08:34 +0000233 unsigned Tag;
234 if (Decl->isStruct())
235 Tag = llvm::dwarf::DW_TAG_structure_type;
236 else if (Decl->isUnion())
237 Tag = llvm::dwarf::DW_TAG_union_type;
238 else {
239 assert(Decl->isClass() && "Unknown RecordType!");
240 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000241 }
242
Sanjiv Gupta507de852008-06-09 10:47:41 +0000243 SourceManager &SM = M->getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000244
Chris Lattner9c85ba32008-11-10 06:08:34 +0000245 // Get overall information about the record type for the debug info.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000246 std::string Name = Decl->getNameAsString();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000247
Chris Lattner9c85ba32008-11-10 06:08:34 +0000248 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000249 unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000250
251
252 // Records and classes and unions can all be recursive. To handle them, we
253 // first generate a debug descriptor for the struct as a forward declaration.
254 // Then (if it is a definition) we go through and get debug info for all of
255 // its members. Finally, we create a descriptor for the complete type (which
256 // may refer to the forward decl if the struct is recursive) and replace all
257 // uses of the forward declaration with the final definition.
258 llvm::DIType FwdDecl =
259 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
260 llvm::DIType(), llvm::DIArray());
261
262 // If this is just a forward declaration, return it.
263 if (!Decl->getDefinition(M->getContext()))
264 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000265
Chris Lattner9c85ba32008-11-10 06:08:34 +0000266 // Otherwise, insert it into the TypeCache so that recursive uses will find
267 // it.
268 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
269
270 // Convert all the elements.
271 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
272
273 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl);
274
275 unsigned FieldNo = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000276 for (RecordDecl::field_iterator I = Decl->field_begin(M->getContext()),
277 E = Decl->field_end(M->getContext());
Douglas Gregora4c46df2008-12-11 17:59:21 +0000278 I != E; ++I, ++FieldNo) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000279 FieldDecl *Field = *I;
280 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattner8ec03f52008-11-24 03:54:41 +0000281
282 std::string FieldName = Field->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000283
284 // Get the location for the field.
285 SourceLocation FieldDefLoc = Field->getLocation();
286 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000287 unsigned FieldLine = SM.getInstantiationLineNumber(FieldDefLoc);
Devang Patelec9b5d52009-03-16 23:47:53 +0000288
289 QualType FType = Field->getType();
290 uint64_t FieldSize = 0;
291 unsigned FieldAlign = 0;
292 if (!FType->isIncompleteArrayType()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000293
Devang Patelec9b5d52009-03-16 23:47:53 +0000294 // Bit size, align and offset of the type.
295 FieldSize = M->getContext().getTypeSize(FType);
296 Expr *BitWidth = Field->getBitWidth();
297 if (BitWidth)
298 FieldSize =
299 BitWidth->getIntegerConstantExprValue(M->getContext()).getZExtValue();
300
301 FieldAlign = M->getContext().getTypeAlign(FType);
302 }
303
Chris Lattner9c85ba32008-11-10 06:08:34 +0000304 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
305
306 // Create a DW_TAG_member node to remember the offset of this field in the
307 // struct. FIXME: This is an absolutely insane way to capture this
308 // information. When we gut debug info, this should be fixed.
309 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
310 FieldName, FieldDefUnit,
311 FieldLine, FieldSize, FieldAlign,
312 FieldOffset, 0, FieldTy);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000313 EltTys.push_back(FieldTy);
314 }
315
316 llvm::DIArray Elements =
317 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
318
319 // Bit size, align and offset of the type.
320 uint64_t Size = M->getContext().getTypeSize(Ty);
321 uint64_t Align = M->getContext().getTypeAlign(Ty);
322
323 llvm::DIType RealDecl =
324 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
325 Align, 0, 0, llvm::DIType(), Elements);
326
327 // Now that we have a real decl for the struct, replace anything using the
328 // old decl with the new one. This will recursively update the debug info.
329 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
330 FwdDecl.getGV()->eraseFromParent();
331
332 return RealDecl;
333}
334
Devang Patel9ca36b62009-02-26 21:10:26 +0000335/// CreateType - get objective-c interface type.
336llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
337 llvm::DICompileUnit Unit) {
338 ObjCInterfaceDecl *Decl = Ty->getDecl();
339
340 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
341 SourceManager &SM = M->getContext().getSourceManager();
342
343 // Get overall information about the record type for the debug info.
344 std::string Name = Decl->getNameAsString();
345
346 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
347 unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation());
348
349
350 // To handle recursive interface, we
351 // first generate a debug descriptor for the struct as a forward declaration.
352 // Then (if it is a definition) we go through and get debug info for all of
353 // its members. Finally, we create a descriptor for the complete type (which
354 // may refer to the forward decl if the struct is recursive) and replace all
355 // uses of the forward declaration with the final definition.
356 llvm::DIType FwdDecl =
357 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
358 llvm::DIType(), llvm::DIArray());
359
360 // If this is just a forward declaration, return it.
361 if (Decl->isForwardDecl())
362 return FwdDecl;
363
364 // Otherwise, insert it into the TypeCache so that recursive uses will find
365 // it.
366 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
367
368 // Convert all the elements.
369 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
370
Devang Patelfbe899f2009-03-10 21:30:26 +0000371 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
372 if (SClass) {
373 llvm::DIType SClassTy =
374 getOrCreateType(M->getContext().getObjCInterfaceType(SClass), Unit);
375 llvm::DIType InhTag =
376 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
377 Unit, "", Unit, 0, 0, 0,
378 0 /* offset */, 0, SClassTy);
379 EltTys.push_back(InhTag);
380 }
381
Devang Patel9ca36b62009-02-26 21:10:26 +0000382 const ASTRecordLayout &RL = M->getContext().getASTObjCInterfaceLayout(Decl);
383
384 unsigned FieldNo = 0;
385 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
386 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
387 ObjCIvarDecl *Field = *I;
388 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
389
390 std::string FieldName = Field->getNameAsString();
391
392 // Get the location for the field.
393 SourceLocation FieldDefLoc = Field->getLocation();
394 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
395 unsigned FieldLine = SM.getInstantiationLineNumber(FieldDefLoc);
Devang Patel99c20eb2009-03-20 18:24:39 +0000396
397 QualType FType = Field->getType();
398 uint64_t FieldSize = 0;
399 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000400
Devang Patel99c20eb2009-03-20 18:24:39 +0000401 if (!FType->isIncompleteArrayType()) {
402
403 // Bit size, align and offset of the type.
404 FieldSize = M->getContext().getTypeSize(FType);
405 Expr *BitWidth = Field->getBitWidth();
406 if (BitWidth)
407 FieldSize =
408 BitWidth->getIntegerConstantExprValue(M->getContext()).getZExtValue();
409
410 FieldAlign = M->getContext().getTypeAlign(FType);
411 }
412
413 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
414
Devang Patelc20482b2009-03-19 00:23:53 +0000415 unsigned Flags = 0;
416 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
417 Flags = llvm::DIType::FlagProtected;
418 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
419 Flags = llvm::DIType::FlagPrivate;
420
Devang Patel9ca36b62009-02-26 21:10:26 +0000421 // Create a DW_TAG_member node to remember the offset of this field in the
422 // struct. FIXME: This is an absolutely insane way to capture this
423 // information. When we gut debug info, this should be fixed.
424 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
425 FieldName, FieldDefUnit,
426 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000427 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000428 EltTys.push_back(FieldTy);
429 }
430
431 llvm::DIArray Elements =
432 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
433
434 // Bit size, align and offset of the type.
435 uint64_t Size = M->getContext().getTypeSize(Ty);
436 uint64_t Align = M->getContext().getTypeAlign(Ty);
437
438 llvm::DIType RealDecl =
439 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
440 Align, 0, 0, llvm::DIType(), Elements);
441
442 // Now that we have a real decl for the struct, replace anything using the
443 // old decl with the new one. This will recursively update the debug info.
444 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
445 FwdDecl.getGV()->eraseFromParent();
446
447 return RealDecl;
448}
449
Chris Lattner9c85ba32008-11-10 06:08:34 +0000450llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
451 llvm::DICompileUnit Unit) {
452 EnumDecl *Decl = Ty->getDecl();
453
454 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
455
456 // Create DIEnumerator elements for each enumerator.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000457 for (EnumDecl::enumerator_iterator
458 Enum = Decl->enumerator_begin(M->getContext()),
459 EnumEnd = Decl->enumerator_end(M->getContext());
Douglas Gregor44b43212008-12-11 16:49:14 +0000460 Enum != EnumEnd; ++Enum) {
461 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(),
462 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000463 }
464
465 // Return a CompositeType for the enum itself.
466 llvm::DIArray EltArray =
467 DebugFactory.GetOrCreateArray(&Enumerators[0], Enumerators.size());
468
Chris Lattner8ec03f52008-11-24 03:54:41 +0000469 std::string EnumName = Decl->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000470 SourceLocation DefLoc = Decl->getLocation();
471 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
472 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000473 unsigned Line = SM.getInstantiationLineNumber(DefLoc);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000474
475 // Size and align of the type.
476 uint64_t Size = M->getContext().getTypeSize(Ty);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000477 unsigned Align = M->getContext().getTypeAlign(Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000478
479 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
480 Unit, EnumName, DefUnit, Line,
481 Size, Align, 0, 0,
482 llvm::DIType(), EltArray);
483}
484
485llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
486 llvm::DICompileUnit Unit) {
487 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
488 return CreateType(RT, Unit);
489 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
490 return CreateType(ET, Unit);
491
492 return llvm::DIType();
493}
494
495llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
496 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000497 uint64_t Size;
498 uint64_t Align;
499
500
Nuno Lopes010d5142009-01-28 00:35:17 +0000501 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +0000502 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000503 Size = 0;
504 Align =
Nuno Lopes010d5142009-01-28 00:35:17 +0000505 M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT));
506 } else if (Ty->isIncompleteArrayType()) {
507 Size = 0;
508 Align = M->getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +0000509 } else {
510 // Size and align of the whole array, not the element type.
511 Size = M->getContext().getTypeSize(Ty);
512 Align = M->getContext().getTypeAlign(Ty);
513 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000514
515 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
516 // interior arrays, do we care? Why aren't nested arrays represented the
517 // obvious/recursive way?
518 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
519 QualType EltTy(Ty, 0);
520 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000521 uint64_t Upper = 0;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000522 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
523 Upper = CAT->getSize().getZExtValue() - 1;
524 // FIXME: Verify this is right for VLAs.
525 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
526 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000527 }
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000528
Chris Lattner9c85ba32008-11-10 06:08:34 +0000529 llvm::DIArray SubscriptArray =
530 DebugFactory.GetOrCreateArray(&Subscripts[0], Subscripts.size());
531
532 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
533 Unit, "", llvm::DICompileUnit(),
534 0, Size, Align, 0, 0,
535 getOrCreateType(EltTy, Unit),
536 SubscriptArray);
537}
538
539
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000540/// getOrCreateType - Get the type from the cache or create a new
541/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000542llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
543 llvm::DICompileUnit Unit) {
544 if (Ty.isNull())
545 return llvm::DIType();
546
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000547 // Check to see if the compile unit already has created this type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000548 llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()];
549 if (!Slot.isNull()) return Slot;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000550
Chris Lattner9c85ba32008-11-10 06:08:34 +0000551 // Handle CVR qualifiers, which recursively handles what they refer to.
552 if (Ty.getCVRQualifiers())
553 return Slot = CreateCVRType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000554
555 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000556 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000557#define TYPE(Class, Base)
558#define ABSTRACT_TYPE(Class, Base)
559#define NON_CANONICAL_TYPE(Class, Base)
560#define DEPENDENT_TYPE(Class, Base) case Type::Class:
561#include "clang/AST/TypeNodes.def"
562 assert(false && "Dependent types cannot show up in debug information");
563
Chris Lattner9c85ba32008-11-10 06:08:34 +0000564 case Type::Complex:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000565 case Type::LValueReference:
566 case Type::RValueReference:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000567 case Type::Vector:
568 case Type::ExtVector:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000569 case Type::ExtQual:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000570 case Type::ObjCQualifiedInterface:
571 case Type::ObjCQualifiedId:
Eli Friedman00524e32009-02-27 23:15:07 +0000572 case Type::FixedWidthInt:
573 case Type::BlockPointer:
574 case Type::MemberPointer:
Douglas Gregor7532dc62009-03-30 22:58:21 +0000575 case Type::TemplateSpecialization:
Douglas Gregore4e5b052009-03-19 00:18:19 +0000576 case Type::QualifiedName:
Eli Friedman00524e32009-02-27 23:15:07 +0000577 case Type::ObjCQualifiedClass:
578 // Unsupported types
Chris Lattner9c85ba32008-11-10 06:08:34 +0000579 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000580
Devang Patele7987062009-03-02 17:58:28 +0000581 case Type::ObjCInterface:
582 Slot = CreateType(cast<ObjCInterfaceType>(Ty), Unit); break;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000583 case Type::Builtin: Slot = CreateType(cast<BuiltinType>(Ty), Unit); break;
584 case Type::Pointer: Slot = CreateType(cast<PointerType>(Ty), Unit); break;
Douglas Gregor72564e72009-02-26 23:50:07 +0000585 case Type::Typedef: Slot = CreateType(cast<TypedefType>(Ty), Unit); break;
586 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000587 case Type::Enum:
588 Slot = CreateType(cast<TagType>(Ty), Unit);
589 break;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000590 case Type::FunctionProto:
591 case Type::FunctionNoProto:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000592 return Slot = CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000593
594 case Type::ConstantArray:
595 case Type::VariableArray:
596 case Type::IncompleteArray:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000597 return Slot = CreateType(cast<ArrayType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000598 case Type::TypeOfExpr:
599 return Slot = getOrCreateType(cast<TypeOfExprType>(Ty)->getUnderlyingExpr()
Chris Lattner3cc5c402008-11-11 07:01:36 +0000600 ->getType(), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000601 case Type::TypeOf:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000602 return Slot = getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(),
603 Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000604 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000605
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000606 return Slot;
607}
608
609/// EmitFunctionStart - Constructs the debug code for entering a function -
610/// "llvm.dbg.func.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000611void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000612 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000613 CGBuilderTy &Builder) {
614 // FIXME: Why is this using CurLoc???
615 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000616 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +0000617 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000618
619 llvm::DISubprogram SP =
620 DebugFactory.CreateSubprogram(Unit, Name, Name, "", Unit, LineNo,
621 getOrCreateType(ReturnType, Unit),
622 Fn->hasInternalLinkage(), true/*definition*/);
623
624 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
625
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000626 // Push function on region stack.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000627 RegionStack.push_back(SP);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000628}
629
630
Chris Lattner9c85ba32008-11-10 06:08:34 +0000631void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000632 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
633
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000634 // Don't bother if things are the same as last time.
635 SourceManager &SM = M->getContext().getSourceManager();
636 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +0000637 || (SM.getInstantiationLineNumber(CurLoc) ==
638 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000639 && SM.isFromSameFile(CurLoc, PrevLoc)))
640 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000641
642 // Update last state.
643 PrevLoc = CurLoc;
644
645 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000646 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +0000647 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
648 DebugFactory.InsertStopPoint(Unit, PLoc.getLine(), PLoc.getColumn(),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000649 Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000650}
651
652/// EmitRegionStart- Constructs the debug code for entering a declarative
653/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000654void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
655 llvm::DIDescriptor D;
Daniel Dunbar5273f512008-10-17 01:07:56 +0000656 if (!RegionStack.empty())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000657 D = RegionStack.back();
658 D = DebugFactory.CreateBlock(D);
659 RegionStack.push_back(D);
660 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000661}
662
663/// EmitRegionEnd - Constructs the debug code for exiting a declarative
664/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +0000665void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000666 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
667
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000668 // Provide an region stop point.
669 EmitStopPoint(Fn, Builder);
670
Chris Lattner9c85ba32008-11-10 06:08:34 +0000671 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000672 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000673}
674
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000675/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000676void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
677 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000678 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
679
Devang Patel07739032009-03-27 23:16:32 +0000680 // Do not emit variable debug information while generating optimized code.
681 // The llvm optimizer and code generator are not yet ready to support
682 // optimized code debugging.
683 const CompileOptions &CO = M->getCompileOpts();
684 if (CO.OptimizationLevel)
685 return;
686
Chris Lattner9c85ba32008-11-10 06:08:34 +0000687 // Get location information.
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000688 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000689 unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000690 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
691
692 // Create the descriptor for the variable.
693 llvm::DIVariable D =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000694 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000695 Unit, Line,
696 getOrCreateType(Decl->getType(), Unit));
697 // Insert an llvm.dbg.declare into the current block.
698 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000699}
700
Chris Lattner9c85ba32008-11-10 06:08:34 +0000701void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
702 llvm::Value *Storage,
703 CGBuilderTy &Builder) {
704 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
705}
706
707/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
708/// variable declaration.
709void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
710 CGBuilderTy &Builder) {
711 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
712}
713
714
715
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000716/// EmitGlobalVariable - Emit information about a global variable.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000717void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
718 const VarDecl *Decl) {
Devang Patel07739032009-03-27 23:16:32 +0000719
720 // Do not emit variable debug information while generating optimized code.
721 // The llvm optimizer and code generator are not yet ready to support
722 // optimized code debugging.
723 const CompileOptions &CO = M->getCompileOpts();
724 if (CO.OptimizationLevel)
725 return;
726
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000727 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000728 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000729 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000730 unsigned LineNo = SM.getInstantiationLineNumber(Decl->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +0000731
732 std::string Name = Decl->getNameAsString();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000733
734 QualType T = Decl->getType();
735 if (T->isIncompleteArrayType()) {
736
737 // CodeGen turns int[] into int[1] so we'll do the same here.
738 llvm::APSInt ConstVal(32);
739
740 ConstVal = 1;
741 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
742
743 T = M->getContext().getConstantArrayType(ET, ConstVal,
744 ArrayType::Normal, 0);
745 }
746
Chris Lattner9c85ba32008-11-10 06:08:34 +0000747 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000748 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000749 Var->hasInternalLinkage(),
750 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000751}
752
Devang Patel9ca36b62009-02-26 21:10:26 +0000753/// EmitGlobalVariable - Emit information about an objective-c interface.
754void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
755 ObjCInterfaceDecl *Decl) {
756 // Create global variable debug descriptor.
757 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
758 SourceManager &SM = M->getContext().getSourceManager();
759 unsigned LineNo = SM.getInstantiationLineNumber(Decl->getLocation());
760
761 std::string Name = Decl->getNameAsString();
762
Chris Lattner03d9f342009-04-01 06:23:52 +0000763 QualType T = M->getContext().getObjCInterfaceType(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000764 if (T->isIncompleteArrayType()) {
765
766 // CodeGen turns int[] into int[1] so we'll do the same here.
767 llvm::APSInt ConstVal(32);
768
769 ConstVal = 1;
770 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
771
772 T = M->getContext().getConstantArrayType(ET, ConstVal,
773 ArrayType::Normal, 0);
774 }
775
776 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
777 getOrCreateType(T, Unit),
778 Var->hasInternalLinkage(),
779 true/*definition*/, Var);
780}
781