blob: 2238c89f8357fadf374a32fd41b4cf3512e91513 [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"
Mike Stumpb1a6e682009-09-30 02:43:10 +000015#include "CodeGenFunction.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000016#include "CodeGenModule.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000017#include "clang/AST/ASTContext.h"
Devang Patel9ca36b62009-02-26 21:10:26 +000018#include "clang/AST/DeclObjC.h"
Chris Lattner3cc5c402008-11-11 07:01:36 +000019#include "clang/AST/Expr.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000020#include "clang/AST/RecordLayout.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000021#include "clang/Basic/SourceManager.h"
22#include "clang/Basic/FileManager.h"
Mike Stump5a862172009-09-15 21:48:34 +000023#include "clang/Basic/Version.h"
Chandler Carruth2811ccf2009-11-12 17:24:48 +000024#include "clang/CodeGen/CodeGenOptions.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000025#include "llvm/Constants.h"
26#include "llvm/DerivedTypes.h"
27#include "llvm/Instructions.h"
28#include "llvm/Intrinsics.h"
29#include "llvm/Module.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000030#include "llvm/ADT/StringExtras.h"
31#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000032#include "llvm/Support/Dwarf.h"
Devang Patel446c6192009-04-17 21:06:59 +000033#include "llvm/System/Path.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000034#include "llvm/Target/TargetMachine.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000035using namespace clang;
36using namespace clang::CodeGen;
37
Anders Carlsson20f12a22009-12-06 18:00:51 +000038CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
39 : CGM(CGM), isMainCompileUnitCreated(false), DebugFactory(CGM.getModule()),
Mike Stump9bc093c2009-05-14 02:03:51 +000040 BlockLiteralGenericSet(false) {
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000041}
42
Chris Lattner9c85ba32008-11-10 06:08:34 +000043CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar66031a52008-10-17 16:15:48 +000044 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000045}
46
Chris Lattner9c85ba32008-11-10 06:08:34 +000047void CGDebugInfo::setLocation(SourceLocation Loc) {
48 if (Loc.isValid())
Anders Carlsson20f12a22009-12-06 18:00:51 +000049 CurLoc = CGM.getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000050}
51
Devang Patel979ec2e2009-10-06 00:35:31 +000052/// getContext - Get context info for the decl.
53llvm::DIDescriptor CGDebugInfo::getContext(const VarDecl *Decl,
54 llvm::DIDescriptor &CompileUnit) {
55 if (Decl->isFileVarDecl())
56 return CompileUnit;
57 if (Decl->getDeclContext()->isFunctionOrMethod()) {
58 // Find the last subprogram in region stack.
59 for (unsigned RI = RegionStack.size(), RE = 0; RI != RE; --RI) {
Devang Patel8fae0602009-11-13 19:10:24 +000060 llvm::DIDescriptor R(RegionStack[RI - 1]);
Devang Patel979ec2e2009-10-06 00:35:31 +000061 if (R.isSubprogram())
62 return R;
63 }
64 }
65 return CompileUnit;
66}
67
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000068/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
Daniel Dunbar25f51dd2008-10-24 08:38:36 +000069/// one if necessary. This returns null for invalid source locations.
Chris Lattner9c85ba32008-11-10 06:08:34 +000070llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) {
Devang Patel446c6192009-04-17 21:06:59 +000071 // Get source file information.
72 const char *FileName = "<unknown>";
Anders Carlsson20f12a22009-12-06 18:00:51 +000073 SourceManager &SM = CGM.getContext().getSourceManager();
Chris Lattneradb1a6f2009-04-19 06:50:29 +000074 unsigned FID = 0;
Daniel Dunbar831570c2009-01-22 00:09:25 +000075 if (Loc.isValid()) {
Devang Patel446c6192009-04-17 21:06:59 +000076 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
77 FileName = PLoc.getFilename();
78 FID = PLoc.getIncludeLoc().getRawEncoding();
Daniel Dunbar831570c2009-01-22 00:09:25 +000079 }
Mike Stump1eb44332009-09-09 15:08:12 +000080
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000081 // See if this compile unit has been used before.
Devang Patel446c6192009-04-17 21:06:59 +000082 llvm::DICompileUnit &Unit = CompileUnitCache[FID];
Chris Lattner9c85ba32008-11-10 06:08:34 +000083 if (!Unit.isNull()) return Unit;
Daniel Dunbarc9abc042009-04-08 05:11:16 +000084
Devang Patel446c6192009-04-17 21:06:59 +000085 // Get absolute path name.
86 llvm::sys::Path AbsFileName(FileName);
Benjamin Kramer47daf682009-12-08 11:02:29 +000087 AbsFileName.makeAbsolute();
Devang Patel446c6192009-04-17 21:06:59 +000088
Devang Patel72240d72009-06-26 18:32:22 +000089 // See if thie compile unit is representing main source file. Each source
90 // file has corresponding compile unit. There is only one main source
91 // file at a time.
92 bool isMain = false;
Anders Carlsson20f12a22009-12-06 18:00:51 +000093 const LangOptions &LO = CGM.getLangOptions();
94 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Devang Patel72240d72009-06-26 18:32:22 +000095 if (isMainCompileUnitCreated == false) {
Daniel Dunbar7d065d02009-11-29 02:38:34 +000096 if (!CGO.MainFileName.empty()) {
97 if (AbsFileName.getLast() == CGO.MainFileName)
Devang Patel72240d72009-06-26 18:32:22 +000098 isMain = true;
99 } else {
100 if (Loc.isValid() && SM.isFromMainFile(Loc))
101 isMain = true;
102 }
103 if (isMain)
104 isMainCompileUnitCreated = true;
Devang Patel446c6192009-04-17 21:06:59 +0000105 }
Daniel Dunbarc9abc042009-04-08 05:11:16 +0000106
Chris Lattner515455a2009-03-25 03:28:08 +0000107 unsigned LangTag;
108 if (LO.CPlusPlus) {
109 if (LO.ObjC1)
110 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
111 else
112 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
113 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000114 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000115 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000116 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000117 } else {
118 LangTag = llvm::dwarf::DW_LANG_C89;
119 }
Devang Patel446c6192009-04-17 21:06:59 +0000120
Benjamin Kramer47daf682009-12-08 11:02:29 +0000121 const char *Producer =
Mike Stumpd8945d62009-10-09 18:38:12 +0000122#ifdef CLANG_VENDOR
123 CLANG_VENDOR
124#endif
125 "clang " CLANG_VERSION_STRING;
Chris Lattnerb95ee582009-05-02 01:04:13 +0000126 bool isOptimized = LO.Optimize;
Chris Lattner4c2577a2009-05-02 01:00:04 +0000127 const char *Flags = ""; // FIXME: Encode command line options.
128
129 // Figure out which version of the ObjC runtime we have.
130 unsigned RuntimeVers = 0;
131 if (LO.ObjC1)
132 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000133
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000134 // Create new compile unit.
Devang Patelab71ff52009-11-12 00:51:46 +0000135 return Unit = DebugFactory.CreateCompileUnit(LangTag,
Jeffrey Yasskine3fdca22009-12-08 01:46:24 +0000136 AbsFileName.getLast(),
137 AbsFileName.getDirname(),
Benjamin Kramer47daf682009-12-08 11:02:29 +0000138 Producer, isMain,
Devang Patelab71ff52009-11-12 00:51:46 +0000139 isOptimized, Flags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000140}
141
Devang Patel65e99f22009-02-25 01:36:11 +0000142/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000143/// one if necessary.
144llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel65e99f22009-02-25 01:36:11 +0000145 llvm::DICompileUnit Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000146 unsigned Encoding = 0;
147 switch (BT->getKind()) {
148 default:
149 case BuiltinType::Void:
150 return llvm::DIType();
151 case BuiltinType::UChar:
152 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
153 case BuiltinType::Char_S:
154 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
155 case BuiltinType::UShort:
156 case BuiltinType::UInt:
157 case BuiltinType::ULong:
158 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
159 case BuiltinType::Short:
160 case BuiltinType::Int:
161 case BuiltinType::Long:
162 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
163 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
164 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000165 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000166 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000167 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000168 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000169 uint64_t Size = CGM.getContext().getTypeSize(BT);
170 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000171 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Devang Patelca80a5f2009-10-20 19:55:01 +0000173 llvm::DIType DbgTy =
174 DebugFactory.CreateBasicType(Unit,
Anders Carlsson20f12a22009-12-06 18:00:51 +0000175 BT->getName(CGM.getContext().getLangOptions()),
Devang Patelca80a5f2009-10-20 19:55:01 +0000176 Unit, 0, Size, Align,
177 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000178 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000179}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000180
Chris Lattnerb7003772009-04-23 06:13:01 +0000181llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
182 llvm::DICompileUnit Unit) {
183 // Bit size, align and offset of the type.
184 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
185 if (Ty->isComplexIntegerType())
186 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Anders Carlsson20f12a22009-12-06 18:00:51 +0000188 uint64_t Size = CGM.getContext().getTypeSize(Ty);
189 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattnerb7003772009-04-23 06:13:01 +0000190 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Devang Patelca80a5f2009-10-20 19:55:01 +0000192 llvm::DIType DbgTy =
193 DebugFactory.CreateBasicType(Unit, "complex",
194 Unit, 0, Size, Align,
195 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000196 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000197}
198
John McCalla1805292009-09-25 01:40:47 +0000199/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000200/// a new one if necessary.
John McCalla1805292009-09-25 01:40:47 +0000201llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DICompileUnit Unit) {
202 QualifierCollector Qc;
203 const Type *T = Qc.strip(Ty);
204
205 // Ignore these qualifiers for now.
206 Qc.removeObjCGCAttr();
207 Qc.removeAddressSpace();
208
Chris Lattner9c85ba32008-11-10 06:08:34 +0000209 // We will create one Derived type for one qualifier and recurse to handle any
210 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000211 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000212 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000213 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000214 Qc.removeConst();
215 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000216 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000217 Qc.removeVolatile();
218 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000219 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000220 Qc.removeRestrict();
221 } else {
222 assert(Qc.empty() && "Unknown type qualifier for debug info");
223 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000224 }
Mike Stump1eb44332009-09-09 15:08:12 +0000225
John McCalla1805292009-09-25 01:40:47 +0000226 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
227
Daniel Dunbar3845f862008-10-31 03:54:29 +0000228 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
229 // CVR derived types.
Devang Patelca80a5f2009-10-20 19:55:01 +0000230 llvm::DIType DbgTy =
231 DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
232 0, 0, 0, 0, 0, FromTy);
Devang Patelca80a5f2009-10-20 19:55:01 +0000233 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000234}
235
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000236llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
237 llvm::DICompileUnit Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000238 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000239 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
240 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000241 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000242}
243
Chris Lattner9c85ba32008-11-10 06:08:34 +0000244llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
245 llvm::DICompileUnit Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000246 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
247 Ty->getPointeeType(), Unit);
248}
249
250llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
251 const Type *Ty,
252 QualType PointeeTy,
253 llvm::DICompileUnit Unit) {
254 llvm::DIType EltTy = getOrCreateType(PointeeTy, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000256 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000257
258 // Size is always the size of a pointer. We can't use getTypeSize here
259 // because that does not return the correct value for references.
260 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000261 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
262 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Devang Patelca80a5f2009-10-20 19:55:01 +0000264 return
Anders Carlssona031b352009-11-06 19:19:55 +0000265 DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
Devang Patelca80a5f2009-10-20 19:55:01 +0000266 0, Size, Align, 0, 0, EltTy);
Anders Carlssona031b352009-11-06 19:19:55 +0000267
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000268}
269
Mike Stump9bc093c2009-05-14 02:03:51 +0000270llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
271 llvm::DICompileUnit Unit) {
272 if (BlockLiteralGenericSet)
273 return BlockLiteralGeneric;
274
275 llvm::DICompileUnit DefUnit;
276 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
277
278 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
279
280 llvm::DIType FieldTy;
281
282 QualType FType;
283 uint64_t FieldSize, FieldOffset;
284 unsigned FieldAlign;
285
286 llvm::DIArray Elements;
287 llvm::DIType EltTy, DescTy;
288
289 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000290 FType = CGM.getContext().UnsignedLongTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000291 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000292 FieldSize = CGM.getContext().getTypeSize(FType);
293 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000294 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
295 "reserved", DefUnit,
296 0, FieldSize, FieldAlign,
297 FieldOffset, 0, FieldTy);
298 EltTys.push_back(FieldTy);
299
300 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000301 FType = CGM.getContext().UnsignedLongTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000302 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000303 FieldSize = CGM.getContext().getTypeSize(FType);
304 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000305 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
306 "Size", DefUnit,
307 0, FieldSize, FieldAlign,
308 FieldOffset, 0, FieldTy);
309 EltTys.push_back(FieldTy);
310
311 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000312 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000313 EltTys.clear();
314
Mike Stump3d363c52009-10-02 02:30:50 +0000315 unsigned Flags = llvm::DIType::FlagAppleBlock;
316
Mike Stump9bc093c2009-05-14 02:03:51 +0000317 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Mike Stump3d363c52009-10-02 02:30:50 +0000318 DefUnit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000319 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Mike Stump9bc093c2009-05-14 02:03:51 +0000321 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000322 uint64_t Size = CGM.getContext().getTypeSize(Ty);
323 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Mike Stump9bc093c2009-05-14 02:03:51 +0000325 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
326 Unit, "", llvm::DICompileUnit(),
327 0, Size, Align, 0, 0, EltTy);
328
329 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000330 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000331 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000332 FieldSize = CGM.getContext().getTypeSize(FType);
333 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000334 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
335 "__isa", DefUnit,
336 0, FieldSize, FieldAlign,
337 FieldOffset, 0, FieldTy);
338 EltTys.push_back(FieldTy);
339
340 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000341 FType = CGM.getContext().IntTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000342 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000343 FieldSize = CGM.getContext().getTypeSize(FType);
344 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000345 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
346 "__flags", DefUnit,
347 0, FieldSize, FieldAlign,
348 FieldOffset, 0, FieldTy);
349 EltTys.push_back(FieldTy);
350
351 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000352 FType = CGM.getContext().IntTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000353 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000354 FieldSize = CGM.getContext().getTypeSize(FType);
355 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000356 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
357 "__reserved", DefUnit,
358 0, FieldSize, FieldAlign,
359 FieldOffset, 0, FieldTy);
360 EltTys.push_back(FieldTy);
361
362 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000363 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000364 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000365 FieldSize = CGM.getContext().getTypeSize(FType);
366 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000367 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
368 "__FuncPtr", DefUnit,
369 0, FieldSize, FieldAlign,
370 FieldOffset, 0, FieldTy);
371 EltTys.push_back(FieldTy);
372
373 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000374 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000375 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000376 FieldSize = CGM.getContext().getTypeSize(Ty);
377 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000378 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
379 "__descriptor", DefUnit,
380 0, FieldSize, FieldAlign,
381 FieldOffset, 0, FieldTy);
382 EltTys.push_back(FieldTy);
383
384 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000385 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000386
387 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Mike Stump944e7052009-10-02 02:23:37 +0000388 DefUnit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000389 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Mike Stump9bc093c2009-05-14 02:03:51 +0000391 BlockLiteralGenericSet = true;
392 BlockLiteralGeneric
393 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
394 "", llvm::DICompileUnit(),
395 0, Size, Align, 0, 0, EltTy);
396 return BlockLiteralGeneric;
397}
398
Chris Lattner9c85ba32008-11-10 06:08:34 +0000399llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
400 llvm::DICompileUnit Unit) {
401 // Typedefs are derived from some other type. If we have a typedef of a
402 // typedef, make sure to emit the whole chain.
403 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Chris Lattner9c85ba32008-11-10 06:08:34 +0000405 // We don't set size information, but do specify where the typedef was
406 // declared.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000407 SourceLocation DefLoc = Ty->getDecl()->getLocation();
408 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000409
Anders Carlsson20f12a22009-12-06 18:00:51 +0000410 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000411 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
412 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000413
Devang Patelca80a5f2009-10-20 19:55:01 +0000414 llvm::DIType DbgTy =
415 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
Devang Patel73621622009-11-25 17:37:31 +0000416 Ty->getDecl()->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000417 DefUnit, Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000418 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000419}
420
Chris Lattner9c85ba32008-11-10 06:08:34 +0000421llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
422 llvm::DICompileUnit Unit) {
423 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000424
Chris Lattner9c85ba32008-11-10 06:08:34 +0000425 // Add the result type at least.
426 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000427
Chris Lattner9c85ba32008-11-10 06:08:34 +0000428 // Set up remainder of arguments if there is a prototype.
429 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000430 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000431 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
432 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
433 } else {
434 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000435 }
436
Chris Lattner9c85ba32008-11-10 06:08:34 +0000437 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000438 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Devang Patelca80a5f2009-10-20 19:55:01 +0000440 llvm::DIType DbgTy =
441 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
442 Unit, "", llvm::DICompileUnit(),
443 0, 0, 0, 0, 0,
444 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000445 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000446}
447
Devang Patel65e99f22009-02-25 01:36:11 +0000448/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000449llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
450 llvm::DICompileUnit Unit) {
Douglas Gregora4c46df2008-12-11 17:59:21 +0000451 RecordDecl *Decl = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Chris Lattner9c85ba32008-11-10 06:08:34 +0000453 unsigned Tag;
454 if (Decl->isStruct())
455 Tag = llvm::dwarf::DW_TAG_structure_type;
456 else if (Decl->isUnion())
457 Tag = llvm::dwarf::DW_TAG_union_type;
458 else {
459 assert(Decl->isClass() && "Unknown RecordType!");
460 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000461 }
462
Anders Carlsson20f12a22009-12-06 18:00:51 +0000463 SourceManager &SM = CGM.getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000464
Chris Lattner9c85ba32008-11-10 06:08:34 +0000465 // Get overall information about the record type for the debug info.
Devang Patel4f6fa232009-04-17 21:35:15 +0000466 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000467 llvm::DICompileUnit DefUnit;
468 unsigned Line = 0;
469 if (!PLoc.isInvalid()) {
470 DefUnit = getOrCreateCompileUnit(Decl->getLocation());
471 Line = PLoc.getLine();
472 }
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Chris Lattner9c85ba32008-11-10 06:08:34 +0000474 // Records and classes and unions can all be recursive. To handle them, we
475 // first generate a debug descriptor for the struct as a forward declaration.
476 // Then (if it is a definition) we go through and get debug info for all of
477 // its members. Finally, we create a descriptor for the complete type (which
478 // may refer to the forward decl if the struct is recursive) and replace all
479 // uses of the forward declaration with the final definition.
Devang Patel0ce73f62009-07-22 18:57:00 +0000480 llvm::DICompositeType FwdDecl =
Devang Patel73621622009-11-25 17:37:31 +0000481 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000482 DefUnit, Line, 0, 0, 0, 0,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000483 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Chris Lattner9c85ba32008-11-10 06:08:34 +0000485 // If this is just a forward declaration, return it.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000486 if (!Decl->getDefinition(CGM.getContext()))
Chris Lattner9c85ba32008-11-10 06:08:34 +0000487 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000488
Eli Friedman14d63652009-11-16 21:04:30 +0000489 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000490 // Otherwise, insert it into the TypeCache so that recursive uses will find
491 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000492 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000493
494 // Convert all the elements.
495 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
496
Anders Carlsson20f12a22009-12-06 18:00:51 +0000497 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(Decl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000498
499 unsigned FieldNo = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000500 for (RecordDecl::field_iterator I = Decl->field_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +0000501 E = Decl->field_end();
Douglas Gregora4c46df2008-12-11 17:59:21 +0000502 I != E; ++I, ++FieldNo) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000503 FieldDecl *Field = *I;
504 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattner8ec03f52008-11-24 03:54:41 +0000505
Devang Patel73621622009-11-25 17:37:31 +0000506 llvm::StringRef FieldName = Field->getName();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000507
Devang Patelde135022009-04-27 22:40:36 +0000508 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +0000509 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +0000510 continue;
511
Chris Lattner9c85ba32008-11-10 06:08:34 +0000512 // Get the location for the field.
513 SourceLocation FieldDefLoc = Field->getLocation();
Devang Patel4f6fa232009-04-17 21:35:15 +0000514 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000515 llvm::DICompileUnit FieldDefUnit;
516 unsigned FieldLine = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000518 if (!PLoc.isInvalid()) {
519 FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
520 FieldLine = PLoc.getLine();
521 }
Devang Patelec9b5d52009-03-16 23:47:53 +0000522
523 QualType FType = Field->getType();
524 uint64_t FieldSize = 0;
525 unsigned FieldAlign = 0;
526 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Devang Patelec9b5d52009-03-16 23:47:53 +0000528 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000529 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patelec9b5d52009-03-16 23:47:53 +0000530 Expr *BitWidth = Field->getBitWidth();
531 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +0000532 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Anders Carlsson20f12a22009-12-06 18:00:51 +0000534 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patelec9b5d52009-03-16 23:47:53 +0000535 }
536
Mike Stump1eb44332009-09-09 15:08:12 +0000537 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
538
Chris Lattner9c85ba32008-11-10 06:08:34 +0000539 // Create a DW_TAG_member node to remember the offset of this field in the
540 // struct. FIXME: This is an absolutely insane way to capture this
541 // information. When we gut debug info, this should be fixed.
542 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
543 FieldName, FieldDefUnit,
544 FieldLine, FieldSize, FieldAlign,
545 FieldOffset, 0, FieldTy);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000546 EltTys.push_back(FieldTy);
547 }
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Chris Lattner9c85ba32008-11-10 06:08:34 +0000549 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000550 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000551
552 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000553 uint64_t Size = CGM.getContext().getTypeSize(Ty);
554 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000555
Devang Patel0ce73f62009-07-22 18:57:00 +0000556 llvm::DICompositeType RealDecl =
Devang Patel73621622009-11-25 17:37:31 +0000557 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000558 DefUnit, Line, Size, Align, 0, 0,
559 llvm::DIType(), Elements);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000560
561 // Now that we have a real decl for the struct, replace anything using the
562 // old decl with the new one. This will recursively update the debug info.
Eli Friedman14d63652009-11-16 21:04:30 +0000563 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000564
Chris Lattner9c85ba32008-11-10 06:08:34 +0000565 return RealDecl;
566}
567
Devang Patel9ca36b62009-02-26 21:10:26 +0000568/// CreateType - get objective-c interface type.
569llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
570 llvm::DICompileUnit Unit) {
571 ObjCInterfaceDecl *Decl = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000572
Devang Patel9ca36b62009-02-26 21:10:26 +0000573 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000574 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel9ca36b62009-02-26 21:10:26 +0000575
576 // Get overall information about the record type for the debug info.
Devang Patel9ca36b62009-02-26 21:10:26 +0000577 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000578 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
579 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
580
Mike Stump1eb44332009-09-09 15:08:12 +0000581
Daniel Dunbard86d3362009-05-18 20:51:58 +0000582 unsigned RuntimeLang = DefUnit.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000583
Devang Patel9ca36b62009-02-26 21:10:26 +0000584 // To handle recursive interface, we
585 // first generate a debug descriptor for the struct as a forward declaration.
586 // Then (if it is a definition) we go through and get debug info for all of
587 // its members. Finally, we create a descriptor for the complete type (which
588 // may refer to the forward decl if the struct is recursive) and replace all
589 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000590 llvm::DICompositeType FwdDecl =
Devang Patel73621622009-11-25 17:37:31 +0000591 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000592 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000593 llvm::DIType(), llvm::DIArray(),
594 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000595
Devang Patel9ca36b62009-02-26 21:10:26 +0000596 // If this is just a forward declaration, return it.
597 if (Decl->isForwardDecl())
598 return FwdDecl;
599
Devang Patelffffb032009-11-16 20:09:38 +0000600 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000601 // Otherwise, insert it into the TypeCache so that recursive uses will find
602 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000603 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000604
605 // Convert all the elements.
606 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
607
Devang Patelfbe899f2009-03-10 21:30:26 +0000608 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
609 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000610 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000611 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000612 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000613 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Chris Lattner9e55b8a2009-05-05 05:05:36 +0000614 Unit, "", llvm::DICompileUnit(), 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000615 0 /* offset */, 0, SClassTy);
616 EltTys.push_back(InhTag);
617 }
618
Anders Carlsson20f12a22009-12-06 18:00:51 +0000619 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000620
621 unsigned FieldNo = 0;
622 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
623 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
624 ObjCIvarDecl *Field = *I;
625 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
626
Devang Patel73621622009-11-25 17:37:31 +0000627 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +0000628
Devang Patelde135022009-04-27 22:40:36 +0000629 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +0000630 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +0000631 continue;
632
Devang Patel9ca36b62009-02-26 21:10:26 +0000633 // Get the location for the field.
634 SourceLocation FieldDefLoc = Field->getLocation();
635 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000636 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
637 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
638
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Devang Patel99c20eb2009-03-20 18:24:39 +0000640 QualType FType = Field->getType();
641 uint64_t FieldSize = 0;
642 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000643
Devang Patel99c20eb2009-03-20 18:24:39 +0000644 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000645
Devang Patel99c20eb2009-03-20 18:24:39 +0000646 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000647 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000648 Expr *BitWidth = Field->getBitWidth();
649 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +0000650 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000651
Anders Carlsson20f12a22009-12-06 18:00:51 +0000652 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000653 }
654
Mike Stump1eb44332009-09-09 15:08:12 +0000655 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
656
Devang Patelc20482b2009-03-19 00:23:53 +0000657 unsigned Flags = 0;
658 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
659 Flags = llvm::DIType::FlagProtected;
660 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
661 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +0000662
Devang Patel9ca36b62009-02-26 21:10:26 +0000663 // Create a DW_TAG_member node to remember the offset of this field in the
664 // struct. FIXME: This is an absolutely insane way to capture this
665 // information. When we gut debug info, this should be fixed.
666 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
667 FieldName, FieldDefUnit,
668 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000669 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000670 EltTys.push_back(FieldTy);
671 }
Mike Stump1eb44332009-09-09 15:08:12 +0000672
Devang Patel9ca36b62009-02-26 21:10:26 +0000673 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000674 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +0000675
676 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000677 uint64_t Size = CGM.getContext().getTypeSize(Ty);
678 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000679
Devang Patel6c1fddf2009-07-27 18:42:03 +0000680 llvm::DICompositeType RealDecl =
Devang Patel73621622009-11-25 17:37:31 +0000681 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +0000682 Line, Size, Align, 0, 0, llvm::DIType(),
683 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000684
685 // Now that we have a real decl for the struct, replace anything using the
686 // old decl with the new one. This will recursively update the debug info.
Devang Patelffffb032009-11-16 20:09:38 +0000687 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000688
Devang Patel9ca36b62009-02-26 21:10:26 +0000689 return RealDecl;
690}
691
Chris Lattner9c85ba32008-11-10 06:08:34 +0000692llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
693 llvm::DICompileUnit Unit) {
694 EnumDecl *Decl = Ty->getDecl();
695
696 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
697
698 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +0000699 for (EnumDecl::enumerator_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000700 Enum = Decl->enumerator_begin(), EnumEnd = Decl->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000701 Enum != EnumEnd; ++Enum) {
Devang Patel73621622009-11-25 17:37:31 +0000702 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor44b43212008-12-11 16:49:14 +0000703 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000704 }
Mike Stump1eb44332009-09-09 15:08:12 +0000705
Chris Lattner9c85ba32008-11-10 06:08:34 +0000706 // Return a CompositeType for the enum itself.
707 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000708 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000709
Chris Lattner9c85ba32008-11-10 06:08:34 +0000710 SourceLocation DefLoc = Decl->getLocation();
711 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000712 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000713 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
714 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
715
Mike Stump1eb44332009-09-09 15:08:12 +0000716
Chris Lattner9c85ba32008-11-10 06:08:34 +0000717 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +0000718 uint64_t Size = 0;
719 unsigned Align = 0;
720 if (!Ty->isIncompleteType()) {
Anders Carlsson20f12a22009-12-06 18:00:51 +0000721 Size = CGM.getContext().getTypeSize(Ty);
722 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman3189e4b2009-05-04 04:39:55 +0000723 }
Mike Stump1eb44332009-09-09 15:08:12 +0000724
Devang Patelca80a5f2009-10-20 19:55:01 +0000725 llvm::DIType DbgTy =
726 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Patel73621622009-11-25 17:37:31 +0000727 Unit, Decl->getName(), DefUnit, Line,
Devang Patelca80a5f2009-10-20 19:55:01 +0000728 Size, Align, 0, 0,
729 llvm::DIType(), EltArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000730 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000731}
732
733llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
734 llvm::DICompileUnit Unit) {
735 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
736 return CreateType(RT, Unit);
737 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
738 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Chris Lattner9c85ba32008-11-10 06:08:34 +0000740 return llvm::DIType();
741}
742
743llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
744 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000745 uint64_t Size;
746 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +0000747
748
Nuno Lopes010d5142009-01-28 00:35:17 +0000749 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +0000750 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000751 Size = 0;
752 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000753 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +0000754 } else if (Ty->isIncompleteArrayType()) {
755 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000756 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +0000757 } else {
758 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000759 Size = CGM.getContext().getTypeSize(Ty);
760 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +0000761 }
Mike Stump1eb44332009-09-09 15:08:12 +0000762
Chris Lattner9c85ba32008-11-10 06:08:34 +0000763 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
764 // interior arrays, do we care? Why aren't nested arrays represented the
765 // obvious/recursive way?
766 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
767 QualType EltTy(Ty, 0);
768 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000769 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000770 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +0000771 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +0000772 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000773 // FIXME: Verify this is right for VLAs.
774 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
775 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000776 }
Mike Stump1eb44332009-09-09 15:08:12 +0000777
Chris Lattner9c85ba32008-11-10 06:08:34 +0000778 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000779 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000780
Devang Patelca80a5f2009-10-20 19:55:01 +0000781 llvm::DIType DbgTy =
782 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
783 Unit, "", llvm::DICompileUnit(),
784 0, Size, Align, 0, 0,
785 getOrCreateType(EltTy, Unit),
786 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000787 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000788}
789
Anders Carlssona031b352009-11-06 19:19:55 +0000790llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
791 llvm::DICompileUnit Unit) {
792 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
793 Ty, Ty->getPointeeType(), Unit);
794}
Chris Lattner9c85ba32008-11-10 06:08:34 +0000795
Anders Carlsson20f12a22009-12-06 18:00:51 +0000796llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
797 llvm::DICompileUnit U) {
798 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
799 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
800
801 if (!Ty->getPointeeType()->isFunctionType()) {
802 // We have a data member pointer type.
803 return PointerDiffDITy;
804 }
805
806 // We have a member function pointer type. Treat it as a struct with two
807 // ptrdiff_t members.
808 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
809
810 uint64_t FieldOffset = 0;
811 llvm::DIDescriptor ElementTypes[2];
812
813 // FIXME: This should probably be a function type instead.
814 ElementTypes[0] =
815 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
816 "ptr", llvm::DICompileUnit(), 0,
817 Info.first, Info.second, FieldOffset, 0,
818 PointerDiffDITy);
819 FieldOffset += Info.first;
820
821 ElementTypes[1] =
822 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
823 "ptr", llvm::DICompileUnit(), 0,
824 Info.first, Info.second, FieldOffset, 0,
825 PointerDiffDITy);
826
827 llvm::DIArray Elements =
828 DebugFactory.GetOrCreateArray(&ElementTypes[0],
829 llvm::array_lengthof(ElementTypes));
830
831 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
832 U, llvm::StringRef("test"),
833 llvm::DICompileUnit(), 0, FieldOffset,
834 0, 0, 0, llvm::DIType(), Elements);
835}
836
Anders Carlsson5b6117a2009-11-14 21:08:12 +0000837static QualType CanonicalizeTypeForDebugInfo(QualType T) {
838 switch (T->getTypeClass()) {
839 default:
840 return T;
841 case Type::TemplateSpecialization:
842 return cast<TemplateSpecializationType>(T)->desugar();
843 case Type::TypeOfExpr: {
844 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
845 return CanonicalizeTypeForDebugInfo(Ty->getUnderlyingExpr()->getType());
846 }
847 case Type::TypeOf:
848 return cast<TypeOfType>(T)->getUnderlyingType();
849 case Type::Decltype:
850 return cast<DecltypeType>(T)->getUnderlyingType();
851 case Type::QualifiedName:
852 return cast<QualifiedNameType>(T)->getNamedType();
853 case Type::SubstTemplateTypeParm:
854 return cast<SubstTemplateTypeParmType>(T)->getReplacementType();
855 case Type::Elaborated:
856 return cast<ElaboratedType>(T)->getUnderlyingType();
857 }
858}
859
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000860/// getOrCreateType - Get the type from the cache or create a new
861/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000862llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
863 llvm::DICompileUnit Unit) {
864 if (Ty.isNull())
865 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +0000866
Anders Carlsson5b6117a2009-11-14 21:08:12 +0000867 // Canonicalize the type.
868 Ty = CanonicalizeTypeForDebugInfo(Ty);
869
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000870 // Check for existing entry.
Daniel Dunbar65f13c32009-09-19 20:17:48 +0000871 std::map<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000872 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +0000873 if (it != TypeCache.end()) {
874 // Verify that the debug info still exists.
875 if (&*it->second)
876 return llvm::DIType(cast<llvm::MDNode>(it->second));
877 }
Daniel Dunbar03faac32009-09-19 19:27:14 +0000878
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000879 // Otherwise create the type.
880 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +0000881
882 // And update the type cache.
883 TypeCache[Ty.getAsOpaquePtr()] = Res.getNode();
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000884 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +0000885}
886
Anders Carlsson0dd57c62009-11-14 20:52:05 +0000887/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +0000888llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
889 llvm::DICompileUnit Unit) {
John McCalla1805292009-09-25 01:40:47 +0000890 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +0000891 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +0000892 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000893
894 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000895 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000896#define TYPE(Class, Base)
897#define ABSTRACT_TYPE(Class, Base)
898#define NON_CANONICAL_TYPE(Class, Base)
899#define DEPENDENT_TYPE(Class, Base) case Type::Class:
900#include "clang/AST/TypeNodes.def"
901 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +0000902
Anders Carlssonbfe69952009-11-06 18:24:04 +0000903 // FIXME: Handle these.
904 case Type::ExtVector:
905 case Type::Vector:
Anders Carlssonba578cb2009-11-07 01:19:37 +0000906 case Type::FixedWidthInt:
Anders Carlssonbfe69952009-11-06 18:24:04 +0000907 return llvm::DIType();
Daniel Dunbar03faac32009-09-19 19:27:14 +0000908 default:
Anders Carlssonba578cb2009-11-07 01:19:37 +0000909 assert(false && "Unhandled type class!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000910 return llvm::DIType();
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000911 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000912 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000913 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000914 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
915 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
916 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
917 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +0000918 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000919 return CreateType(cast<BlockPointerType>(Ty), Unit);
920 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000921 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000922 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000923 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000924 case Type::FunctionProto:
925 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000926 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000927 case Type::ConstantArray:
928 case Type::VariableArray:
929 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000930 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +0000931
932 case Type::LValueReference:
933 return CreateType(cast<LValueReferenceType>(Ty), Unit);
934
Anders Carlsson20f12a22009-12-06 18:00:51 +0000935 case Type::MemberPointer:
936 return CreateType(cast<MemberPointerType>(Ty), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000937 }
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000938}
939
940/// EmitFunctionStart - Constructs the debug code for entering a function -
941/// "llvm.dbg.func.start.".
Benjamin Kramer155fd792009-12-08 14:04:35 +0000942void CGDebugInfo::EmitFunctionStart(llvm::StringRef Name, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000943 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000944 CGBuilderTy &Builder) {
Benjamin Kramer155fd792009-12-08 14:04:35 +0000945 llvm::StringRef LinkageName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +0000946
Daniel Dunbara2893932009-05-13 23:08:57 +0000947 // Skip the asm prefix if it exists.
Daniel Dunbarbbd53af2009-05-14 01:45:24 +0000948 //
949 // FIXME: This should probably be the unmangled name?
Daniel Dunbara2893932009-05-13 23:08:57 +0000950 if (Name[0] == '\01')
Benjamin Kramer155fd792009-12-08 14:04:35 +0000951 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +0000952
Chris Lattner9c85ba32008-11-10 06:08:34 +0000953 // FIXME: Why is this using CurLoc???
954 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000955 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +0000956 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Mike Stump1eb44332009-09-09 15:08:12 +0000957
Chris Lattner9c85ba32008-11-10 06:08:34 +0000958 llvm::DISubprogram SP =
Devang Patel6dba4322009-07-14 21:31:22 +0000959 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +0000960 getOrCreateType(FnType, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000961 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump1eb44332009-09-09 15:08:12 +0000962
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000963 // Push function on region stack.
Devang Patel8fae0602009-11-13 19:10:24 +0000964 RegionStack.push_back(SP.getNode());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000965}
966
967
Chris Lattner9c85ba32008-11-10 06:08:34 +0000968void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000969 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000970
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000971 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000972 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000973 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +0000974 || (SM.getInstantiationLineNumber(CurLoc) ==
975 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000976 && SM.isFromSameFile(CurLoc, PrevLoc)))
977 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000978
979 // Update last state.
980 PrevLoc = CurLoc;
981
982 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000983 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +0000984 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patelbbd9fa42009-10-06 18:36:08 +0000985
Devang Patel8fae0602009-11-13 19:10:24 +0000986 llvm::DIDescriptor DR(RegionStack.back());
Devang Patelbbd9fa42009-10-06 18:36:08 +0000987 llvm::DIScope DS = llvm::DIScope(DR.getNode());
988 llvm::DILocation DO(NULL);
989 llvm::DILocation DL =
990 DebugFactory.CreateLocation(PLoc.getLine(), PLoc.getColumn(),
991 DS, DO);
992 Builder.SetCurrentDebugLocation(DL.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000993}
994
995/// EmitRegionStart- Constructs the debug code for entering a declarative
996/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000997void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +0000998 llvm::DIDescriptor D =
999 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1000 llvm::DIDescriptor() :
1001 llvm::DIDescriptor(RegionStack.back()));
1002 RegionStack.push_back(D.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001003}
1004
1005/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1006/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +00001007void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001008 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1009
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001010 // Provide an region stop point.
1011 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001012
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001013 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001014}
1015
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001016/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001017void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
1018 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001019 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1020
Devang Patel07739032009-03-27 23:16:32 +00001021 // Do not emit variable debug information while generating optimized code.
1022 // The llvm optimizer and code generator are not yet ready to support
1023 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001024 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001025 if (CGO.OptimizationLevel)
Devang Patel07739032009-03-27 23:16:32 +00001026 return;
1027
Chris Lattner650cea92009-05-05 04:57:08 +00001028 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Mike Stump39605b42009-09-22 02:12:52 +00001029 QualType Type = Decl->getType();
1030 llvm::DIType Ty = getOrCreateType(Type, Unit);
1031 if (Decl->hasAttr<BlocksAttr>()) {
1032 llvm::DICompileUnit DefUnit;
1033 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1034
1035 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1036
1037 llvm::DIType FieldTy;
1038
1039 QualType FType;
1040 uint64_t FieldSize, FieldOffset;
1041 unsigned FieldAlign;
1042
1043 llvm::DIArray Elements;
1044 llvm::DIType EltTy;
1045
1046 // Build up structure for the byref. See BuildByRefType.
1047 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001048 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001049 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001050 FieldSize = CGM.getContext().getTypeSize(FType);
1051 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001052 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1053 "__isa", DefUnit,
1054 0, FieldSize, FieldAlign,
1055 FieldOffset, 0, FieldTy);
1056 EltTys.push_back(FieldTy);
1057 FieldOffset += FieldSize;
1058
Anders Carlsson20f12a22009-12-06 18:00:51 +00001059 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001060 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001061 FieldSize = CGM.getContext().getTypeSize(FType);
1062 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001063 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1064 "__forwarding", DefUnit,
1065 0, FieldSize, FieldAlign,
1066 FieldOffset, 0, FieldTy);
1067 EltTys.push_back(FieldTy);
1068 FieldOffset += FieldSize;
1069
Anders Carlsson20f12a22009-12-06 18:00:51 +00001070 FType = CGM.getContext().getFixedWidthIntType(32, true); // Int32Ty;
Mike Stump39605b42009-09-22 02:12:52 +00001071 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001072 FieldSize = CGM.getContext().getTypeSize(FType);
1073 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001074 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1075 "__flags", DefUnit,
1076 0, FieldSize, FieldAlign,
1077 FieldOffset, 0, FieldTy);
1078 EltTys.push_back(FieldTy);
1079 FieldOffset += FieldSize;
1080
Anders Carlsson20f12a22009-12-06 18:00:51 +00001081 FType = CGM.getContext().getFixedWidthIntType(32, true); // Int32Ty;
Mike Stump39605b42009-09-22 02:12:52 +00001082 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001083 FieldSize = CGM.getContext().getTypeSize(FType);
1084 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001085 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1086 "__size", DefUnit,
1087 0, FieldSize, FieldAlign,
1088 FieldOffset, 0, FieldTy);
1089 EltTys.push_back(FieldTy);
1090 FieldOffset += FieldSize;
1091
Anders Carlsson20f12a22009-12-06 18:00:51 +00001092 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
Mike Stump39605b42009-09-22 02:12:52 +00001093 if (HasCopyAndDispose) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001094 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001095 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001096 FieldSize = CGM.getContext().getTypeSize(FType);
1097 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001098 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1099 "__copy_helper", DefUnit,
1100 0, FieldSize, FieldAlign,
1101 FieldOffset, 0, FieldTy);
1102 EltTys.push_back(FieldTy);
1103 FieldOffset += FieldSize;
1104
Anders Carlsson20f12a22009-12-06 18:00:51 +00001105 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001106 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001107 FieldSize = CGM.getContext().getTypeSize(FType);
1108 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001109 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1110 "__destroy_helper", DefUnit,
1111 0, FieldSize, FieldAlign,
1112 FieldOffset, 0, FieldTy);
1113 EltTys.push_back(FieldTy);
1114 FieldOffset += FieldSize;
1115 }
1116
Anders Carlsson20f12a22009-12-06 18:00:51 +00001117 unsigned Align = CGM.getContext().getDeclAlignInBytes(Decl);
1118 if (Align > CGM.getContext().Target.getPointerAlign(0) / 8) {
Mike Stump39605b42009-09-22 02:12:52 +00001119 unsigned AlignedOffsetInBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001120 = llvm::RoundUpToAlignment(FieldOffset/8, Align);
Mike Stump39605b42009-09-22 02:12:52 +00001121 unsigned NumPaddingBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001122 = AlignedOffsetInBytes - FieldOffset/8;
Mike Stump39605b42009-09-22 02:12:52 +00001123
1124 if (NumPaddingBytes > 0) {
1125 llvm::APInt pad(32, NumPaddingBytes);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001126 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
Mike Stump39605b42009-09-22 02:12:52 +00001127 pad, ArrayType::Normal, 0);
1128 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001129 FieldSize = CGM.getContext().getTypeSize(FType);
1130 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001131 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1132 Unit, "", DefUnit,
1133 0, FieldSize, FieldAlign,
1134 FieldOffset, 0, FieldTy);
1135 EltTys.push_back(FieldTy);
1136 FieldOffset += FieldSize;
1137 }
1138 }
1139
1140 FType = Type;
1141 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001142 FieldSize = CGM.getContext().getTypeSize(FType);
Mike Stumpfd47b312009-09-22 02:44:17 +00001143 FieldAlign = Align*8;
Mike Stump39605b42009-09-22 02:12:52 +00001144
1145 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel73621622009-11-25 17:37:31 +00001146 Decl->getName(), DefUnit,
Mike Stump39605b42009-09-22 02:12:52 +00001147 0, FieldSize, FieldAlign,
1148 FieldOffset, 0, FieldTy);
1149 EltTys.push_back(FieldTy);
1150 FieldOffset += FieldSize;
1151
1152 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1153
1154 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1155
1156 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1157 llvm::DICompileUnit(),
1158 0, FieldOffset, 0, 0, Flags,
1159 llvm::DIType(), Elements);
1160 }
Chris Lattner650cea92009-05-05 04:57:08 +00001161
Chris Lattner9c85ba32008-11-10 06:08:34 +00001162 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001163 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001164 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +00001165 unsigned Line = 0;
Eli Friedman1468ac72009-11-16 20:33:31 +00001166 unsigned Column = 0;
1167 if (!PLoc.isInvalid()) {
Chris Lattner650cea92009-05-05 04:57:08 +00001168 Line = PLoc.getLine();
Eli Friedman1468ac72009-11-16 20:33:31 +00001169 Column = PLoc.getColumn();
1170 } else {
Chris Lattner650cea92009-05-05 04:57:08 +00001171 Unit = llvm::DICompileUnit();
Eli Friedman1468ac72009-11-16 20:33:31 +00001172 }
Mike Stump1eb44332009-09-09 15:08:12 +00001173
Chris Lattner9c85ba32008-11-10 06:08:34 +00001174 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001175 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001176 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel73621622009-11-25 17:37:31 +00001177 Decl->getName(),
Chris Lattner650cea92009-05-05 04:57:08 +00001178 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001179 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001180 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001181 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001182
Devang Patel8fae0602009-11-13 19:10:24 +00001183 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001184 llvm::DILocation DO(NULL);
1185 llvm::DILocation DL =
Eli Friedman1468ac72009-11-16 20:33:31 +00001186 DebugFactory.CreateLocation(Line, Column, DS, DO);
Devang Patel23908b82009-11-12 18:21:39 +00001187 Builder.SetDebugLocation(Call, DL.getNode());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001188}
1189
Mike Stumpb1a6e682009-09-30 02:43:10 +00001190/// EmitDeclare - Emit local variable declaration debug info.
1191void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1192 llvm::Value *Storage, CGBuilderTy &Builder,
1193 CodeGenFunction *CGF) {
1194 const ValueDecl *Decl = BDRE->getDecl();
1195 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1196
1197 // Do not emit variable debug information while generating optimized code.
1198 // The llvm optimizer and code generator are not yet ready to support
1199 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001200 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001201 if (CGO.OptimizationLevel || Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001202 return;
1203
1204 uint64_t XOffset = 0;
1205 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
1206 QualType Type = Decl->getType();
1207 llvm::DIType Ty = getOrCreateType(Type, Unit);
1208 if (Decl->hasAttr<BlocksAttr>()) {
1209 llvm::DICompileUnit DefUnit;
1210 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1211
1212 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1213
1214 llvm::DIType FieldTy;
1215
1216 QualType FType;
1217 uint64_t FieldSize, FieldOffset;
1218 unsigned FieldAlign;
1219
1220 llvm::DIArray Elements;
1221 llvm::DIType EltTy;
1222
1223 // Build up structure for the byref. See BuildByRefType.
1224 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001225 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001226 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001227 FieldSize = CGM.getContext().getTypeSize(FType);
1228 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001229 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1230 "__isa", DefUnit,
1231 0, FieldSize, FieldAlign,
1232 FieldOffset, 0, FieldTy);
1233 EltTys.push_back(FieldTy);
1234 FieldOffset += FieldSize;
1235
Anders Carlsson20f12a22009-12-06 18:00:51 +00001236 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001237 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001238 FieldSize = CGM.getContext().getTypeSize(FType);
1239 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001240 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1241 "__forwarding", DefUnit,
1242 0, FieldSize, FieldAlign,
1243 FieldOffset, 0, FieldTy);
1244 EltTys.push_back(FieldTy);
1245 FieldOffset += FieldSize;
1246
Anders Carlsson20f12a22009-12-06 18:00:51 +00001247 FType = CGM.getContext().getFixedWidthIntType(32, true); // Int32Ty;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001248 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001249 FieldSize = CGM.getContext().getTypeSize(FType);
1250 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001251 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1252 "__flags", DefUnit,
1253 0, FieldSize, FieldAlign,
1254 FieldOffset, 0, FieldTy);
1255 EltTys.push_back(FieldTy);
1256 FieldOffset += FieldSize;
1257
Anders Carlsson20f12a22009-12-06 18:00:51 +00001258 FType = CGM.getContext().getFixedWidthIntType(32, true); // Int32Ty;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001259 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001260 FieldSize = CGM.getContext().getTypeSize(FType);
1261 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001262 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1263 "__size", DefUnit,
1264 0, FieldSize, FieldAlign,
1265 FieldOffset, 0, FieldTy);
1266 EltTys.push_back(FieldTy);
1267 FieldOffset += FieldSize;
1268
Anders Carlsson20f12a22009-12-06 18:00:51 +00001269 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001270 if (HasCopyAndDispose) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001271 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001272 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001273 FieldSize = CGM.getContext().getTypeSize(FType);
1274 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001275 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1276 "__copy_helper", DefUnit,
1277 0, FieldSize, FieldAlign,
1278 FieldOffset, 0, FieldTy);
1279 EltTys.push_back(FieldTy);
1280 FieldOffset += FieldSize;
1281
Anders Carlsson20f12a22009-12-06 18:00:51 +00001282 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001283 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001284 FieldSize = CGM.getContext().getTypeSize(FType);
1285 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001286 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1287 "__destroy_helper", DefUnit,
1288 0, FieldSize, FieldAlign,
1289 FieldOffset, 0, FieldTy);
1290 EltTys.push_back(FieldTy);
1291 FieldOffset += FieldSize;
1292 }
1293
Anders Carlsson20f12a22009-12-06 18:00:51 +00001294 unsigned Align = CGM.getContext().getDeclAlignInBytes(Decl);
1295 if (Align > CGM.getContext().Target.getPointerAlign(0) / 8) {
Mike Stumpb1a6e682009-09-30 02:43:10 +00001296 unsigned AlignedOffsetInBytes
1297 = llvm::RoundUpToAlignment(FieldOffset/8, Align);
1298 unsigned NumPaddingBytes
1299 = AlignedOffsetInBytes - FieldOffset/8;
1300
1301 if (NumPaddingBytes > 0) {
1302 llvm::APInt pad(32, NumPaddingBytes);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001303 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001304 pad, ArrayType::Normal, 0);
1305 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001306 FieldSize = CGM.getContext().getTypeSize(FType);
1307 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001308 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1309 Unit, "", DefUnit,
1310 0, FieldSize, FieldAlign,
1311 FieldOffset, 0, FieldTy);
1312 EltTys.push_back(FieldTy);
1313 FieldOffset += FieldSize;
1314 }
1315 }
1316
1317 FType = Type;
1318 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001319 FieldSize = CGM.getContext().getTypeSize(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001320 FieldAlign = Align*8;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001321
1322 XOffset = FieldOffset;
1323 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel73621622009-11-25 17:37:31 +00001324 Decl->getName(), DefUnit,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001325 0, FieldSize, FieldAlign,
1326 FieldOffset, 0, FieldTy);
1327 EltTys.push_back(FieldTy);
1328 FieldOffset += FieldSize;
1329
1330 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1331
1332 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1333
1334 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1335 llvm::DICompileUnit(),
1336 0, FieldOffset, 0, 0, Flags,
1337 llvm::DIType(), Elements);
1338 }
1339
1340 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001341 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001342 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1343 unsigned Line = 0;
1344 if (!PLoc.isInvalid())
1345 Line = PLoc.getLine();
1346 else
1347 Unit = llvm::DICompileUnit();
1348
1349 uint64_t offset = CGF->BlockDecls[Decl];
1350 llvm::SmallVector<llvm::Value *, 9> addr;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001351 llvm::LLVMContext &VMContext = CGM.getLLVMContext();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001352 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1353 llvm::DIFactory::OpDeref));
1354 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1355 llvm::DIFactory::OpPlus));
1356 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1357 offset));
1358 if (BDRE->isByRef()) {
1359 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1360 llvm::DIFactory::OpDeref));
1361 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1362 llvm::DIFactory::OpPlus));
1363 offset = CGF->LLVMPointerWidth/8; // offset of __forwarding field
1364 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1365 offset));
1366 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1367 llvm::DIFactory::OpDeref));
1368 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1369 llvm::DIFactory::OpPlus));
1370 offset = XOffset/8; // offset of x field
1371 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1372 offset));
1373 }
1374
1375 // Create the descriptor for the variable.
1376 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001377 DebugFactory.CreateComplexVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel73621622009-11-25 17:37:31 +00001378 Decl->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001379 addr);
1380 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001381 llvm::Instruction *Call =
1382 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertPoint());
Devang Patel23908b82009-11-12 18:21:39 +00001383
Devang Patel8fae0602009-11-13 19:10:24 +00001384 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001385 llvm::DILocation DO(NULL);
1386 llvm::DILocation DL =
1387 DebugFactory.CreateLocation(Line, PLoc.getColumn(), DS, DO);
1388 Builder.SetDebugLocation(Call, DL.getNode());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001389}
1390
Chris Lattner9c85ba32008-11-10 06:08:34 +00001391void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
1392 llvm::Value *Storage,
1393 CGBuilderTy &Builder) {
1394 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
1395}
1396
Mike Stumpb1a6e682009-09-30 02:43:10 +00001397void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1398 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1399 CodeGenFunction *CGF) {
1400 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1401}
1402
Chris Lattner9c85ba32008-11-10 06:08:34 +00001403/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1404/// variable declaration.
1405void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
1406 CGBuilderTy &Builder) {
1407 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
1408}
1409
1410
1411
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001412/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001413void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001414 const VarDecl *Decl) {
Devang Patel07739032009-03-27 23:16:32 +00001415
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001416 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001417 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001418 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001419 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1420 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001421
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001422 QualType T = Decl->getType();
1423 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001424
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001425 // CodeGen turns int[] into int[1] so we'll do the same here.
1426 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001427
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001428 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001429 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001430
Anders Carlsson20f12a22009-12-06 18:00:51 +00001431 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001432 ArrayType::Normal, 0);
1433 }
Devang Patel73621622009-11-25 17:37:31 +00001434 llvm::StringRef DeclName = Decl->getName();
Devang Patelab71ff52009-11-12 00:51:46 +00001435 DebugFactory.CreateGlobalVariable(getContext(Decl, Unit), DeclName, DeclName,
Devang Patel73621622009-11-25 17:37:31 +00001436 llvm::StringRef(), Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001437 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001438 Var->hasInternalLinkage(),
1439 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001440}
1441
Devang Patel9ca36b62009-02-26 21:10:26 +00001442/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001443void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel9ca36b62009-02-26 21:10:26 +00001444 ObjCInterfaceDecl *Decl) {
1445 // Create global variable debug descriptor.
1446 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001447 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001448 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1449 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +00001450
Devang Patel73621622009-11-25 17:37:31 +00001451 llvm::StringRef Name = Decl->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001452
Anders Carlsson20f12a22009-12-06 18:00:51 +00001453 QualType T = CGM.getContext().getObjCInterfaceType(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001454 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001455
Devang Patel9ca36b62009-02-26 21:10:26 +00001456 // CodeGen turns int[] into int[1] so we'll do the same here.
1457 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001458
Devang Patel9ca36b62009-02-26 21:10:26 +00001459 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001460 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001461
Anders Carlsson20f12a22009-12-06 18:00:51 +00001462 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001463 ArrayType::Normal, 0);
1464 }
1465
Devang Patelf6a39b72009-10-20 18:26:30 +00001466 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001467 getOrCreateType(T, Unit),
1468 Var->hasInternalLinkage(),
1469 true/*definition*/, Var);
1470}