blob: c9bcb1b7e0437f5f118903c7470d5f9f41ef3d2e [file] [log] [blame]
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the debug information generation while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGDebugInfo.h"
Mike Stumpb1a6e682009-09-30 02:43:10 +000015#include "CodeGenFunction.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000016#include "CodeGenModule.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000017#include "clang/AST/ASTContext.h"
Devang Patel9ca36b62009-02-26 21:10:26 +000018#include "clang/AST/DeclObjC.h"
Chris Lattner3cc5c402008-11-11 07:01:36 +000019#include "clang/AST/Expr.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000020#include "clang/AST/RecordLayout.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000021#include "clang/Basic/SourceManager.h"
22#include "clang/Basic/FileManager.h"
Mike Stump5a862172009-09-15 21:48:34 +000023#include "clang/Basic/Version.h"
Chandler Carruth2811ccf2009-11-12 17:24:48 +000024#include "clang/CodeGen/CodeGenOptions.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000025#include "llvm/Constants.h"
26#include "llvm/DerivedTypes.h"
27#include "llvm/Instructions.h"
28#include "llvm/Intrinsics.h"
29#include "llvm/Module.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000030#include "llvm/ADT/StringExtras.h"
31#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000032#include "llvm/Support/Dwarf.h"
Devang Patel446c6192009-04-17 21:06:59 +000033#include "llvm/System/Path.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000034#include "llvm/Target/TargetMachine.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000035using namespace clang;
36using namespace clang::CodeGen;
37
Anders Carlsson20f12a22009-12-06 18:00:51 +000038CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
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.
John McCallf746aa62010-03-19 23:29:14 +0000570 MangleBuffer MethodLinkageName;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000571 if (!IsCtorOrDtor)
John McCallf746aa62010-03-19 23:29:14 +0000572 CGM.getMangledName(MethodLinkageName, 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 Pateld0f251b2010-01-20 23:56:40 +0000761
Devang Pateld6c5a262010-02-01 21:52:22 +0000762 // A RD->getName() is not unique. However, the debug info descriptors
Devang Patelce78c972010-02-01 22:51:29 +0000763 // are uniqued so use type name to ensure uniquness.
Benjamin Kramerfea3d4d2010-03-13 12:06:51 +0000764 llvm::SmallString<128> FwdDeclName;
765 llvm::raw_svector_ostream(FwdDeclName) << "fwd.type." << FwdDeclCount++;
Devang Patel411894b2010-02-01 22:40:08 +0000766 llvm::DIDescriptor FDContext =
767 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel0ce73f62009-07-22 18:57:00 +0000768 llvm::DICompositeType FwdDecl =
Devang Patel7573f8b2010-03-09 21:32:27 +0000769 DebugFactory.CreateCompositeType(Tag, FDContext, FwdDeclName,
Devang Patelab71ff52009-11-12 00:51:46 +0000770 DefUnit, Line, 0, 0, 0, 0,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000771 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000772
Chris Lattner9c85ba32008-11-10 06:08:34 +0000773 // If this is just a forward declaration, return it.
Douglas Gregor952b0172010-02-11 01:04:33 +0000774 if (!RD->getDefinition())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000775 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000776
Devang Patelab699792010-05-07 18:12:35 +0000777 llvm::MDNode *MN = FwdDecl;
778 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000779 // Otherwise, insert it into the TypeCache so that recursive uses will find
780 // it.
Devang Patelab699792010-05-07 18:12:35 +0000781 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000782 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000783 RegionStack.push_back(FwdDeclNode);
784 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000785
786 // Convert all the elements.
787 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
788
Devang Pateld6c5a262010-02-01 21:52:22 +0000789 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000790 if (CXXDecl) {
791 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson046c2942010-04-17 20:15:18 +0000792 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000793 }
Devang Pateld6c5a262010-02-01 21:52:22 +0000794 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +0000795 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +0000796 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +0000797 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +0000798
799 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +0000800 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel0ac8f312010-01-28 00:54:21 +0000801 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
802 ContainingType =
Devang Patelab699792010-05-07 18:12:35 +0000803 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patel0ac8f312010-01-28 00:54:21 +0000804 else if (CXXDecl->isDynamicClass())
Devang Patelab699792010-05-07 18:12:35 +0000805 ContainingType = FwdDecl;
Devang Patela245c5b2010-01-25 23:32:18 +0000806 }
Mike Stump1eb44332009-09-09 15:08:12 +0000807
Chris Lattner9c85ba32008-11-10 06:08:34 +0000808 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000809 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000810
811 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000812 uint64_t Size = CGM.getContext().getTypeSize(Ty);
813 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000814
Devang Patele4c1ea02010-03-11 20:01:48 +0000815 RegionStack.pop_back();
816 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
817 RegionMap.find(Ty->getDecl());
818 if (RI != RegionMap.end())
819 RegionMap.erase(RI);
820
Devang Patel411894b2010-02-01 22:40:08 +0000821 llvm::DIDescriptor RDContext =
822 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel0ce73f62009-07-22 18:57:00 +0000823 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +0000824 DebugFactory.CreateCompositeType(Tag, RDContext,
825 RD->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000826 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +0000827 llvm::DIType(), Elements,
828 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000829
830 // Now that we have a real decl for the struct, replace anything using the
831 // old decl with the new one. This will recursively update the debug info.
Eli Friedman14d63652009-11-16 21:04:30 +0000832 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +0000833 RegionMap[RD] = llvm::WeakVH(RealDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000834 return RealDecl;
835}
836
John McCallc12c5bb2010-05-15 11:32:37 +0000837/// CreateType - get objective-c object type.
838llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
839 llvm::DIFile Unit) {
840 // Ignore protocols.
841 return getOrCreateType(Ty->getBaseType(), Unit);
842}
843
Devang Patel9ca36b62009-02-26 21:10:26 +0000844/// CreateType - get objective-c interface type.
845llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000846 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000847 ObjCInterfaceDecl *ID = Ty->getDecl();
Devang Patel9ca36b62009-02-26 21:10:26 +0000848 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Patel9ca36b62009-02-26 21:10:26 +0000849
850 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +0000851 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +0000852 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel17800552010-03-09 00:44:50 +0000853 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000854
Devang Patel9ca36b62009-02-26 21:10:26 +0000855 // To handle recursive interface, we
856 // first generate a debug descriptor for the struct as a forward declaration.
857 // Then (if it is a definition) we go through and get debug info for all of
858 // its members. Finally, we create a descriptor for the complete type (which
859 // may refer to the forward decl if the struct is recursive) and replace all
860 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000861 llvm::DICompositeType FwdDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +0000862 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000863 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000864 llvm::DIType(), llvm::DIArray(),
865 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000866
Devang Patel9ca36b62009-02-26 21:10:26 +0000867 // If this is just a forward declaration, return it.
Devang Pateld6c5a262010-02-01 21:52:22 +0000868 if (ID->isForwardDecl())
Devang Patel9ca36b62009-02-26 21:10:26 +0000869 return FwdDecl;
870
Devang Patelab699792010-05-07 18:12:35 +0000871 llvm::MDNode *MN = FwdDecl;
872 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patel9ca36b62009-02-26 21:10:26 +0000873 // Otherwise, insert it into the TypeCache so that recursive uses will find
874 // it.
Devang Patelab699792010-05-07 18:12:35 +0000875 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000876 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000877 RegionStack.push_back(FwdDeclNode);
878 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000879
880 // Convert all the elements.
881 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
882
Devang Pateld6c5a262010-02-01 21:52:22 +0000883 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +0000884 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000885 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000886 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000887 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000888 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Pateld58562e2010-03-09 22:49:11 +0000889 Unit, "", Unit, 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000890 0 /* offset */, 0, SClassTy);
891 EltTys.push_back(InhTag);
892 }
893
Devang Pateld6c5a262010-02-01 21:52:22 +0000894 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +0000895
896 unsigned FieldNo = 0;
Devang Pateld6c5a262010-02-01 21:52:22 +0000897 for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(),
898 E = ID->ivar_end(); I != E; ++I, ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +0000899 ObjCIvarDecl *Field = *I;
900 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
901
Devang Patel73621622009-11-25 17:37:31 +0000902 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +0000903
Devang Patelde135022009-04-27 22:40:36 +0000904 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +0000905 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +0000906 continue;
907
Devang Patel9ca36b62009-02-26 21:10:26 +0000908 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +0000909 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
910 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel99c20eb2009-03-20 18:24:39 +0000911 QualType FType = Field->getType();
912 uint64_t FieldSize = 0;
913 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000914
Devang Patel99c20eb2009-03-20 18:24:39 +0000915 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Devang Patel99c20eb2009-03-20 18:24:39 +0000917 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000918 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000919 Expr *BitWidth = Field->getBitWidth();
920 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +0000921 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000922
Anders Carlsson20f12a22009-12-06 18:00:51 +0000923 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000924 }
925
Mike Stump1eb44332009-09-09 15:08:12 +0000926 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
927
Devang Patelc20482b2009-03-19 00:23:53 +0000928 unsigned Flags = 0;
929 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
930 Flags = llvm::DIType::FlagProtected;
931 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
932 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Devang Patel9ca36b62009-02-26 21:10:26 +0000934 // Create a DW_TAG_member node to remember the offset of this field in the
935 // struct. FIXME: This is an absolutely insane way to capture this
936 // information. When we gut debug info, this should be fixed.
937 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
938 FieldName, FieldDefUnit,
939 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000940 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000941 EltTys.push_back(FieldTy);
942 }
Mike Stump1eb44332009-09-09 15:08:12 +0000943
Devang Patel9ca36b62009-02-26 21:10:26 +0000944 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000945 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +0000946
Devang Patele4c1ea02010-03-11 20:01:48 +0000947 RegionStack.pop_back();
948 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
949 RegionMap.find(Ty->getDecl());
950 if (RI != RegionMap.end())
951 RegionMap.erase(RI);
952
Devang Patel9ca36b62009-02-26 21:10:26 +0000953 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000954 uint64_t Size = CGM.getContext().getTypeSize(Ty);
955 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000956
Devang Patel6c1fddf2009-07-27 18:42:03 +0000957 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +0000958 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +0000959 Line, Size, Align, 0, 0, llvm::DIType(),
960 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000961
962 // Now that we have a real decl for the struct, replace anything using the
963 // old decl with the new one. This will recursively update the debug info.
Devang Patelffffb032009-11-16 20:09:38 +0000964 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +0000965 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000966
Devang Patel9ca36b62009-02-26 21:10:26 +0000967 return RealDecl;
968}
969
Chris Lattner9c85ba32008-11-10 06:08:34 +0000970llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000971 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000972 EnumDecl *ED = Ty->getDecl();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000973
974 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
975
976 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +0000977 for (EnumDecl::enumerator_iterator
Devang Pateld6c5a262010-02-01 21:52:22 +0000978 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000979 Enum != EnumEnd; ++Enum) {
Devang Patel73621622009-11-25 17:37:31 +0000980 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor44b43212008-12-11 16:49:14 +0000981 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000982 }
Mike Stump1eb44332009-09-09 15:08:12 +0000983
Chris Lattner9c85ba32008-11-10 06:08:34 +0000984 // Return a CompositeType for the enum itself.
985 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000986 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000987
Devang Patel8ab870d2010-05-12 23:46:38 +0000988 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
989 unsigned Line = getLineNumber(ED->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000990
Chris Lattner9c85ba32008-11-10 06:08:34 +0000991 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +0000992 uint64_t Size = 0;
993 unsigned Align = 0;
994 if (!Ty->isIncompleteType()) {
Anders Carlsson20f12a22009-12-06 18:00:51 +0000995 Size = CGM.getContext().getTypeSize(Ty);
996 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman3189e4b2009-05-04 04:39:55 +0000997 }
Mike Stump1eb44332009-09-09 15:08:12 +0000998
Devang Patelca80a5f2009-10-20 19:55:01 +0000999 llvm::DIType DbgTy =
1000 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Pateld6c5a262010-02-01 21:52:22 +00001001 Unit, ED->getName(), DefUnit, Line,
Devang Patelca80a5f2009-10-20 19:55:01 +00001002 Size, Align, 0, 0,
1003 llvm::DIType(), EltArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001004 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001005}
1006
1007llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001008 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001009 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1010 return CreateType(RT, Unit);
1011 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1012 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001013
Chris Lattner9c85ba32008-11-10 06:08:34 +00001014 return llvm::DIType();
1015}
1016
Devang Patel70c23cd2010-02-23 22:59:39 +00001017llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001018 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001019 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1020 uint64_t NumElems = Ty->getNumElements();
1021 if (NumElems > 0)
1022 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001023
Benjamin Kramerad468862010-03-30 11:36:44 +00001024 llvm::DIDescriptor Subscript = DebugFactory.GetOrCreateSubrange(0, NumElems);
1025 llvm::DIArray SubscriptArray = DebugFactory.GetOrCreateArray(&Subscript, 1);
Devang Patel70c23cd2010-02-23 22:59:39 +00001026
1027 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1028 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1029
1030 return
1031 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001032 Unit, "", Unit,
Devang Patel70c23cd2010-02-23 22:59:39 +00001033 0, Size, Align, 0, 0,
1034 ElementTy, SubscriptArray);
1035}
1036
Chris Lattner9c85ba32008-11-10 06:08:34 +00001037llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001038 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001039 uint64_t Size;
1040 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001041
1042
Nuno Lopes010d5142009-01-28 00:35:17 +00001043 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001044 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001045 Size = 0;
1046 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001047 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001048 } else if (Ty->isIncompleteArrayType()) {
1049 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001050 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001051 } else {
1052 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001053 Size = CGM.getContext().getTypeSize(Ty);
1054 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001055 }
Mike Stump1eb44332009-09-09 15:08:12 +00001056
Chris Lattner9c85ba32008-11-10 06:08:34 +00001057 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1058 // interior arrays, do we care? Why aren't nested arrays represented the
1059 // obvious/recursive way?
1060 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1061 QualType EltTy(Ty, 0);
1062 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +00001063 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001064 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +00001065 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +00001066 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001067 // FIXME: Verify this is right for VLAs.
1068 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1069 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +00001070 }
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Chris Lattner9c85ba32008-11-10 06:08:34 +00001072 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001073 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001074
Devang Patelca80a5f2009-10-20 19:55:01 +00001075 llvm::DIType DbgTy =
1076 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001077 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +00001078 0, Size, Align, 0, 0,
1079 getOrCreateType(EltTy, Unit),
1080 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001081 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001082}
1083
Anders Carlssona031b352009-11-06 19:19:55 +00001084llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001085 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001086 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1087 Ty, Ty->getPointeeType(), Unit);
1088}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001089
Anders Carlsson20f12a22009-12-06 18:00:51 +00001090llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001091 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001092 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1093 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1094
1095 if (!Ty->getPointeeType()->isFunctionType()) {
1096 // We have a data member pointer type.
1097 return PointerDiffDITy;
1098 }
1099
1100 // We have a member function pointer type. Treat it as a struct with two
1101 // ptrdiff_t members.
1102 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1103
1104 uint64_t FieldOffset = 0;
1105 llvm::DIDescriptor ElementTypes[2];
1106
1107 // FIXME: This should probably be a function type instead.
1108 ElementTypes[0] =
1109 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001110 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001111 Info.first, Info.second, FieldOffset, 0,
1112 PointerDiffDITy);
1113 FieldOffset += Info.first;
1114
1115 ElementTypes[1] =
1116 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001117 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001118 Info.first, Info.second, FieldOffset, 0,
1119 PointerDiffDITy);
1120
1121 llvm::DIArray Elements =
1122 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1123 llvm::array_lengthof(ElementTypes));
1124
1125 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1126 U, llvm::StringRef("test"),
Devang Pateld58562e2010-03-09 22:49:11 +00001127 U, 0, FieldOffset,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001128 0, 0, 0, llvm::DIType(), Elements);
1129}
1130
Douglas Gregor840943d2009-12-21 20:18:30 +00001131static QualType UnwrapTypeForDebugInfo(QualType T) {
1132 do {
1133 QualType LastT = T;
1134 switch (T->getTypeClass()) {
1135 default:
1136 return T;
1137 case Type::TemplateSpecialization:
1138 T = cast<TemplateSpecializationType>(T)->desugar();
1139 break;
1140 case Type::TypeOfExpr: {
1141 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1142 T = Ty->getUnderlyingExpr()->getType();
1143 break;
1144 }
1145 case Type::TypeOf:
1146 T = cast<TypeOfType>(T)->getUnderlyingType();
1147 break;
1148 case Type::Decltype:
1149 T = cast<DecltypeType>(T)->getUnderlyingType();
1150 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001151 case Type::Elaborated:
1152 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001153 break;
1154 case Type::SubstTemplateTypeParm:
1155 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1156 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001157 }
1158
1159 assert(T != LastT && "Type unwrapping failed to unwrap!");
1160 if (T == LastT)
1161 return T;
1162 } while (true);
1163
1164 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001165}
1166
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001167/// getOrCreateType - Get the type from the cache or create a new
1168/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001169llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001170 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001171 if (Ty.isNull())
1172 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001173
Douglas Gregor840943d2009-12-21 20:18:30 +00001174 // Unwrap the type as needed for debug information.
1175 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001176
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001177 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001178 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001179 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001180 if (it != TypeCache.end()) {
1181 // Verify that the debug info still exists.
1182 if (&*it->second)
1183 return llvm::DIType(cast<llvm::MDNode>(it->second));
1184 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001185
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001186 // Otherwise create the type.
1187 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001188
1189 // And update the type cache.
Devang Patelab699792010-05-07 18:12:35 +00001190 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001191 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001192}
1193
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001194/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001195llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001196 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001197 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001198 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001199 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001200
Douglas Gregor2101a822009-12-21 19:57:21 +00001201 const char *Diag = 0;
1202
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001203 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001204 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001205#define TYPE(Class, Base)
1206#define ABSTRACT_TYPE(Class, Base)
1207#define NON_CANONICAL_TYPE(Class, Base)
1208#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1209#include "clang/AST/TypeNodes.def"
1210 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001211
Anders Carlssonbfe69952009-11-06 18:24:04 +00001212 // FIXME: Handle these.
1213 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001214 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001215
1216 case Type::Vector:
1217 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001218 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001219 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCallc12c5bb2010-05-15 11:32:37 +00001220 case Type::ObjCObject:
1221 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001222 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001223 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1224 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1225 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1226 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001227 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001228 return CreateType(cast<BlockPointerType>(Ty), Unit);
1229 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001230 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001231 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001232 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001233 case Type::FunctionProto:
1234 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001235 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001236 case Type::ConstantArray:
1237 case Type::VariableArray:
1238 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001239 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001240
1241 case Type::LValueReference:
1242 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1243
Anders Carlsson20f12a22009-12-06 18:00:51 +00001244 case Type::MemberPointer:
1245 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001246
1247 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001248 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001249 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001250 case Type::TypeOfExpr:
1251 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001252 case Type::Decltype:
1253 llvm_unreachable("type should have been unwrapped!");
1254 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001255
1256 case Type::RValueReference:
1257 // FIXME: Implement!
1258 Diag = "rvalue references";
1259 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001260 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001261
1262 assert(Diag && "Fall through without a diagnostic?");
1263 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1264 "debug information for %0 is not yet supported");
1265 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1266 << Diag;
1267 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001268}
1269
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001270/// CreateMemberType - Create new member and increase Offset by FType's size.
1271llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
1272 llvm::StringRef Name,
1273 uint64_t *Offset) {
1274 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1275 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1276 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
1277 llvm::DIType Ty = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1278 Unit, Name, Unit, 0,
1279 FieldSize, FieldAlign,
1280 *Offset, 0, FieldTy);
1281 *Offset += FieldSize;
1282 return Ty;
1283}
1284
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001285/// EmitFunctionStart - Constructs the debug code for entering a function -
1286/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001287void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001288 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001289 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001290
Devang Patel9c6c3a02010-01-14 00:36:21 +00001291 llvm::StringRef Name;
John McCallf746aa62010-03-19 23:29:14 +00001292 MangleBuffer LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001293
1294 const Decl *D = GD.getDecl();
1295 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001296 // If there is a DISubprogram for this function available then use it.
1297 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1298 FI = SPCache.find(FD);
1299 if (FI != SPCache.end()) {
Devang Patel0804e6e2010-03-08 20:53:17 +00001300 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
Devang Patelab699792010-05-07 18:12:35 +00001301 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1302 llvm::MDNode *SPN = SP;
1303 RegionStack.push_back(SPN);
1304 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00001305 return;
1306 }
1307 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001308 Name = getFunctionName(FD);
1309 // Use mangled name as linkage name for c/c++ functions.
John McCallf746aa62010-03-19 23:29:14 +00001310 CGM.getMangledName(LinkageName, GD);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001311 } else {
1312 // Use llvm function name as linkage name.
1313 Name = Fn->getName();
John McCallf746aa62010-03-19 23:29:14 +00001314 LinkageName.setString(Name);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001315 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001316 if (!Name.empty() && Name[0] == '\01')
1317 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001318
Devang Patel970c6182010-04-24 00:49:16 +00001319 // It is expected that CurLoc is set before using EmitFunctionStart.
1320 // Usually, CurLoc points to the left bracket location of compound
1321 // statement representing function body.
1322 llvm::DIFile Unit = getOrCreateFile(CurLoc);
Devang Patel8ab870d2010-05-12 23:46:38 +00001323 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001324
Chris Lattner9c85ba32008-11-10 06:08:34 +00001325 llvm::DISubprogram SP =
Devang Patel970c6182010-04-24 00:49:16 +00001326 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001327 getOrCreateType(FnType, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001328 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump1eb44332009-09-09 15:08:12 +00001329
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001330 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001331 llvm::MDNode *SPN = SP;
1332 RegionStack.push_back(SPN);
1333 RegionMap[D] = llvm::WeakVH(SP);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001334}
1335
1336
Chris Lattner9c85ba32008-11-10 06:08:34 +00001337void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001338 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001339
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001340 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001341 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001342 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001343 || (SM.getInstantiationLineNumber(CurLoc) ==
1344 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001345 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patel4800ea62010-04-05 21:09:15 +00001346 // New Builder may not be in sync with CGDebugInfo.
1347 if (!Builder.getCurrentDebugLocation().isUnknown())
1348 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001349
1350 // Update last state.
1351 PrevLoc = CurLoc;
1352
Chris Lattnerc6034632010-04-01 06:31:43 +00001353 llvm::MDNode *Scope = RegionStack.back();
Devang Patel8ab870d2010-05-12 23:46:38 +00001354 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1355 getColumnNumber(CurLoc),
Chris Lattnere541d012010-04-02 20:21:43 +00001356 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001357}
1358
1359/// EmitRegionStart- Constructs the debug code for entering a declarative
1360/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +00001361void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001362 llvm::DIDescriptor D =
1363 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1364 llvm::DIDescriptor() :
Devang Pateld19429f2010-02-16 21:41:20 +00001365 llvm::DIDescriptor(RegionStack.back()),
Devang Patel8ab870d2010-05-12 23:46:38 +00001366 getLineNumber(CurLoc),
1367 getColumnNumber(CurLoc));
Devang Patelab699792010-05-07 18:12:35 +00001368 llvm::MDNode *DN = D;
1369 RegionStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001370}
1371
1372/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1373/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +00001374void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001375 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1376
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001377 // Provide an region stop point.
1378 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001379
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001380 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001381}
1382
Devang Patel809b9bb2010-02-10 18:49:08 +00001383// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1384// See BuildByRefType.
1385llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1386 uint64_t *XOffset) {
1387
1388 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1389
1390 QualType FType;
1391 uint64_t FieldSize, FieldOffset;
1392 unsigned FieldAlign;
1393
Devang Patel17800552010-03-09 00:44:50 +00001394 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001395 QualType Type = VD->getType();
1396
1397 FieldOffset = 0;
1398 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001399 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1400 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001401 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001402 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1403 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1404
Devang Patel809b9bb2010-02-10 18:49:08 +00001405 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1406 if (HasCopyAndDispose) {
1407 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001408 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1409 &FieldOffset));
1410 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1411 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001412 }
1413
1414 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1415 if (Align > CharUnits::fromQuantity(
1416 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1417 unsigned AlignedOffsetInBytes
1418 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1419 unsigned NumPaddingBytes
1420 = AlignedOffsetInBytes - FieldOffset/8;
1421
1422 if (NumPaddingBytes > 0) {
1423 llvm::APInt pad(32, NumPaddingBytes);
1424 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1425 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001426 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001427 }
1428 }
1429
1430 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001431 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00001432 FieldSize = CGM.getContext().getTypeSize(FType);
1433 FieldAlign = Align.getQuantity()*8;
1434
1435 *XOffset = FieldOffset;
1436 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001437 VD->getName(), Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001438 0, FieldSize, FieldAlign,
1439 FieldOffset, 0, FieldTy);
1440 EltTys.push_back(FieldTy);
1441 FieldOffset += FieldSize;
1442
1443 llvm::DIArray Elements =
1444 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1445
1446 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1447
1448 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001449 Unit, "", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001450 0, FieldOffset, 0, 0, Flags,
1451 llvm::DIType(), Elements);
1452
1453}
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001454/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001455void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001456 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001457 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1458
Devang Patel17800552010-03-09 00:44:50 +00001459 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001460 llvm::DIType Ty;
1461 uint64_t XOffset = 0;
1462 if (VD->hasAttr<BlocksAttr>())
1463 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1464 else
1465 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001466
Devang Patelf4e54a22010-05-07 23:05:55 +00001467 // If there is not any debug info for type then do not emit debug info
1468 // for this variable.
1469 if (!Ty)
1470 return;
1471
Chris Lattner9c85ba32008-11-10 06:08:34 +00001472 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001473 unsigned Line = getLineNumber(VD->getLocation());
1474 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001475
Chris Lattner9c85ba32008-11-10 06:08:34 +00001476 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001477 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001478 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel239cec62010-02-01 21:39:52 +00001479 VD->getName(),
Chris Lattner650cea92009-05-05 04:57:08 +00001480 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001481 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001482 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001483 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001484
Chris Lattnerc6034632010-04-01 06:31:43 +00001485 llvm::MDNode *Scope = RegionStack.back();
Chris Lattnere541d012010-04-02 20:21:43 +00001486 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001487}
1488
Mike Stumpb1a6e682009-09-30 02:43:10 +00001489/// EmitDeclare - Emit local variable declaration debug info.
1490void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1491 llvm::Value *Storage, CGBuilderTy &Builder,
1492 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001493 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001494 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1495
Devang Patel2b594b92010-04-26 23:28:46 +00001496 if (Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001497 return;
1498
1499 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001500 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001501 llvm::DIType Ty;
1502 if (VD->hasAttr<BlocksAttr>())
1503 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1504 else
1505 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001506
1507 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001508 unsigned Line = getLineNumber(VD->getLocation());
1509 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001510
Devang Pateld6c5a262010-02-01 21:52:22 +00001511 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001512 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001513 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1514 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1515 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1516 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001517 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001518 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1519 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001520 // offset of __forwarding field
1521 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001522 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1523 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1524 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001525 // offset of x field
1526 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001527 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001528 }
1529
1530 // Create the descriptor for the variable.
1531 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001532 DebugFactory.CreateComplexVariable(Tag,
1533 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001534 VD->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001535 addr);
1536 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001537 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001538 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattnerd5b89022009-12-28 21:44:41 +00001539
Chris Lattnerc6034632010-04-01 06:31:43 +00001540 llvm::MDNode *Scope = RegionStack.back();
Devang Patelf8e10a52010-05-10 23:48:38 +00001541 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001542}
1543
Devang Pateld6c5a262010-02-01 21:52:22 +00001544void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001545 llvm::Value *Storage,
1546 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001547 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001548}
1549
Mike Stumpb1a6e682009-09-30 02:43:10 +00001550void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1551 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1552 CodeGenFunction *CGF) {
1553 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1554}
1555
Chris Lattner9c85ba32008-11-10 06:08:34 +00001556/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1557/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001558void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001559 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001560 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001561}
1562
1563
1564
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001565/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001566void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001567 const VarDecl *D) {
1568
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001569 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001570 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001571 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +00001572
Devang Pateleb6d79b2010-02-01 21:34:11 +00001573 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001574 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001575
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001576 // CodeGen turns int[] into int[1] so we'll do the same here.
1577 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001578
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001579 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001580 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001581
Anders Carlsson20f12a22009-12-06 18:00:51 +00001582 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001583 ArrayType::Normal, 0);
1584 }
Devang Patel5d822f02010-04-29 17:48:37 +00001585 llvm::StringRef DeclName = D->getName();
Devang Patel8b90a782010-05-13 23:52:37 +00001586 llvm::StringRef LinkageName;
Devang Patel0fd3d1f2010-05-14 16:55:25 +00001587 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()))
Devang Patel8b90a782010-05-13 23:52:37 +00001588 LinkageName = Var->getName();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001589 llvm::DIDescriptor DContext =
1590 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
Devang Patel8b90a782010-05-13 23:52:37 +00001591 DebugFactory.CreateGlobalVariable(DContext, DeclName, DeclName, LinkageName,
1592 Unit, LineNo, getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001593 Var->hasInternalLinkage(),
1594 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001595}
1596
Devang Patel9ca36b62009-02-26 21:10:26 +00001597/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001598void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001599 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001600 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001601 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001602 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patel9ca36b62009-02-26 21:10:26 +00001603
Devang Pateld6c5a262010-02-01 21:52:22 +00001604 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001605
Devang Pateld6c5a262010-02-01 21:52:22 +00001606 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001607 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001608
Devang Patel9ca36b62009-02-26 21:10:26 +00001609 // CodeGen turns int[] into int[1] so we'll do the same here.
1610 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001611
Devang Patel9ca36b62009-02-26 21:10:26 +00001612 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001613 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001614
Anders Carlsson20f12a22009-12-06 18:00:51 +00001615 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001616 ArrayType::Normal, 0);
1617 }
1618
Devang Patelf6a39b72009-10-20 18:26:30 +00001619 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001620 getOrCreateType(T, Unit),
1621 Var->hasInternalLinkage(),
1622 true/*definition*/, Var);
1623}
Devang Patelabb485f2010-02-01 19:16:32 +00001624
1625/// getOrCreateNamesSpace - Return namespace descriptor for the given
1626/// namespace decl.
1627llvm::DINameSpace
1628CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1629 llvm::DIDescriptor Unit) {
1630 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1631 NameSpaceCache.find(NSDecl);
1632 if (I != NameSpaceCache.end())
1633 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1634
Devang Patel8ab870d2010-05-12 23:46:38 +00001635 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patelabb485f2010-02-01 19:16:32 +00001636
1637 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00001638 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00001639 llvm::DINameSpace NS =
1640 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
Devang Patelab699792010-05-07 18:12:35 +00001641 llvm::DIFile(Unit), LineNo);
1642 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00001643 return NS;
1644}