blob: 8fa1e9c920fd65750152d14598bbbd3d3ea92625 [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 Patel72240d72009-06-26 18:32:22 +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.
78 bool isMain = false;
79 const LangOptions &LO = M->getLangOptions();
80 const char *MainFileName = LO.getMainFileName();
81 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 Patel72240d72009-06-26 18:32:22 +0000117 return Unit = DebugFactory.CreateCompileUnit(LangTag, AbsFileName.getLast(),
118 AbsFileName.getDirname(),
119 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
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000153 return DebugFactory.CreateBasicType(Unit,
Chris Lattnere4f21422009-06-30 01:26:17 +0000154 BT->getName(M->getContext().getLangOptions()),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000155 Unit, 0, Size, Align,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000156 Offset, /*flags*/ 0, Encoding);
157}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000158
Chris Lattnerb7003772009-04-23 06:13:01 +0000159llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
160 llvm::DICompileUnit Unit) {
161 // Bit size, align and offset of the type.
162 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
163 if (Ty->isComplexIntegerType())
164 Encoding = llvm::dwarf::DW_ATE_lo_user;
165
166 uint64_t Size = M->getContext().getTypeSize(Ty);
167 uint64_t Align = M->getContext().getTypeAlign(Ty);
168 uint64_t Offset = 0;
169
170 return DebugFactory.CreateBasicType(Unit, "complex",
171 Unit, 0, Size, Align,
172 Offset, /*flags*/ 0, Encoding);
173}
174
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000175/// getOrCreateCVRType - Get the CVR qualified type from the cache or create
176/// a new one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000177llvm::DIType CGDebugInfo::CreateCVRType(QualType Ty, llvm::DICompileUnit Unit) {
178 // We will create one Derived type for one qualifier and recurse to handle any
179 // additional ones.
180 llvm::DIType FromTy;
181 unsigned Tag;
182 if (Ty.isConstQualified()) {
183 Tag = llvm::dwarf::DW_TAG_const_type;
184 Ty.removeConst();
185 FromTy = getOrCreateType(Ty, Unit);
186 } else if (Ty.isVolatileQualified()) {
187 Tag = llvm::dwarf::DW_TAG_volatile_type;
188 Ty.removeVolatile();
189 FromTy = getOrCreateType(Ty, Unit);
190 } else {
191 assert(Ty.isRestrictQualified() && "Unknown type qualifier for debug info");
192 Tag = llvm::dwarf::DW_TAG_restrict_type;
193 Ty.removeRestrict();
194 FromTy = getOrCreateType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000195 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000196
Daniel Dunbar3845f862008-10-31 03:54:29 +0000197 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
198 // CVR derived types.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000199 return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
200 0, 0, 0, 0, 0, FromTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000201}
202
Chris Lattner9c85ba32008-11-10 06:08:34 +0000203llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
204 llvm::DICompileUnit Unit) {
205 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000206
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000207 // Bit size, align and offset of the type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000208 uint64_t Size = M->getContext().getTypeSize(Ty);
209 uint64_t Align = M->getContext().getTypeAlign(Ty);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000210
Chris Lattner9c85ba32008-11-10 06:08:34 +0000211 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
212 "", llvm::DICompileUnit(),
213 0, Size, Align, 0, 0, EltTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000214}
215
Mike Stump9bc093c2009-05-14 02:03:51 +0000216llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
217 llvm::DICompileUnit Unit) {
218 if (BlockLiteralGenericSet)
219 return BlockLiteralGeneric;
220
221 llvm::DICompileUnit DefUnit;
222 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
223
224 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
225
226 llvm::DIType FieldTy;
227
228 QualType FType;
229 uint64_t FieldSize, FieldOffset;
230 unsigned FieldAlign;
231
232 llvm::DIArray Elements;
233 llvm::DIType EltTy, DescTy;
234
235 FieldOffset = 0;
236 FType = M->getContext().UnsignedLongTy;
237 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
238 FieldSize = M->getContext().getTypeSize(FType);
239 FieldAlign = M->getContext().getTypeAlign(FType);
240 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
241 "reserved", DefUnit,
242 0, FieldSize, FieldAlign,
243 FieldOffset, 0, FieldTy);
244 EltTys.push_back(FieldTy);
245
246 FieldOffset += FieldSize;
247 FType = M->getContext().UnsignedLongTy;
248 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
249 FieldSize = M->getContext().getTypeSize(FType);
250 FieldAlign = M->getContext().getTypeAlign(FType);
251 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
252 "Size", DefUnit,
253 0, FieldSize, FieldAlign,
254 FieldOffset, 0, FieldTy);
255 EltTys.push_back(FieldTy);
256
257 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000258 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000259 EltTys.clear();
260
261 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
262 DefUnit, 0, FieldOffset, 0, 0, 0,
263 llvm::DIType(), Elements);
264
265 // Bit size, align and offset of the type.
266 uint64_t Size = M->getContext().getTypeSize(Ty);
267 uint64_t Align = M->getContext().getTypeAlign(Ty);
268
269 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
270 Unit, "", llvm::DICompileUnit(),
271 0, Size, Align, 0, 0, EltTy);
272
273 FieldOffset = 0;
274 FType = M->getContext().getPointerType(M->getContext().VoidTy);
275 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
276 FieldSize = M->getContext().getTypeSize(FType);
277 FieldAlign = M->getContext().getTypeAlign(FType);
278 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
279 "__isa", DefUnit,
280 0, FieldSize, FieldAlign,
281 FieldOffset, 0, FieldTy);
282 EltTys.push_back(FieldTy);
283
284 FieldOffset += FieldSize;
285 FType = M->getContext().IntTy;
286 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
287 FieldSize = M->getContext().getTypeSize(FType);
288 FieldAlign = M->getContext().getTypeAlign(FType);
289 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
290 "__flags", DefUnit,
291 0, FieldSize, FieldAlign,
292 FieldOffset, 0, FieldTy);
293 EltTys.push_back(FieldTy);
294
295 FieldOffset += FieldSize;
296 FType = M->getContext().IntTy;
297 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
298 FieldSize = M->getContext().getTypeSize(FType);
299 FieldAlign = M->getContext().getTypeAlign(FType);
300 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
301 "__reserved", DefUnit,
302 0, FieldSize, FieldAlign,
303 FieldOffset, 0, FieldTy);
304 EltTys.push_back(FieldTy);
305
306 FieldOffset += FieldSize;
307 FType = M->getContext().getPointerType(M->getContext().VoidTy);
308 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
309 FieldSize = M->getContext().getTypeSize(FType);
310 FieldAlign = M->getContext().getTypeAlign(FType);
311 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
312 "__FuncPtr", DefUnit,
313 0, FieldSize, FieldAlign,
314 FieldOffset, 0, FieldTy);
315 EltTys.push_back(FieldTy);
316
317 FieldOffset += FieldSize;
318 FType = M->getContext().getPointerType(M->getContext().VoidTy);
319 FieldTy = DescTy;
320 FieldSize = M->getContext().getTypeSize(Ty);
321 FieldAlign = M->getContext().getTypeAlign(Ty);
322 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
323 "__descriptor", DefUnit,
324 0, FieldSize, FieldAlign,
325 FieldOffset, 0, FieldTy);
326 EltTys.push_back(FieldTy);
327
328 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000329 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000330
331 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
332 DefUnit, 0, FieldOffset, 0, 0, 0,
333 llvm::DIType(), Elements);
334
335 BlockLiteralGenericSet = true;
336 BlockLiteralGeneric
337 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
338 "", llvm::DICompileUnit(),
339 0, Size, Align, 0, 0, EltTy);
340 return BlockLiteralGeneric;
341}
342
Chris Lattner9c85ba32008-11-10 06:08:34 +0000343llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
344 llvm::DICompileUnit Unit) {
345 // Typedefs are derived from some other type. If we have a typedef of a
346 // typedef, make sure to emit the whole chain.
347 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
348
349 // We don't set size information, but do specify where the typedef was
350 // declared.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000351 std::string TyName = Ty->getDecl()->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000352 SourceLocation DefLoc = Ty->getDecl()->getLocation();
353 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000354
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000355 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000356 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
357 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000358
Chris Lattner9c85ba32008-11-10 06:08:34 +0000359 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
360 TyName, DefUnit, Line, 0, 0, 0, 0, Src);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000361}
362
Chris Lattner9c85ba32008-11-10 06:08:34 +0000363llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
364 llvm::DICompileUnit Unit) {
365 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000366
Chris Lattner9c85ba32008-11-10 06:08:34 +0000367 // Add the result type at least.
368 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
369
370 // Set up remainder of arguments if there is a prototype.
371 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000372 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000373 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
374 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
375 } else {
376 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000377 }
378
Chris Lattner9c85ba32008-11-10 06:08:34 +0000379 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000380 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000381
382 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
383 Unit, "", llvm::DICompileUnit(),
384 0, 0, 0, 0, 0,
385 llvm::DIType(), EltTypeArray);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000386}
387
Devang Patel65e99f22009-02-25 01:36:11 +0000388/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000389llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
390 llvm::DICompileUnit Unit) {
Douglas Gregora4c46df2008-12-11 17:59:21 +0000391 RecordDecl *Decl = Ty->getDecl();
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000392
Chris Lattner9c85ba32008-11-10 06:08:34 +0000393 unsigned Tag;
394 if (Decl->isStruct())
395 Tag = llvm::dwarf::DW_TAG_structure_type;
396 else if (Decl->isUnion())
397 Tag = llvm::dwarf::DW_TAG_union_type;
398 else {
399 assert(Decl->isClass() && "Unknown RecordType!");
400 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000401 }
402
Sanjiv Gupta507de852008-06-09 10:47:41 +0000403 SourceManager &SM = M->getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000404
Chris Lattner9c85ba32008-11-10 06:08:34 +0000405 // Get overall information about the record type for the debug info.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000406 std::string Name = Decl->getNameAsString();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000407
Devang Patel4f6fa232009-04-17 21:35:15 +0000408 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000409 llvm::DICompileUnit DefUnit;
410 unsigned Line = 0;
411 if (!PLoc.isInvalid()) {
412 DefUnit = getOrCreateCompileUnit(Decl->getLocation());
413 Line = PLoc.getLine();
414 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000415
416 // Records and classes and unions can all be recursive. To handle them, we
417 // first generate a debug descriptor for the struct as a forward declaration.
418 // Then (if it is a definition) we go through and get debug info for all of
419 // its members. Finally, we create a descriptor for the complete type (which
420 // may refer to the forward decl if the struct is recursive) and replace all
421 // uses of the forward declaration with the final definition.
422 llvm::DIType FwdDecl =
423 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
424 llvm::DIType(), llvm::DIArray());
425
426 // If this is just a forward declaration, return it.
427 if (!Decl->getDefinition(M->getContext()))
428 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000429
Chris Lattner9c85ba32008-11-10 06:08:34 +0000430 // Otherwise, insert it into the TypeCache so that recursive uses will find
431 // it.
432 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
433
434 // Convert all the elements.
435 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
436
437 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl);
438
439 unsigned FieldNo = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000440 for (RecordDecl::field_iterator I = Decl->field_begin(),
441 E = Decl->field_end();
Douglas Gregora4c46df2008-12-11 17:59:21 +0000442 I != E; ++I, ++FieldNo) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000443 FieldDecl *Field = *I;
444 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattner8ec03f52008-11-24 03:54:41 +0000445
446 std::string FieldName = Field->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000447
Devang Patelde135022009-04-27 22:40:36 +0000448 // Ignore unnamed fields.
449 if (FieldName.empty())
450 continue;
451
Chris Lattner9c85ba32008-11-10 06:08:34 +0000452 // Get the location for the field.
453 SourceLocation FieldDefLoc = Field->getLocation();
Devang Patel4f6fa232009-04-17 21:35:15 +0000454 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000455 llvm::DICompileUnit FieldDefUnit;
456 unsigned FieldLine = 0;
457
458 if (!PLoc.isInvalid()) {
459 FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
460 FieldLine = PLoc.getLine();
461 }
Devang Patelec9b5d52009-03-16 23:47:53 +0000462
463 QualType FType = Field->getType();
464 uint64_t FieldSize = 0;
465 unsigned FieldAlign = 0;
466 if (!FType->isIncompleteArrayType()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000467
Devang Patelec9b5d52009-03-16 23:47:53 +0000468 // Bit size, align and offset of the type.
469 FieldSize = M->getContext().getTypeSize(FType);
470 Expr *BitWidth = Field->getBitWidth();
471 if (BitWidth)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000472 FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue();
Devang Patelec9b5d52009-03-16 23:47:53 +0000473
474 FieldAlign = M->getContext().getTypeAlign(FType);
475 }
476
Chris Lattner9c85ba32008-11-10 06:08:34 +0000477 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
478
479 // Create a DW_TAG_member node to remember the offset of this field in the
480 // struct. FIXME: This is an absolutely insane way to capture this
481 // information. When we gut debug info, this should be fixed.
482 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
483 FieldName, FieldDefUnit,
484 FieldLine, FieldSize, FieldAlign,
485 FieldOffset, 0, FieldTy);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000486 EltTys.push_back(FieldTy);
487 }
488
489 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000490 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000491
492 // Bit size, align and offset of the type.
493 uint64_t Size = M->getContext().getTypeSize(Ty);
494 uint64_t Align = M->getContext().getTypeAlign(Ty);
495
496 llvm::DIType RealDecl =
497 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
498 Align, 0, 0, llvm::DIType(), Elements);
499
500 // Now that we have a real decl for the struct, replace anything using the
501 // old decl with the new one. This will recursively update the debug info.
502 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
503 FwdDecl.getGV()->eraseFromParent();
504
505 return RealDecl;
506}
507
Devang Patel9ca36b62009-02-26 21:10:26 +0000508/// CreateType - get objective-c interface type.
509llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
510 llvm::DICompileUnit Unit) {
511 ObjCInterfaceDecl *Decl = Ty->getDecl();
512
513 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
514 SourceManager &SM = M->getContext().getSourceManager();
515
516 // Get overall information about the record type for the debug info.
517 std::string Name = Decl->getNameAsString();
518
519 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000520 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
521 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
522
Devang Patel9ca36b62009-02-26 21:10:26 +0000523
Daniel Dunbard86d3362009-05-18 20:51:58 +0000524 unsigned RuntimeLang = DefUnit.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000525
Devang Patel9ca36b62009-02-26 21:10:26 +0000526 // To handle recursive interface, we
527 // first generate a debug descriptor for the struct as a forward declaration.
528 // Then (if it is a definition) we go through and get debug info for all of
529 // its members. Finally, we create a descriptor for the complete type (which
530 // may refer to the forward decl if the struct is recursive) and replace all
531 // uses of the forward declaration with the final definition.
532 llvm::DIType FwdDecl =
533 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000534 llvm::DIType(), llvm::DIArray(),
535 RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000536
537 // If this is just a forward declaration, return it.
538 if (Decl->isForwardDecl())
539 return FwdDecl;
540
541 // Otherwise, insert it into the TypeCache so that recursive uses will find
542 // it.
543 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
544
545 // Convert all the elements.
546 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
547
Devang Patelfbe899f2009-03-10 21:30:26 +0000548 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
549 if (SClass) {
550 llvm::DIType SClassTy =
551 getOrCreateType(M->getContext().getObjCInterfaceType(SClass), Unit);
552 llvm::DIType InhTag =
553 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Chris Lattner9e55b8a2009-05-05 05:05:36 +0000554 Unit, "", llvm::DICompileUnit(), 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000555 0 /* offset */, 0, SClassTy);
556 EltTys.push_back(InhTag);
557 }
558
Devang Patel9ca36b62009-02-26 21:10:26 +0000559 const ASTRecordLayout &RL = M->getContext().getASTObjCInterfaceLayout(Decl);
560
561 unsigned FieldNo = 0;
562 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
563 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
564 ObjCIvarDecl *Field = *I;
565 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
566
567 std::string FieldName = Field->getNameAsString();
568
Devang Patelde135022009-04-27 22:40:36 +0000569 // Ignore unnamed fields.
570 if (FieldName.empty())
571 continue;
572
Devang Patel9ca36b62009-02-26 21:10:26 +0000573 // Get the location for the field.
574 SourceLocation FieldDefLoc = Field->getLocation();
575 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000576 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
577 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
578
Devang Patel99c20eb2009-03-20 18:24:39 +0000579
580 QualType FType = Field->getType();
581 uint64_t FieldSize = 0;
582 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000583
Devang Patel99c20eb2009-03-20 18:24:39 +0000584 if (!FType->isIncompleteArrayType()) {
585
586 // Bit size, align and offset of the type.
587 FieldSize = M->getContext().getTypeSize(FType);
588 Expr *BitWidth = Field->getBitWidth();
589 if (BitWidth)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000590 FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue();
591
Devang Patel99c20eb2009-03-20 18:24:39 +0000592 FieldAlign = M->getContext().getTypeAlign(FType);
593 }
594
595 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
596
Devang Patelc20482b2009-03-19 00:23:53 +0000597 unsigned Flags = 0;
598 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
599 Flags = llvm::DIType::FlagProtected;
600 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
601 Flags = llvm::DIType::FlagPrivate;
602
Devang Patel9ca36b62009-02-26 21:10:26 +0000603 // Create a DW_TAG_member node to remember the offset of this field in the
604 // struct. FIXME: This is an absolutely insane way to capture this
605 // information. When we gut debug info, this should be fixed.
606 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
607 FieldName, FieldDefUnit,
608 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000609 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000610 EltTys.push_back(FieldTy);
611 }
612
613 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000614 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +0000615
616 // Bit size, align and offset of the type.
617 uint64_t Size = M->getContext().getTypeSize(Ty);
618 uint64_t Align = M->getContext().getTypeAlign(Ty);
619
620 llvm::DIType RealDecl =
621 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000622 Align, 0, 0, llvm::DIType(), Elements,
623 RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000624
625 // Now that we have a real decl for the struct, replace anything using the
626 // old decl with the new one. This will recursively update the debug info.
627 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
628 FwdDecl.getGV()->eraseFromParent();
629
630 return RealDecl;
631}
632
Chris Lattner9c85ba32008-11-10 06:08:34 +0000633llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
634 llvm::DICompileUnit Unit) {
635 EnumDecl *Decl = Ty->getDecl();
636
637 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
638
639 // Create DIEnumerator elements for each enumerator.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000640 for (EnumDecl::enumerator_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000641 Enum = Decl->enumerator_begin(), EnumEnd = Decl->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000642 Enum != EnumEnd; ++Enum) {
643 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(),
644 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000645 }
646
647 // Return a CompositeType for the enum itself.
648 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000649 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000650
Chris Lattner8ec03f52008-11-24 03:54:41 +0000651 std::string EnumName = Decl->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000652 SourceLocation DefLoc = Decl->getLocation();
653 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
654 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000655 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
656 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
657
Chris Lattner9c85ba32008-11-10 06:08:34 +0000658
659 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +0000660 uint64_t Size = 0;
661 unsigned Align = 0;
662 if (!Ty->isIncompleteType()) {
663 Size = M->getContext().getTypeSize(Ty);
664 Align = M->getContext().getTypeAlign(Ty);
665 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000666
667 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
668 Unit, EnumName, DefUnit, Line,
669 Size, Align, 0, 0,
670 llvm::DIType(), EltArray);
671}
672
673llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
674 llvm::DICompileUnit Unit) {
675 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
676 return CreateType(RT, Unit);
677 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
678 return CreateType(ET, Unit);
679
680 return llvm::DIType();
681}
682
683llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
684 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000685 uint64_t Size;
686 uint64_t Align;
687
688
Nuno Lopes010d5142009-01-28 00:35:17 +0000689 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +0000690 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000691 Size = 0;
692 Align =
Nuno Lopes010d5142009-01-28 00:35:17 +0000693 M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT));
694 } else if (Ty->isIncompleteArrayType()) {
695 Size = 0;
696 Align = M->getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +0000697 } else {
698 // Size and align of the whole array, not the element type.
699 Size = M->getContext().getTypeSize(Ty);
700 Align = M->getContext().getTypeAlign(Ty);
701 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000702
703 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
704 // interior arrays, do we care? Why aren't nested arrays represented the
705 // obvious/recursive way?
706 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
707 QualType EltTy(Ty, 0);
708 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000709 uint64_t Upper = 0;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000710 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
711 Upper = CAT->getSize().getZExtValue() - 1;
712 // FIXME: Verify this is right for VLAs.
713 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
714 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000715 }
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000716
Chris Lattner9c85ba32008-11-10 06:08:34 +0000717 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000718 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000719
720 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
721 Unit, "", llvm::DICompileUnit(),
722 0, Size, Align, 0, 0,
723 getOrCreateType(EltTy, Unit),
724 SubscriptArray);
725}
726
727
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000728/// getOrCreateType - Get the type from the cache or create a new
729/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000730llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
731 llvm::DICompileUnit Unit) {
732 if (Ty.isNull())
733 return llvm::DIType();
734
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000735 // Check to see if the compile unit already has created this type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000736 llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()];
737 if (!Slot.isNull()) return Slot;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000738
Chris Lattner9c85ba32008-11-10 06:08:34 +0000739 // Handle CVR qualifiers, which recursively handles what they refer to.
740 if (Ty.getCVRQualifiers())
741 return Slot = CreateCVRType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000742
743 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000744 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000745#define TYPE(Class, Base)
746#define ABSTRACT_TYPE(Class, Base)
747#define NON_CANONICAL_TYPE(Class, Base)
748#define DEPENDENT_TYPE(Class, Base) case Type::Class:
749#include "clang/AST/TypeNodes.def"
750 assert(false && "Dependent types cannot show up in debug information");
751
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000752 case Type::LValueReference:
753 case Type::RValueReference:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000754 case Type::Vector:
755 case Type::ExtVector:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000756 case Type::ExtQual:
Eli Friedman00524e32009-02-27 23:15:07 +0000757 case Type::FixedWidthInt:
Eli Friedman00524e32009-02-27 23:15:07 +0000758 case Type::MemberPointer:
Douglas Gregor7532dc62009-03-30 22:58:21 +0000759 case Type::TemplateSpecialization:
Douglas Gregore4e5b052009-03-19 00:18:19 +0000760 case Type::QualifiedName:
Eli Friedman00524e32009-02-27 23:15:07 +0000761 // Unsupported types
Chris Lattner9c85ba32008-11-10 06:08:34 +0000762 return llvm::DIType();
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000763 case Type::ObjCObjectPointer: // Encode id<p> in debug info just like id.
Chris Lattneraa2b5792009-04-22 06:58:56 +0000764 return Slot = getOrCreateType(M->getContext().getObjCIdType(), Unit);
765
766 case Type::ObjCQualifiedInterface: // Drop protocols from interface.
Devang Patele7987062009-03-02 17:58:28 +0000767 case Type::ObjCInterface:
Chris Lattneraa2b5792009-04-22 06:58:56 +0000768 return Slot = CreateType(cast<ObjCInterfaceType>(Ty), Unit);
769 case Type::Builtin: return Slot = CreateType(cast<BuiltinType>(Ty), Unit);
Chris Lattnerb7003772009-04-23 06:13:01 +0000770 case Type::Complex: return Slot = CreateType(cast<ComplexType>(Ty), Unit);
Chris Lattneraa2b5792009-04-22 06:58:56 +0000771 case Type::Pointer: return Slot = CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +0000772 case Type::BlockPointer:
773 return Slot = CreateType(cast<BlockPointerType>(Ty), Unit);
Chris Lattneraa2b5792009-04-22 06:58:56 +0000774 case Type::Typedef: return Slot = CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000775 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000776 case Type::Enum:
Chris Lattneraa2b5792009-04-22 06:58:56 +0000777 return Slot = CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000778 case Type::FunctionProto:
779 case Type::FunctionNoProto:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000780 return Slot = CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000781
782 case Type::ConstantArray:
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000783 case Type::ConstantArrayWithExpr:
784 case Type::ConstantArrayWithoutExpr:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000785 case Type::VariableArray:
786 case Type::IncompleteArray:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000787 return Slot = CreateType(cast<ArrayType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000788 case Type::TypeOfExpr:
789 return Slot = getOrCreateType(cast<TypeOfExprType>(Ty)->getUnderlyingExpr()
Chris Lattner3cc5c402008-11-11 07:01:36 +0000790 ->getType(), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000791 case Type::TypeOf:
Chris Lattner3cc5c402008-11-11 07:01:36 +0000792 return Slot = getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(),
793 Unit);
Anders Carlsson395b4752009-06-24 19:06:50 +0000794 case Type::Decltype:
Anders Carlsson563a03b2009-07-10 19:20:26 +0000795 return Slot = getOrCreateType(cast<DecltypeType>(Ty)->getUnderlyingType(),
796 Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000797 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000798
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000799 return Slot;
800}
801
802/// EmitFunctionStart - Constructs the debug code for entering a function -
803/// "llvm.dbg.func.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000804void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000805 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000806 CGBuilderTy &Builder) {
Daniel Dunbarbbd53af2009-05-14 01:45:24 +0000807 const char *LinkageName = Name;
808
Daniel Dunbara2893932009-05-13 23:08:57 +0000809 // Skip the asm prefix if it exists.
Daniel Dunbarbbd53af2009-05-14 01:45:24 +0000810 //
811 // FIXME: This should probably be the unmangled name?
Daniel Dunbara2893932009-05-13 23:08:57 +0000812 if (Name[0] == '\01')
813 ++Name;
814
Chris Lattner9c85ba32008-11-10 06:08:34 +0000815 // FIXME: Why is this using CurLoc???
816 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000817 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +0000818 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000819
820 llvm::DISubprogram SP =
Daniel Dunbarbbd53af2009-05-14 01:45:24 +0000821 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000822 getOrCreateType(ReturnType, Unit),
823 Fn->hasInternalLinkage(), true/*definition*/);
824
825 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
826
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000827 // Push function on region stack.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000828 RegionStack.push_back(SP);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000829}
830
831
Chris Lattner9c85ba32008-11-10 06:08:34 +0000832void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000833 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
834
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000835 // Don't bother if things are the same as last time.
836 SourceManager &SM = M->getContext().getSourceManager();
837 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +0000838 || (SM.getInstantiationLineNumber(CurLoc) ==
839 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000840 && SM.isFromSameFile(CurLoc, PrevLoc)))
841 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000842
843 // Update last state.
844 PrevLoc = CurLoc;
845
846 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000847 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +0000848 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
849 DebugFactory.InsertStopPoint(Unit, PLoc.getLine(), PLoc.getColumn(),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000850 Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000851}
852
853/// EmitRegionStart- Constructs the debug code for entering a declarative
854/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000855void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
856 llvm::DIDescriptor D;
Daniel Dunbar5273f512008-10-17 01:07:56 +0000857 if (!RegionStack.empty())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000858 D = RegionStack.back();
859 D = DebugFactory.CreateBlock(D);
860 RegionStack.push_back(D);
861 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000862}
863
864/// EmitRegionEnd - Constructs the debug code for exiting a declarative
865/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +0000866void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000867 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
868
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000869 // Provide an region stop point.
870 EmitStopPoint(Fn, Builder);
871
Chris Lattner9c85ba32008-11-10 06:08:34 +0000872 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000873 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000874}
875
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000876/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000877void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
878 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000879 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
880
Devang Patel07739032009-03-27 23:16:32 +0000881 // Do not emit variable debug information while generating optimized code.
882 // The llvm optimizer and code generator are not yet ready to support
883 // optimized code debugging.
884 const CompileOptions &CO = M->getCompileOpts();
885 if (CO.OptimizationLevel)
886 return;
887
Chris Lattner650cea92009-05-05 04:57:08 +0000888 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
889 llvm::DIType Ty = getOrCreateType(Decl->getType(), Unit);
890
Chris Lattner9c85ba32008-11-10 06:08:34 +0000891 // Get location information.
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000892 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000893 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +0000894 unsigned Line = 0;
895 if (!PLoc.isInvalid())
896 Line = PLoc.getLine();
897 else
898 Unit = llvm::DICompileUnit();
899
Chris Lattner9c85ba32008-11-10 06:08:34 +0000900
901 // Create the descriptor for the variable.
902 llvm::DIVariable D =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000903 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner650cea92009-05-05 04:57:08 +0000904 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000905 // Insert an llvm.dbg.declare into the current block.
906 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000907}
908
Chris Lattner9c85ba32008-11-10 06:08:34 +0000909void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
910 llvm::Value *Storage,
911 CGBuilderTy &Builder) {
912 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
913}
914
915/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
916/// variable declaration.
917void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
918 CGBuilderTy &Builder) {
919 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
920}
921
922
923
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000924/// EmitGlobalVariable - Emit information about a global variable.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000925void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
926 const VarDecl *Decl) {
Devang Patel07739032009-03-27 23:16:32 +0000927
928 // Do not emit variable debug information while generating optimized code.
929 // The llvm optimizer and code generator are not yet ready to support
930 // optimized code debugging.
931 const CompileOptions &CO = M->getCompileOpts();
932 if (CO.OptimizationLevel)
933 return;
934
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000935 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000936 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000937 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000938 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
939 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000940
941 std::string Name = Decl->getNameAsString();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000942
943 QualType T = Decl->getType();
944 if (T->isIncompleteArrayType()) {
945
946 // CodeGen turns int[] into int[1] so we'll do the same here.
947 llvm::APSInt ConstVal(32);
948
949 ConstVal = 1;
950 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
951
952 T = M->getContext().getConstantArrayType(ET, ConstVal,
953 ArrayType::Normal, 0);
954 }
955
Chris Lattner9c85ba32008-11-10 06:08:34 +0000956 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +0000957 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000958 Var->hasInternalLinkage(),
959 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000960}
961
Devang Patel9ca36b62009-02-26 21:10:26 +0000962/// EmitGlobalVariable - Emit information about an objective-c interface.
963void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
964 ObjCInterfaceDecl *Decl) {
965 // Create global variable debug descriptor.
966 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
967 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000968 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
969 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +0000970
971 std::string Name = Decl->getNameAsString();
972
Chris Lattner03d9f342009-04-01 06:23:52 +0000973 QualType T = M->getContext().getObjCInterfaceType(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000974 if (T->isIncompleteArrayType()) {
975
976 // CodeGen turns int[] into int[1] so we'll do the same here.
977 llvm::APSInt ConstVal(32);
978
979 ConstVal = 1;
980 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
981
982 T = M->getContext().getConstantArrayType(ET, ConstVal,
983 ArrayType::Normal, 0);
984 }
985
986 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
987 getOrCreateType(T, Unit),
988 Var->hasInternalLinkage(),
989 true/*definition*/, Var);
990}
991