blob: d1de84fef7b9d1663330aa00a8af5f573f4220dd [file] [log] [blame]
Sanjiv Gupta15cb6692008-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 Stump2e722b92009-09-30 02:43:10 +000015#include "CodeGenFunction.h"
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000016#include "CodeGenModule.h"
Sanjiv Gupta98070572008-05-25 05:15:42 +000017#include "clang/AST/ASTContext.h"
Devang Patelf4c205b2009-02-26 21:10:26 +000018#include "clang/AST/DeclObjC.h"
Chris Lattnercd2523b2008-11-11 07:01:36 +000019#include "clang/AST/Expr.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000020#include "clang/AST/RecordLayout.h"
Sanjiv Gupta98070572008-05-25 05:15:42 +000021#include "clang/Basic/SourceManager.h"
22#include "clang/Basic/FileManager.h"
Mike Stumpc3844be2009-09-15 21:48:34 +000023#include "clang/Basic/Version.h"
Chandler Carruth85098242010-06-15 23:19:56 +000024#include "clang/Frontend/CodeGenOptions.h"
Sanjiv Gupta15cb6692008-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 Gupta15cb6692008-05-08 08:54:20 +000030#include "llvm/ADT/StringExtras.h"
31#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta98070572008-05-25 05:15:42 +000032#include "llvm/Support/Dwarf.h"
Devang Patel75009452009-04-17 21:06:59 +000033#include "llvm/System/Path.h"
Sanjiv Gupta98070572008-05-25 05:15:42 +000034#include "llvm/Target/TargetMachine.h"
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000035using namespace clang;
36using namespace clang::CodeGen;
37
Anders Carlsson3efc6e62009-12-06 18:00:51 +000038CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Devang Patel408dcf62010-03-09 00:44:50 +000039 : CGM(CGM), DebugFactory(CGM.getModule()),
Devang Patel4f262052010-03-09 21:32:27 +000040 FwdDeclCount(0), BlockLiteralGenericSet(false) {
Devang Patel408dcf62010-03-09 00:44:50 +000041 CreateCompileUnit();
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000042}
43
Chris Lattneraffb3732008-11-10 06:08:34 +000044CGDebugInfo::~CGDebugInfo() {
Daniel Dunbarb9fd9022008-10-17 16:15:48 +000045 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000046}
47
Chris Lattneraffb3732008-11-10 06:08:34 +000048void CGDebugInfo::setLocation(SourceLocation Loc) {
49 if (Loc.isValid())
Anders Carlsson3efc6e62009-12-06 18:00:51 +000050 CurLoc = CGM.getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta98070572008-05-25 05:15:42 +000051}
52
Devang Patel7bfc5962010-01-28 23:15:27 +000053/// getContextDescriptor - Get context info for the decl.
Devang Patel7b7f46f2010-02-01 21:34:11 +000054llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context,
Devang Patel7bfc5962010-01-28 23:15:27 +000055 llvm::DIDescriptor &CompileUnit) {
Devang Patel7b7f46f2010-02-01 21:34:11 +000056 if (!Context)
57 return CompileUnit;
58
59 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
60 I = RegionMap.find(Context);
61 if (I != RegionMap.end())
62 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(I->second));
Devang Patele8fb4b72010-02-01 22:40:08 +000063
Devang Patel7b7f46f2010-02-01 21:34:11 +000064 // Check namespace.
65 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
66 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl, CompileUnit));
Devang Patel98f21712010-05-13 23:52:37 +000067
68 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context)) {
69 if (!RDecl->isDependentType()) {
70 llvm::DIType Ty = getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
71 llvm::DIFile(CompileUnit));
72 return llvm::DIDescriptor(Ty);
73 }
74 }
Devang Patelfaf7e9a2009-10-06 00:35:31 +000075 return CompileUnit;
76}
77
Devang Patel934661e2010-01-14 00:36:21 +000078/// getFunctionName - Get function name for the given FunctionDecl. If the
79/// name is constructred on demand (e.g. C++ destructor) then the name
80/// is stored on the side.
81llvm::StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
82 assert (FD && "Invalid FunctionDecl!");
83 IdentifierInfo *FII = FD->getIdentifier();
84 if (FII)
85 return FII->getName();
86
87 // Otherwise construct human readable name for debug info.
88 std::string NS = FD->getNameAsString();
89
90 // Copy this name on the side and use its reference.
Devang Patel0d61eeb2010-01-28 18:21:00 +000091 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer8f8f4052010-01-23 18:16:07 +000092 memcpy(StrPtr, NS.data(), NS.length());
93 return llvm::StringRef(StrPtr, NS.length());
Devang Patel934661e2010-01-14 00:36:21 +000094}
95
Devang Patel408dcf62010-03-09 00:44:50 +000096/// getOrCreateFile - Get the file debug info descriptor for the input location.
97llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Ted Kremenekdbb8cd12010-03-30 00:27:51 +000098 if (!Loc.isValid())
Devang Patel408dcf62010-03-09 00:44:50 +000099 // If Location is not valid then use main input file.
100 return DebugFactory.CreateFile(TheCU.getFilename(), TheCU.getDirectory(),
101 TheCU);
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000102 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel408dcf62010-03-09 00:44:50 +0000103 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Ted Kremenekdbb8cd12010-03-30 00:27:51 +0000104
105 // Cache the results.
106 const char *fname = PLoc.getFilename();
107 llvm::DenseMap<const char *, llvm::WeakVH>::iterator it =
108 DIFileCache.find(fname);
109
110 if (it != DIFileCache.end()) {
111 // Verify that the information still exists.
112 if (&*it->second)
113 return llvm::DIFile(cast<llvm::MDNode>(it->second));
114 }
115
116 // FIXME: We shouldn't even need to call 'makeAbsolute()' in the cases
117 // where we can consult the FileEntry.
Devang Patel408dcf62010-03-09 00:44:50 +0000118 llvm::sys::Path AbsFileName(PLoc.getFilename());
Benjamin Kramer1d205642009-12-08 11:02:29 +0000119 AbsFileName.makeAbsolute();
Devang Patel75009452009-04-17 21:06:59 +0000120
Ted Kremenekdbb8cd12010-03-30 00:27:51 +0000121 llvm::DIFile F = DebugFactory.CreateFile(AbsFileName.getLast(),
122 AbsFileName.getDirname(), TheCU);
123
Devang Patelba4ad7f2010-05-07 18:12:35 +0000124 DIFileCache[fname] = F;
Ted Kremenekdbb8cd12010-03-30 00:27:51 +0000125 return F;
126
Devang Patel408dcf62010-03-09 00:44:50 +0000127}
Devang Patelc5ffabc2010-05-12 23:46:38 +0000128
129/// getLineNumber - Get line number for the location. If location is invalid
130/// then use current location.
131unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
132 assert (CurLoc.isValid() && "Invalid current location!");
133 SourceManager &SM = CGM.getContext().getSourceManager();
134 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
135 return PLoc.getLine();
136}
137
138/// getColumnNumber - Get column number for the location. If location is
139/// invalid then use current location.
140unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) {
141 assert (CurLoc.isValid() && "Invalid current location!");
142 SourceManager &SM = CGM.getContext().getSourceManager();
143 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
144 return PLoc.getColumn();
145}
146
Devang Patel408dcf62010-03-09 00:44:50 +0000147/// CreateCompileUnit - Create new compile unit.
148void CGDebugInfo::CreateCompileUnit() {
149
150 // Get absolute path name.
Douglas Gregorc6b5a3d2010-03-18 23:46:43 +0000151 SourceManager &SM = CGM.getContext().getSourceManager();
Douglas Gregorfc7a4812010-03-19 14:49:09 +0000152 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
153 if (MainFileName.empty())
Devang Patela42d3ea2010-03-12 21:04:27 +0000154 MainFileName = "<unknown>";
Douglas Gregorfc7a4812010-03-19 14:49:09 +0000155
Devang Patela42d3ea2010-03-12 21:04:27 +0000156 llvm::sys::Path AbsFileName(MainFileName);
Devang Patel408dcf62010-03-09 00:44:50 +0000157 AbsFileName.makeAbsolute();
Daniel Dunbar3b358a32009-04-08 05:11:16 +0000158
Douglas Gregor65f7a3f2010-03-22 21:28:29 +0000159 // The main file name provided via the "-main-file-name" option contains just
160 // the file name itself with no path information. This file name may have had
161 // a relative path, so we look into the actual file entry for the main
162 // file to determine the real absolute path for the file.
Douglas Gregorfc7a4812010-03-19 14:49:09 +0000163 std::string MainFileDir;
164 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID()))
165 MainFileDir = MainFile->getDir()->getName();
166 else
167 MainFileDir = AbsFileName.getDirname();
168
Chris Lattner8c37df42009-03-25 03:28:08 +0000169 unsigned LangTag;
Devang Patel408dcf62010-03-09 00:44:50 +0000170 const LangOptions &LO = CGM.getLangOptions();
Chris Lattner8c37df42009-03-25 03:28:08 +0000171 if (LO.CPlusPlus) {
172 if (LO.ObjC1)
173 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
174 else
175 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
176 } else if (LO.ObjC1) {
Devang Patel94406c92009-03-24 20:35:51 +0000177 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner8c37df42009-03-25 03:28:08 +0000178 } else if (LO.C99) {
Devang Patel94406c92009-03-24 20:35:51 +0000179 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner8c37df42009-03-25 03:28:08 +0000180 } else {
181 LangTag = llvm::dwarf::DW_LANG_C89;
182 }
Devang Patel75009452009-04-17 21:06:59 +0000183
Benjamin Kramer1d205642009-12-08 11:02:29 +0000184 const char *Producer =
Mike Stumpfc8ff632009-10-09 18:38:12 +0000185#ifdef CLANG_VENDOR
186 CLANG_VENDOR
187#endif
188 "clang " CLANG_VERSION_STRING;
Chris Lattner5912de12009-05-02 01:00:04 +0000189
190 // Figure out which version of the ObjC runtime we have.
191 unsigned RuntimeVers = 0;
192 if (LO.ObjC1)
193 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump11289f42009-09-09 15:08:12 +0000194
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000195 // Create new compile unit.
Devang Patel408dcf62010-03-09 00:44:50 +0000196 TheCU = DebugFactory.CreateCompileUnit(
Douglas Gregorfc7a4812010-03-19 14:49:09 +0000197 LangTag, AbsFileName.getLast(), MainFileDir, Producer, true,
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +0000198 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000199}
200
Devang Patel410dc002009-02-25 01:36:11 +0000201/// CreateType - Get the Basic type from the cache or create a new
Chris Lattneraffb3732008-11-10 06:08:34 +0000202/// one if necessary.
203llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel408dcf62010-03-09 00:44:50 +0000204 llvm::DIFile Unit) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000205 unsigned Encoding = 0;
206 switch (BT->getKind()) {
207 default:
208 case BuiltinType::Void:
209 return llvm::DIType();
210 case BuiltinType::UChar:
211 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
212 case BuiltinType::Char_S:
213 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
214 case BuiltinType::UShort:
215 case BuiltinType::UInt:
216 case BuiltinType::ULong:
217 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
218 case BuiltinType::Short:
219 case BuiltinType::Int:
220 case BuiltinType::Long:
221 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
222 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
223 case BuiltinType::Float:
Devang Patel551e1122009-10-12 22:28:31 +0000224 case BuiltinType::LongDouble:
Chris Lattneraffb3732008-11-10 06:08:34 +0000225 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump11289f42009-09-09 15:08:12 +0000226 }
Chris Lattneraffb3732008-11-10 06:08:34 +0000227 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000228 uint64_t Size = CGM.getContext().getTypeSize(BT);
229 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattneraffb3732008-11-10 06:08:34 +0000230 uint64_t Offset = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000231
Devang Patele21912d2009-10-20 19:55:01 +0000232 llvm::DIType DbgTy =
233 DebugFactory.CreateBasicType(Unit,
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000234 BT->getName(CGM.getContext().getLangOptions()),
Devang Patele21912d2009-10-20 19:55:01 +0000235 Unit, 0, Size, Align,
236 Offset, /*flags*/ 0, Encoding);
Devang Patele21912d2009-10-20 19:55:01 +0000237 return DbgTy;
Chris Lattneraffb3732008-11-10 06:08:34 +0000238}
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000239
Chris Lattner7b0344f2009-04-23 06:13:01 +0000240llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000241 llvm::DIFile Unit) {
Chris Lattner7b0344f2009-04-23 06:13:01 +0000242 // Bit size, align and offset of the type.
243 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
244 if (Ty->isComplexIntegerType())
245 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump11289f42009-09-09 15:08:12 +0000246
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000247 uint64_t Size = CGM.getContext().getTypeSize(Ty);
248 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattner7b0344f2009-04-23 06:13:01 +0000249 uint64_t Offset = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000250
Devang Patele21912d2009-10-20 19:55:01 +0000251 llvm::DIType DbgTy =
252 DebugFactory.CreateBasicType(Unit, "complex",
253 Unit, 0, Size, Align,
254 Offset, /*flags*/ 0, Encoding);
Devang Patele21912d2009-10-20 19:55:01 +0000255 return DbgTy;
Chris Lattner7b0344f2009-04-23 06:13:01 +0000256}
257
John McCall0cf15512009-09-25 01:40:47 +0000258/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Gupta19292422008-06-07 04:46:53 +0000259/// a new one if necessary.
Devang Patel408dcf62010-03-09 00:44:50 +0000260llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCall0cf15512009-09-25 01:40:47 +0000261 QualifierCollector Qc;
262 const Type *T = Qc.strip(Ty);
263
264 // Ignore these qualifiers for now.
265 Qc.removeObjCGCAttr();
266 Qc.removeAddressSpace();
267
Chris Lattneraffb3732008-11-10 06:08:34 +0000268 // We will create one Derived type for one qualifier and recurse to handle any
269 // additional ones.
Chris Lattneraffb3732008-11-10 06:08:34 +0000270 unsigned Tag;
John McCall0cf15512009-09-25 01:40:47 +0000271 if (Qc.hasConst()) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000272 Tag = llvm::dwarf::DW_TAG_const_type;
John McCall0cf15512009-09-25 01:40:47 +0000273 Qc.removeConst();
274 } else if (Qc.hasVolatile()) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000275 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCall0cf15512009-09-25 01:40:47 +0000276 Qc.removeVolatile();
277 } else if (Qc.hasRestrict()) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000278 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCall0cf15512009-09-25 01:40:47 +0000279 Qc.removeRestrict();
280 } else {
281 assert(Qc.empty() && "Unknown type qualifier for debug info");
282 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta98070572008-05-25 05:15:42 +0000283 }
Mike Stump11289f42009-09-09 15:08:12 +0000284
John McCall0cf15512009-09-25 01:40:47 +0000285 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
286
Daniel Dunbara290ded2008-10-31 03:54:29 +0000287 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
288 // CVR derived types.
Devang Patele21912d2009-10-20 19:55:01 +0000289 llvm::DIType DbgTy =
Devang Patel93f274c2010-03-09 22:49:11 +0000290 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patele21912d2009-10-20 19:55:01 +0000291 0, 0, 0, 0, 0, FromTy);
Devang Patele21912d2009-10-20 19:55:01 +0000292 return DbgTy;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000293}
294
Daniel Dunbarf5c79702009-07-14 01:20:56 +0000295llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000296 llvm::DIFile Unit) {
Devang Patele21912d2009-10-20 19:55:01 +0000297 llvm::DIType DbgTy =
Anders Carlsson443f6772009-11-06 19:19:55 +0000298 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
299 Ty->getPointeeType(), Unit);
Devang Patele21912d2009-10-20 19:55:01 +0000300 return DbgTy;
Daniel Dunbarf5c79702009-07-14 01:20:56 +0000301}
302
Chris Lattneraffb3732008-11-10 06:08:34 +0000303llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000304 llvm::DIFile Unit) {
Anders Carlsson443f6772009-11-06 19:19:55 +0000305 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
306 Ty->getPointeeType(), Unit);
307}
308
309llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
310 const Type *Ty,
311 QualType PointeeTy,
Devang Patel408dcf62010-03-09 00:44:50 +0000312 llvm::DIFile Unit) {
Anders Carlsson443f6772009-11-06 19:19:55 +0000313 llvm::DIType EltTy = getOrCreateType(PointeeTy, Unit);
Mike Stump11289f42009-09-09 15:08:12 +0000314
Sanjiv Gupta98070572008-05-25 05:15:42 +0000315 // Bit size, align and offset of the type.
Anders Carlsson443f6772009-11-06 19:19:55 +0000316
317 // Size is always the size of a pointer. We can't use getTypeSize here
318 // because that does not return the correct value for references.
319 uint64_t Size =
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000320 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
321 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000322
Devang Patele21912d2009-10-20 19:55:01 +0000323 return
Devang Patel93f274c2010-03-09 22:49:11 +0000324 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patele21912d2009-10-20 19:55:01 +0000325 0, Size, Align, 0, 0, EltTy);
Anders Carlsson443f6772009-11-06 19:19:55 +0000326
Sanjiv Gupta98070572008-05-25 05:15:42 +0000327}
328
Mike Stump31f099c2009-05-14 02:03:51 +0000329llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000330 llvm::DIFile Unit) {
Mike Stump31f099c2009-05-14 02:03:51 +0000331 if (BlockLiteralGenericSet)
332 return BlockLiteralGeneric;
333
Mike Stump31f099c2009-05-14 02:03:51 +0000334 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
335
336 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
337
338 llvm::DIType FieldTy;
339
340 QualType FType;
341 uint64_t FieldSize, FieldOffset;
342 unsigned FieldAlign;
343
344 llvm::DIArray Elements;
345 llvm::DIType EltTy, DescTy;
346
347 FieldOffset = 0;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000348 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +0000349 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
350 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump31f099c2009-05-14 02:03:51 +0000351
Daniel Dunbarbee70bd2009-05-26 19:40:20 +0000352 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump31f099c2009-05-14 02:03:51 +0000353 EltTys.clear();
354
Mike Stump581b9ad2009-10-02 02:30:50 +0000355 unsigned Flags = llvm::DIType::FlagAppleBlock;
Devang Patelc5ffabc2010-05-12 23:46:38 +0000356 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump581b9ad2009-10-02 02:30:50 +0000357
Mike Stump31f099c2009-05-14 02:03:51 +0000358 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Devang Patelc5ffabc2010-05-12 23:46:38 +0000359 Unit, LineNo, FieldOffset, 0, 0,
360 Flags, llvm::DIType(), Elements);
Mike Stump11289f42009-09-09 15:08:12 +0000361
Mike Stump31f099c2009-05-14 02:03:51 +0000362 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000363 uint64_t Size = CGM.getContext().getTypeSize(Ty);
364 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000365
Mike Stump31f099c2009-05-14 02:03:51 +0000366 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Patel93f274c2010-03-09 22:49:11 +0000367 Unit, "", Unit,
Devang Patelc5ffabc2010-05-12 23:46:38 +0000368 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump31f099c2009-05-14 02:03:51 +0000369
370 FieldOffset = 0;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000371 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +0000372 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000373 FType = CGM.getContext().IntTy;
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +0000374 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
375 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramer20f2d432010-04-24 20:26:20 +0000376 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +0000377 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump31f099c2009-05-14 02:03:51 +0000378
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000379 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump31f099c2009-05-14 02:03:51 +0000380 FieldTy = DescTy;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000381 FieldSize = CGM.getContext().getTypeSize(Ty);
382 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump31f099c2009-05-14 02:03:51 +0000383 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel93f274c2010-03-09 22:49:11 +0000384 "__descriptor", Unit,
Devang Patelc5ffabc2010-05-12 23:46:38 +0000385 LineNo, FieldSize, FieldAlign,
Mike Stump31f099c2009-05-14 02:03:51 +0000386 FieldOffset, 0, FieldTy);
387 EltTys.push_back(FieldTy);
388
389 FieldOffset += FieldSize;
Daniel Dunbarbee70bd2009-05-26 19:40:20 +0000390 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump31f099c2009-05-14 02:03:51 +0000391
392 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Devang Patelc5ffabc2010-05-12 23:46:38 +0000393 Unit, LineNo, FieldOffset, 0, 0,
394 Flags, llvm::DIType(), Elements);
Mike Stump11289f42009-09-09 15:08:12 +0000395
Mike Stump31f099c2009-05-14 02:03:51 +0000396 BlockLiteralGenericSet = true;
397 BlockLiteralGeneric
398 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
Devang Patel93f274c2010-03-09 22:49:11 +0000399 "", Unit,
Devang Patelc5ffabc2010-05-12 23:46:38 +0000400 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump31f099c2009-05-14 02:03:51 +0000401 return BlockLiteralGeneric;
402}
403
Chris Lattneraffb3732008-11-10 06:08:34 +0000404llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000405 llvm::DIFile Unit) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000406 // Typedefs are derived from some other type. If we have a typedef of a
407 // typedef, make sure to emit the whole chain.
408 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump11289f42009-09-09 15:08:12 +0000409
Chris Lattneraffb3732008-11-10 06:08:34 +0000410 // We don't set size information, but do specify where the typedef was
411 // declared.
Devang Patelc5ffabc2010-05-12 23:46:38 +0000412 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Sanjiv Gupta98070572008-05-25 05:15:42 +0000413
Devang Patel7b7f46f2010-02-01 21:34:11 +0000414 llvm::DIDescriptor TyContext
415 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
416 Unit);
Devang Patele21912d2009-10-20 19:55:01 +0000417 llvm::DIType DbgTy =
Devang Patelbb4820d2010-01-29 22:29:31 +0000418 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Patel7b7f46f2010-02-01 21:34:11 +0000419 TyContext,
Devang Patelbb4820d2010-01-29 22:29:31 +0000420 Ty->getDecl()->getName(), Unit,
421 Line, 0, 0, 0, 0, Src);
Devang Patele21912d2009-10-20 19:55:01 +0000422 return DbgTy;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000423}
424
Chris Lattneraffb3732008-11-10 06:08:34 +0000425llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000426 llvm::DIFile Unit) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000427 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000428
Chris Lattneraffb3732008-11-10 06:08:34 +0000429 // Add the result type at least.
430 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump11289f42009-09-09 15:08:12 +0000431
Chris Lattneraffb3732008-11-10 06:08:34 +0000432 // Set up remainder of arguments if there is a prototype.
433 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000434 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000435 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
436 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
437 } else {
438 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta98070572008-05-25 05:15:42 +0000439 }
440
Chris Lattneraffb3732008-11-10 06:08:34 +0000441 llvm::DIArray EltTypeArray =
Daniel Dunbarbee70bd2009-05-26 19:40:20 +0000442 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump11289f42009-09-09 15:08:12 +0000443
Devang Patele21912d2009-10-20 19:55:01 +0000444 llvm::DIType DbgTy =
445 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Patel93f274c2010-03-09 22:49:11 +0000446 Unit, "", Unit,
Devang Patele21912d2009-10-20 19:55:01 +0000447 0, 0, 0, 0, 0,
448 llvm::DIType(), EltTypeArray);
Devang Patele21912d2009-10-20 19:55:01 +0000449 return DbgTy;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000450}
451
Devang Patel889ce762010-01-19 00:00:59 +0000452/// CollectRecordFields - A helper function to collect debug info for
453/// record fields. This is used while creating debug info entry for a Record.
454void CGDebugInfo::
Devang Patel408dcf62010-03-09 00:44:50 +0000455CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit,
Devang Patel889ce762010-01-19 00:00:59 +0000456 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
457 unsigned FieldNo = 0;
Devang Patel1c0954c2010-02-01 21:39:52 +0000458 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
459 for (RecordDecl::field_iterator I = RD->field_begin(),
460 E = RD->field_end();
Devang Patel889ce762010-01-19 00:00:59 +0000461 I != E; ++I, ++FieldNo) {
462 FieldDecl *Field = *I;
463 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
464
465 llvm::StringRef FieldName = Field->getName();
466
Devang Patelf4df65c2010-02-12 01:31:06 +0000467 // Ignore unnamed fields. Do not ignore unnamed records.
468 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel889ce762010-01-19 00:00:59 +0000469 continue;
470
471 // Get the location for the field.
Devang Patelc5ffabc2010-05-12 23:46:38 +0000472 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
473 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel889ce762010-01-19 00:00:59 +0000474 QualType FType = Field->getType();
475 uint64_t FieldSize = 0;
476 unsigned FieldAlign = 0;
477 if (!FType->isIncompleteArrayType()) {
478
479 // Bit size, align and offset of the type.
480 FieldSize = CGM.getContext().getTypeSize(FType);
481 Expr *BitWidth = Field->getBitWidth();
482 if (BitWidth)
483 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
484
485 FieldAlign = CGM.getContext().getTypeAlign(FType);
486 }
487
488 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
489
Devang Patelb9ab3092010-04-21 23:12:37 +0000490 unsigned Flags = 0;
491 AccessSpecifier Access = I->getAccess();
492 if (Access == clang::AS_private)
493 Flags |= llvm::DIType::FlagPrivate;
494 else if (Access == clang::AS_protected)
495 Flags |= llvm::DIType::FlagProtected;
496
Devang Patel889ce762010-01-19 00:00:59 +0000497 // Create a DW_TAG_member node to remember the offset of this field in the
498 // struct. FIXME: This is an absolutely insane way to capture this
499 // information. When we gut debug info, this should be fixed.
500 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
501 FieldName, FieldDefUnit,
502 FieldLine, FieldSize, FieldAlign,
Devang Patelb9ab3092010-04-21 23:12:37 +0000503 FieldOffset, Flags, FieldTy);
Devang Patel889ce762010-01-19 00:00:59 +0000504 EltTys.push_back(FieldTy);
505 }
506}
507
Devang Patel3d4e6d92010-01-28 00:28:01 +0000508/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
509/// function type is not updated to include implicit "this" pointer. Use this
510/// routine to get a method type which includes "this" pointer.
511llvm::DIType
512CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel408dcf62010-03-09 00:44:50 +0000513 llvm::DIFile Unit) {
Douglas Gregorc8be9522010-05-04 18:18:31 +0000514 llvm::DIType FnTy
515 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
516 0),
517 Unit);
Devang Patel4c3e7e92010-01-28 21:43:50 +0000518
519 // Static methods do not need "this" pointer argument.
520 if (Method->isStatic())
521 return FnTy;
522
Devang Patel3d4e6d92010-01-28 00:28:01 +0000523 // Add "this" pointer.
524
Devang Patelba4ad7f2010-05-07 18:12:35 +0000525 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patel3d4e6d92010-01-28 00:28:01 +0000526 assert (Args.getNumElements() && "Invalid number of arguments!");
527
528 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
529
530 // First element is always return type. For 'void' functions it is NULL.
531 Elts.push_back(Args.getElement(0));
532
533 // "this" pointer is always first argument.
534 ASTContext &Context = CGM.getContext();
535 QualType ThisPtr =
536 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
Devang Patelcce7e852010-02-09 17:57:50 +0000537 llvm::DIType ThisPtrType =
538 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
Devang Patel0a34e312010-07-13 00:24:30 +0000539
Devang Patelba4ad7f2010-05-07 18:12:35 +0000540 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
Devang Patelcce7e852010-02-09 17:57:50 +0000541 Elts.push_back(ThisPtrType);
Devang Patel3d4e6d92010-01-28 00:28:01 +0000542
543 // Copy rest of the arguments.
544 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
545 Elts.push_back(Args.getElement(i));
546
547 llvm::DIArray EltTypeArray =
548 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
549
550 return
551 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Patel93f274c2010-03-09 22:49:11 +0000552 Unit, "", Unit,
Devang Patel3d4e6d92010-01-28 00:28:01 +0000553 0, 0, 0, 0, 0,
554 llvm::DIType(), EltTypeArray);
555}
556
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000557/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
558/// a single member function GlobalDecl.
559llvm::DISubprogram
Anders Carlsson17ed0492010-01-26 05:19:50 +0000560CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel408dcf62010-03-09 00:44:50 +0000561 llvm::DIFile Unit,
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000562 llvm::DICompositeType &RecordTy) {
Anders Carlsson17ed0492010-01-26 05:19:50 +0000563 bool IsCtorOrDtor =
564 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
565
566 llvm::StringRef MethodName = getFunctionName(Method);
Devang Patel3d4e6d92010-01-28 00:28:01 +0000567 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson17ed0492010-01-26 05:19:50 +0000568
569 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
570 // make sense to give a single ctor/dtor a linkage name.
Anders Carlssonea836bc2010-06-22 16:16:50 +0000571 llvm::StringRef MethodLinkageName;
Anders Carlsson17ed0492010-01-26 05:19:50 +0000572 if (!IsCtorOrDtor)
Anders Carlssonea836bc2010-06-22 16:16:50 +0000573 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson17ed0492010-01-26 05:19:50 +0000574
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000575 // Get the location for the method.
Devang Patelc5ffabc2010-05-12 23:46:38 +0000576 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
577 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000578
579 // Collect virtual method info.
580 llvm::DIType ContainingType;
581 unsigned Virtuality = 0;
582 unsigned VIndex = 0;
Anders Carlsson17ed0492010-01-26 05:19:50 +0000583
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000584 if (Method->isVirtual()) {
Anders Carlsson17ed0492010-01-26 05:19:50 +0000585 if (Method->isPure())
586 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
587 else
588 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
589
590 // It doesn't make sense to give a virtual destructor a vtable index,
591 // since a single destructor has two entries in the vtable.
592 if (!isa<CXXDestructorDecl>(Method))
Anders Carlsson11e51402010-04-17 20:15:18 +0000593 VIndex = CGM.getVTables().getMethodVTableIndex(Method);
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000594 ContainingType = RecordTy;
595 }
596
597 llvm::DISubprogram SP =
598 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
599 MethodLinkageName,
600 MethodDefUnit, MethodLine,
601 MethodTy, /*isLocalToUnit=*/false,
Devang Patel0aabb122010-07-12 22:54:41 +0000602 /* isDefintion=*/ false,
Devang Patelb3026df2010-07-15 22:57:00 +0000603 Virtuality, VIndex, ContainingType,
604 Method->isImplicit());
Anders Carlsson17ed0492010-01-26 05:19:50 +0000605
606 // Don't cache ctors or dtors since we have to emit multiple functions for
607 // a single ctor or dtor.
608 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
Devang Patelba4ad7f2010-05-07 18:12:35 +0000609 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000610
611 return SP;
612}
613
Devang Patel7a12ad02010-01-19 01:54:44 +0000614/// CollectCXXMemberFunctions - A helper function to collect debug info for
615/// C++ member functions.This is used while creating debug info entry for
616/// a Record.
617void CGDebugInfo::
Devang Patel408dcf62010-03-09 00:44:50 +0000618CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel7a12ad02010-01-19 01:54:44 +0000619 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
620 llvm::DICompositeType &RecordTy) {
Devang Patel1c0954c2010-02-01 21:39:52 +0000621 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
622 E = RD->method_end(); I != E; ++I) {
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000623 const CXXMethodDecl *Method = *I;
Anders Carlssonc1821152010-01-26 04:40:11 +0000624
Devang Patel0ae70d12010-02-09 19:09:28 +0000625 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonc1821152010-01-26 04:40:11 +0000626 continue;
Devang Patel7a12ad02010-01-19 01:54:44 +0000627
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000628 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel7a12ad02010-01-19 01:54:44 +0000629 }
630}
631
Devang Patelc54353d2010-01-25 23:32:18 +0000632/// CollectCXXBases - A helper function to collect debug info for
633/// C++ base classes. This is used while creating debug info entry for
634/// a Record.
635void CGDebugInfo::
Devang Patel408dcf62010-03-09 00:44:50 +0000636CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patelc54353d2010-01-25 23:32:18 +0000637 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
638 llvm::DICompositeType &RecordTy) {
639
Devang Patel1c0954c2010-02-01 21:39:52 +0000640 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
641 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
642 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patel128aa9d2010-01-28 21:54:15 +0000643 unsigned BFlags = 0;
644 uint64_t BaseOffset;
645
646 const CXXRecordDecl *Base =
647 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
648
649 if (BI->isVirtual()) {
Anders Carlsson4cbe83c2010-03-11 07:15:17 +0000650 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Patel0ae70d12010-02-09 19:09:28 +0000651 // expression where it expects +ve number.
Anders Carlssona864caf2010-03-23 04:11:45 +0000652 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base);
Devang Patel128aa9d2010-01-28 21:54:15 +0000653 BFlags = llvm::DIType::FlagVirtual;
654 } else
655 BaseOffset = RL.getBaseClassOffset(Base);
656
657 AccessSpecifier Access = BI->getAccessSpecifier();
658 if (Access == clang::AS_private)
659 BFlags |= llvm::DIType::FlagPrivate;
660 else if (Access == clang::AS_protected)
661 BFlags |= llvm::DIType::FlagProtected;
662
663 llvm::DIType DTy =
664 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
665 RecordTy, llvm::StringRef(),
Devang Patel93f274c2010-03-09 22:49:11 +0000666 Unit, 0, 0, 0,
Devang Patel128aa9d2010-01-28 21:54:15 +0000667 BaseOffset, BFlags,
668 getOrCreateType(BI->getType(),
669 Unit));
670 EltTys.push_back(DTy);
671 }
Devang Patelc54353d2010-01-25 23:32:18 +0000672}
673
Devang Patel84033fb2010-01-28 18:11:52 +0000674/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel408dcf62010-03-09 00:44:50 +0000675llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Pateld3cbaa12010-03-08 20:53:17 +0000676 if (VTablePtrType.isValid())
Devang Patel84033fb2010-01-28 18:11:52 +0000677 return VTablePtrType;
678
679 ASTContext &Context = CGM.getContext();
680
681 /* Function type */
Benjamin Kramer9e649c32010-03-30 11:36:44 +0000682 llvm::DIDescriptor STy = getOrCreateType(Context.IntTy, Unit);
683 llvm::DIArray SElements = DebugFactory.GetOrCreateArray(&STy, 1);
Devang Patel84033fb2010-01-28 18:11:52 +0000684 llvm::DIType SubTy =
685 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Patel93f274c2010-03-09 22:49:11 +0000686 Unit, "", Unit,
Devang Patel84033fb2010-01-28 18:11:52 +0000687 0, 0, 0, 0, 0, llvm::DIType(), SElements);
688
689 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
690 llvm::DIType vtbl_ptr_type
691 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Patel93f274c2010-03-09 22:49:11 +0000692 Unit, "__vtbl_ptr_type", Unit,
Devang Patel84033fb2010-01-28 18:11:52 +0000693 0, Size, 0, 0, 0, SubTy);
694
Devang Patel93f274c2010-03-09 22:49:11 +0000695 VTablePtrType =
696 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
697 Unit, "", Unit,
698 0, Size, 0, 0, 0, vtbl_ptr_type);
Devang Patel84033fb2010-01-28 18:11:52 +0000699 return VTablePtrType;
700}
701
Anders Carlsson11e51402010-04-17 20:15:18 +0000702/// getVTableName - Get vtable name for the given Class.
703llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel84033fb2010-01-28 18:11:52 +0000704 // Otherwise construct gdb compatible name name.
Devang Patel1c0954c2010-02-01 21:39:52 +0000705 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel84033fb2010-01-28 18:11:52 +0000706
707 // Copy this name on the side and use its reference.
Devang Patel0d61eeb2010-01-28 18:21:00 +0000708 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel84033fb2010-01-28 18:11:52 +0000709 memcpy(StrPtr, Name.data(), Name.length());
710 return llvm::StringRef(StrPtr, Name.length());
711}
712
713
Anders Carlsson11e51402010-04-17 20:15:18 +0000714/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel84033fb2010-01-28 18:11:52 +0000715/// debug info entry in EltTys vector.
716void CGDebugInfo::
Anders Carlsson11e51402010-04-17 20:15:18 +0000717CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel84033fb2010-01-28 18:11:52 +0000718 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel1c0954c2010-02-01 21:39:52 +0000719 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel84033fb2010-01-28 18:11:52 +0000720
721 // If there is a primary base then it will hold vtable info.
722 if (RL.getPrimaryBase())
723 return;
724
725 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel1c0954c2010-02-01 21:39:52 +0000726 if (!RD->isDynamicClass())
Devang Patel84033fb2010-01-28 18:11:52 +0000727 return;
728
729 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
730 llvm::DIType VPTR
731 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Anders Carlsson11e51402010-04-17 20:15:18 +0000732 getVTableName(RD), Unit,
Devang Patel84033fb2010-01-28 18:11:52 +0000733 0, Size, 0, 0, 0,
734 getOrCreateVTablePtrType(Unit));
735 EltTys.push_back(VPTR);
736}
737
Devang Patel410dc002009-02-25 01:36:11 +0000738/// CreateType - get structure or union type.
Chris Lattneraffb3732008-11-10 06:08:34 +0000739llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000740 llvm::DIFile Unit) {
Devang Patel3efd1472010-02-01 21:52:22 +0000741 RecordDecl *RD = Ty->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000742
Chris Lattneraffb3732008-11-10 06:08:34 +0000743 unsigned Tag;
Devang Patel3efd1472010-02-01 21:52:22 +0000744 if (RD->isStruct())
Chris Lattneraffb3732008-11-10 06:08:34 +0000745 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Patel3efd1472010-02-01 21:52:22 +0000746 else if (RD->isUnion())
Chris Lattneraffb3732008-11-10 06:08:34 +0000747 Tag = llvm::dwarf::DW_TAG_union_type;
748 else {
Devang Patel3efd1472010-02-01 21:52:22 +0000749 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattneraffb3732008-11-10 06:08:34 +0000750 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Gupta19292422008-06-07 04:46:53 +0000751 }
752
Chris Lattneraffb3732008-11-10 06:08:34 +0000753 // Get overall information about the record type for the debug info.
Devang Patelc5ffabc2010-05-12 23:46:38 +0000754 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
755 unsigned Line = getLineNumber(RD->getLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000756
Chris Lattneraffb3732008-11-10 06:08:34 +0000757 // Records and classes and unions can all be recursive. To handle them, we
758 // first generate a debug descriptor for the struct as a forward declaration.
759 // Then (if it is a definition) we go through and get debug info for all of
760 // its members. Finally, we create a descriptor for the complete type (which
761 // may refer to the forward decl if the struct is recursive) and replace all
762 // uses of the forward declaration with the final definition.
Devang Patel8f3f76f2010-07-08 19:56:29 +0000763 llvm::DIDescriptor FDContext =
764 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
765
766 // If this is just a forward declaration, construct an appropriately
767 // marked node and just return it.
768 if (!RD->getDefinition()) {
769 llvm::DICompositeType FwdDecl =
770 DebugFactory.CreateCompositeType(Tag, FDContext, RD->getName(),
771 DefUnit, Line, 0, 0, 0,
772 llvm::DIType::FlagFwdDecl,
773 llvm::DIType(), llvm::DIArray());
774
775 return FwdDecl;
776 }
Devang Patel3f4a77e2010-01-20 23:56:40 +0000777
Devang Patel3efd1472010-02-01 21:52:22 +0000778 // A RD->getName() is not unique. However, the debug info descriptors
Devang Patelab793232010-02-01 22:51:29 +0000779 // are uniqued so use type name to ensure uniquness.
Benjamin Kramer69b3c432010-03-13 12:06:51 +0000780 llvm::SmallString<128> FwdDeclName;
781 llvm::raw_svector_ostream(FwdDeclName) << "fwd.type." << FwdDeclCount++;
Devang Patel06cceef2009-07-22 18:57:00 +0000782 llvm::DICompositeType FwdDecl =
Devang Patel4f262052010-03-09 21:32:27 +0000783 DebugFactory.CreateCompositeType(Tag, FDContext, FwdDeclName,
Devang Patel7bdf0962009-11-12 00:51:46 +0000784 DefUnit, Line, 0, 0, 0, 0,
Chris Lattneraffb3732008-11-10 06:08:34 +0000785 llvm::DIType(), llvm::DIArray());
Mike Stump11289f42009-09-09 15:08:12 +0000786
Devang Patelba4ad7f2010-05-07 18:12:35 +0000787 llvm::MDNode *MN = FwdDecl;
788 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattneraffb3732008-11-10 06:08:34 +0000789 // Otherwise, insert it into the TypeCache so that recursive uses will find
790 // it.
Devang Patelba4ad7f2010-05-07 18:12:35 +0000791 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patel01bb5ce2010-03-11 20:01:48 +0000792 // Push the struct on region stack.
Devang Patelba4ad7f2010-05-07 18:12:35 +0000793 RegionStack.push_back(FwdDeclNode);
794 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattneraffb3732008-11-10 06:08:34 +0000795
796 // Convert all the elements.
797 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
798
Devang Patel3efd1472010-02-01 21:52:22 +0000799 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel946edc12010-01-28 21:41:35 +0000800 if (CXXDecl) {
801 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson11e51402010-04-17 20:15:18 +0000802 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel946edc12010-01-28 21:41:35 +0000803 }
Devang Patel3efd1472010-02-01 21:52:22 +0000804 CollectRecordFields(RD, Unit, EltTys);
Devang Patelabb44132010-01-28 00:54:21 +0000805 llvm::MDNode *ContainingType = NULL;
Devang Patel84033fb2010-01-28 18:11:52 +0000806 if (CXXDecl) {
Devang Patel7a12ad02010-01-19 01:54:44 +0000807 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patelabb44132010-01-28 00:54:21 +0000808
809 // A class's primary base or the class itself contains the vtable.
Devang Patel3efd1472010-02-01 21:52:22 +0000810 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patelabb44132010-01-28 00:54:21 +0000811 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
812 ContainingType =
Devang Patelba4ad7f2010-05-07 18:12:35 +0000813 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patelabb44132010-01-28 00:54:21 +0000814 else if (CXXDecl->isDynamicClass())
Devang Patelba4ad7f2010-05-07 18:12:35 +0000815 ContainingType = FwdDecl;
Devang Patelc54353d2010-01-25 23:32:18 +0000816 }
Mike Stump11289f42009-09-09 15:08:12 +0000817
Chris Lattneraffb3732008-11-10 06:08:34 +0000818 llvm::DIArray Elements =
Daniel Dunbarbee70bd2009-05-26 19:40:20 +0000819 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattneraffb3732008-11-10 06:08:34 +0000820
821 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000822 uint64_t Size = CGM.getContext().getTypeSize(Ty);
823 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000824
Devang Patel01bb5ce2010-03-11 20:01:48 +0000825 RegionStack.pop_back();
826 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
827 RegionMap.find(Ty->getDecl());
828 if (RI != RegionMap.end())
829 RegionMap.erase(RI);
830
Devang Patele8fb4b72010-02-01 22:40:08 +0000831 llvm::DIDescriptor RDContext =
832 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel06cceef2009-07-22 18:57:00 +0000833 llvm::DICompositeType RealDecl =
Devang Patele8fb4b72010-02-01 22:40:08 +0000834 DebugFactory.CreateCompositeType(Tag, RDContext,
835 RD->getName(),
Devang Patel7bdf0962009-11-12 00:51:46 +0000836 DefUnit, Line, Size, Align, 0, 0,
Devang Patelabb44132010-01-28 00:54:21 +0000837 llvm::DIType(), Elements,
838 0, ContainingType);
Chris Lattneraffb3732008-11-10 06:08:34 +0000839
840 // Now that we have a real decl for the struct, replace anything using the
841 // old decl with the new one. This will recursively update the debug info.
Eli Friedman00dbf4c2009-11-16 21:04:30 +0000842 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelba4ad7f2010-05-07 18:12:35 +0000843 RegionMap[RD] = llvm::WeakVH(RealDecl);
Chris Lattneraffb3732008-11-10 06:08:34 +0000844 return RealDecl;
845}
846
John McCall8b07ec22010-05-15 11:32:37 +0000847/// CreateType - get objective-c object type.
848llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
849 llvm::DIFile Unit) {
850 // Ignore protocols.
851 return getOrCreateType(Ty->getBaseType(), Unit);
852}
853
Devang Patelf4c205b2009-02-26 21:10:26 +0000854/// CreateType - get objective-c interface type.
855llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000856 llvm::DIFile Unit) {
Devang Patel3efd1472010-02-01 21:52:22 +0000857 ObjCInterfaceDecl *ID = Ty->getDecl();
Devang Patelf4c205b2009-02-26 21:10:26 +0000858 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Patelf4c205b2009-02-26 21:10:26 +0000859
860 // Get overall information about the record type for the debug info.
Devang Patel408dcf62010-03-09 00:44:50 +0000861 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patelc5ffabc2010-05-12 23:46:38 +0000862 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel408dcf62010-03-09 00:44:50 +0000863 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerc6ad2582009-05-02 01:13:16 +0000864
Devang Patelf4c205b2009-02-26 21:10:26 +0000865 // To handle recursive interface, we
866 // first generate a debug descriptor for the struct as a forward declaration.
867 // Then (if it is a definition) we go through and get debug info for all of
868 // its members. Finally, we create a descriptor for the complete type (which
869 // may refer to the forward decl if the struct is recursive) and replace all
870 // uses of the forward declaration with the final definition.
Devang Patel6a3b3fe2009-07-27 18:42:03 +0000871 llvm::DICompositeType FwdDecl =
Devang Patel3efd1472010-02-01 21:52:22 +0000872 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
Devang Patel7bdf0962009-11-12 00:51:46 +0000873 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerc6ad2582009-05-02 01:13:16 +0000874 llvm::DIType(), llvm::DIArray(),
875 RuntimeLang);
Mike Stump11289f42009-09-09 15:08:12 +0000876
Devang Patelf4c205b2009-02-26 21:10:26 +0000877 // If this is just a forward declaration, return it.
Devang Patel3efd1472010-02-01 21:52:22 +0000878 if (ID->isForwardDecl())
Devang Patelf4c205b2009-02-26 21:10:26 +0000879 return FwdDecl;
880
Devang Patelba4ad7f2010-05-07 18:12:35 +0000881 llvm::MDNode *MN = FwdDecl;
882 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patelf4c205b2009-02-26 21:10:26 +0000883 // Otherwise, insert it into the TypeCache so that recursive uses will find
884 // it.
Devang Patelba4ad7f2010-05-07 18:12:35 +0000885 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patel01bb5ce2010-03-11 20:01:48 +0000886 // Push the struct on region stack.
Devang Patelba4ad7f2010-05-07 18:12:35 +0000887 RegionStack.push_back(FwdDeclNode);
888 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patelf4c205b2009-02-26 21:10:26 +0000889
890 // Convert all the elements.
891 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
892
Devang Patel3efd1472010-02-01 21:52:22 +0000893 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelc0f58ea2009-03-10 21:30:26 +0000894 if (SClass) {
Mike Stump11289f42009-09-09 15:08:12 +0000895 llvm::DIType SClassTy =
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000896 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump11289f42009-09-09 15:08:12 +0000897 llvm::DIType InhTag =
Devang Patelc0f58ea2009-03-10 21:30:26 +0000898 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Patel93f274c2010-03-09 22:49:11 +0000899 Unit, "", Unit, 0, 0, 0,
Devang Patelc0f58ea2009-03-10 21:30:26 +0000900 0 /* offset */, 0, SClassTy);
901 EltTys.push_back(InhTag);
902 }
903
Devang Patel3efd1472010-02-01 21:52:22 +0000904 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patelf4c205b2009-02-26 21:10:26 +0000905
906 unsigned FieldNo = 0;
Devang Patel3efd1472010-02-01 21:52:22 +0000907 for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(),
908 E = ID->ivar_end(); I != E; ++I, ++FieldNo) {
Devang Patelf4c205b2009-02-26 21:10:26 +0000909 ObjCIvarDecl *Field = *I;
910 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
911
Devang Patel58bf6e12009-11-25 17:37:31 +0000912 llvm::StringRef FieldName = Field->getName();
Devang Patelf4c205b2009-02-26 21:10:26 +0000913
Devang Pateldf348f12009-04-27 22:40:36 +0000914 // Ignore unnamed fields.
Devang Patel58bf6e12009-11-25 17:37:31 +0000915 if (FieldName.empty())
Devang Pateldf348f12009-04-27 22:40:36 +0000916 continue;
917
Devang Patelf4c205b2009-02-26 21:10:26 +0000918 // Get the location for the field.
Devang Patelc5ffabc2010-05-12 23:46:38 +0000919 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
920 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel9f804932009-03-20 18:24:39 +0000921 QualType FType = Field->getType();
922 uint64_t FieldSize = 0;
923 unsigned FieldAlign = 0;
Devang Patelec4bad52009-03-19 00:23:53 +0000924
Devang Patel9f804932009-03-20 18:24:39 +0000925 if (!FType->isIncompleteArrayType()) {
Mike Stump11289f42009-09-09 15:08:12 +0000926
Devang Patel9f804932009-03-20 18:24:39 +0000927 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000928 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel9f804932009-03-20 18:24:39 +0000929 Expr *BitWidth = Field->getBitWidth();
930 if (BitWidth)
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000931 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman1c4a1752009-04-26 19:19:15 +0000932
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000933 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel9f804932009-03-20 18:24:39 +0000934 }
935
Mike Stump11289f42009-09-09 15:08:12 +0000936 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
937
Devang Patelec4bad52009-03-19 00:23:53 +0000938 unsigned Flags = 0;
939 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
940 Flags = llvm::DIType::FlagProtected;
941 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
942 Flags = llvm::DIType::FlagPrivate;
Mike Stump11289f42009-09-09 15:08:12 +0000943
Devang Patelf4c205b2009-02-26 21:10:26 +0000944 // Create a DW_TAG_member node to remember the offset of this field in the
945 // struct. FIXME: This is an absolutely insane way to capture this
946 // information. When we gut debug info, this should be fixed.
947 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
948 FieldName, FieldDefUnit,
949 FieldLine, FieldSize, FieldAlign,
Devang Patelec4bad52009-03-19 00:23:53 +0000950 FieldOffset, Flags, FieldTy);
Devang Patelf4c205b2009-02-26 21:10:26 +0000951 EltTys.push_back(FieldTy);
952 }
Mike Stump11289f42009-09-09 15:08:12 +0000953
Devang Patelf4c205b2009-02-26 21:10:26 +0000954 llvm::DIArray Elements =
Jay Foad7d0479f2009-05-21 09:52:38 +0000955 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patelf4c205b2009-02-26 21:10:26 +0000956
Devang Patel01bb5ce2010-03-11 20:01:48 +0000957 RegionStack.pop_back();
958 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
959 RegionMap.find(Ty->getDecl());
960 if (RI != RegionMap.end())
961 RegionMap.erase(RI);
962
Devang Patelf4c205b2009-02-26 21:10:26 +0000963 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000964 uint64_t Size = CGM.getContext().getTypeSize(Ty);
965 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000966
Devang Patel6a3b3fe2009-07-27 18:42:03 +0000967 llvm::DICompositeType RealDecl =
Devang Patel3efd1472010-02-01 21:52:22 +0000968 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patel7bdf0962009-11-12 00:51:46 +0000969 Line, Size, Align, 0, 0, llvm::DIType(),
970 Elements, RuntimeLang);
Devang Patelf4c205b2009-02-26 21:10:26 +0000971
972 // Now that we have a real decl for the struct, replace anything using the
973 // old decl with the new one. This will recursively update the debug info.
Devang Patel10909d52009-11-16 20:09:38 +0000974 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelba4ad7f2010-05-07 18:12:35 +0000975 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patel9c3a0182009-07-13 17:03:14 +0000976
Devang Patelf4c205b2009-02-26 21:10:26 +0000977 return RealDecl;
978}
979
Chris Lattneraffb3732008-11-10 06:08:34 +0000980llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000981 llvm::DIFile Unit) {
Devang Patel3efd1472010-02-01 21:52:22 +0000982 EnumDecl *ED = Ty->getDecl();
Chris Lattneraffb3732008-11-10 06:08:34 +0000983
984 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
985
986 // Create DIEnumerator elements for each enumerator.
Mike Stump11289f42009-09-09 15:08:12 +0000987 for (EnumDecl::enumerator_iterator
Devang Patel3efd1472010-02-01 21:52:22 +0000988 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
Douglas Gregor91f84212008-12-11 16:49:14 +0000989 Enum != EnumEnd; ++Enum) {
Devang Patel58bf6e12009-11-25 17:37:31 +0000990 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor91f84212008-12-11 16:49:14 +0000991 Enum->getInitVal().getZExtValue()));
Chris Lattneraffb3732008-11-10 06:08:34 +0000992 }
Mike Stump11289f42009-09-09 15:08:12 +0000993
Chris Lattneraffb3732008-11-10 06:08:34 +0000994 // Return a CompositeType for the enum itself.
995 llvm::DIArray EltArray =
Jay Foad7d0479f2009-05-21 09:52:38 +0000996 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattneraffb3732008-11-10 06:08:34 +0000997
Devang Patelc5ffabc2010-05-12 23:46:38 +0000998 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
999 unsigned Line = getLineNumber(ED->getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001000
Chris Lattneraffb3732008-11-10 06:08:34 +00001001 // Size and align of the type.
Eli Friedman2ad7e172009-05-04 04:39:55 +00001002 uint64_t Size = 0;
1003 unsigned Align = 0;
1004 if (!Ty->isIncompleteType()) {
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001005 Size = CGM.getContext().getTypeSize(Ty);
1006 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman2ad7e172009-05-04 04:39:55 +00001007 }
Mike Stump11289f42009-09-09 15:08:12 +00001008
Devang Patele21912d2009-10-20 19:55:01 +00001009 llvm::DIType DbgTy =
1010 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Patel3efd1472010-02-01 21:52:22 +00001011 Unit, ED->getName(), DefUnit, Line,
Devang Patele21912d2009-10-20 19:55:01 +00001012 Size, Align, 0, 0,
1013 llvm::DIType(), EltArray);
Devang Patele21912d2009-10-20 19:55:01 +00001014 return DbgTy;
Chris Lattneraffb3732008-11-10 06:08:34 +00001015}
1016
1017llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001018 llvm::DIFile Unit) {
Chris Lattneraffb3732008-11-10 06:08:34 +00001019 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1020 return CreateType(RT, Unit);
1021 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1022 return CreateType(ET, Unit);
Mike Stump11289f42009-09-09 15:08:12 +00001023
Chris Lattneraffb3732008-11-10 06:08:34 +00001024 return llvm::DIType();
1025}
1026
Devang Patelb4073382010-02-23 22:59:39 +00001027llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001028 llvm::DIFile Unit) {
Devang Patelb4073382010-02-23 22:59:39 +00001029 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1030 uint64_t NumElems = Ty->getNumElements();
1031 if (NumElems > 0)
1032 --NumElems;
Devang Patelb4073382010-02-23 22:59:39 +00001033
Benjamin Kramer9e649c32010-03-30 11:36:44 +00001034 llvm::DIDescriptor Subscript = DebugFactory.GetOrCreateSubrange(0, NumElems);
1035 llvm::DIArray SubscriptArray = DebugFactory.GetOrCreateArray(&Subscript, 1);
Devang Patelb4073382010-02-23 22:59:39 +00001036
1037 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1038 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1039
1040 return
1041 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Patel93f274c2010-03-09 22:49:11 +00001042 Unit, "", Unit,
Devang Patelb4073382010-02-23 22:59:39 +00001043 0, Size, Align, 0, 0,
1044 ElementTy, SubscriptArray);
1045}
1046
Chris Lattneraffb3732008-11-10 06:08:34 +00001047llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001048 llvm::DIFile Unit) {
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001049 uint64_t Size;
1050 uint64_t Align;
Mike Stump11289f42009-09-09 15:08:12 +00001051
1052
Nuno Lopesbb537dc2009-01-28 00:35:17 +00001053 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001054 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001055 Size = 0;
1056 Align =
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001057 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopesbb537dc2009-01-28 00:35:17 +00001058 } else if (Ty->isIncompleteArrayType()) {
1059 Size = 0;
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001060 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001061 } else {
1062 // Size and align of the whole array, not the element type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001063 Size = CGM.getContext().getTypeSize(Ty);
1064 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001065 }
Mike Stump11289f42009-09-09 15:08:12 +00001066
Chris Lattneraffb3732008-11-10 06:08:34 +00001067 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1068 // interior arrays, do we care? Why aren't nested arrays represented the
1069 // obvious/recursive way?
1070 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1071 QualType EltTy(Ty, 0);
1072 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta224e8ea2008-06-09 10:47:41 +00001073 uint64_t Upper = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001074 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Pateld4bbb082009-08-14 20:57:45 +00001075 if (CAT->getSize().getZExtValue())
Mike Stump11289f42009-09-09 15:08:12 +00001076 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattneraffb3732008-11-10 06:08:34 +00001077 // FIXME: Verify this is right for VLAs.
1078 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1079 EltTy = Ty->getElementType();
Sanjiv Gupta224e8ea2008-06-09 10:47:41 +00001080 }
Mike Stump11289f42009-09-09 15:08:12 +00001081
Chris Lattneraffb3732008-11-10 06:08:34 +00001082 llvm::DIArray SubscriptArray =
Daniel Dunbarbee70bd2009-05-26 19:40:20 +00001083 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattneraffb3732008-11-10 06:08:34 +00001084
Devang Patele21912d2009-10-20 19:55:01 +00001085 llvm::DIType DbgTy =
1086 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Patel93f274c2010-03-09 22:49:11 +00001087 Unit, "", Unit,
Devang Patele21912d2009-10-20 19:55:01 +00001088 0, Size, Align, 0, 0,
1089 getOrCreateType(EltTy, Unit),
1090 SubscriptArray);
Devang Patele21912d2009-10-20 19:55:01 +00001091 return DbgTy;
Chris Lattneraffb3732008-11-10 06:08:34 +00001092}
1093
Anders Carlsson443f6772009-11-06 19:19:55 +00001094llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001095 llvm::DIFile Unit) {
Anders Carlsson443f6772009-11-06 19:19:55 +00001096 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1097 Ty, Ty->getPointeeType(), Unit);
1098}
Chris Lattneraffb3732008-11-10 06:08:34 +00001099
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001100llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001101 llvm::DIFile U) {
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001102 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1103 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1104
1105 if (!Ty->getPointeeType()->isFunctionType()) {
1106 // We have a data member pointer type.
1107 return PointerDiffDITy;
1108 }
1109
1110 // We have a member function pointer type. Treat it as a struct with two
1111 // ptrdiff_t members.
1112 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1113
1114 uint64_t FieldOffset = 0;
1115 llvm::DIDescriptor ElementTypes[2];
1116
1117 // FIXME: This should probably be a function type instead.
1118 ElementTypes[0] =
1119 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Patel93f274c2010-03-09 22:49:11 +00001120 "ptr", U, 0,
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001121 Info.first, Info.second, FieldOffset, 0,
1122 PointerDiffDITy);
1123 FieldOffset += Info.first;
1124
1125 ElementTypes[1] =
1126 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Patel93f274c2010-03-09 22:49:11 +00001127 "ptr", U, 0,
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001128 Info.first, Info.second, FieldOffset, 0,
1129 PointerDiffDITy);
1130
1131 llvm::DIArray Elements =
1132 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1133 llvm::array_lengthof(ElementTypes));
1134
1135 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1136 U, llvm::StringRef("test"),
Devang Patel93f274c2010-03-09 22:49:11 +00001137 U, 0, FieldOffset,
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001138 0, 0, 0, llvm::DIType(), Elements);
1139}
1140
Douglas Gregor0f139a12009-12-21 20:18:30 +00001141static QualType UnwrapTypeForDebugInfo(QualType T) {
1142 do {
1143 QualType LastT = T;
1144 switch (T->getTypeClass()) {
1145 default:
1146 return T;
1147 case Type::TemplateSpecialization:
1148 T = cast<TemplateSpecializationType>(T)->desugar();
1149 break;
1150 case Type::TypeOfExpr: {
1151 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1152 T = Ty->getUnderlyingExpr()->getType();
1153 break;
1154 }
1155 case Type::TypeOf:
1156 T = cast<TypeOfType>(T)->getUnderlyingType();
1157 break;
1158 case Type::Decltype:
1159 T = cast<DecltypeType>(T)->getUnderlyingType();
1160 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +00001161 case Type::Elaborated:
1162 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor0f139a12009-12-21 20:18:30 +00001163 break;
1164 case Type::SubstTemplateTypeParm:
1165 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1166 break;
Douglas Gregor0f139a12009-12-21 20:18:30 +00001167 }
1168
1169 assert(T != LastT && "Type unwrapping failed to unwrap!");
1170 if (T == LastT)
1171 return T;
1172 } while (true);
1173
1174 return T;
Anders Carlsson0acee6e2009-11-14 21:08:12 +00001175}
1176
Sanjiv Gupta98070572008-05-25 05:15:42 +00001177/// getOrCreateType - Get the type from the cache or create a new
1178/// one if necessary.
Chris Lattneraffb3732008-11-10 06:08:34 +00001179llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001180 llvm::DIFile Unit) {
Chris Lattneraffb3732008-11-10 06:08:34 +00001181 if (Ty.isNull())
1182 return llvm::DIType();
Mike Stump11289f42009-09-09 15:08:12 +00001183
Douglas Gregor0f139a12009-12-21 20:18:30 +00001184 // Unwrap the type as needed for debug information.
1185 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson0acee6e2009-11-14 21:08:12 +00001186
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001187 // Check for existing entry.
Ted Kremenek23c29f02010-03-29 18:29:57 +00001188 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001189 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar99961382009-09-19 20:17:48 +00001190 if (it != TypeCache.end()) {
1191 // Verify that the debug info still exists.
1192 if (&*it->second)
1193 return llvm::DIType(cast<llvm::MDNode>(it->second));
1194 }
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001195
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001196 // Otherwise create the type.
1197 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson6037e782009-11-14 20:52:05 +00001198
1199 // And update the type cache.
Devang Patelba4ad7f2010-05-07 18:12:35 +00001200 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001201 return Res;
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001202}
1203
Anders Carlsson6037e782009-11-14 20:52:05 +00001204/// CreateTypeNode - Create a new debug type node.
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001205llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001206 llvm::DIFile Unit) {
John McCall0cf15512009-09-25 01:40:47 +00001207 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001208 if (Ty.hasLocalQualifiers())
John McCall0cf15512009-09-25 01:40:47 +00001209 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta98070572008-05-25 05:15:42 +00001210
Douglas Gregor0915b432009-12-21 19:57:21 +00001211 const char *Diag = 0;
1212
Sanjiv Gupta98070572008-05-25 05:15:42 +00001213 // Work out details of type.
Chris Lattneraffb3732008-11-10 06:08:34 +00001214 switch (Ty->getTypeClass()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001215#define TYPE(Class, Base)
1216#define ABSTRACT_TYPE(Class, Base)
1217#define NON_CANONICAL_TYPE(Class, Base)
1218#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1219#include "clang/AST/TypeNodes.def"
1220 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidise9189262009-08-19 01:28:17 +00001221
Anders Carlsson25ed5c22009-11-06 18:24:04 +00001222 // FIXME: Handle these.
1223 case Type::ExtVector:
Anders Carlsson25ed5c22009-11-06 18:24:04 +00001224 return llvm::DIType();
Devang Patelb4073382010-02-23 22:59:39 +00001225
1226 case Type::Vector:
1227 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbarf5c79702009-07-14 01:20:56 +00001228 case Type::ObjCObjectPointer:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001229 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCall8b07ec22010-05-15 11:32:37 +00001230 case Type::ObjCObject:
1231 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump11289f42009-09-09 15:08:12 +00001232 case Type::ObjCInterface:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001233 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1234 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1235 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1236 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump31f099c2009-05-14 02:03:51 +00001237 case Type::BlockPointer:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001238 return CreateType(cast<BlockPointerType>(Ty), Unit);
1239 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001240 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001241 case Type::Enum:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001242 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattneraffb3732008-11-10 06:08:34 +00001243 case Type::FunctionProto:
1244 case Type::FunctionNoProto:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001245 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattneraffb3732008-11-10 06:08:34 +00001246 case Type::ConstantArray:
1247 case Type::VariableArray:
1248 case Type::IncompleteArray:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001249 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlsson443f6772009-11-06 19:19:55 +00001250
1251 case Type::LValueReference:
1252 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1253
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001254 case Type::MemberPointer:
1255 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor0915b432009-12-21 19:57:21 +00001256
1257 case Type::TemplateSpecialization:
Douglas Gregor0915b432009-12-21 19:57:21 +00001258 case Type::Elaborated:
Douglas Gregor0915b432009-12-21 19:57:21 +00001259 case Type::SubstTemplateTypeParm:
Douglas Gregor0915b432009-12-21 19:57:21 +00001260 case Type::TypeOfExpr:
1261 case Type::TypeOf:
Douglas Gregor0f139a12009-12-21 20:18:30 +00001262 case Type::Decltype:
1263 llvm_unreachable("type should have been unwrapped!");
1264 return llvm::DIType();
Douglas Gregor0915b432009-12-21 19:57:21 +00001265
1266 case Type::RValueReference:
1267 // FIXME: Implement!
1268 Diag = "rvalue references";
1269 break;
Sanjiv Gupta98070572008-05-25 05:15:42 +00001270 }
Douglas Gregor0915b432009-12-21 19:57:21 +00001271
1272 assert(Diag && "Fall through without a diagnostic?");
1273 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1274 "debug information for %0 is not yet supported");
1275 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1276 << Diag;
1277 return llvm::DIType();
Sanjiv Gupta98070572008-05-25 05:15:42 +00001278}
1279
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001280/// CreateMemberType - Create new member and increase Offset by FType's size.
1281llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
1282 llvm::StringRef Name,
1283 uint64_t *Offset) {
1284 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1285 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1286 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
1287 llvm::DIType Ty = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1288 Unit, Name, Unit, 0,
1289 FieldSize, FieldAlign,
1290 *Offset, 0, FieldTy);
1291 *Offset += FieldSize;
1292 return Ty;
1293}
1294
Sanjiv Gupta98070572008-05-25 05:15:42 +00001295/// EmitFunctionStart - Constructs the debug code for entering a function -
1296/// "llvm.dbg.func.start.".
Devang Patel934661e2010-01-14 00:36:21 +00001297void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta98070572008-05-25 05:15:42 +00001298 llvm::Function *Fn,
Chris Lattneraffb3732008-11-10 06:08:34 +00001299 CGBuilderTy &Builder) {
Mike Stump11289f42009-09-09 15:08:12 +00001300
Devang Patel934661e2010-01-14 00:36:21 +00001301 llvm::StringRef Name;
Anders Carlssonea836bc2010-06-22 16:16:50 +00001302 llvm::StringRef LinkageName;
Devang Patel934661e2010-01-14 00:36:21 +00001303
1304 const Decl *D = GD.getDecl();
1305 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel7a12ad02010-01-19 01:54:44 +00001306 // If there is a DISubprogram for this function available then use it.
1307 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1308 FI = SPCache.find(FD);
1309 if (FI != SPCache.end()) {
Devang Pateld3cbaa12010-03-08 20:53:17 +00001310 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
Devang Patelba4ad7f2010-05-07 18:12:35 +00001311 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1312 llvm::MDNode *SPN = SP;
1313 RegionStack.push_back(SPN);
1314 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel7a12ad02010-01-19 01:54:44 +00001315 return;
1316 }
1317 }
Devang Patel934661e2010-01-14 00:36:21 +00001318 Name = getFunctionName(FD);
1319 // Use mangled name as linkage name for c/c++ functions.
Anders Carlssonea836bc2010-06-22 16:16:50 +00001320 LinkageName = CGM.getMangledName(GD);
Devang Patel934661e2010-01-14 00:36:21 +00001321 } else {
1322 // Use llvm function name as linkage name.
1323 Name = Fn->getName();
Anders Carlssonea836bc2010-06-22 16:16:50 +00001324 LinkageName = Name;
Devang Patel934661e2010-01-14 00:36:21 +00001325 }
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001326 if (!Name.empty() && Name[0] == '\01')
1327 Name = Name.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00001328
Devang Patel84715932010-04-24 00:49:16 +00001329 // It is expected that CurLoc is set before using EmitFunctionStart.
1330 // Usually, CurLoc points to the left bracket location of compound
1331 // statement representing function body.
1332 llvm::DIFile Unit = getOrCreateFile(CurLoc);
Devang Patelc5ffabc2010-05-12 23:46:38 +00001333 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001334
Chris Lattneraffb3732008-11-10 06:08:34 +00001335 llvm::DISubprogram SP =
Devang Patel84715932010-04-24 00:49:16 +00001336 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stumpae2559a2009-10-23 01:52:13 +00001337 getOrCreateType(FnType, Unit),
Chris Lattneraffb3732008-11-10 06:08:34 +00001338 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump11289f42009-09-09 15:08:12 +00001339
Sanjiv Gupta98070572008-05-25 05:15:42 +00001340 // Push function on region stack.
Devang Patelba4ad7f2010-05-07 18:12:35 +00001341 llvm::MDNode *SPN = SP;
1342 RegionStack.push_back(SPN);
1343 RegionMap[D] = llvm::WeakVH(SP);
Sanjiv Gupta98070572008-05-25 05:15:42 +00001344}
1345
1346
Chris Lattneraffb3732008-11-10 06:08:34 +00001347void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta98070572008-05-25 05:15:42 +00001348 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump11289f42009-09-09 15:08:12 +00001349
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001350 // Don't bother if things are the same as last time.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001351 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +00001352 if (CurLoc == PrevLoc
Chris Lattner88ea93e2009-02-04 01:06:56 +00001353 || (SM.getInstantiationLineNumber(CurLoc) ==
1354 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001355 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patela2c048e2010-04-05 21:09:15 +00001356 // New Builder may not be in sync with CGDebugInfo.
1357 if (!Builder.getCurrentDebugLocation().isUnknown())
1358 return;
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001359
1360 // Update last state.
1361 PrevLoc = CurLoc;
1362
Chris Lattnere675d0f2010-04-01 06:31:43 +00001363 llvm::MDNode *Scope = RegionStack.back();
Devang Patelc5ffabc2010-05-12 23:46:38 +00001364 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1365 getColumnNumber(CurLoc),
Chris Lattner18a584b2010-04-02 20:21:43 +00001366 Scope));
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001367}
1368
1369/// EmitRegionStart- Constructs the debug code for entering a declarative
1370/// region - "llvm.dbg.region.start.".
Chris Lattneraffb3732008-11-10 06:08:34 +00001371void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
Devang Patelb40f2952009-11-13 19:10:24 +00001372 llvm::DIDescriptor D =
1373 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1374 llvm::DIDescriptor() :
Devang Patel75855802010-02-16 21:41:20 +00001375 llvm::DIDescriptor(RegionStack.back()),
Devang Patelc5ffabc2010-05-12 23:46:38 +00001376 getLineNumber(CurLoc),
1377 getColumnNumber(CurLoc));
Devang Patelba4ad7f2010-05-07 18:12:35 +00001378 llvm::MDNode *DN = D;
1379 RegionStack.push_back(DN);
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001380}
1381
1382/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1383/// region - "llvm.dbg.region.end."
Chris Lattneraffb3732008-11-10 06:08:34 +00001384void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar380827c2008-10-17 01:07:56 +00001385 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1386
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001387 // Provide an region stop point.
1388 EmitStopPoint(Fn, Builder);
Mike Stump11289f42009-09-09 15:08:12 +00001389
Sanjiv Gupta98070572008-05-25 05:15:42 +00001390 RegionStack.pop_back();
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001391}
1392
Devang Patel535fdaf2010-02-10 18:49:08 +00001393// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1394// See BuildByRefType.
1395llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1396 uint64_t *XOffset) {
1397
1398 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1399
1400 QualType FType;
1401 uint64_t FieldSize, FieldOffset;
1402 unsigned FieldAlign;
1403
Devang Patel408dcf62010-03-09 00:44:50 +00001404 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel535fdaf2010-02-10 18:49:08 +00001405 QualType Type = VD->getType();
1406
1407 FieldOffset = 0;
1408 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001409 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1410 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel535fdaf2010-02-10 18:49:08 +00001411 FType = CGM.getContext().IntTy;
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001412 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1413 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1414
Devang Patel535fdaf2010-02-10 18:49:08 +00001415 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1416 if (HasCopyAndDispose) {
1417 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001418 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1419 &FieldOffset));
1420 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1421 &FieldOffset));
Devang Patel535fdaf2010-02-10 18:49:08 +00001422 }
1423
1424 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1425 if (Align > CharUnits::fromQuantity(
1426 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1427 unsigned AlignedOffsetInBytes
1428 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1429 unsigned NumPaddingBytes
1430 = AlignedOffsetInBytes - FieldOffset/8;
1431
1432 if (NumPaddingBytes > 0) {
1433 llvm::APInt pad(32, NumPaddingBytes);
1434 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1435 pad, ArrayType::Normal, 0);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001436 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel535fdaf2010-02-10 18:49:08 +00001437 }
1438 }
1439
1440 FType = Type;
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001441 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel535fdaf2010-02-10 18:49:08 +00001442 FieldSize = CGM.getContext().getTypeSize(FType);
1443 FieldAlign = Align.getQuantity()*8;
1444
1445 *XOffset = FieldOffset;
1446 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel93f274c2010-03-09 22:49:11 +00001447 VD->getName(), Unit,
Devang Patel535fdaf2010-02-10 18:49:08 +00001448 0, FieldSize, FieldAlign,
1449 FieldOffset, 0, FieldTy);
1450 EltTys.push_back(FieldTy);
1451 FieldOffset += FieldSize;
1452
1453 llvm::DIArray Elements =
1454 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1455
1456 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1457
1458 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Patel93f274c2010-03-09 22:49:11 +00001459 Unit, "", Unit,
Devang Patel535fdaf2010-02-10 18:49:08 +00001460 0, FieldOffset, 0, 0, Flags,
1461 llvm::DIType(), Elements);
1462
1463}
Sanjiv Gupta18de6242008-05-30 10:30:31 +00001464/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel1c0954c2010-02-01 21:39:52 +00001465void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattneraffb3732008-11-10 06:08:34 +00001466 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar380827c2008-10-17 01:07:56 +00001467 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1468
Devang Patel408dcf62010-03-09 00:44:50 +00001469 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel535fdaf2010-02-10 18:49:08 +00001470 llvm::DIType Ty;
1471 uint64_t XOffset = 0;
1472 if (VD->hasAttr<BlocksAttr>())
1473 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1474 else
1475 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner362d8ae2009-05-05 04:57:08 +00001476
Devang Patel67eba802010-05-07 23:05:55 +00001477 // If there is not any debug info for type then do not emit debug info
1478 // for this variable.
1479 if (!Ty)
1480 return;
1481
Chris Lattneraffb3732008-11-10 06:08:34 +00001482 // Get location information.
Devang Patelc5ffabc2010-05-12 23:46:38 +00001483 unsigned Line = getLineNumber(VD->getLocation());
1484 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001485
Chris Lattneraffb3732008-11-10 06:08:34 +00001486 // Create the descriptor for the variable.
Mike Stump11289f42009-09-09 15:08:12 +00001487 llvm::DIVariable D =
Devang Patelb40f2952009-11-13 19:10:24 +00001488 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel1c0954c2010-02-01 21:39:52 +00001489 VD->getName(),
Devang Patel6ccba0f2010-06-05 01:14:40 +00001490 Unit, Line, Ty, CGM.getLangOptions().Optimize);
Chris Lattneraffb3732008-11-10 06:08:34 +00001491 // Insert an llvm.dbg.declare into the current block.
Devang Patel53485152009-11-11 19:10:19 +00001492 llvm::Instruction *Call =
Devang Patelaf993bf2009-11-10 23:07:24 +00001493 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel94f798c2009-11-12 18:21:39 +00001494
Chris Lattnere675d0f2010-04-01 06:31:43 +00001495 llvm::MDNode *Scope = RegionStack.back();
Chris Lattner18a584b2010-04-02 20:21:43 +00001496 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Sanjiv Gupta18de6242008-05-30 10:30:31 +00001497}
1498
Mike Stump2e722b92009-09-30 02:43:10 +00001499/// EmitDeclare - Emit local variable declaration debug info.
1500void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1501 llvm::Value *Storage, CGBuilderTy &Builder,
1502 CodeGenFunction *CGF) {
Devang Patel3efd1472010-02-01 21:52:22 +00001503 const ValueDecl *VD = BDRE->getDecl();
Mike Stump2e722b92009-09-30 02:43:10 +00001504 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1505
Devang Patel42fb6f82010-04-26 23:28:46 +00001506 if (Builder.GetInsertBlock() == 0)
Mike Stump2e722b92009-09-30 02:43:10 +00001507 return;
1508
1509 uint64_t XOffset = 0;
Devang Patel408dcf62010-03-09 00:44:50 +00001510 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel535fdaf2010-02-10 18:49:08 +00001511 llvm::DIType Ty;
1512 if (VD->hasAttr<BlocksAttr>())
1513 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1514 else
1515 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stump2e722b92009-09-30 02:43:10 +00001516
1517 // Get location information.
Devang Patelc5ffabc2010-05-12 23:46:38 +00001518 unsigned Line = getLineNumber(VD->getLocation());
1519 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stump2e722b92009-09-30 02:43:10 +00001520
Devang Patel3efd1472010-02-01 21:52:22 +00001521 CharUnits offset = CGF->BlockDecls[VD];
Mike Stump2e722b92009-09-30 02:43:10 +00001522 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattnerbf784782010-01-25 03:29:35 +00001523 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1524 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1525 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1526 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stump2e722b92009-09-30 02:43:10 +00001527 if (BDRE->isByRef()) {
Chris Lattnerbf784782010-01-25 03:29:35 +00001528 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1529 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck40775002010-01-11 17:06:35 +00001530 // offset of __forwarding field
1531 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattnerbf784782010-01-25 03:29:35 +00001532 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1533 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1534 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck40775002010-01-11 17:06:35 +00001535 // offset of x field
1536 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattnerbf784782010-01-25 03:29:35 +00001537 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stump2e722b92009-09-30 02:43:10 +00001538 }
1539
1540 // Create the descriptor for the variable.
1541 llvm::DIVariable D =
Chris Lattner83b0dd12010-01-25 03:34:56 +00001542 DebugFactory.CreateComplexVariable(Tag,
1543 llvm::DIDescriptor(RegionStack.back()),
Devang Patel3efd1472010-02-01 21:52:22 +00001544 VD->getName(), Unit, Line, Ty,
Mike Stump2e722b92009-09-30 02:43:10 +00001545 addr);
1546 // Insert an llvm.dbg.declare into the current block.
Devang Patel53485152009-11-11 19:10:19 +00001547 llvm::Instruction *Call =
Chris Lattner83b0dd12010-01-25 03:34:56 +00001548 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattner5e124bf2009-12-28 21:44:41 +00001549
Chris Lattnere675d0f2010-04-01 06:31:43 +00001550 llvm::MDNode *Scope = RegionStack.back();
Devang Patel82bbfb52010-05-10 23:48:38 +00001551 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stump2e722b92009-09-30 02:43:10 +00001552}
1553
Devang Patel3efd1472010-02-01 21:52:22 +00001554void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattneraffb3732008-11-10 06:08:34 +00001555 llvm::Value *Storage,
1556 CGBuilderTy &Builder) {
Devang Patel3efd1472010-02-01 21:52:22 +00001557 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattneraffb3732008-11-10 06:08:34 +00001558}
1559
Mike Stump2e722b92009-09-30 02:43:10 +00001560void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1561 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1562 CodeGenFunction *CGF) {
1563 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1564}
1565
Chris Lattneraffb3732008-11-10 06:08:34 +00001566/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1567/// variable declaration.
Devang Patel3efd1472010-02-01 21:52:22 +00001568void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattneraffb3732008-11-10 06:08:34 +00001569 CGBuilderTy &Builder) {
Devang Patel3efd1472010-02-01 21:52:22 +00001570 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattneraffb3732008-11-10 06:08:34 +00001571}
1572
1573
1574
Sanjiv Gupta158143a2008-06-05 08:59:10 +00001575/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump11289f42009-09-09 15:08:12 +00001576void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel7b7f46f2010-02-01 21:34:11 +00001577 const VarDecl *D) {
1578
Sanjiv Gupta158143a2008-06-05 08:59:10 +00001579 // Create global variable debug descriptor.
Devang Patel408dcf62010-03-09 00:44:50 +00001580 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patelc5ffabc2010-05-12 23:46:38 +00001581 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner86d7d912008-11-24 03:54:41 +00001582
Devang Patel7b7f46f2010-02-01 21:34:11 +00001583 QualType T = D->getType();
Anders Carlssonf7a9a922008-11-26 17:40:42 +00001584 if (T->isIncompleteArrayType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001585
Anders Carlssonf7a9a922008-11-26 17:40:42 +00001586 // CodeGen turns int[] into int[1] so we'll do the same here.
1587 llvm::APSInt ConstVal(32);
Mike Stump11289f42009-09-09 15:08:12 +00001588
Anders Carlssonf7a9a922008-11-26 17:40:42 +00001589 ConstVal = 1;
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001590 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00001591
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001592 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlssonf7a9a922008-11-26 17:40:42 +00001593 ArrayType::Normal, 0);
1594 }
Devang Pateldfcd0662010-04-29 17:48:37 +00001595 llvm::StringRef DeclName = D->getName();
Devang Patel98f21712010-05-13 23:52:37 +00001596 llvm::StringRef LinkageName;
Devang Patelec2a9ab2010-05-14 16:55:25 +00001597 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()))
Devang Patel98f21712010-05-13 23:52:37 +00001598 LinkageName = Var->getName();
Devang Patel7b7f46f2010-02-01 21:34:11 +00001599 llvm::DIDescriptor DContext =
1600 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
Devang Patel98f21712010-05-13 23:52:37 +00001601 DebugFactory.CreateGlobalVariable(DContext, DeclName, DeclName, LinkageName,
1602 Unit, LineNo, getOrCreateType(T, Unit),
Chris Lattneraffb3732008-11-10 06:08:34 +00001603 Var->hasInternalLinkage(),
1604 true/*definition*/, Var);
Sanjiv Gupta158143a2008-06-05 08:59:10 +00001605}
1606
Devang Patelf4c205b2009-02-26 21:10:26 +00001607/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump11289f42009-09-09 15:08:12 +00001608void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel3efd1472010-02-01 21:52:22 +00001609 ObjCInterfaceDecl *ID) {
Devang Patelf4c205b2009-02-26 21:10:26 +00001610 // Create global variable debug descriptor.
Devang Patel408dcf62010-03-09 00:44:50 +00001611 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patelc5ffabc2010-05-12 23:46:38 +00001612 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patelf4c205b2009-02-26 21:10:26 +00001613
Devang Patel3efd1472010-02-01 21:52:22 +00001614 llvm::StringRef Name = ID->getName();
Devang Patelf4c205b2009-02-26 21:10:26 +00001615
Devang Patel3efd1472010-02-01 21:52:22 +00001616 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patelf4c205b2009-02-26 21:10:26 +00001617 if (T->isIncompleteArrayType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001618
Devang Patelf4c205b2009-02-26 21:10:26 +00001619 // CodeGen turns int[] into int[1] so we'll do the same here.
1620 llvm::APSInt ConstVal(32);
Mike Stump11289f42009-09-09 15:08:12 +00001621
Devang Patelf4c205b2009-02-26 21:10:26 +00001622 ConstVal = 1;
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001623 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00001624
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001625 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patelf4c205b2009-02-26 21:10:26 +00001626 ArrayType::Normal, 0);
1627 }
1628
Devang Patele4f2b2a2009-10-20 18:26:30 +00001629 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patelf4c205b2009-02-26 21:10:26 +00001630 getOrCreateType(T, Unit),
1631 Var->hasInternalLinkage(),
1632 true/*definition*/, Var);
1633}
Devang Patel973f2eb2010-02-01 19:16:32 +00001634
1635/// getOrCreateNamesSpace - Return namespace descriptor for the given
1636/// namespace decl.
1637llvm::DINameSpace
1638CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1639 llvm::DIDescriptor Unit) {
1640 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1641 NameSpaceCache.find(NSDecl);
1642 if (I != NameSpaceCache.end())
1643 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1644
Devang Patelc5ffabc2010-05-12 23:46:38 +00001645 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patel973f2eb2010-02-01 19:16:32 +00001646
1647 llvm::DIDescriptor Context =
Devang Patel7b7f46f2010-02-01 21:34:11 +00001648 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patel973f2eb2010-02-01 19:16:32 +00001649 llvm::DINameSpace NS =
1650 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
Devang Patelba4ad7f2010-05-07 18:12:35 +00001651 llvm::DIFile(Unit), LineNo);
1652 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patel973f2eb2010-02-01 19:16:32 +00001653 return NS;
1654}