blob: c871b7bcd5fc2652f09038ea80eb350fc7a2f84f [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)
Mike Stump9bc093c2009-05-14 02:03:51 +000037 : M(m), isMainCompileUnitCreated(false), DebugFactory(M->getModule()),
38 BlockLiteralGenericSet(false) {
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000039}
40
Chris Lattner9c85ba32008-11-10 06:08:34 +000041CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar66031a52008-10-17 16:15:48 +000042 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000043}
44
Chris Lattner9c85ba32008-11-10 06:08:34 +000045void CGDebugInfo::setLocation(SourceLocation Loc) {
46 if (Loc.isValid())
Chris Lattnerf7cf85b2009-01-16 07:36:28 +000047 CurLoc = M->getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000048}
49
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000050/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
Daniel Dunbar25f51dd2008-10-24 08:38:36 +000051/// one if necessary. This returns null for invalid source locations.
Chris Lattner9c85ba32008-11-10 06:08:34 +000052llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) {
Devang Patel446c6192009-04-17 21:06:59 +000053 // Get source file information.
54 const char *FileName = "<unknown>";
Devang Patel77820222009-02-24 23:16:03 +000055 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattneradb1a6f2009-04-19 06:50:29 +000056 unsigned FID = 0;
Daniel Dunbar831570c2009-01-22 00:09:25 +000057 if (Loc.isValid()) {
Devang Patel446c6192009-04-17 21:06:59 +000058 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
59 FileName = PLoc.getFilename();
60 FID = PLoc.getIncludeLoc().getRawEncoding();
Daniel Dunbar831570c2009-01-22 00:09:25 +000061 }
62
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000063 // See if this compile unit has been used before.
Devang Patel446c6192009-04-17 21:06:59 +000064 llvm::DICompileUnit &Unit = CompileUnitCache[FID];
Chris Lattner9c85ba32008-11-10 06:08:34 +000065 if (!Unit.isNull()) return Unit;
Daniel Dunbarc9abc042009-04-08 05:11:16 +000066
Devang Patel446c6192009-04-17 21:06:59 +000067 // Get absolute path name.
68 llvm::sys::Path AbsFileName(FileName);
69 if (!AbsFileName.isAbsolute()) {
70 llvm::sys::Path tmp = llvm::sys::Path::GetCurrentDirectory();
71 tmp.appendComponent(FileName);
72 AbsFileName = tmp;
73 }
74
Devang Patel87de6492009-04-23 18:09:16 +000075 // See if thie compile unit is representing main source file. Each source
76 // file has corresponding compile unit. There is only one main source
77 // file at a time.
Devang Patel446c6192009-04-17 21:06:59 +000078 bool isMain = false;
79 const LangOptions &LO = M->getLangOptions();
80 const char *MainFileName = LO.getMainFileName();
Devang Patel87de6492009-04-23 18:09:16 +000081 if (isMainCompileUnitCreated == false) {
82 if (MainFileName) {
83 if (!strcmp(AbsFileName.getLast().c_str(), MainFileName))
84 isMain = true;
85 } else {
86 if (Loc.isValid() && SM.isFromMainFile(Loc))
87 isMain = true;
88 }
89 if (isMain)
90 isMainCompileUnitCreated = true;
Devang Patel446c6192009-04-17 21:06:59 +000091 }
Daniel Dunbarc9abc042009-04-08 05:11:16 +000092
Chris Lattner515455a2009-03-25 03:28:08 +000093 unsigned LangTag;
94 if (LO.CPlusPlus) {
95 if (LO.ObjC1)
96 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
97 else
98 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
99 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000100 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000101 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000102 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000103 } else {
104 LangTag = llvm::dwarf::DW_LANG_C89;
105 }
Devang Patel446c6192009-04-17 21:06:59 +0000106
Chris Lattnerb95ee582009-05-02 01:04:13 +0000107 std::string Producer = "clang 1.0";// FIXME: clang version.
108 bool isOptimized = LO.Optimize;
Chris Lattner4c2577a2009-05-02 01:00:04 +0000109 const char *Flags = ""; // FIXME: Encode command line options.
110
111 // Figure out which version of the ObjC runtime we have.
112 unsigned RuntimeVers = 0;
113 if (LO.ObjC1)
114 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
115
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000116 // Create new compile unit.
Devang Patel446c6192009-04-17 21:06:59 +0000117 return Unit = DebugFactory.CreateCompileUnit(LangTag, AbsFileName.getLast(),
118 AbsFileName.getDirname(),
Chris Lattner4c2577a2009-05-02 01:00:04 +0000119 Producer, isMain, isOptimized,
120 Flags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000121}
122
Devang Patel65e99f22009-02-25 01:36:11 +0000123/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000124/// one if necessary.
125llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel65e99f22009-02-25 01:36:11 +0000126 llvm::DICompileUnit Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000127 unsigned Encoding = 0;
128 switch (BT->getKind()) {
129 default:
130 case BuiltinType::Void:
131 return llvm::DIType();
132 case BuiltinType::UChar:
133 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
134 case BuiltinType::Char_S:
135 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
136 case BuiltinType::UShort:
137 case BuiltinType::UInt:
138 case BuiltinType::ULong:
139 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
140 case BuiltinType::Short:
141 case BuiltinType::Int:
142 case BuiltinType::Long:
143 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
144 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
145 case BuiltinType::Float:
146 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
147 }
148 // Bit size, align and offset of the type.
149 uint64_t Size = M->getContext().getTypeSize(BT);
150 uint64_t Align = M->getContext().getTypeAlign(BT);
151 uint64_t Offset = 0;
152
153 return DebugFactory.CreateBasicType(Unit, BT->getName(), Unit, 0, Size, Align,
154 Offset, /*flags*/ 0, Encoding);
155}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000156
Chris Lattnerb7003772009-04-23 06:13:01 +0000157llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
158 llvm::DICompileUnit Unit) {
159 // Bit size, align and offset of the type.
160 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
161 if (Ty->isComplexIntegerType())
162 Encoding = llvm::dwarf::DW_ATE_lo_user;
163
164 uint64_t Size = M->getContext().getTypeSize(Ty);
165 uint64_t Align = M->getContext().getTypeAlign(Ty);
166 uint64_t Offset = 0;
167
168 return DebugFactory.CreateBasicType(Unit, "complex",
169 Unit, 0, Size, Align,
170 Offset, /*flags*/ 0, Encoding);
171}
172
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000173/// getOrCreateCVRType - Get the CVR qualified type from the cache or create
174/// a new one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000175llvm::DIType CGDebugInfo::CreateCVRType(QualType Ty, llvm::DICompileUnit Unit) {
176 // We will create one Derived type for one qualifier and recurse to handle any
177 // additional ones.
178 llvm::DIType FromTy;
179 unsigned Tag;
180 if (Ty.isConstQualified()) {
181 Tag = llvm::dwarf::DW_TAG_const_type;
182 Ty.removeConst();
183 FromTy = getOrCreateType(Ty, Unit);
184 } else if (Ty.isVolatileQualified()) {
185 Tag = llvm::dwarf::DW_TAG_volatile_type;
186 Ty.removeVolatile();
187 FromTy = getOrCreateType(Ty, Unit);
188 } else {
189 assert(Ty.isRestrictQualified() && "Unknown type qualifier for debug info");
190 Tag = llvm::dwarf::DW_TAG_restrict_type;
191 Ty.removeRestrict();
192 FromTy = getOrCreateType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000193 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000194
Daniel Dunbar3845f862008-10-31 03:54:29 +0000195 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
196 // CVR derived types.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000197 return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
198 0, 0, 0, 0, 0, FromTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000199}
200
Chris Lattner9c85ba32008-11-10 06:08:34 +0000201llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
202 llvm::DICompileUnit Unit) {
203 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000204
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000205 // Bit size, align and offset of the type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000206 uint64_t Size = M->getContext().getTypeSize(Ty);
207 uint64_t Align = M->getContext().getTypeAlign(Ty);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000208
Chris Lattner9c85ba32008-11-10 06:08:34 +0000209 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
210 "", llvm::DICompileUnit(),
211 0, Size, Align, 0, 0, EltTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000212}
213
Mike Stump9bc093c2009-05-14 02:03:51 +0000214llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
215 llvm::DICompileUnit Unit) {
216 if (BlockLiteralGenericSet)
217 return BlockLiteralGeneric;
218
219 llvm::DICompileUnit DefUnit;
220 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
221
222 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
223
224 llvm::DIType FieldTy;
225
226 QualType FType;
227 uint64_t FieldSize, FieldOffset;
228 unsigned FieldAlign;
229
230 llvm::DIArray Elements;
231 llvm::DIType EltTy, DescTy;
232
233 FieldOffset = 0;
234 FType = M->getContext().UnsignedLongTy;
235 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
236 FieldSize = M->getContext().getTypeSize(FType);
237 FieldAlign = M->getContext().getTypeAlign(FType);
238 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
239 "reserved", DefUnit,
240 0, FieldSize, FieldAlign,
241 FieldOffset, 0, FieldTy);
242 EltTys.push_back(FieldTy);
243
244 FieldOffset += FieldSize;
245 FType = M->getContext().UnsignedLongTy;
246 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
247 FieldSize = M->getContext().getTypeSize(FType);
248 FieldAlign = M->getContext().getTypeAlign(FType);
249 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
250 "Size", DefUnit,
251 0, FieldSize, FieldAlign,
252 FieldOffset, 0, FieldTy);
253 EltTys.push_back(FieldTy);
254
255 FieldOffset += FieldSize;
256 Elements = DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
257 EltTys.clear();
258
259 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
260 DefUnit, 0, FieldOffset, 0, 0, 0,
261 llvm::DIType(), Elements);
262
263 // Bit size, align and offset of the type.
264 uint64_t Size = M->getContext().getTypeSize(Ty);
265 uint64_t Align = M->getContext().getTypeAlign(Ty);
266
267 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
268 Unit, "", llvm::DICompileUnit(),
269 0, Size, Align, 0, 0, EltTy);
270
271 FieldOffset = 0;
272 FType = M->getContext().getPointerType(M->getContext().VoidTy);
273 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
274 FieldSize = M->getContext().getTypeSize(FType);
275 FieldAlign = M->getContext().getTypeAlign(FType);
276 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
277 "__isa", DefUnit,
278 0, FieldSize, FieldAlign,
279 FieldOffset, 0, FieldTy);
280 EltTys.push_back(FieldTy);
281
282 FieldOffset += FieldSize;
283 FType = M->getContext().IntTy;
284 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
285 FieldSize = M->getContext().getTypeSize(FType);
286 FieldAlign = M->getContext().getTypeAlign(FType);
287 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
288 "__flags", DefUnit,
289 0, FieldSize, FieldAlign,
290 FieldOffset, 0, FieldTy);
291 EltTys.push_back(FieldTy);
292
293 FieldOffset += FieldSize;
294 FType = M->getContext().IntTy;
295 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
296 FieldSize = M->getContext().getTypeSize(FType);
297 FieldAlign = M->getContext().getTypeAlign(FType);
298 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
299 "__reserved", DefUnit,
300 0, FieldSize, FieldAlign,
301 FieldOffset, 0, FieldTy);
302 EltTys.push_back(FieldTy);
303
304 FieldOffset += FieldSize;
305 FType = M->getContext().getPointerType(M->getContext().VoidTy);
306 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
307 FieldSize = M->getContext().getTypeSize(FType);
308 FieldAlign = M->getContext().getTypeAlign(FType);
309 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
310 "__FuncPtr", DefUnit,
311 0, FieldSize, FieldAlign,
312 FieldOffset, 0, FieldTy);
313 EltTys.push_back(FieldTy);
314
315 FieldOffset += FieldSize;
316 FType = M->getContext().getPointerType(M->getContext().VoidTy);
317 FieldTy = DescTy;
318 FieldSize = M->getContext().getTypeSize(Ty);
319 FieldAlign = M->getContext().getTypeAlign(Ty);
320 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
321 "__descriptor", DefUnit,
322 0, FieldSize, FieldAlign,
323 FieldOffset, 0, FieldTy);
324 EltTys.push_back(FieldTy);
325
326 FieldOffset += FieldSize;
327 Elements = DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
328
329 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
330 DefUnit, 0, FieldOffset, 0, 0, 0,
331 llvm::DIType(), Elements);
332
333 BlockLiteralGenericSet = true;
334 BlockLiteralGeneric
335 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
336 "", llvm::DICompileUnit(),
337 0, Size, Align, 0, 0, EltTy);
338 return BlockLiteralGeneric;
339}
340
Chris Lattner9c85ba32008-11-10 06:08:34 +0000341llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
342 llvm::DICompileUnit Unit) {
343 // Typedefs are derived from some other type. If we have a typedef of a
344 // typedef, make sure to emit the whole chain.
345 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
346
347 // We don't set size information, but do specify where the typedef was
348 // declared.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000349 std::string TyName = Ty->getDecl()->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000350 SourceLocation DefLoc = Ty->getDecl()->getLocation();
351 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000352
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000353 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000354 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
355 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000356
Chris Lattner9c85ba32008-11-10 06:08:34 +0000357 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
358 TyName, DefUnit, Line, 0, 0, 0, 0, Src);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000359}
360
Chris Lattner9c85ba32008-11-10 06:08:34 +0000361llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
362 llvm::DICompileUnit Unit) {
363 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000364
Chris Lattner9c85ba32008-11-10 06:08:34 +0000365 // Add the result type at least.
366 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
367
368 // Set up remainder of arguments if there is a prototype.
369 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000370 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000371 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
372 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
373 } else {
374 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000375 }
376
Chris Lattner9c85ba32008-11-10 06:08:34 +0000377 llvm::DIArray EltTypeArray =
378 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
379
380 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
381 Unit, "", llvm::DICompileUnit(),
382 0, 0, 0, 0, 0,
383 llvm::DIType(), EltTypeArray);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000384}
385
Devang Patel65e99f22009-02-25 01:36:11 +0000386/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000387llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
388 llvm::DICompileUnit Unit) {
Douglas Gregora4c46df2008-12-11 17:59:21 +0000389 RecordDecl *Decl = Ty->getDecl();
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000390
Chris Lattner9c85ba32008-11-10 06:08:34 +0000391 unsigned Tag;
392 if (Decl->isStruct())
393 Tag = llvm::dwarf::DW_TAG_structure_type;
394 else if (Decl->isUnion())
395 Tag = llvm::dwarf::DW_TAG_union_type;
396 else {
397 assert(Decl->isClass() && "Unknown RecordType!");
398 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000399 }
400
Sanjiv Gupta507de852008-06-09 10:47:41 +0000401 SourceManager &SM = M->getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000402
Chris Lattner9c85ba32008-11-10 06:08:34 +0000403 // Get overall information about the record type for the debug info.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000404 std::string Name = Decl->getNameAsString();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000405
Devang Patel4f6fa232009-04-17 21:35:15 +0000406 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000407 llvm::DICompileUnit DefUnit;
408 unsigned Line = 0;
409 if (!PLoc.isInvalid()) {
410 DefUnit = getOrCreateCompileUnit(Decl->getLocation());
411 Line = PLoc.getLine();
412 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000413
414 // Records and classes and unions can all be recursive. To handle them, we
415 // first generate a debug descriptor for the struct as a forward declaration.
416 // Then (if it is a definition) we go through and get debug info for all of
417 // its members. Finally, we create a descriptor for the complete type (which
418 // may refer to the forward decl if the struct is recursive) and replace all
419 // uses of the forward declaration with the final definition.
420 llvm::DIType FwdDecl =
421 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
422 llvm::DIType(), llvm::DIArray());
423
424 // If this is just a forward declaration, return it.
425 if (!Decl->getDefinition(M->getContext()))
426 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000427
Chris Lattner9c85ba32008-11-10 06:08:34 +0000428 // Otherwise, insert it into the TypeCache so that recursive uses will find
429 // it.
430 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
431
432 // Convert all the elements.
433 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
434
435 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl);
436
437 unsigned FieldNo = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000438 for (RecordDecl::field_iterator I = Decl->field_begin(M->getContext()),
439 E = Decl->field_end(M->getContext());
Douglas Gregora4c46df2008-12-11 17:59:21 +0000440 I != E; ++I, ++FieldNo) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000441 FieldDecl *Field = *I;
442 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattner8ec03f52008-11-24 03:54:41 +0000443
444 std::string FieldName = Field->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000445
Devang Patelde135022009-04-27 22:40:36 +0000446 // Ignore unnamed fields.
447 if (FieldName.empty())
448 continue;
449
Chris Lattner9c85ba32008-11-10 06:08:34 +0000450 // Get the location for the field.
451 SourceLocation FieldDefLoc = Field->getLocation();
Devang Patel4f6fa232009-04-17 21:35:15 +0000452 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000453 llvm::DICompileUnit FieldDefUnit;
454 unsigned FieldLine = 0;
455
456 if (!PLoc.isInvalid()) {
457 FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
458 FieldLine = PLoc.getLine();
459 }
Devang Patelec9b5d52009-03-16 23:47:53 +0000460
461 QualType FType = Field->getType();
462 uint64_t FieldSize = 0;
463 unsigned FieldAlign = 0;
464 if (!FType->isIncompleteArrayType()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000465
Devang Patelec9b5d52009-03-16 23:47:53 +0000466 // Bit size, align and offset of the type.
467 FieldSize = M->getContext().getTypeSize(FType);
468 Expr *BitWidth = Field->getBitWidth();
469 if (BitWidth)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000470 FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue();
Devang Patelec9b5d52009-03-16 23:47:53 +0000471
472 FieldAlign = M->getContext().getTypeAlign(FType);
473 }
474
Chris Lattner9c85ba32008-11-10 06:08:34 +0000475 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
476
477 // Create a DW_TAG_member node to remember the offset of this field in the
478 // struct. FIXME: This is an absolutely insane way to capture this
479 // information. When we gut debug info, this should be fixed.
480 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
481 FieldName, FieldDefUnit,
482 FieldLine, FieldSize, FieldAlign,
483 FieldOffset, 0, FieldTy);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000484 EltTys.push_back(FieldTy);
485 }
486
487 llvm::DIArray Elements =
488 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
489
490 // Bit size, align and offset of the type.
491 uint64_t Size = M->getContext().getTypeSize(Ty);
492 uint64_t Align = M->getContext().getTypeAlign(Ty);
493
494 llvm::DIType RealDecl =
495 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
496 Align, 0, 0, llvm::DIType(), Elements);
497
498 // Now that we have a real decl for the struct, replace anything using the
499 // old decl with the new one. This will recursively update the debug info.
500 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
501 FwdDecl.getGV()->eraseFromParent();
502
503 return RealDecl;
504}
505
Devang Patel9ca36b62009-02-26 21:10:26 +0000506/// CreateType - get objective-c interface type.
507llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
508 llvm::DICompileUnit Unit) {
509 ObjCInterfaceDecl *Decl = Ty->getDecl();
510
511 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
512 SourceManager &SM = M->getContext().getSourceManager();
513
514 // Get overall information about the record type for the debug info.
515 std::string Name = Decl->getNameAsString();
516
517 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000518 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
519 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
520
Devang Patel9ca36b62009-02-26 21:10:26 +0000521
Chris Lattnerac7c8142009-05-02 01:13:16 +0000522 unsigned RuntimeLang = DefUnit.getRunTimeVersion();
523
Devang Patel9ca36b62009-02-26 21:10:26 +0000524 // To handle recursive interface, we
525 // first generate a debug descriptor for the struct as a forward declaration.
526 // Then (if it is a definition) we go through and get debug info for all of
527 // its members. Finally, we create a descriptor for the complete type (which
528 // may refer to the forward decl if the struct is recursive) and replace all
529 // uses of the forward declaration with the final definition.
530 llvm::DIType FwdDecl =
531 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000532 llvm::DIType(), llvm::DIArray(),
533 RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000534
535 // If this is just a forward declaration, return it.
536 if (Decl->isForwardDecl())
537 return FwdDecl;
538
539 // Otherwise, insert it into the TypeCache so that recursive uses will find
540 // it.
541 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
542
543 // Convert all the elements.
544 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
545
Devang Patelfbe899f2009-03-10 21:30:26 +0000546 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
547 if (SClass) {
548 llvm::DIType SClassTy =
549 getOrCreateType(M->getContext().getObjCInterfaceType(SClass), Unit);
550 llvm::DIType InhTag =
551 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Chris Lattner9e55b8a2009-05-05 05:05:36 +0000552 Unit, "", llvm::DICompileUnit(), 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000553 0 /* offset */, 0, SClassTy);
554 EltTys.push_back(InhTag);
555 }
556
Devang Patel9ca36b62009-02-26 21:10:26 +0000557 const ASTRecordLayout &RL = M->getContext().getASTObjCInterfaceLayout(Decl);
558
559 unsigned FieldNo = 0;
560 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
561 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
562 ObjCIvarDecl *Field = *I;
563 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
564
565 std::string FieldName = Field->getNameAsString();
566
Devang Patelde135022009-04-27 22:40:36 +0000567 // Ignore unnamed fields.
568 if (FieldName.empty())
569 continue;
570
Devang Patel9ca36b62009-02-26 21:10:26 +0000571 // Get the location for the field.
572 SourceLocation FieldDefLoc = Field->getLocation();
573 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000574 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
575 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
576
Devang Patel99c20eb2009-03-20 18:24:39 +0000577
578 QualType FType = Field->getType();
579 uint64_t FieldSize = 0;
580 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000581
Devang Patel99c20eb2009-03-20 18:24:39 +0000582 if (!FType->isIncompleteArrayType()) {
583
584 // Bit size, align and offset of the type.
585 FieldSize = M->getContext().getTypeSize(FType);
586 Expr *BitWidth = Field->getBitWidth();
587 if (BitWidth)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000588 FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue();
589
Devang Patel99c20eb2009-03-20 18:24:39 +0000590 FieldAlign = M->getContext().getTypeAlign(FType);
591 }
592
593 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
594
Devang Patelc20482b2009-03-19 00:23:53 +0000595 unsigned Flags = 0;
596 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
597 Flags = llvm::DIType::FlagProtected;
598 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
599 Flags = llvm::DIType::FlagPrivate;
600
Devang Patel9ca36b62009-02-26 21:10:26 +0000601 // Create a DW_TAG_member node to remember the offset of this field in the
602 // struct. FIXME: This is an absolutely insane way to capture this
603 // information. When we gut debug info, this should be fixed.
604 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
605 FieldName, FieldDefUnit,
606 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000607 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000608 EltTys.push_back(FieldTy);
609 }
610
611 llvm::DIArray Elements =
612 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
613
614 // Bit size, align and offset of the type.
615 uint64_t Size = M->getContext().getTypeSize(Ty);
616 uint64_t Align = M->getContext().getTypeAlign(Ty);
617
618 llvm::DIType RealDecl =
619 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000620 Align, 0, 0, llvm::DIType(), Elements,
621 RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000622
623 // Now that we have a real decl for the struct, replace anything using the
624 // old decl with the new one. This will recursively update the debug info.
625 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
626 FwdDecl.getGV()->eraseFromParent();
627
628 return RealDecl;
629}
630
Chris Lattner9c85ba32008-11-10 06:08:34 +0000631llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
632 llvm::DICompileUnit Unit) {
633 EnumDecl *Decl = Ty->getDecl();
634
635 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
636
637 // Create DIEnumerator elements for each enumerator.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000638 for (EnumDecl::enumerator_iterator
639 Enum = Decl->enumerator_begin(M->getContext()),
640 EnumEnd = Decl->enumerator_end(M->getContext());
Douglas Gregor44b43212008-12-11 16:49:14 +0000641 Enum != EnumEnd; ++Enum) {
642 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(),
643 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000644 }
645
646 // Return a CompositeType for the enum itself.
647 llvm::DIArray EltArray =
648 DebugFactory.GetOrCreateArray(&Enumerators[0], Enumerators.size());
649
Chris Lattner8ec03f52008-11-24 03:54:41 +0000650 std::string EnumName = Decl->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000651 SourceLocation DefLoc = Decl->getLocation();
652 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
653 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000654 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
655 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
656
Chris Lattner9c85ba32008-11-10 06:08:34 +0000657
658 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +0000659 uint64_t Size = 0;
660 unsigned Align = 0;
661 if (!Ty->isIncompleteType()) {
662 Size = M->getContext().getTypeSize(Ty);
663 Align = M->getContext().getTypeAlign(Ty);
664 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000665
666 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
667 Unit, EnumName, DefUnit, Line,
668 Size, Align, 0, 0,
669 llvm::DIType(), EltArray);
670}
671
672llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
673 llvm::DICompileUnit Unit) {
674 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
675 return CreateType(RT, Unit);
676 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
677 return CreateType(ET, Unit);
678
679 return llvm::DIType();
680}
681
682llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
683 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000684 uint64_t Size;
685 uint64_t Align;
686
687
Nuno Lopes010d5142009-01-28 00:35:17 +0000688 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +0000689 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000690 Size = 0;
691 Align =
Nuno Lopes010d5142009-01-28 00:35:17 +0000692 M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT));
693 } else if (Ty->isIncompleteArrayType()) {
694 Size = 0;
695 Align = M->getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +0000696 } else {
697 // Size and align of the whole array, not the element type.
698 Size = M->getContext().getTypeSize(Ty);
699 Align = M->getContext().getTypeAlign(Ty);
700 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000701
702 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
703 // interior arrays, do we care? Why aren't nested arrays represented the
704 // obvious/recursive way?
705 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
706 QualType EltTy(Ty, 0);
707 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000708 uint64_t Upper = 0;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000709 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
710 Upper = CAT->getSize().getZExtValue() - 1;
711 // FIXME: Verify this is right for VLAs.
712 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
713 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000714 }
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000715
Chris Lattner9c85ba32008-11-10 06:08:34 +0000716 llvm::DIArray SubscriptArray =
717 DebugFactory.GetOrCreateArray(&Subscripts[0], Subscripts.size());
718
719 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
720 Unit, "", llvm::DICompileUnit(),
721 0, Size, Align, 0, 0,
722 getOrCreateType(EltTy, Unit),
723 SubscriptArray);
724}
725
726
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000727/// getOrCreateType - Get the type from the cache or create a new
728/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000729llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
730 llvm::DICompileUnit Unit) {
731 if (Ty.isNull())
732 return llvm::DIType();
733
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000734 // Check to see if the compile unit already has created this type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000735 llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()];
736 if (!Slot.isNull()) return Slot;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000737
Chris Lattner9c85ba32008-11-10 06:08:34 +0000738 // Handle CVR qualifiers, which recursively handles what they refer to.
739 if (Ty.getCVRQualifiers())
740 return Slot = CreateCVRType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000741
742 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000743 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000744#define TYPE(Class, Base)
745#define ABSTRACT_TYPE(Class, Base)
746#define NON_CANONICAL_TYPE(Class, Base)
747#define DEPENDENT_TYPE(Class, Base) case Type::Class:
748#include "clang/AST/TypeNodes.def"
749 assert(false && "Dependent types cannot show up in debug information");
750
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000751 case Type::LValueReference:
752 case Type::RValueReference:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000753 case Type::Vector:
754 case Type::ExtVector:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000755 case Type::ExtQual:
Eli Friedman00524e32009-02-27 23:15:07 +0000756 case Type::FixedWidthInt:
Eli Friedman00524e32009-02-27 23:15:07 +0000757 case Type::MemberPointer:
Douglas Gregor7532dc62009-03-30 22:58:21 +0000758 case Type::TemplateSpecialization:
Douglas Gregore4e5b052009-03-19 00:18:19 +0000759 case Type::QualifiedName:
Eli Friedman00524e32009-02-27 23:15:07 +0000760 // Unsupported types
Chris Lattner9c85ba32008-11-10 06:08:34 +0000761 return llvm::DIType();
Chris Lattneraa2b5792009-04-22 06:58:56 +0000762 case Type::ObjCQualifiedId: // Encode id<p> in debug info just like id.
763 return Slot = getOrCreateType(M->getContext().getObjCIdType(), Unit);
764
765 case Type::ObjCQualifiedInterface: // Drop protocols from interface.
Devang Patele7987062009-03-02 17:58:28 +0000766 case Type::ObjCInterface:
Chris Lattneraa2b5792009-04-22 06:58:56 +0000767 return Slot = CreateType(cast<ObjCInterfaceType>(Ty), Unit);
768 case Type::Builtin: return Slot = CreateType(cast<BuiltinType>(Ty), Unit);
Chris Lattnerb7003772009-04-23 06:13:01 +0000769 case Type::Complex: return Slot = CreateType(cast<ComplexType>(Ty), Unit);
Chris Lattneraa2b5792009-04-22 06:58:56 +0000770 case Type::Pointer: return Slot = CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +0000771 case Type::BlockPointer:
772 return Slot = CreateType(cast<BlockPointerType>(Ty), Unit);
Chris Lattneraa2b5792009-04-22 06:58:56 +0000773 case Type::Typedef: return Slot = CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000774 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000775 case Type::Enum:
Chris Lattneraa2b5792009-04-22 06:58:56 +0000776 return Slot = CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000777 case Type::FunctionProto:
778 case Type::FunctionNoProto:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000779 return Slot = CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000780
781 case Type::ConstantArray:
782 case Type::VariableArray:
783 case Type::IncompleteArray:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000784 return Slot = CreateType(cast<ArrayType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000785 case Type::TypeOfExpr:
786 return Slot = getOrCreateType(cast<TypeOfExprType>(Ty)->getUnderlyingExpr()
Chris Lattner3cc5c402008-11-11 07:01:36 +0000787 ->getType(), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000788 case Type::TypeOf:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000789 return Slot = getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(),
790 Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000791 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000792
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000793 return Slot;
794}
795
796/// EmitFunctionStart - Constructs the debug code for entering a function -
797/// "llvm.dbg.func.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000798void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000799 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000800 CGBuilderTy &Builder) {
Daniel Dunbarbbd53af2009-05-14 01:45:24 +0000801 const char *LinkageName = Name;
802
Daniel Dunbara2893932009-05-13 23:08:57 +0000803 // Skip the asm prefix if it exists.
Daniel Dunbarbbd53af2009-05-14 01:45:24 +0000804 //
805 // FIXME: This should probably be the unmangled name?
Daniel Dunbara2893932009-05-13 23:08:57 +0000806 if (Name[0] == '\01')
807 ++Name;
808
Chris Lattner9c85ba32008-11-10 06:08:34 +0000809 // FIXME: Why is this using CurLoc???
810 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000811 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +0000812 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000813
814 llvm::DISubprogram SP =
Daniel Dunbarbbd53af2009-05-14 01:45:24 +0000815 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000816 getOrCreateType(ReturnType, Unit),
817 Fn->hasInternalLinkage(), true/*definition*/);
818
819 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
820
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000821 // Push function on region stack.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000822 RegionStack.push_back(SP);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000823}
824
825
Chris Lattner9c85ba32008-11-10 06:08:34 +0000826void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000827 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
828
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000829 // Don't bother if things are the same as last time.
830 SourceManager &SM = M->getContext().getSourceManager();
831 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +0000832 || (SM.getInstantiationLineNumber(CurLoc) ==
833 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000834 && SM.isFromSameFile(CurLoc, PrevLoc)))
835 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000836
837 // Update last state.
838 PrevLoc = CurLoc;
839
840 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000841 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +0000842 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
843 DebugFactory.InsertStopPoint(Unit, PLoc.getLine(), PLoc.getColumn(),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000844 Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000845}
846
847/// EmitRegionStart- Constructs the debug code for entering a declarative
848/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000849void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
850 llvm::DIDescriptor D;
Daniel Dunbar5273f512008-10-17 01:07:56 +0000851 if (!RegionStack.empty())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000852 D = RegionStack.back();
853 D = DebugFactory.CreateBlock(D);
854 RegionStack.push_back(D);
855 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000856}
857
858/// EmitRegionEnd - Constructs the debug code for exiting a declarative
859/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +0000860void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000861 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
862
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000863 // Provide an region stop point.
864 EmitStopPoint(Fn, Builder);
865
Chris Lattner9c85ba32008-11-10 06:08:34 +0000866 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000867 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000868}
869
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000870/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000871void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
872 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000873 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
874
Devang Patel07739032009-03-27 23:16:32 +0000875 // Do not emit variable debug information while generating optimized code.
876 // The llvm optimizer and code generator are not yet ready to support
877 // optimized code debugging.
878 const CompileOptions &CO = M->getCompileOpts();
879 if (CO.OptimizationLevel)
880 return;
881
Chris Lattner650cea92009-05-05 04:57:08 +0000882 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
883 llvm::DIType Ty = getOrCreateType(Decl->getType(), Unit);
884
Chris Lattner9c85ba32008-11-10 06:08:34 +0000885 // Get location information.
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000886 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000887 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +0000888 unsigned Line = 0;
889 if (!PLoc.isInvalid())
890 Line = PLoc.getLine();
891 else
892 Unit = llvm::DICompileUnit();
893
Chris Lattner9c85ba32008-11-10 06:08:34 +0000894
895 // Create the descriptor for the variable.
896 llvm::DIVariable D =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000897 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner650cea92009-05-05 04:57:08 +0000898 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000899 // Insert an llvm.dbg.declare into the current block.
900 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000901}
902
Chris Lattner9c85ba32008-11-10 06:08:34 +0000903void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
904 llvm::Value *Storage,
905 CGBuilderTy &Builder) {
906 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
907}
908
909/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
910/// variable declaration.
911void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
912 CGBuilderTy &Builder) {
913 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
914}
915
916
917
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000918/// EmitGlobalVariable - Emit information about a global variable.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000919void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
920 const VarDecl *Decl) {
Devang Patel07739032009-03-27 23:16:32 +0000921
922 // Do not emit variable debug information while generating optimized code.
923 // The llvm optimizer and code generator are not yet ready to support
924 // optimized code debugging.
925 const CompileOptions &CO = M->getCompileOpts();
926 if (CO.OptimizationLevel)
927 return;
928
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000929 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000930 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000931 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000932 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
933 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000934
935 std::string Name = Decl->getNameAsString();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000936
937 QualType T = Decl->getType();
938 if (T->isIncompleteArrayType()) {
939
940 // CodeGen turns int[] into int[1] so we'll do the same here.
941 llvm::APSInt ConstVal(32);
942
943 ConstVal = 1;
944 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
945
946 T = M->getContext().getConstantArrayType(ET, ConstVal,
947 ArrayType::Normal, 0);
948 }
949
Chris Lattner9c85ba32008-11-10 06:08:34 +0000950 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000951 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000952 Var->hasInternalLinkage(),
953 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000954}
955
Devang Patel9ca36b62009-02-26 21:10:26 +0000956/// EmitGlobalVariable - Emit information about an objective-c interface.
957void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
958 ObjCInterfaceDecl *Decl) {
959 // Create global variable debug descriptor.
960 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
961 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000962 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
963 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +0000964
965 std::string Name = Decl->getNameAsString();
966
Chris Lattner03d9f342009-04-01 06:23:52 +0000967 QualType T = M->getContext().getObjCInterfaceType(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000968 if (T->isIncompleteArrayType()) {
969
970 // CodeGen turns int[] into int[1] so we'll do the same here.
971 llvm::APSInt ConstVal(32);
972
973 ConstVal = 1;
974 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
975
976 T = M->getContext().getConstantArrayType(ET, ConstVal,
977 ArrayType::Normal, 0);
978 }
979
980 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
981 getOrCreateType(T, Unit),
982 Var->hasInternalLinkage(),
983 true/*definition*/, Var);
984}
985