blob: d63db2666674765b9cbba069c54d2115e88ffdf4 [file] [log] [blame]
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the debug information generation while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGDebugInfo.h"
Mike Stumpb1a6e682009-09-30 02:43:10 +000015#include "CodeGenFunction.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000016#include "CodeGenModule.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000017#include "clang/AST/ASTContext.h"
Devang Patel9ca36b62009-02-26 21:10:26 +000018#include "clang/AST/DeclObjC.h"
Chris Lattner3cc5c402008-11-11 07:01:36 +000019#include "clang/AST/Expr.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000020#include "clang/AST/RecordLayout.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000021#include "clang/Basic/SourceManager.h"
22#include "clang/Basic/FileManager.h"
Mike Stump5a862172009-09-15 21:48:34 +000023#include "clang/Basic/Version.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000024#include "clang/Frontend/CodeGenOptions.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000025#include "llvm/Constants.h"
26#include "llvm/DerivedTypes.h"
27#include "llvm/Instructions.h"
28#include "llvm/Intrinsics.h"
29#include "llvm/Module.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000030#include "llvm/ADT/StringExtras.h"
31#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000032#include "llvm/Support/Dwarf.h"
Devang Patel446c6192009-04-17 21:06:59 +000033#include "llvm/System/Path.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000034#include "llvm/Target/TargetMachine.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000035using namespace clang;
36using namespace clang::CodeGen;
37
Anders Carlsson20f12a22009-12-06 18:00:51 +000038CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Devang Patel17800552010-03-09 00:44:50 +000039 : CGM(CGM), DebugFactory(CGM.getModule()),
Devang Patel7573f8b2010-03-09 21:32:27 +000040 FwdDeclCount(0), BlockLiteralGenericSet(false) {
Devang Patel17800552010-03-09 00:44:50 +000041 CreateCompileUnit();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000042}
43
Chris Lattner9c85ba32008-11-10 06:08:34 +000044CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar66031a52008-10-17 16:15:48 +000045 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000046}
47
Chris Lattner9c85ba32008-11-10 06:08:34 +000048void CGDebugInfo::setLocation(SourceLocation Loc) {
49 if (Loc.isValid())
Anders Carlsson20f12a22009-12-06 18:00:51 +000050 CurLoc = CGM.getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000051}
52
Devang Patel33583052010-01-28 23:15:27 +000053/// getContextDescriptor - Get context info for the decl.
Devang Pateleb6d79b2010-02-01 21:34:11 +000054llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context,
Devang Patel33583052010-01-28 23:15:27 +000055 llvm::DIDescriptor &CompileUnit) {
Devang Pateleb6d79b2010-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 Patel411894b2010-02-01 22:40:08 +000063
Devang Pateleb6d79b2010-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 Patel8b90a782010-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 Patel979ec2e2009-10-06 00:35:31 +000075 return CompileUnit;
76}
77
Devang Patel9c6c3a02010-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 Patel89f05f82010-01-28 18:21:00 +000091 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer1b627dc2010-01-23 18:16:07 +000092 memcpy(StrPtr, NS.data(), NS.length());
93 return llvm::StringRef(StrPtr, NS.length());
Devang Patel9c6c3a02010-01-14 00:36:21 +000094}
95
Devang Patel17800552010-03-09 00:44:50 +000096/// getOrCreateFile - Get the file debug info descriptor for the input location.
97llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Ted Kremenek9c250392010-03-30 00:27:51 +000098 if (!Loc.isValid())
Devang Patel17800552010-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 Carlsson20f12a22009-12-06 18:00:51 +0000102 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel17800552010-03-09 00:44:50 +0000103 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Ted Kremenek9c250392010-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 Patel17800552010-03-09 00:44:50 +0000118 llvm::sys::Path AbsFileName(PLoc.getFilename());
Benjamin Kramer47daf682009-12-08 11:02:29 +0000119 AbsFileName.makeAbsolute();
Devang Patel446c6192009-04-17 21:06:59 +0000120
Ted Kremenek9c250392010-03-30 00:27:51 +0000121 llvm::DIFile F = DebugFactory.CreateFile(AbsFileName.getLast(),
122 AbsFileName.getDirname(), TheCU);
123
Devang Patelab699792010-05-07 18:12:35 +0000124 DIFileCache[fname] = F;
Ted Kremenek9c250392010-03-30 00:27:51 +0000125 return F;
126
Devang Patel17800552010-03-09 00:44:50 +0000127}
Devang Patel8ab870d2010-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 Patel17800552010-03-09 00:44:50 +0000147/// CreateCompileUnit - Create new compile unit.
148void CGDebugInfo::CreateCompileUnit() {
149
150 // Get absolute path name.
Douglas Gregorac91b4c2010-03-18 23:46:43 +0000151 SourceManager &SM = CGM.getContext().getSourceManager();
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000152 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
153 if (MainFileName.empty())
Devang Patel22fe5852010-03-12 21:04:27 +0000154 MainFileName = "<unknown>";
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000155
Devang Patel22fe5852010-03-12 21:04:27 +0000156 llvm::sys::Path AbsFileName(MainFileName);
Devang Patel17800552010-03-09 00:44:50 +0000157 AbsFileName.makeAbsolute();
Daniel Dunbarc9abc042009-04-08 05:11:16 +0000158
Douglas Gregorf6728fc2010-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 Gregorf7ad5002010-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 Lattner515455a2009-03-25 03:28:08 +0000169 unsigned LangTag;
Devang Patel17800552010-03-09 00:44:50 +0000170 const LangOptions &LO = CGM.getLangOptions();
Chris Lattner515455a2009-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 Patel8d9aefc2009-03-24 20:35:51 +0000177 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000178 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000179 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000180 } else {
181 LangTag = llvm::dwarf::DW_LANG_C89;
182 }
Devang Patel446c6192009-04-17 21:06:59 +0000183
Benjamin Kramer47daf682009-12-08 11:02:29 +0000184 const char *Producer =
Mike Stumpd8945d62009-10-09 18:38:12 +0000185#ifdef CLANG_VENDOR
186 CLANG_VENDOR
187#endif
188 "clang " CLANG_VERSION_STRING;
Chris Lattner4c2577a2009-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 Stump1eb44332009-09-09 15:08:12 +0000194
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000195 // Create new compile unit.
Devang Patel17800552010-03-09 00:44:50 +0000196 TheCU = DebugFactory.CreateCompileUnit(
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000197 LangTag, AbsFileName.getLast(), MainFileDir, Producer, true,
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000198 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000199}
200
Devang Patel65e99f22009-02-25 01:36:11 +0000201/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000202/// one if necessary.
203llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel17800552010-03-09 00:44:50 +0000204 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-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 Patel7c173cb2009-10-12 22:28:31 +0000224 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000225 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000226 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000227 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000228 uint64_t Size = CGM.getContext().getTypeSize(BT);
229 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000230 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000231
Devang Patelca80a5f2009-10-20 19:55:01 +0000232 llvm::DIType DbgTy =
233 DebugFactory.CreateBasicType(Unit,
Anders Carlsson20f12a22009-12-06 18:00:51 +0000234 BT->getName(CGM.getContext().getLangOptions()),
Devang Patelca80a5f2009-10-20 19:55:01 +0000235 Unit, 0, Size, Align,
236 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000237 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000238}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000239
Chris Lattnerb7003772009-04-23 06:13:01 +0000240llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000241 llvm::DIFile Unit) {
Chris Lattnerb7003772009-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 Stump1eb44332009-09-09 15:08:12 +0000246
Anders Carlsson20f12a22009-12-06 18:00:51 +0000247 uint64_t Size = CGM.getContext().getTypeSize(Ty);
248 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattnerb7003772009-04-23 06:13:01 +0000249 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Devang Patelca80a5f2009-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 Patelca80a5f2009-10-20 19:55:01 +0000255 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000256}
257
John McCalla1805292009-09-25 01:40:47 +0000258/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000259/// a new one if necessary.
Devang Patel17800552010-03-09 00:44:50 +0000260llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-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 Lattner9c85ba32008-11-10 06:08:34 +0000268 // We will create one Derived type for one qualifier and recurse to handle any
269 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000270 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000271 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000272 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000273 Qc.removeConst();
274 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000275 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000276 Qc.removeVolatile();
277 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000278 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-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 Gupta1c6a38b2008-05-25 05:15:42 +0000283 }
Mike Stump1eb44332009-09-09 15:08:12 +0000284
John McCalla1805292009-09-25 01:40:47 +0000285 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
286
Daniel Dunbar3845f862008-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 Patelca80a5f2009-10-20 19:55:01 +0000289 llvm::DIType DbgTy =
Devang Pateld58562e2010-03-09 22:49:11 +0000290 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000291 0, 0, 0, 0, 0, FromTy);
Devang Patelca80a5f2009-10-20 19:55:01 +0000292 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000293}
294
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000295llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000296 llvm::DIFile Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000297 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000298 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
299 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000300 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000301}
302
Chris Lattner9c85ba32008-11-10 06:08:34 +0000303llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000304 llvm::DIFile Unit) {
Anders Carlssona031b352009-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 Patel17800552010-03-09 00:44:50 +0000312 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000313 llvm::DIType EltTy = getOrCreateType(PointeeTy, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000315 // Bit size, align and offset of the type.
Anders Carlssona031b352009-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 Carlsson20f12a22009-12-06 18:00:51 +0000320 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
321 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Devang Patelca80a5f2009-10-20 19:55:01 +0000323 return
Devang Pateld58562e2010-03-09 22:49:11 +0000324 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000325 0, Size, Align, 0, 0, EltTy);
Anders Carlssona031b352009-11-06 19:19:55 +0000326
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000327}
328
Mike Stump9bc093c2009-05-14 02:03:51 +0000329llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000330 llvm::DIFile Unit) {
Mike Stump9bc093c2009-05-14 02:03:51 +0000331 if (BlockLiteralGenericSet)
332 return BlockLiteralGeneric;
333
Mike Stump9bc093c2009-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 Carlsson20f12a22009-12-06 18:00:51 +0000348 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000349 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
350 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000351
Daniel Dunbarca308df2009-05-26 19:40:20 +0000352 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000353 EltTys.clear();
354
Mike Stump3d363c52009-10-02 02:30:50 +0000355 unsigned Flags = llvm::DIType::FlagAppleBlock;
Devang Patel8ab870d2010-05-12 23:46:38 +0000356 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump3d363c52009-10-02 02:30:50 +0000357
Mike Stump9bc093c2009-05-14 02:03:51 +0000358 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Devang Patel8ab870d2010-05-12 23:46:38 +0000359 Unit, LineNo, FieldOffset, 0, 0,
360 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Mike Stump9bc093c2009-05-14 02:03:51 +0000362 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000363 uint64_t Size = CGM.getContext().getTypeSize(Ty);
364 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Mike Stump9bc093c2009-05-14 02:03:51 +0000366 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000367 Unit, "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000368 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000369
370 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000371 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000372 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson20f12a22009-12-06 18:00:51 +0000373 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000374 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
375 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramerd3651cc2010-04-24 20:26:20 +0000376 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000377 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000378
Anders Carlsson20f12a22009-12-06 18:00:51 +0000379 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000380 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000381 FieldSize = CGM.getContext().getTypeSize(Ty);
382 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000383 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000384 "__descriptor", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000385 LineNo, FieldSize, FieldAlign,
Mike Stump9bc093c2009-05-14 02:03:51 +0000386 FieldOffset, 0, FieldTy);
387 EltTys.push_back(FieldTy);
388
389 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000390 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000391
392 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Devang Patel8ab870d2010-05-12 23:46:38 +0000393 Unit, LineNo, FieldOffset, 0, 0,
394 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Mike Stump9bc093c2009-05-14 02:03:51 +0000396 BlockLiteralGenericSet = true;
397 BlockLiteralGeneric
398 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000399 "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000400 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000401 return BlockLiteralGeneric;
402}
403
Chris Lattner9c85ba32008-11-10 06:08:34 +0000404llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000405 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-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 Stump1eb44332009-09-09 15:08:12 +0000409
Chris Lattner9c85ba32008-11-10 06:08:34 +0000410 // We don't set size information, but do specify where the typedef was
411 // declared.
Devang Patel8ab870d2010-05-12 23:46:38 +0000412 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000413
Devang Pateleb6d79b2010-02-01 21:34:11 +0000414 llvm::DIDescriptor TyContext
415 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
416 Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000417 llvm::DIType DbgTy =
Devang Pateld5289052010-01-29 22:29:31 +0000418 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Pateleb6d79b2010-02-01 21:34:11 +0000419 TyContext,
Devang Pateld5289052010-01-29 22:29:31 +0000420 Ty->getDecl()->getName(), Unit,
421 Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000422 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000423}
424
Chris Lattner9c85ba32008-11-10 06:08:34 +0000425llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000426 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000427 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000428
Chris Lattner9c85ba32008-11-10 06:08:34 +0000429 // Add the result type at least.
430 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Chris Lattner9c85ba32008-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 Gregor72564e72009-02-26 23:50:07 +0000434 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-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 Gupta1c6a38b2008-05-25 05:15:42 +0000439 }
440
Chris Lattner9c85ba32008-11-10 06:08:34 +0000441 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000442 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000443
Devang Patelca80a5f2009-10-20 19:55:01 +0000444 llvm::DIType DbgTy =
445 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000446 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000447 0, 0, 0, 0, 0,
448 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000449 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000450}
451
Devang Patel428deb52010-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 Patel17800552010-03-09 00:44:50 +0000455CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit,
Devang Patel428deb52010-01-19 00:00:59 +0000456 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
457 unsigned FieldNo = 0;
Devang Patel239cec62010-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 Patel428deb52010-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 Patel4835fdd2010-02-12 01:31:06 +0000467 // Ignore unnamed fields. Do not ignore unnamed records.
468 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel428deb52010-01-19 00:00:59 +0000469 continue;
470
471 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +0000472 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
473 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel428deb52010-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 Patel71f4ff62010-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 Patel428deb52010-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 Patel71f4ff62010-04-21 23:12:37 +0000503 FieldOffset, Flags, FieldTy);
Devang Patel428deb52010-01-19 00:00:59 +0000504 EltTys.push_back(FieldTy);
505 }
506}
507
Devang Patela6da1922010-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 Patel17800552010-03-09 00:44:50 +0000513 llvm::DIFile Unit) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +0000514 llvm::DIType FnTy
515 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
516 0),
517 Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000518
519 // Static methods do not need "this" pointer argument.
520 if (Method->isStatic())
521 return FnTy;
522
Devang Patela6da1922010-01-28 00:28:01 +0000523 // Add "this" pointer.
524
Devang Patelab699792010-05-07 18:12:35 +0000525 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patela6da1922010-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 Patel337472d2010-02-09 17:57:50 +0000537 llvm::DIType ThisPtrType =
538 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
Devang Patelab699792010-05-07 18:12:35 +0000539 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
Devang Patel337472d2010-02-09 17:57:50 +0000540 Elts.push_back(ThisPtrType);
Devang Patela6da1922010-01-28 00:28:01 +0000541
542 // Copy rest of the arguments.
543 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
544 Elts.push_back(Args.getElement(i));
545
546 llvm::DIArray EltTypeArray =
547 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
548
549 return
550 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000551 Unit, "", Unit,
Devang Patela6da1922010-01-28 00:28:01 +0000552 0, 0, 0, 0, 0,
553 llvm::DIType(), EltTypeArray);
554}
555
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000556/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
557/// a single member function GlobalDecl.
558llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000559CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000560 llvm::DIFile Unit,
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000561 llvm::DICompositeType &RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000562 bool IsCtorOrDtor =
563 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
564
565 llvm::StringRef MethodName = getFunctionName(Method);
Devang Patela6da1922010-01-28 00:28:01 +0000566 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000567
568 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
569 // make sense to give a single ctor/dtor a linkage name.
Anders Carlsson9a20d552010-06-22 16:16:50 +0000570 llvm::StringRef MethodLinkageName;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000571 if (!IsCtorOrDtor)
Anders Carlsson9a20d552010-06-22 16:16:50 +0000572 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000573
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000574 // Get the location for the method.
Devang Patel8ab870d2010-05-12 23:46:38 +0000575 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
576 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000577
578 // Collect virtual method info.
579 llvm::DIType ContainingType;
580 unsigned Virtuality = 0;
581 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000582
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000583 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000584 if (Method->isPure())
585 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
586 else
587 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
588
589 // It doesn't make sense to give a virtual destructor a vtable index,
590 // since a single destructor has two entries in the vtable.
591 if (!isa<CXXDestructorDecl>(Method))
Anders Carlsson046c2942010-04-17 20:15:18 +0000592 VIndex = CGM.getVTables().getMethodVTableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000593 ContainingType = RecordTy;
594 }
595
596 llvm::DISubprogram SP =
597 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
598 MethodLinkageName,
599 MethodDefUnit, MethodLine,
600 MethodTy, /*isLocalToUnit=*/false,
601 Method->isThisDeclarationADefinition(),
602 Virtuality, VIndex, ContainingType);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000603
604 // Don't cache ctors or dtors since we have to emit multiple functions for
605 // a single ctor or dtor.
606 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
Devang Patelab699792010-05-07 18:12:35 +0000607 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000608
609 return SP;
610}
611
Devang Patel4125fd22010-01-19 01:54:44 +0000612/// CollectCXXMemberFunctions - A helper function to collect debug info for
613/// C++ member functions.This is used while creating debug info entry for
614/// a Record.
615void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000616CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000617 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
618 llvm::DICompositeType &RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000619 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
620 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000621 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000622
Devang Pateld5322da2010-02-09 19:09:28 +0000623 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000624 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000625
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000626 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000627 }
628}
629
Devang Patela245c5b2010-01-25 23:32:18 +0000630/// CollectCXXBases - A helper function to collect debug info for
631/// C++ base classes. This is used while creating debug info entry for
632/// a Record.
633void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000634CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000635 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
636 llvm::DICompositeType &RecordTy) {
637
Devang Patel239cec62010-02-01 21:39:52 +0000638 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
639 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
640 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000641 unsigned BFlags = 0;
642 uint64_t BaseOffset;
643
644 const CXXRecordDecl *Base =
645 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
646
647 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000648 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000649 // expression where it expects +ve number.
Anders Carlssonaf440352010-03-23 04:11:45 +0000650 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base);
Devang Patelca7daed2010-01-28 21:54:15 +0000651 BFlags = llvm::DIType::FlagVirtual;
652 } else
653 BaseOffset = RL.getBaseClassOffset(Base);
654
655 AccessSpecifier Access = BI->getAccessSpecifier();
656 if (Access == clang::AS_private)
657 BFlags |= llvm::DIType::FlagPrivate;
658 else if (Access == clang::AS_protected)
659 BFlags |= llvm::DIType::FlagProtected;
660
661 llvm::DIType DTy =
662 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
663 RecordTy, llvm::StringRef(),
Devang Pateld58562e2010-03-09 22:49:11 +0000664 Unit, 0, 0, 0,
Devang Patelca7daed2010-01-28 21:54:15 +0000665 BaseOffset, BFlags,
666 getOrCreateType(BI->getType(),
667 Unit));
668 EltTys.push_back(DTy);
669 }
Devang Patela245c5b2010-01-25 23:32:18 +0000670}
671
Devang Patel4ce3f202010-01-28 18:11:52 +0000672/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000673llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000674 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000675 return VTablePtrType;
676
677 ASTContext &Context = CGM.getContext();
678
679 /* Function type */
Benjamin Kramerad468862010-03-30 11:36:44 +0000680 llvm::DIDescriptor STy = getOrCreateType(Context.IntTy, Unit);
681 llvm::DIArray SElements = DebugFactory.GetOrCreateArray(&STy, 1);
Devang Patel4ce3f202010-01-28 18:11:52 +0000682 llvm::DIType SubTy =
683 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000684 Unit, "", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000685 0, 0, 0, 0, 0, llvm::DIType(), SElements);
686
687 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
688 llvm::DIType vtbl_ptr_type
689 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000690 Unit, "__vtbl_ptr_type", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000691 0, Size, 0, 0, 0, SubTy);
692
Devang Pateld58562e2010-03-09 22:49:11 +0000693 VTablePtrType =
694 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
695 Unit, "", Unit,
696 0, Size, 0, 0, 0, vtbl_ptr_type);
Devang Patel4ce3f202010-01-28 18:11:52 +0000697 return VTablePtrType;
698}
699
Anders Carlsson046c2942010-04-17 20:15:18 +0000700/// getVTableName - Get vtable name for the given Class.
701llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000702 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000703 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000704
705 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000706 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000707 memcpy(StrPtr, Name.data(), Name.length());
708 return llvm::StringRef(StrPtr, Name.length());
709}
710
711
Anders Carlsson046c2942010-04-17 20:15:18 +0000712/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel4ce3f202010-01-28 18:11:52 +0000713/// debug info entry in EltTys vector.
714void CGDebugInfo::
Anders Carlsson046c2942010-04-17 20:15:18 +0000715CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000716 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000717 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000718
719 // If there is a primary base then it will hold vtable info.
720 if (RL.getPrimaryBase())
721 return;
722
723 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000724 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000725 return;
726
727 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
728 llvm::DIType VPTR
729 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Anders Carlsson046c2942010-04-17 20:15:18 +0000730 getVTableName(RD), Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000731 0, Size, 0, 0, 0,
732 getOrCreateVTablePtrType(Unit));
733 EltTys.push_back(VPTR);
734}
735
Devang Patel65e99f22009-02-25 01:36:11 +0000736/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000737llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000738 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000739 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000740
Chris Lattner9c85ba32008-11-10 06:08:34 +0000741 unsigned Tag;
Devang Pateld6c5a262010-02-01 21:52:22 +0000742 if (RD->isStruct())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000743 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Pateld6c5a262010-02-01 21:52:22 +0000744 else if (RD->isUnion())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000745 Tag = llvm::dwarf::DW_TAG_union_type;
746 else {
Devang Pateld6c5a262010-02-01 21:52:22 +0000747 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000748 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000749 }
750
Chris Lattner9c85ba32008-11-10 06:08:34 +0000751 // Get overall information about the record type for the debug info.
Devang Patel8ab870d2010-05-12 23:46:38 +0000752 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
753 unsigned Line = getLineNumber(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Chris Lattner9c85ba32008-11-10 06:08:34 +0000755 // Records and classes and unions can all be recursive. To handle them, we
756 // first generate a debug descriptor for the struct as a forward declaration.
757 // Then (if it is a definition) we go through and get debug info for all of
758 // its members. Finally, we create a descriptor for the complete type (which
759 // may refer to the forward decl if the struct is recursive) and replace all
760 // uses of the forward declaration with the final definition.
Devang Patel0b897992010-07-08 19:56:29 +0000761 llvm::DIDescriptor FDContext =
762 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
763
764 // If this is just a forward declaration, construct an appropriately
765 // marked node and just return it.
766 if (!RD->getDefinition()) {
767 llvm::DICompositeType FwdDecl =
768 DebugFactory.CreateCompositeType(Tag, FDContext, RD->getName(),
769 DefUnit, Line, 0, 0, 0,
770 llvm::DIType::FlagFwdDecl,
771 llvm::DIType(), llvm::DIArray());
772
773 return FwdDecl;
774 }
Devang Pateld0f251b2010-01-20 23:56:40 +0000775
Devang Pateld6c5a262010-02-01 21:52:22 +0000776 // A RD->getName() is not unique. However, the debug info descriptors
Devang Patelce78c972010-02-01 22:51:29 +0000777 // are uniqued so use type name to ensure uniquness.
Benjamin Kramerfea3d4d2010-03-13 12:06:51 +0000778 llvm::SmallString<128> FwdDeclName;
779 llvm::raw_svector_ostream(FwdDeclName) << "fwd.type." << FwdDeclCount++;
Devang Patel0ce73f62009-07-22 18:57:00 +0000780 llvm::DICompositeType FwdDecl =
Devang Patel7573f8b2010-03-09 21:32:27 +0000781 DebugFactory.CreateCompositeType(Tag, FDContext, FwdDeclName,
Devang Patelab71ff52009-11-12 00:51:46 +0000782 DefUnit, Line, 0, 0, 0, 0,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000783 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000784
Devang Patelab699792010-05-07 18:12:35 +0000785 llvm::MDNode *MN = FwdDecl;
786 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000787 // Otherwise, insert it into the TypeCache so that recursive uses will find
788 // it.
Devang Patelab699792010-05-07 18:12:35 +0000789 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000790 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000791 RegionStack.push_back(FwdDeclNode);
792 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000793
794 // Convert all the elements.
795 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
796
Devang Pateld6c5a262010-02-01 21:52:22 +0000797 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000798 if (CXXDecl) {
799 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson046c2942010-04-17 20:15:18 +0000800 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000801 }
Devang Pateld6c5a262010-02-01 21:52:22 +0000802 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +0000803 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +0000804 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +0000805 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +0000806
807 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +0000808 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel0ac8f312010-01-28 00:54:21 +0000809 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
810 ContainingType =
Devang Patelab699792010-05-07 18:12:35 +0000811 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patel0ac8f312010-01-28 00:54:21 +0000812 else if (CXXDecl->isDynamicClass())
Devang Patelab699792010-05-07 18:12:35 +0000813 ContainingType = FwdDecl;
Devang Patela245c5b2010-01-25 23:32:18 +0000814 }
Mike Stump1eb44332009-09-09 15:08:12 +0000815
Chris Lattner9c85ba32008-11-10 06:08:34 +0000816 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000817 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000818
819 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000820 uint64_t Size = CGM.getContext().getTypeSize(Ty);
821 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000822
Devang Patele4c1ea02010-03-11 20:01:48 +0000823 RegionStack.pop_back();
824 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
825 RegionMap.find(Ty->getDecl());
826 if (RI != RegionMap.end())
827 RegionMap.erase(RI);
828
Devang Patel411894b2010-02-01 22:40:08 +0000829 llvm::DIDescriptor RDContext =
830 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel0ce73f62009-07-22 18:57:00 +0000831 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +0000832 DebugFactory.CreateCompositeType(Tag, RDContext,
833 RD->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000834 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +0000835 llvm::DIType(), Elements,
836 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000837
838 // Now that we have a real decl for the struct, replace anything using the
839 // old decl with the new one. This will recursively update the debug info.
Eli Friedman14d63652009-11-16 21:04:30 +0000840 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +0000841 RegionMap[RD] = llvm::WeakVH(RealDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000842 return RealDecl;
843}
844
John McCallc12c5bb2010-05-15 11:32:37 +0000845/// CreateType - get objective-c object type.
846llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
847 llvm::DIFile Unit) {
848 // Ignore protocols.
849 return getOrCreateType(Ty->getBaseType(), Unit);
850}
851
Devang Patel9ca36b62009-02-26 21:10:26 +0000852/// CreateType - get objective-c interface type.
853llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000854 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000855 ObjCInterfaceDecl *ID = Ty->getDecl();
Devang Patel9ca36b62009-02-26 21:10:26 +0000856 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Patel9ca36b62009-02-26 21:10:26 +0000857
858 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +0000859 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +0000860 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel17800552010-03-09 00:44:50 +0000861 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000862
Devang Patel9ca36b62009-02-26 21:10:26 +0000863 // To handle recursive interface, we
864 // first generate a debug descriptor for the struct as a forward declaration.
865 // Then (if it is a definition) we go through and get debug info for all of
866 // its members. Finally, we create a descriptor for the complete type (which
867 // may refer to the forward decl if the struct is recursive) and replace all
868 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000869 llvm::DICompositeType FwdDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +0000870 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000871 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000872 llvm::DIType(), llvm::DIArray(),
873 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Devang Patel9ca36b62009-02-26 21:10:26 +0000875 // If this is just a forward declaration, return it.
Devang Pateld6c5a262010-02-01 21:52:22 +0000876 if (ID->isForwardDecl())
Devang Patel9ca36b62009-02-26 21:10:26 +0000877 return FwdDecl;
878
Devang Patelab699792010-05-07 18:12:35 +0000879 llvm::MDNode *MN = FwdDecl;
880 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patel9ca36b62009-02-26 21:10:26 +0000881 // Otherwise, insert it into the TypeCache so that recursive uses will find
882 // it.
Devang Patelab699792010-05-07 18:12:35 +0000883 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000884 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000885 RegionStack.push_back(FwdDeclNode);
886 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000887
888 // Convert all the elements.
889 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
890
Devang Pateld6c5a262010-02-01 21:52:22 +0000891 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +0000892 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000893 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000894 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000895 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000896 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Pateld58562e2010-03-09 22:49:11 +0000897 Unit, "", Unit, 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000898 0 /* offset */, 0, SClassTy);
899 EltTys.push_back(InhTag);
900 }
901
Devang Pateld6c5a262010-02-01 21:52:22 +0000902 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +0000903
904 unsigned FieldNo = 0;
Devang Pateld6c5a262010-02-01 21:52:22 +0000905 for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(),
906 E = ID->ivar_end(); I != E; ++I, ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +0000907 ObjCIvarDecl *Field = *I;
908 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
909
Devang Patel73621622009-11-25 17:37:31 +0000910 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +0000911
Devang Patelde135022009-04-27 22:40:36 +0000912 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +0000913 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +0000914 continue;
915
Devang Patel9ca36b62009-02-26 21:10:26 +0000916 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +0000917 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
918 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel99c20eb2009-03-20 18:24:39 +0000919 QualType FType = Field->getType();
920 uint64_t FieldSize = 0;
921 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000922
Devang Patel99c20eb2009-03-20 18:24:39 +0000923 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000924
Devang Patel99c20eb2009-03-20 18:24:39 +0000925 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000926 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000927 Expr *BitWidth = Field->getBitWidth();
928 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +0000929 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000930
Anders Carlsson20f12a22009-12-06 18:00:51 +0000931 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000932 }
933
Mike Stump1eb44332009-09-09 15:08:12 +0000934 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
935
Devang Patelc20482b2009-03-19 00:23:53 +0000936 unsigned Flags = 0;
937 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
938 Flags = llvm::DIType::FlagProtected;
939 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
940 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Devang Patel9ca36b62009-02-26 21:10:26 +0000942 // Create a DW_TAG_member node to remember the offset of this field in the
943 // struct. FIXME: This is an absolutely insane way to capture this
944 // information. When we gut debug info, this should be fixed.
945 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
946 FieldName, FieldDefUnit,
947 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000948 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000949 EltTys.push_back(FieldTy);
950 }
Mike Stump1eb44332009-09-09 15:08:12 +0000951
Devang Patel9ca36b62009-02-26 21:10:26 +0000952 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000953 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +0000954
Devang Patele4c1ea02010-03-11 20:01:48 +0000955 RegionStack.pop_back();
956 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
957 RegionMap.find(Ty->getDecl());
958 if (RI != RegionMap.end())
959 RegionMap.erase(RI);
960
Devang Patel9ca36b62009-02-26 21:10:26 +0000961 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000962 uint64_t Size = CGM.getContext().getTypeSize(Ty);
963 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000964
Devang Patel6c1fddf2009-07-27 18:42:03 +0000965 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +0000966 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +0000967 Line, Size, Align, 0, 0, llvm::DIType(),
968 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000969
970 // Now that we have a real decl for the struct, replace anything using the
971 // old decl with the new one. This will recursively update the debug info.
Devang Patelffffb032009-11-16 20:09:38 +0000972 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +0000973 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000974
Devang Patel9ca36b62009-02-26 21:10:26 +0000975 return RealDecl;
976}
977
Chris Lattner9c85ba32008-11-10 06:08:34 +0000978llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000979 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000980 EnumDecl *ED = Ty->getDecl();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000981
982 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
983
984 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +0000985 for (EnumDecl::enumerator_iterator
Devang Pateld6c5a262010-02-01 21:52:22 +0000986 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000987 Enum != EnumEnd; ++Enum) {
Devang Patel73621622009-11-25 17:37:31 +0000988 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor44b43212008-12-11 16:49:14 +0000989 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000990 }
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Chris Lattner9c85ba32008-11-10 06:08:34 +0000992 // Return a CompositeType for the enum itself.
993 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000994 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000995
Devang Patel8ab870d2010-05-12 23:46:38 +0000996 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
997 unsigned Line = getLineNumber(ED->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000998
Chris Lattner9c85ba32008-11-10 06:08:34 +0000999 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +00001000 uint64_t Size = 0;
1001 unsigned Align = 0;
1002 if (!Ty->isIncompleteType()) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001003 Size = CGM.getContext().getTypeSize(Ty);
1004 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman3189e4b2009-05-04 04:39:55 +00001005 }
Mike Stump1eb44332009-09-09 15:08:12 +00001006
Devang Patelca80a5f2009-10-20 19:55:01 +00001007 llvm::DIType DbgTy =
1008 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Pateld6c5a262010-02-01 21:52:22 +00001009 Unit, ED->getName(), DefUnit, Line,
Devang Patelca80a5f2009-10-20 19:55:01 +00001010 Size, Align, 0, 0,
1011 llvm::DIType(), EltArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001012 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001013}
1014
1015llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001016 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001017 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1018 return CreateType(RT, Unit);
1019 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1020 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001021
Chris Lattner9c85ba32008-11-10 06:08:34 +00001022 return llvm::DIType();
1023}
1024
Devang Patel70c23cd2010-02-23 22:59:39 +00001025llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001026 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001027 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1028 uint64_t NumElems = Ty->getNumElements();
1029 if (NumElems > 0)
1030 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001031
Benjamin Kramerad468862010-03-30 11:36:44 +00001032 llvm::DIDescriptor Subscript = DebugFactory.GetOrCreateSubrange(0, NumElems);
1033 llvm::DIArray SubscriptArray = DebugFactory.GetOrCreateArray(&Subscript, 1);
Devang Patel70c23cd2010-02-23 22:59:39 +00001034
1035 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1036 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1037
1038 return
1039 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001040 Unit, "", Unit,
Devang Patel70c23cd2010-02-23 22:59:39 +00001041 0, Size, Align, 0, 0,
1042 ElementTy, SubscriptArray);
1043}
1044
Chris Lattner9c85ba32008-11-10 06:08:34 +00001045llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001046 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001047 uint64_t Size;
1048 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001049
1050
Nuno Lopes010d5142009-01-28 00:35:17 +00001051 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001052 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001053 Size = 0;
1054 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001055 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001056 } else if (Ty->isIncompleteArrayType()) {
1057 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001058 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001059 } else {
1060 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001061 Size = CGM.getContext().getTypeSize(Ty);
1062 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001063 }
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Chris Lattner9c85ba32008-11-10 06:08:34 +00001065 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1066 // interior arrays, do we care? Why aren't nested arrays represented the
1067 // obvious/recursive way?
1068 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1069 QualType EltTy(Ty, 0);
1070 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +00001071 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001072 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +00001073 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +00001074 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001075 // FIXME: Verify this is right for VLAs.
1076 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1077 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +00001078 }
Mike Stump1eb44332009-09-09 15:08:12 +00001079
Chris Lattner9c85ba32008-11-10 06:08:34 +00001080 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001081 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001082
Devang Patelca80a5f2009-10-20 19:55:01 +00001083 llvm::DIType DbgTy =
1084 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001085 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +00001086 0, Size, Align, 0, 0,
1087 getOrCreateType(EltTy, Unit),
1088 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001089 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001090}
1091
Anders Carlssona031b352009-11-06 19:19:55 +00001092llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001093 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001094 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1095 Ty, Ty->getPointeeType(), Unit);
1096}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001097
Anders Carlsson20f12a22009-12-06 18:00:51 +00001098llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001099 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001100 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1101 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1102
1103 if (!Ty->getPointeeType()->isFunctionType()) {
1104 // We have a data member pointer type.
1105 return PointerDiffDITy;
1106 }
1107
1108 // We have a member function pointer type. Treat it as a struct with two
1109 // ptrdiff_t members.
1110 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1111
1112 uint64_t FieldOffset = 0;
1113 llvm::DIDescriptor ElementTypes[2];
1114
1115 // FIXME: This should probably be a function type instead.
1116 ElementTypes[0] =
1117 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001118 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001119 Info.first, Info.second, FieldOffset, 0,
1120 PointerDiffDITy);
1121 FieldOffset += Info.first;
1122
1123 ElementTypes[1] =
1124 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001125 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001126 Info.first, Info.second, FieldOffset, 0,
1127 PointerDiffDITy);
1128
1129 llvm::DIArray Elements =
1130 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1131 llvm::array_lengthof(ElementTypes));
1132
1133 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1134 U, llvm::StringRef("test"),
Devang Pateld58562e2010-03-09 22:49:11 +00001135 U, 0, FieldOffset,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001136 0, 0, 0, llvm::DIType(), Elements);
1137}
1138
Douglas Gregor840943d2009-12-21 20:18:30 +00001139static QualType UnwrapTypeForDebugInfo(QualType T) {
1140 do {
1141 QualType LastT = T;
1142 switch (T->getTypeClass()) {
1143 default:
1144 return T;
1145 case Type::TemplateSpecialization:
1146 T = cast<TemplateSpecializationType>(T)->desugar();
1147 break;
1148 case Type::TypeOfExpr: {
1149 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1150 T = Ty->getUnderlyingExpr()->getType();
1151 break;
1152 }
1153 case Type::TypeOf:
1154 T = cast<TypeOfType>(T)->getUnderlyingType();
1155 break;
1156 case Type::Decltype:
1157 T = cast<DecltypeType>(T)->getUnderlyingType();
1158 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001159 case Type::Elaborated:
1160 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001161 break;
1162 case Type::SubstTemplateTypeParm:
1163 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1164 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001165 }
1166
1167 assert(T != LastT && "Type unwrapping failed to unwrap!");
1168 if (T == LastT)
1169 return T;
1170 } while (true);
1171
1172 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001173}
1174
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001175/// getOrCreateType - Get the type from the cache or create a new
1176/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001177llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001178 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001179 if (Ty.isNull())
1180 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001181
Douglas Gregor840943d2009-12-21 20:18:30 +00001182 // Unwrap the type as needed for debug information.
1183 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001184
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001185 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001186 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001187 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001188 if (it != TypeCache.end()) {
1189 // Verify that the debug info still exists.
1190 if (&*it->second)
1191 return llvm::DIType(cast<llvm::MDNode>(it->second));
1192 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001193
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001194 // Otherwise create the type.
1195 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001196
1197 // And update the type cache.
Devang Patelab699792010-05-07 18:12:35 +00001198 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001199 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001200}
1201
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001202/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001203llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001204 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001205 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001206 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001207 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001208
Douglas Gregor2101a822009-12-21 19:57:21 +00001209 const char *Diag = 0;
1210
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001211 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001212 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001213#define TYPE(Class, Base)
1214#define ABSTRACT_TYPE(Class, Base)
1215#define NON_CANONICAL_TYPE(Class, Base)
1216#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1217#include "clang/AST/TypeNodes.def"
1218 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001219
Anders Carlssonbfe69952009-11-06 18:24:04 +00001220 // FIXME: Handle these.
1221 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001222 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001223
1224 case Type::Vector:
1225 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001226 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001227 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCallc12c5bb2010-05-15 11:32:37 +00001228 case Type::ObjCObject:
1229 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001230 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001231 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1232 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1233 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1234 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001235 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001236 return CreateType(cast<BlockPointerType>(Ty), Unit);
1237 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001238 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001239 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001240 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001241 case Type::FunctionProto:
1242 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001243 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001244 case Type::ConstantArray:
1245 case Type::VariableArray:
1246 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001247 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001248
1249 case Type::LValueReference:
1250 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1251
Anders Carlsson20f12a22009-12-06 18:00:51 +00001252 case Type::MemberPointer:
1253 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001254
1255 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001256 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001257 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001258 case Type::TypeOfExpr:
1259 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001260 case Type::Decltype:
1261 llvm_unreachable("type should have been unwrapped!");
1262 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001263
1264 case Type::RValueReference:
1265 // FIXME: Implement!
1266 Diag = "rvalue references";
1267 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001268 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001269
1270 assert(Diag && "Fall through without a diagnostic?");
1271 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1272 "debug information for %0 is not yet supported");
1273 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1274 << Diag;
1275 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001276}
1277
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001278/// CreateMemberType - Create new member and increase Offset by FType's size.
1279llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
1280 llvm::StringRef Name,
1281 uint64_t *Offset) {
1282 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1283 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1284 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
1285 llvm::DIType Ty = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1286 Unit, Name, Unit, 0,
1287 FieldSize, FieldAlign,
1288 *Offset, 0, FieldTy);
1289 *Offset += FieldSize;
1290 return Ty;
1291}
1292
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001293/// EmitFunctionStart - Constructs the debug code for entering a function -
1294/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001295void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001296 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001297 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001298
Devang Patel9c6c3a02010-01-14 00:36:21 +00001299 llvm::StringRef Name;
Anders Carlsson9a20d552010-06-22 16:16:50 +00001300 llvm::StringRef LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001301
1302 const Decl *D = GD.getDecl();
1303 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001304 // If there is a DISubprogram for this function available then use it.
1305 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1306 FI = SPCache.find(FD);
1307 if (FI != SPCache.end()) {
Devang Patel0804e6e2010-03-08 20:53:17 +00001308 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
Devang Patelab699792010-05-07 18:12:35 +00001309 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1310 llvm::MDNode *SPN = SP;
1311 RegionStack.push_back(SPN);
1312 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00001313 return;
1314 }
1315 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001316 Name = getFunctionName(FD);
1317 // Use mangled name as linkage name for c/c++ functions.
Anders Carlsson9a20d552010-06-22 16:16:50 +00001318 LinkageName = CGM.getMangledName(GD);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001319 } else {
1320 // Use llvm function name as linkage name.
1321 Name = Fn->getName();
Anders Carlsson9a20d552010-06-22 16:16:50 +00001322 LinkageName = Name;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001323 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001324 if (!Name.empty() && Name[0] == '\01')
1325 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001326
Devang Patel970c6182010-04-24 00:49:16 +00001327 // It is expected that CurLoc is set before using EmitFunctionStart.
1328 // Usually, CurLoc points to the left bracket location of compound
1329 // statement representing function body.
1330 llvm::DIFile Unit = getOrCreateFile(CurLoc);
Devang Patel8ab870d2010-05-12 23:46:38 +00001331 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001332
Chris Lattner9c85ba32008-11-10 06:08:34 +00001333 llvm::DISubprogram SP =
Devang Patel970c6182010-04-24 00:49:16 +00001334 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001335 getOrCreateType(FnType, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001336 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump1eb44332009-09-09 15:08:12 +00001337
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001338 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001339 llvm::MDNode *SPN = SP;
1340 RegionStack.push_back(SPN);
1341 RegionMap[D] = llvm::WeakVH(SP);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001342}
1343
1344
Chris Lattner9c85ba32008-11-10 06:08:34 +00001345void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001346 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001347
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001348 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001349 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001350 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001351 || (SM.getInstantiationLineNumber(CurLoc) ==
1352 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001353 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patel4800ea62010-04-05 21:09:15 +00001354 // New Builder may not be in sync with CGDebugInfo.
1355 if (!Builder.getCurrentDebugLocation().isUnknown())
1356 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001357
1358 // Update last state.
1359 PrevLoc = CurLoc;
1360
Chris Lattnerc6034632010-04-01 06:31:43 +00001361 llvm::MDNode *Scope = RegionStack.back();
Devang Patel8ab870d2010-05-12 23:46:38 +00001362 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1363 getColumnNumber(CurLoc),
Chris Lattnere541d012010-04-02 20:21:43 +00001364 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001365}
1366
1367/// EmitRegionStart- Constructs the debug code for entering a declarative
1368/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +00001369void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001370 llvm::DIDescriptor D =
1371 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1372 llvm::DIDescriptor() :
Devang Pateld19429f2010-02-16 21:41:20 +00001373 llvm::DIDescriptor(RegionStack.back()),
Devang Patel8ab870d2010-05-12 23:46:38 +00001374 getLineNumber(CurLoc),
1375 getColumnNumber(CurLoc));
Devang Patelab699792010-05-07 18:12:35 +00001376 llvm::MDNode *DN = D;
1377 RegionStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001378}
1379
1380/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1381/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +00001382void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001383 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1384
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001385 // Provide an region stop point.
1386 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001387
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001388 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001389}
1390
Devang Patel809b9bb2010-02-10 18:49:08 +00001391// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1392// See BuildByRefType.
1393llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1394 uint64_t *XOffset) {
1395
1396 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1397
1398 QualType FType;
1399 uint64_t FieldSize, FieldOffset;
1400 unsigned FieldAlign;
1401
Devang Patel17800552010-03-09 00:44:50 +00001402 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001403 QualType Type = VD->getType();
1404
1405 FieldOffset = 0;
1406 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001407 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1408 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001409 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001410 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1411 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1412
Devang Patel809b9bb2010-02-10 18:49:08 +00001413 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1414 if (HasCopyAndDispose) {
1415 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001416 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1417 &FieldOffset));
1418 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1419 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001420 }
1421
1422 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1423 if (Align > CharUnits::fromQuantity(
1424 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1425 unsigned AlignedOffsetInBytes
1426 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1427 unsigned NumPaddingBytes
1428 = AlignedOffsetInBytes - FieldOffset/8;
1429
1430 if (NumPaddingBytes > 0) {
1431 llvm::APInt pad(32, NumPaddingBytes);
1432 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1433 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001434 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001435 }
1436 }
1437
1438 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001439 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00001440 FieldSize = CGM.getContext().getTypeSize(FType);
1441 FieldAlign = Align.getQuantity()*8;
1442
1443 *XOffset = FieldOffset;
1444 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001445 VD->getName(), Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001446 0, FieldSize, FieldAlign,
1447 FieldOffset, 0, FieldTy);
1448 EltTys.push_back(FieldTy);
1449 FieldOffset += FieldSize;
1450
1451 llvm::DIArray Elements =
1452 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1453
1454 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1455
1456 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001457 Unit, "", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001458 0, FieldOffset, 0, 0, Flags,
1459 llvm::DIType(), Elements);
1460
1461}
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001462/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001463void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001464 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001465 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1466
Devang Patel17800552010-03-09 00:44:50 +00001467 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001468 llvm::DIType Ty;
1469 uint64_t XOffset = 0;
1470 if (VD->hasAttr<BlocksAttr>())
1471 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1472 else
1473 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001474
Devang Patelf4e54a22010-05-07 23:05:55 +00001475 // If there is not any debug info for type then do not emit debug info
1476 // for this variable.
1477 if (!Ty)
1478 return;
1479
Chris Lattner9c85ba32008-11-10 06:08:34 +00001480 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001481 unsigned Line = getLineNumber(VD->getLocation());
1482 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001483
Chris Lattner9c85ba32008-11-10 06:08:34 +00001484 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001485 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001486 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel239cec62010-02-01 21:39:52 +00001487 VD->getName(),
Devang Patel44eeeba2010-06-05 01:14:40 +00001488 Unit, Line, Ty, CGM.getLangOptions().Optimize);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001489 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001490 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001491 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001492
Chris Lattnerc6034632010-04-01 06:31:43 +00001493 llvm::MDNode *Scope = RegionStack.back();
Chris Lattnere541d012010-04-02 20:21:43 +00001494 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001495}
1496
Mike Stumpb1a6e682009-09-30 02:43:10 +00001497/// EmitDeclare - Emit local variable declaration debug info.
1498void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1499 llvm::Value *Storage, CGBuilderTy &Builder,
1500 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001501 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001502 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1503
Devang Patel2b594b92010-04-26 23:28:46 +00001504 if (Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001505 return;
1506
1507 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001508 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001509 llvm::DIType Ty;
1510 if (VD->hasAttr<BlocksAttr>())
1511 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1512 else
1513 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001514
1515 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001516 unsigned Line = getLineNumber(VD->getLocation());
1517 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001518
Devang Pateld6c5a262010-02-01 21:52:22 +00001519 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001520 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001521 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1522 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1523 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1524 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001525 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001526 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1527 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001528 // offset of __forwarding field
1529 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001530 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1531 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1532 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001533 // offset of x field
1534 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001535 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001536 }
1537
1538 // Create the descriptor for the variable.
1539 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001540 DebugFactory.CreateComplexVariable(Tag,
1541 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001542 VD->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001543 addr);
1544 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001545 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001546 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattnerd5b89022009-12-28 21:44:41 +00001547
Chris Lattnerc6034632010-04-01 06:31:43 +00001548 llvm::MDNode *Scope = RegionStack.back();
Devang Patelf8e10a52010-05-10 23:48:38 +00001549 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001550}
1551
Devang Pateld6c5a262010-02-01 21:52:22 +00001552void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001553 llvm::Value *Storage,
1554 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001555 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001556}
1557
Mike Stumpb1a6e682009-09-30 02:43:10 +00001558void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1559 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1560 CodeGenFunction *CGF) {
1561 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1562}
1563
Chris Lattner9c85ba32008-11-10 06:08:34 +00001564/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1565/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001566void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001567 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001568 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001569}
1570
1571
1572
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001573/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001574void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001575 const VarDecl *D) {
1576
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001577 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001578 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001579 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +00001580
Devang Pateleb6d79b2010-02-01 21:34:11 +00001581 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001582 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001583
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001584 // CodeGen turns int[] into int[1] so we'll do the same here.
1585 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001586
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001587 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001588 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001589
Anders Carlsson20f12a22009-12-06 18:00:51 +00001590 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001591 ArrayType::Normal, 0);
1592 }
Devang Patel5d822f02010-04-29 17:48:37 +00001593 llvm::StringRef DeclName = D->getName();
Devang Patel8b90a782010-05-13 23:52:37 +00001594 llvm::StringRef LinkageName;
Devang Patel0fd3d1f2010-05-14 16:55:25 +00001595 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()))
Devang Patel8b90a782010-05-13 23:52:37 +00001596 LinkageName = Var->getName();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001597 llvm::DIDescriptor DContext =
1598 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
Devang Patel8b90a782010-05-13 23:52:37 +00001599 DebugFactory.CreateGlobalVariable(DContext, DeclName, DeclName, LinkageName,
1600 Unit, LineNo, getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001601 Var->hasInternalLinkage(),
1602 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001603}
1604
Devang Patel9ca36b62009-02-26 21:10:26 +00001605/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001606void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001607 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001608 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001609 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001610 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patel9ca36b62009-02-26 21:10:26 +00001611
Devang Pateld6c5a262010-02-01 21:52:22 +00001612 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001613
Devang Pateld6c5a262010-02-01 21:52:22 +00001614 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001615 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001616
Devang Patel9ca36b62009-02-26 21:10:26 +00001617 // CodeGen turns int[] into int[1] so we'll do the same here.
1618 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001619
Devang Patel9ca36b62009-02-26 21:10:26 +00001620 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001621 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001622
Anders Carlsson20f12a22009-12-06 18:00:51 +00001623 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001624 ArrayType::Normal, 0);
1625 }
1626
Devang Patelf6a39b72009-10-20 18:26:30 +00001627 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001628 getOrCreateType(T, Unit),
1629 Var->hasInternalLinkage(),
1630 true/*definition*/, Var);
1631}
Devang Patelabb485f2010-02-01 19:16:32 +00001632
1633/// getOrCreateNamesSpace - Return namespace descriptor for the given
1634/// namespace decl.
1635llvm::DINameSpace
1636CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1637 llvm::DIDescriptor Unit) {
1638 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1639 NameSpaceCache.find(NSDecl);
1640 if (I != NameSpaceCache.end())
1641 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1642
Devang Patel8ab870d2010-05-12 23:46:38 +00001643 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patelabb485f2010-02-01 19:16:32 +00001644
1645 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00001646 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00001647 llvm::DINameSpace NS =
1648 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
Devang Patelab699792010-05-07 18:12:35 +00001649 llvm::DIFile(Unit), LineNo);
1650 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00001651 return NS;
1652}