blob: 30764875396cb5f3898ef4995a6a9374e6e68eb9 [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));
67
Devang Patel979ec2e2009-10-06 00:35:31 +000068 return CompileUnit;
69}
70
Devang Patel9c6c3a02010-01-14 00:36:21 +000071/// getFunctionName - Get function name for the given FunctionDecl. If the
72/// name is constructred on demand (e.g. C++ destructor) then the name
73/// is stored on the side.
74llvm::StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
75 assert (FD && "Invalid FunctionDecl!");
76 IdentifierInfo *FII = FD->getIdentifier();
77 if (FII)
78 return FII->getName();
79
80 // Otherwise construct human readable name for debug info.
81 std::string NS = FD->getNameAsString();
82
83 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +000084 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer1b627dc2010-01-23 18:16:07 +000085 memcpy(StrPtr, NS.data(), NS.length());
86 return llvm::StringRef(StrPtr, NS.length());
Devang Patel9c6c3a02010-01-14 00:36:21 +000087}
88
Devang Patel17800552010-03-09 00:44:50 +000089/// getOrCreateFile - Get the file debug info descriptor for the input location.
90llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Ted Kremenek9c250392010-03-30 00:27:51 +000091 if (!Loc.isValid())
Devang Patel17800552010-03-09 00:44:50 +000092 // If Location is not valid then use main input file.
93 return DebugFactory.CreateFile(TheCU.getFilename(), TheCU.getDirectory(),
94 TheCU);
Anders Carlsson20f12a22009-12-06 18:00:51 +000095 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel17800552010-03-09 00:44:50 +000096 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Ted Kremenek9c250392010-03-30 00:27:51 +000097
98 // Cache the results.
99 const char *fname = PLoc.getFilename();
100 llvm::DenseMap<const char *, llvm::WeakVH>::iterator it =
101 DIFileCache.find(fname);
102
103 if (it != DIFileCache.end()) {
104 // Verify that the information still exists.
105 if (&*it->second)
106 return llvm::DIFile(cast<llvm::MDNode>(it->second));
107 }
108
109 // FIXME: We shouldn't even need to call 'makeAbsolute()' in the cases
110 // where we can consult the FileEntry.
Devang Patel17800552010-03-09 00:44:50 +0000111 llvm::sys::Path AbsFileName(PLoc.getFilename());
Benjamin Kramer47daf682009-12-08 11:02:29 +0000112 AbsFileName.makeAbsolute();
Devang Patel446c6192009-04-17 21:06:59 +0000113
Ted Kremenek9c250392010-03-30 00:27:51 +0000114 llvm::DIFile F = DebugFactory.CreateFile(AbsFileName.getLast(),
115 AbsFileName.getDirname(), TheCU);
116
Devang Patelab699792010-05-07 18:12:35 +0000117 DIFileCache[fname] = F;
Ted Kremenek9c250392010-03-30 00:27:51 +0000118 return F;
119
Devang Patel17800552010-03-09 00:44:50 +0000120}
Devang Patel8ab870d2010-05-12 23:46:38 +0000121
122/// getLineNumber - Get line number for the location. If location is invalid
123/// then use current location.
124unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
125 assert (CurLoc.isValid() && "Invalid current location!");
126 SourceManager &SM = CGM.getContext().getSourceManager();
127 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
128 return PLoc.getLine();
129}
130
131/// getColumnNumber - Get column number for the location. If location is
132/// invalid then use current location.
133unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) {
134 assert (CurLoc.isValid() && "Invalid current location!");
135 SourceManager &SM = CGM.getContext().getSourceManager();
136 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
137 return PLoc.getColumn();
138}
139
Devang Patel17800552010-03-09 00:44:50 +0000140/// CreateCompileUnit - Create new compile unit.
141void CGDebugInfo::CreateCompileUnit() {
142
143 // Get absolute path name.
Douglas Gregorac91b4c2010-03-18 23:46:43 +0000144 SourceManager &SM = CGM.getContext().getSourceManager();
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000145 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
146 if (MainFileName.empty())
Devang Patel22fe5852010-03-12 21:04:27 +0000147 MainFileName = "<unknown>";
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000148
Devang Patel22fe5852010-03-12 21:04:27 +0000149 llvm::sys::Path AbsFileName(MainFileName);
Devang Patel17800552010-03-09 00:44:50 +0000150 AbsFileName.makeAbsolute();
Daniel Dunbarc9abc042009-04-08 05:11:16 +0000151
Douglas Gregorf6728fc2010-03-22 21:28:29 +0000152 // The main file name provided via the "-main-file-name" option contains just
153 // the file name itself with no path information. This file name may have had
154 // a relative path, so we look into the actual file entry for the main
155 // file to determine the real absolute path for the file.
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000156 std::string MainFileDir;
157 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID()))
158 MainFileDir = MainFile->getDir()->getName();
159 else
160 MainFileDir = AbsFileName.getDirname();
161
Chris Lattner515455a2009-03-25 03:28:08 +0000162 unsigned LangTag;
Devang Patel17800552010-03-09 00:44:50 +0000163 const LangOptions &LO = CGM.getLangOptions();
Chris Lattner515455a2009-03-25 03:28:08 +0000164 if (LO.CPlusPlus) {
165 if (LO.ObjC1)
166 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
167 else
168 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
169 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000170 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000171 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000172 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000173 } else {
174 LangTag = llvm::dwarf::DW_LANG_C89;
175 }
Devang Patel446c6192009-04-17 21:06:59 +0000176
Benjamin Kramer47daf682009-12-08 11:02:29 +0000177 const char *Producer =
Mike Stumpd8945d62009-10-09 18:38:12 +0000178#ifdef CLANG_VENDOR
179 CLANG_VENDOR
180#endif
181 "clang " CLANG_VERSION_STRING;
Chris Lattner4c2577a2009-05-02 01:00:04 +0000182
183 // Figure out which version of the ObjC runtime we have.
184 unsigned RuntimeVers = 0;
185 if (LO.ObjC1)
186 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000188 // Create new compile unit.
Devang Patel17800552010-03-09 00:44:50 +0000189 TheCU = DebugFactory.CreateCompileUnit(
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000190 LangTag, AbsFileName.getLast(), MainFileDir, Producer, true,
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000191 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000192}
193
Devang Patel65e99f22009-02-25 01:36:11 +0000194/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000195/// one if necessary.
196llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel17800552010-03-09 00:44:50 +0000197 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000198 unsigned Encoding = 0;
199 switch (BT->getKind()) {
200 default:
201 case BuiltinType::Void:
202 return llvm::DIType();
203 case BuiltinType::UChar:
204 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
205 case BuiltinType::Char_S:
206 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
207 case BuiltinType::UShort:
208 case BuiltinType::UInt:
209 case BuiltinType::ULong:
210 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
211 case BuiltinType::Short:
212 case BuiltinType::Int:
213 case BuiltinType::Long:
214 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
215 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
216 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000217 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000218 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000219 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000220 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000221 uint64_t Size = CGM.getContext().getTypeSize(BT);
222 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000223 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Devang Patelca80a5f2009-10-20 19:55:01 +0000225 llvm::DIType DbgTy =
226 DebugFactory.CreateBasicType(Unit,
Anders Carlsson20f12a22009-12-06 18:00:51 +0000227 BT->getName(CGM.getContext().getLangOptions()),
Devang Patelca80a5f2009-10-20 19:55:01 +0000228 Unit, 0, Size, Align,
229 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000230 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000231}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000232
Chris Lattnerb7003772009-04-23 06:13:01 +0000233llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000234 llvm::DIFile Unit) {
Chris Lattnerb7003772009-04-23 06:13:01 +0000235 // Bit size, align and offset of the type.
236 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
237 if (Ty->isComplexIntegerType())
238 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000239
Anders Carlsson20f12a22009-12-06 18:00:51 +0000240 uint64_t Size = CGM.getContext().getTypeSize(Ty);
241 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattnerb7003772009-04-23 06:13:01 +0000242 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000243
Devang Patelca80a5f2009-10-20 19:55:01 +0000244 llvm::DIType DbgTy =
245 DebugFactory.CreateBasicType(Unit, "complex",
246 Unit, 0, Size, Align,
247 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000248 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000249}
250
John McCalla1805292009-09-25 01:40:47 +0000251/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000252/// a new one if necessary.
Devang Patel17800552010-03-09 00:44:50 +0000253llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +0000254 QualifierCollector Qc;
255 const Type *T = Qc.strip(Ty);
256
257 // Ignore these qualifiers for now.
258 Qc.removeObjCGCAttr();
259 Qc.removeAddressSpace();
260
Chris Lattner9c85ba32008-11-10 06:08:34 +0000261 // We will create one Derived type for one qualifier and recurse to handle any
262 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000263 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000264 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000265 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000266 Qc.removeConst();
267 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000268 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000269 Qc.removeVolatile();
270 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000271 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000272 Qc.removeRestrict();
273 } else {
274 assert(Qc.empty() && "Unknown type qualifier for debug info");
275 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000276 }
Mike Stump1eb44332009-09-09 15:08:12 +0000277
John McCalla1805292009-09-25 01:40:47 +0000278 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
279
Daniel Dunbar3845f862008-10-31 03:54:29 +0000280 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
281 // CVR derived types.
Devang Patelca80a5f2009-10-20 19:55:01 +0000282 llvm::DIType DbgTy =
Devang Pateld58562e2010-03-09 22:49:11 +0000283 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000284 0, 0, 0, 0, 0, FromTy);
Devang Patelca80a5f2009-10-20 19:55:01 +0000285 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000286}
287
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000288llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000289 llvm::DIFile Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000290 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000291 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
292 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000293 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000294}
295
Chris Lattner9c85ba32008-11-10 06:08:34 +0000296llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000297 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000298 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
299 Ty->getPointeeType(), Unit);
300}
301
302llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
303 const Type *Ty,
304 QualType PointeeTy,
Devang Patel17800552010-03-09 00:44:50 +0000305 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000306 llvm::DIType EltTy = getOrCreateType(PointeeTy, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000308 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000309
310 // Size is always the size of a pointer. We can't use getTypeSize here
311 // because that does not return the correct value for references.
312 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000313 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
314 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Devang Patelca80a5f2009-10-20 19:55:01 +0000316 return
Devang Pateld58562e2010-03-09 22:49:11 +0000317 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000318 0, Size, Align, 0, 0, EltTy);
Anders Carlssona031b352009-11-06 19:19:55 +0000319
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000320}
321
Mike Stump9bc093c2009-05-14 02:03:51 +0000322llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000323 llvm::DIFile Unit) {
Mike Stump9bc093c2009-05-14 02:03:51 +0000324 if (BlockLiteralGenericSet)
325 return BlockLiteralGeneric;
326
Mike Stump9bc093c2009-05-14 02:03:51 +0000327 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
328
329 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
330
331 llvm::DIType FieldTy;
332
333 QualType FType;
334 uint64_t FieldSize, FieldOffset;
335 unsigned FieldAlign;
336
337 llvm::DIArray Elements;
338 llvm::DIType EltTy, DescTy;
339
340 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000341 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000342 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
343 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000344
Daniel Dunbarca308df2009-05-26 19:40:20 +0000345 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000346 EltTys.clear();
347
Mike Stump3d363c52009-10-02 02:30:50 +0000348 unsigned Flags = llvm::DIType::FlagAppleBlock;
Devang Patel8ab870d2010-05-12 23:46:38 +0000349 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump3d363c52009-10-02 02:30:50 +0000350
Mike Stump9bc093c2009-05-14 02:03:51 +0000351 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Devang Patel8ab870d2010-05-12 23:46:38 +0000352 Unit, LineNo, FieldOffset, 0, 0,
353 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Mike Stump9bc093c2009-05-14 02:03:51 +0000355 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000356 uint64_t Size = CGM.getContext().getTypeSize(Ty);
357 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Mike Stump9bc093c2009-05-14 02:03:51 +0000359 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000360 Unit, "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000361 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000362
363 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000364 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000365 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson20f12a22009-12-06 18:00:51 +0000366 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000367 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
368 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramerd3651cc2010-04-24 20:26:20 +0000369 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000370 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000371
Anders Carlsson20f12a22009-12-06 18:00:51 +0000372 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000373 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000374 FieldSize = CGM.getContext().getTypeSize(Ty);
375 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000376 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000377 "__descriptor", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000378 LineNo, FieldSize, FieldAlign,
Mike Stump9bc093c2009-05-14 02:03:51 +0000379 FieldOffset, 0, FieldTy);
380 EltTys.push_back(FieldTy);
381
382 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000383 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000384
385 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Devang Patel8ab870d2010-05-12 23:46:38 +0000386 Unit, LineNo, FieldOffset, 0, 0,
387 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Mike Stump9bc093c2009-05-14 02:03:51 +0000389 BlockLiteralGenericSet = true;
390 BlockLiteralGeneric
391 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000392 "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000393 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000394 return BlockLiteralGeneric;
395}
396
Chris Lattner9c85ba32008-11-10 06:08:34 +0000397llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000398 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000399 // Typedefs are derived from some other type. If we have a typedef of a
400 // typedef, make sure to emit the whole chain.
401 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Chris Lattner9c85ba32008-11-10 06:08:34 +0000403 // We don't set size information, but do specify where the typedef was
404 // declared.
Devang Patel8ab870d2010-05-12 23:46:38 +0000405 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000406
Devang Pateleb6d79b2010-02-01 21:34:11 +0000407 llvm::DIDescriptor TyContext
408 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
409 Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000410 llvm::DIType DbgTy =
Devang Pateld5289052010-01-29 22:29:31 +0000411 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Pateleb6d79b2010-02-01 21:34:11 +0000412 TyContext,
Devang Pateld5289052010-01-29 22:29:31 +0000413 Ty->getDecl()->getName(), Unit,
414 Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000415 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000416}
417
Chris Lattner9c85ba32008-11-10 06:08:34 +0000418llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000419 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000420 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000421
Chris Lattner9c85ba32008-11-10 06:08:34 +0000422 // Add the result type at least.
423 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Chris Lattner9c85ba32008-11-10 06:08:34 +0000425 // Set up remainder of arguments if there is a prototype.
426 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000427 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000428 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
429 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
430 } else {
431 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000432 }
433
Chris Lattner9c85ba32008-11-10 06:08:34 +0000434 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000435 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Devang Patelca80a5f2009-10-20 19:55:01 +0000437 llvm::DIType DbgTy =
438 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000439 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000440 0, 0, 0, 0, 0,
441 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000442 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000443}
444
Devang Patel428deb52010-01-19 00:00:59 +0000445/// CollectRecordFields - A helper function to collect debug info for
446/// record fields. This is used while creating debug info entry for a Record.
447void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000448CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit,
Devang Patel428deb52010-01-19 00:00:59 +0000449 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
450 unsigned FieldNo = 0;
Devang Patel239cec62010-02-01 21:39:52 +0000451 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
452 for (RecordDecl::field_iterator I = RD->field_begin(),
453 E = RD->field_end();
Devang Patel428deb52010-01-19 00:00:59 +0000454 I != E; ++I, ++FieldNo) {
455 FieldDecl *Field = *I;
456 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
457
458 llvm::StringRef FieldName = Field->getName();
459
Devang Patel4835fdd2010-02-12 01:31:06 +0000460 // Ignore unnamed fields. Do not ignore unnamed records.
461 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel428deb52010-01-19 00:00:59 +0000462 continue;
463
464 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +0000465 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
466 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel428deb52010-01-19 00:00:59 +0000467 QualType FType = Field->getType();
468 uint64_t FieldSize = 0;
469 unsigned FieldAlign = 0;
470 if (!FType->isIncompleteArrayType()) {
471
472 // Bit size, align and offset of the type.
473 FieldSize = CGM.getContext().getTypeSize(FType);
474 Expr *BitWidth = Field->getBitWidth();
475 if (BitWidth)
476 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
477
478 FieldAlign = CGM.getContext().getTypeAlign(FType);
479 }
480
481 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
482
Devang Patel71f4ff62010-04-21 23:12:37 +0000483 unsigned Flags = 0;
484 AccessSpecifier Access = I->getAccess();
485 if (Access == clang::AS_private)
486 Flags |= llvm::DIType::FlagPrivate;
487 else if (Access == clang::AS_protected)
488 Flags |= llvm::DIType::FlagProtected;
489
Devang Patel428deb52010-01-19 00:00:59 +0000490 // Create a DW_TAG_member node to remember the offset of this field in the
491 // struct. FIXME: This is an absolutely insane way to capture this
492 // information. When we gut debug info, this should be fixed.
493 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
494 FieldName, FieldDefUnit,
495 FieldLine, FieldSize, FieldAlign,
Devang Patel71f4ff62010-04-21 23:12:37 +0000496 FieldOffset, Flags, FieldTy);
Devang Patel428deb52010-01-19 00:00:59 +0000497 EltTys.push_back(FieldTy);
498 }
499}
500
Devang Patela6da1922010-01-28 00:28:01 +0000501/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
502/// function type is not updated to include implicit "this" pointer. Use this
503/// routine to get a method type which includes "this" pointer.
504llvm::DIType
505CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000506 llvm::DIFile Unit) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +0000507 llvm::DIType FnTy
508 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
509 0),
510 Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000511
512 // Static methods do not need "this" pointer argument.
513 if (Method->isStatic())
514 return FnTy;
515
Devang Patela6da1922010-01-28 00:28:01 +0000516 // Add "this" pointer.
517
Devang Patelab699792010-05-07 18:12:35 +0000518 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patela6da1922010-01-28 00:28:01 +0000519 assert (Args.getNumElements() && "Invalid number of arguments!");
520
521 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
522
523 // First element is always return type. For 'void' functions it is NULL.
524 Elts.push_back(Args.getElement(0));
525
526 // "this" pointer is always first argument.
527 ASTContext &Context = CGM.getContext();
528 QualType ThisPtr =
529 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
Devang Patel337472d2010-02-09 17:57:50 +0000530 llvm::DIType ThisPtrType =
531 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
Devang Patelab699792010-05-07 18:12:35 +0000532 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
Devang Patel337472d2010-02-09 17:57:50 +0000533 Elts.push_back(ThisPtrType);
Devang Patela6da1922010-01-28 00:28:01 +0000534
535 // Copy rest of the arguments.
536 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
537 Elts.push_back(Args.getElement(i));
538
539 llvm::DIArray EltTypeArray =
540 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
541
542 return
543 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000544 Unit, "", Unit,
Devang Patela6da1922010-01-28 00:28:01 +0000545 0, 0, 0, 0, 0,
546 llvm::DIType(), EltTypeArray);
547}
548
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000549/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
550/// a single member function GlobalDecl.
551llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000552CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000553 llvm::DIFile Unit,
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000554 llvm::DICompositeType &RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000555 bool IsCtorOrDtor =
556 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
557
558 llvm::StringRef MethodName = getFunctionName(Method);
Devang Patela6da1922010-01-28 00:28:01 +0000559 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000560
561 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
562 // make sense to give a single ctor/dtor a linkage name.
John McCallf746aa62010-03-19 23:29:14 +0000563 MangleBuffer MethodLinkageName;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000564 if (!IsCtorOrDtor)
John McCallf746aa62010-03-19 23:29:14 +0000565 CGM.getMangledName(MethodLinkageName, Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000566
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000567 // Get the location for the method.
Devang Patel8ab870d2010-05-12 23:46:38 +0000568 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
569 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000570
571 // Collect virtual method info.
572 llvm::DIType ContainingType;
573 unsigned Virtuality = 0;
574 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000575
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000576 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000577 if (Method->isPure())
578 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
579 else
580 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
581
582 // It doesn't make sense to give a virtual destructor a vtable index,
583 // since a single destructor has two entries in the vtable.
584 if (!isa<CXXDestructorDecl>(Method))
Anders Carlsson046c2942010-04-17 20:15:18 +0000585 VIndex = CGM.getVTables().getMethodVTableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000586 ContainingType = RecordTy;
587 }
588
589 llvm::DISubprogram SP =
590 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
591 MethodLinkageName,
592 MethodDefUnit, MethodLine,
593 MethodTy, /*isLocalToUnit=*/false,
594 Method->isThisDeclarationADefinition(),
595 Virtuality, VIndex, ContainingType);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000596
597 // Don't cache ctors or dtors since we have to emit multiple functions for
598 // a single ctor or dtor.
599 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
Devang Patelab699792010-05-07 18:12:35 +0000600 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000601
602 return SP;
603}
604
Devang Patel4125fd22010-01-19 01:54:44 +0000605/// CollectCXXMemberFunctions - A helper function to collect debug info for
606/// C++ member functions.This is used while creating debug info entry for
607/// a Record.
608void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000609CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000610 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
611 llvm::DICompositeType &RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000612 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
613 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000614 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000615
Devang Pateld5322da2010-02-09 19:09:28 +0000616 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000617 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000618
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000619 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000620 }
621}
622
Devang Patela245c5b2010-01-25 23:32:18 +0000623/// CollectCXXBases - A helper function to collect debug info for
624/// C++ base classes. This is used while creating debug info entry for
625/// a Record.
626void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000627CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000628 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
629 llvm::DICompositeType &RecordTy) {
630
Devang Patel239cec62010-02-01 21:39:52 +0000631 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
632 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
633 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000634 unsigned BFlags = 0;
635 uint64_t BaseOffset;
636
637 const CXXRecordDecl *Base =
638 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
639
640 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000641 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000642 // expression where it expects +ve number.
Anders Carlssonaf440352010-03-23 04:11:45 +0000643 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base);
Devang Patelca7daed2010-01-28 21:54:15 +0000644 BFlags = llvm::DIType::FlagVirtual;
645 } else
646 BaseOffset = RL.getBaseClassOffset(Base);
647
648 AccessSpecifier Access = BI->getAccessSpecifier();
649 if (Access == clang::AS_private)
650 BFlags |= llvm::DIType::FlagPrivate;
651 else if (Access == clang::AS_protected)
652 BFlags |= llvm::DIType::FlagProtected;
653
654 llvm::DIType DTy =
655 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
656 RecordTy, llvm::StringRef(),
Devang Pateld58562e2010-03-09 22:49:11 +0000657 Unit, 0, 0, 0,
Devang Patelca7daed2010-01-28 21:54:15 +0000658 BaseOffset, BFlags,
659 getOrCreateType(BI->getType(),
660 Unit));
661 EltTys.push_back(DTy);
662 }
Devang Patela245c5b2010-01-25 23:32:18 +0000663}
664
Devang Patel4ce3f202010-01-28 18:11:52 +0000665/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000666llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000667 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000668 return VTablePtrType;
669
670 ASTContext &Context = CGM.getContext();
671
672 /* Function type */
Benjamin Kramerad468862010-03-30 11:36:44 +0000673 llvm::DIDescriptor STy = getOrCreateType(Context.IntTy, Unit);
674 llvm::DIArray SElements = DebugFactory.GetOrCreateArray(&STy, 1);
Devang Patel4ce3f202010-01-28 18:11:52 +0000675 llvm::DIType SubTy =
676 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000677 Unit, "", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000678 0, 0, 0, 0, 0, llvm::DIType(), SElements);
679
680 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
681 llvm::DIType vtbl_ptr_type
682 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000683 Unit, "__vtbl_ptr_type", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000684 0, Size, 0, 0, 0, SubTy);
685
Devang Pateld58562e2010-03-09 22:49:11 +0000686 VTablePtrType =
687 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
688 Unit, "", Unit,
689 0, Size, 0, 0, 0, vtbl_ptr_type);
Devang Patel4ce3f202010-01-28 18:11:52 +0000690 return VTablePtrType;
691}
692
Anders Carlsson046c2942010-04-17 20:15:18 +0000693/// getVTableName - Get vtable name for the given Class.
694llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000695 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000696 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000697
698 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000699 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000700 memcpy(StrPtr, Name.data(), Name.length());
701 return llvm::StringRef(StrPtr, Name.length());
702}
703
704
Anders Carlsson046c2942010-04-17 20:15:18 +0000705/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel4ce3f202010-01-28 18:11:52 +0000706/// debug info entry in EltTys vector.
707void CGDebugInfo::
Anders Carlsson046c2942010-04-17 20:15:18 +0000708CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000709 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000710 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000711
712 // If there is a primary base then it will hold vtable info.
713 if (RL.getPrimaryBase())
714 return;
715
716 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000717 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000718 return;
719
720 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
721 llvm::DIType VPTR
722 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Anders Carlsson046c2942010-04-17 20:15:18 +0000723 getVTableName(RD), Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000724 0, Size, 0, 0, 0,
725 getOrCreateVTablePtrType(Unit));
726 EltTys.push_back(VPTR);
727}
728
Devang Patel65e99f22009-02-25 01:36:11 +0000729/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000730llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000731 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000732 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000733
Chris Lattner9c85ba32008-11-10 06:08:34 +0000734 unsigned Tag;
Devang Pateld6c5a262010-02-01 21:52:22 +0000735 if (RD->isStruct())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000736 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Pateld6c5a262010-02-01 21:52:22 +0000737 else if (RD->isUnion())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000738 Tag = llvm::dwarf::DW_TAG_union_type;
739 else {
Devang Pateld6c5a262010-02-01 21:52:22 +0000740 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000741 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000742 }
743
Chris Lattner9c85ba32008-11-10 06:08:34 +0000744 // Get overall information about the record type for the debug info.
Devang Patel8ab870d2010-05-12 23:46:38 +0000745 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
746 unsigned Line = getLineNumber(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000747
Chris Lattner9c85ba32008-11-10 06:08:34 +0000748 // Records and classes and unions can all be recursive. To handle them, we
749 // first generate a debug descriptor for the struct as a forward declaration.
750 // Then (if it is a definition) we go through and get debug info for all of
751 // its members. Finally, we create a descriptor for the complete type (which
752 // may refer to the forward decl if the struct is recursive) and replace all
753 // uses of the forward declaration with the final definition.
Devang Pateld0f251b2010-01-20 23:56:40 +0000754
Devang Pateld6c5a262010-02-01 21:52:22 +0000755 // A RD->getName() is not unique. However, the debug info descriptors
Devang Patelce78c972010-02-01 22:51:29 +0000756 // are uniqued so use type name to ensure uniquness.
Benjamin Kramerfea3d4d2010-03-13 12:06:51 +0000757 llvm::SmallString<128> FwdDeclName;
758 llvm::raw_svector_ostream(FwdDeclName) << "fwd.type." << FwdDeclCount++;
Devang Patel411894b2010-02-01 22:40:08 +0000759 llvm::DIDescriptor FDContext =
760 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel0ce73f62009-07-22 18:57:00 +0000761 llvm::DICompositeType FwdDecl =
Devang Patel7573f8b2010-03-09 21:32:27 +0000762 DebugFactory.CreateCompositeType(Tag, FDContext, FwdDeclName,
Devang Patelab71ff52009-11-12 00:51:46 +0000763 DefUnit, Line, 0, 0, 0, 0,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000764 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000765
Chris Lattner9c85ba32008-11-10 06:08:34 +0000766 // If this is just a forward declaration, return it.
Douglas Gregor952b0172010-02-11 01:04:33 +0000767 if (!RD->getDefinition())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000768 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000769
Devang Patelab699792010-05-07 18:12:35 +0000770 llvm::MDNode *MN = FwdDecl;
771 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000772 // Otherwise, insert it into the TypeCache so that recursive uses will find
773 // it.
Devang Patelab699792010-05-07 18:12:35 +0000774 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000775 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000776 RegionStack.push_back(FwdDeclNode);
777 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000778
779 // Convert all the elements.
780 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
781
Devang Pateld6c5a262010-02-01 21:52:22 +0000782 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000783 if (CXXDecl) {
784 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson046c2942010-04-17 20:15:18 +0000785 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000786 }
Devang Pateld6c5a262010-02-01 21:52:22 +0000787 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +0000788 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +0000789 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +0000790 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +0000791
792 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +0000793 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel0ac8f312010-01-28 00:54:21 +0000794 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
795 ContainingType =
Devang Patelab699792010-05-07 18:12:35 +0000796 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patel0ac8f312010-01-28 00:54:21 +0000797 else if (CXXDecl->isDynamicClass())
Devang Patelab699792010-05-07 18:12:35 +0000798 ContainingType = FwdDecl;
Devang Patela245c5b2010-01-25 23:32:18 +0000799 }
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Chris Lattner9c85ba32008-11-10 06:08:34 +0000801 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000802 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000803
804 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000805 uint64_t Size = CGM.getContext().getTypeSize(Ty);
806 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000807
Devang Patele4c1ea02010-03-11 20:01:48 +0000808 RegionStack.pop_back();
809 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
810 RegionMap.find(Ty->getDecl());
811 if (RI != RegionMap.end())
812 RegionMap.erase(RI);
813
Devang Patel411894b2010-02-01 22:40:08 +0000814 llvm::DIDescriptor RDContext =
815 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel0ce73f62009-07-22 18:57:00 +0000816 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +0000817 DebugFactory.CreateCompositeType(Tag, RDContext,
818 RD->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000819 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +0000820 llvm::DIType(), Elements,
821 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000822
823 // Now that we have a real decl for the struct, replace anything using the
824 // old decl with the new one. This will recursively update the debug info.
Eli Friedman14d63652009-11-16 21:04:30 +0000825 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +0000826 RegionMap[RD] = llvm::WeakVH(RealDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000827 return RealDecl;
828}
829
Devang Patel9ca36b62009-02-26 21:10:26 +0000830/// CreateType - get objective-c interface type.
831llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000832 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000833 ObjCInterfaceDecl *ID = Ty->getDecl();
Devang Patel9ca36b62009-02-26 21:10:26 +0000834 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Patel9ca36b62009-02-26 21:10:26 +0000835
836 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +0000837 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +0000838 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel17800552010-03-09 00:44:50 +0000839 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000840
Devang Patel9ca36b62009-02-26 21:10:26 +0000841 // To handle recursive interface, we
842 // first generate a debug descriptor for the struct as a forward declaration.
843 // Then (if it is a definition) we go through and get debug info for all of
844 // its members. Finally, we create a descriptor for the complete type (which
845 // may refer to the forward decl if the struct is recursive) and replace all
846 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000847 llvm::DICompositeType FwdDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +0000848 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000849 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000850 llvm::DIType(), llvm::DIArray(),
851 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Devang Patel9ca36b62009-02-26 21:10:26 +0000853 // If this is just a forward declaration, return it.
Devang Pateld6c5a262010-02-01 21:52:22 +0000854 if (ID->isForwardDecl())
Devang Patel9ca36b62009-02-26 21:10:26 +0000855 return FwdDecl;
856
Devang Patelab699792010-05-07 18:12:35 +0000857 llvm::MDNode *MN = FwdDecl;
858 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patel9ca36b62009-02-26 21:10:26 +0000859 // Otherwise, insert it into the TypeCache so that recursive uses will find
860 // it.
Devang Patelab699792010-05-07 18:12:35 +0000861 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000862 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000863 RegionStack.push_back(FwdDeclNode);
864 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000865
866 // Convert all the elements.
867 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
868
Devang Pateld6c5a262010-02-01 21:52:22 +0000869 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +0000870 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000871 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000872 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000873 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000874 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Pateld58562e2010-03-09 22:49:11 +0000875 Unit, "", Unit, 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000876 0 /* offset */, 0, SClassTy);
877 EltTys.push_back(InhTag);
878 }
879
Devang Pateld6c5a262010-02-01 21:52:22 +0000880 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +0000881
882 unsigned FieldNo = 0;
Devang Pateld6c5a262010-02-01 21:52:22 +0000883 for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(),
884 E = ID->ivar_end(); I != E; ++I, ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +0000885 ObjCIvarDecl *Field = *I;
886 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
887
Devang Patel73621622009-11-25 17:37:31 +0000888 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +0000889
Devang Patelde135022009-04-27 22:40:36 +0000890 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +0000891 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +0000892 continue;
893
Devang Patel9ca36b62009-02-26 21:10:26 +0000894 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +0000895 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
896 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel99c20eb2009-03-20 18:24:39 +0000897 QualType FType = Field->getType();
898 uint64_t FieldSize = 0;
899 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000900
Devang Patel99c20eb2009-03-20 18:24:39 +0000901 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000902
Devang Patel99c20eb2009-03-20 18:24:39 +0000903 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000904 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000905 Expr *BitWidth = Field->getBitWidth();
906 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +0000907 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000908
Anders Carlsson20f12a22009-12-06 18:00:51 +0000909 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000910 }
911
Mike Stump1eb44332009-09-09 15:08:12 +0000912 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
913
Devang Patelc20482b2009-03-19 00:23:53 +0000914 unsigned Flags = 0;
915 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
916 Flags = llvm::DIType::FlagProtected;
917 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
918 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Devang Patel9ca36b62009-02-26 21:10:26 +0000920 // Create a DW_TAG_member node to remember the offset of this field in the
921 // struct. FIXME: This is an absolutely insane way to capture this
922 // information. When we gut debug info, this should be fixed.
923 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
924 FieldName, FieldDefUnit,
925 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000926 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000927 EltTys.push_back(FieldTy);
928 }
Mike Stump1eb44332009-09-09 15:08:12 +0000929
Devang Patel9ca36b62009-02-26 21:10:26 +0000930 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000931 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +0000932
Devang Patele4c1ea02010-03-11 20:01:48 +0000933 RegionStack.pop_back();
934 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
935 RegionMap.find(Ty->getDecl());
936 if (RI != RegionMap.end())
937 RegionMap.erase(RI);
938
Devang Patel9ca36b62009-02-26 21:10:26 +0000939 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000940 uint64_t Size = CGM.getContext().getTypeSize(Ty);
941 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000942
Devang Patel6c1fddf2009-07-27 18:42:03 +0000943 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +0000944 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +0000945 Line, Size, Align, 0, 0, llvm::DIType(),
946 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000947
948 // Now that we have a real decl for the struct, replace anything using the
949 // old decl with the new one. This will recursively update the debug info.
Devang Patelffffb032009-11-16 20:09:38 +0000950 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +0000951 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000952
Devang Patel9ca36b62009-02-26 21:10:26 +0000953 return RealDecl;
954}
955
Chris Lattner9c85ba32008-11-10 06:08:34 +0000956llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000957 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000958 EnumDecl *ED = Ty->getDecl();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000959
960 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
961
962 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +0000963 for (EnumDecl::enumerator_iterator
Devang Pateld6c5a262010-02-01 21:52:22 +0000964 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000965 Enum != EnumEnd; ++Enum) {
Devang Patel73621622009-11-25 17:37:31 +0000966 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor44b43212008-12-11 16:49:14 +0000967 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000968 }
Mike Stump1eb44332009-09-09 15:08:12 +0000969
Chris Lattner9c85ba32008-11-10 06:08:34 +0000970 // Return a CompositeType for the enum itself.
971 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000972 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000973
Devang Patel8ab870d2010-05-12 23:46:38 +0000974 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
975 unsigned Line = getLineNumber(ED->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Chris Lattner9c85ba32008-11-10 06:08:34 +0000977 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +0000978 uint64_t Size = 0;
979 unsigned Align = 0;
980 if (!Ty->isIncompleteType()) {
Anders Carlsson20f12a22009-12-06 18:00:51 +0000981 Size = CGM.getContext().getTypeSize(Ty);
982 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman3189e4b2009-05-04 04:39:55 +0000983 }
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Devang Patelca80a5f2009-10-20 19:55:01 +0000985 llvm::DIType DbgTy =
986 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Pateld6c5a262010-02-01 21:52:22 +0000987 Unit, ED->getName(), DefUnit, Line,
Devang Patelca80a5f2009-10-20 19:55:01 +0000988 Size, Align, 0, 0,
989 llvm::DIType(), EltArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000990 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000991}
992
993llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000994 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000995 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
996 return CreateType(RT, Unit);
997 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
998 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000999
Chris Lattner9c85ba32008-11-10 06:08:34 +00001000 return llvm::DIType();
1001}
1002
Devang Patel70c23cd2010-02-23 22:59:39 +00001003llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001004 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001005 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1006 uint64_t NumElems = Ty->getNumElements();
1007 if (NumElems > 0)
1008 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001009
Benjamin Kramerad468862010-03-30 11:36:44 +00001010 llvm::DIDescriptor Subscript = DebugFactory.GetOrCreateSubrange(0, NumElems);
1011 llvm::DIArray SubscriptArray = DebugFactory.GetOrCreateArray(&Subscript, 1);
Devang Patel70c23cd2010-02-23 22:59:39 +00001012
1013 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1014 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1015
1016 return
1017 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001018 Unit, "", Unit,
Devang Patel70c23cd2010-02-23 22:59:39 +00001019 0, Size, Align, 0, 0,
1020 ElementTy, SubscriptArray);
1021}
1022
Chris Lattner9c85ba32008-11-10 06:08:34 +00001023llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001024 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001025 uint64_t Size;
1026 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001027
1028
Nuno Lopes010d5142009-01-28 00:35:17 +00001029 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001030 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001031 Size = 0;
1032 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001033 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001034 } else if (Ty->isIncompleteArrayType()) {
1035 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001036 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001037 } else {
1038 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001039 Size = CGM.getContext().getTypeSize(Ty);
1040 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001041 }
Mike Stump1eb44332009-09-09 15:08:12 +00001042
Chris Lattner9c85ba32008-11-10 06:08:34 +00001043 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1044 // interior arrays, do we care? Why aren't nested arrays represented the
1045 // obvious/recursive way?
1046 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1047 QualType EltTy(Ty, 0);
1048 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +00001049 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001050 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +00001051 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +00001052 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001053 // FIXME: Verify this is right for VLAs.
1054 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1055 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +00001056 }
Mike Stump1eb44332009-09-09 15:08:12 +00001057
Chris Lattner9c85ba32008-11-10 06:08:34 +00001058 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001059 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001060
Devang Patelca80a5f2009-10-20 19:55:01 +00001061 llvm::DIType DbgTy =
1062 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001063 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +00001064 0, Size, Align, 0, 0,
1065 getOrCreateType(EltTy, Unit),
1066 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001067 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001068}
1069
Anders Carlssona031b352009-11-06 19:19:55 +00001070llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001071 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001072 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1073 Ty, Ty->getPointeeType(), Unit);
1074}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001075
Anders Carlsson20f12a22009-12-06 18:00:51 +00001076llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001077 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001078 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1079 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1080
1081 if (!Ty->getPointeeType()->isFunctionType()) {
1082 // We have a data member pointer type.
1083 return PointerDiffDITy;
1084 }
1085
1086 // We have a member function pointer type. Treat it as a struct with two
1087 // ptrdiff_t members.
1088 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1089
1090 uint64_t FieldOffset = 0;
1091 llvm::DIDescriptor ElementTypes[2];
1092
1093 // FIXME: This should probably be a function type instead.
1094 ElementTypes[0] =
1095 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001096 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001097 Info.first, Info.second, FieldOffset, 0,
1098 PointerDiffDITy);
1099 FieldOffset += Info.first;
1100
1101 ElementTypes[1] =
1102 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001103 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001104 Info.first, Info.second, FieldOffset, 0,
1105 PointerDiffDITy);
1106
1107 llvm::DIArray Elements =
1108 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1109 llvm::array_lengthof(ElementTypes));
1110
1111 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1112 U, llvm::StringRef("test"),
Devang Pateld58562e2010-03-09 22:49:11 +00001113 U, 0, FieldOffset,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001114 0, 0, 0, llvm::DIType(), Elements);
1115}
1116
Douglas Gregor840943d2009-12-21 20:18:30 +00001117static QualType UnwrapTypeForDebugInfo(QualType T) {
1118 do {
1119 QualType LastT = T;
1120 switch (T->getTypeClass()) {
1121 default:
1122 return T;
1123 case Type::TemplateSpecialization:
1124 T = cast<TemplateSpecializationType>(T)->desugar();
1125 break;
1126 case Type::TypeOfExpr: {
1127 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1128 T = Ty->getUnderlyingExpr()->getType();
1129 break;
1130 }
1131 case Type::TypeOf:
1132 T = cast<TypeOfType>(T)->getUnderlyingType();
1133 break;
1134 case Type::Decltype:
1135 T = cast<DecltypeType>(T)->getUnderlyingType();
1136 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001137 case Type::Elaborated:
1138 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001139 break;
1140 case Type::SubstTemplateTypeParm:
1141 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1142 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001143 }
1144
1145 assert(T != LastT && "Type unwrapping failed to unwrap!");
1146 if (T == LastT)
1147 return T;
1148 } while (true);
1149
1150 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001151}
1152
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001153/// getOrCreateType - Get the type from the cache or create a new
1154/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001155llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001156 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001157 if (Ty.isNull())
1158 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001159
Douglas Gregor840943d2009-12-21 20:18:30 +00001160 // Unwrap the type as needed for debug information.
1161 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001162
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001163 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001164 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001165 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001166 if (it != TypeCache.end()) {
1167 // Verify that the debug info still exists.
1168 if (&*it->second)
1169 return llvm::DIType(cast<llvm::MDNode>(it->second));
1170 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001171
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001172 // Otherwise create the type.
1173 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001174
1175 // And update the type cache.
Devang Patelab699792010-05-07 18:12:35 +00001176 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001177 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001178}
1179
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001180/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001181llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001182 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001183 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001184 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001185 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001186
Douglas Gregor2101a822009-12-21 19:57:21 +00001187 const char *Diag = 0;
1188
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001189 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001190 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001191#define TYPE(Class, Base)
1192#define ABSTRACT_TYPE(Class, Base)
1193#define NON_CANONICAL_TYPE(Class, Base)
1194#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1195#include "clang/AST/TypeNodes.def"
1196 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001197
Anders Carlssonbfe69952009-11-06 18:24:04 +00001198 // FIXME: Handle these.
1199 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001200 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001201
1202 case Type::Vector:
1203 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001204 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001205 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001206 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001207 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1208 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1209 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1210 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001211 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001212 return CreateType(cast<BlockPointerType>(Ty), Unit);
1213 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001214 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001215 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001216 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001217 case Type::FunctionProto:
1218 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001219 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001220 case Type::ConstantArray:
1221 case Type::VariableArray:
1222 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001223 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001224
1225 case Type::LValueReference:
1226 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1227
Anders Carlsson20f12a22009-12-06 18:00:51 +00001228 case Type::MemberPointer:
1229 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001230
1231 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001232 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001233 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001234 case Type::TypeOfExpr:
1235 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001236 case Type::Decltype:
1237 llvm_unreachable("type should have been unwrapped!");
1238 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001239
1240 case Type::RValueReference:
1241 // FIXME: Implement!
1242 Diag = "rvalue references";
1243 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001244 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001245
1246 assert(Diag && "Fall through without a diagnostic?");
1247 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1248 "debug information for %0 is not yet supported");
1249 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1250 << Diag;
1251 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001252}
1253
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001254/// CreateMemberType - Create new member and increase Offset by FType's size.
1255llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
1256 llvm::StringRef Name,
1257 uint64_t *Offset) {
1258 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1259 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1260 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
1261 llvm::DIType Ty = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1262 Unit, Name, Unit, 0,
1263 FieldSize, FieldAlign,
1264 *Offset, 0, FieldTy);
1265 *Offset += FieldSize;
1266 return Ty;
1267}
1268
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001269/// EmitFunctionStart - Constructs the debug code for entering a function -
1270/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001271void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001272 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001273 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001274
Devang Patel9c6c3a02010-01-14 00:36:21 +00001275 llvm::StringRef Name;
John McCallf746aa62010-03-19 23:29:14 +00001276 MangleBuffer LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001277
1278 const Decl *D = GD.getDecl();
1279 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001280 // If there is a DISubprogram for this function available then use it.
1281 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1282 FI = SPCache.find(FD);
1283 if (FI != SPCache.end()) {
Devang Patel0804e6e2010-03-08 20:53:17 +00001284 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
Devang Patelab699792010-05-07 18:12:35 +00001285 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1286 llvm::MDNode *SPN = SP;
1287 RegionStack.push_back(SPN);
1288 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00001289 return;
1290 }
1291 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001292 Name = getFunctionName(FD);
1293 // Use mangled name as linkage name for c/c++ functions.
John McCallf746aa62010-03-19 23:29:14 +00001294 CGM.getMangledName(LinkageName, GD);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001295 } else {
1296 // Use llvm function name as linkage name.
1297 Name = Fn->getName();
John McCallf746aa62010-03-19 23:29:14 +00001298 LinkageName.setString(Name);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001299 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001300 if (!Name.empty() && Name[0] == '\01')
1301 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001302
Devang Patel970c6182010-04-24 00:49:16 +00001303 // It is expected that CurLoc is set before using EmitFunctionStart.
1304 // Usually, CurLoc points to the left bracket location of compound
1305 // statement representing function body.
1306 llvm::DIFile Unit = getOrCreateFile(CurLoc);
Devang Patel8ab870d2010-05-12 23:46:38 +00001307 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001308
Chris Lattner9c85ba32008-11-10 06:08:34 +00001309 llvm::DISubprogram SP =
Devang Patel970c6182010-04-24 00:49:16 +00001310 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001311 getOrCreateType(FnType, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001312 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump1eb44332009-09-09 15:08:12 +00001313
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001314 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001315 llvm::MDNode *SPN = SP;
1316 RegionStack.push_back(SPN);
1317 RegionMap[D] = llvm::WeakVH(SP);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001318}
1319
1320
Chris Lattner9c85ba32008-11-10 06:08:34 +00001321void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001322 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001323
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001324 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001325 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001326 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001327 || (SM.getInstantiationLineNumber(CurLoc) ==
1328 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001329 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patel4800ea62010-04-05 21:09:15 +00001330 // New Builder may not be in sync with CGDebugInfo.
1331 if (!Builder.getCurrentDebugLocation().isUnknown())
1332 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001333
1334 // Update last state.
1335 PrevLoc = CurLoc;
1336
Chris Lattnerc6034632010-04-01 06:31:43 +00001337 llvm::MDNode *Scope = RegionStack.back();
Devang Patel8ab870d2010-05-12 23:46:38 +00001338 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1339 getColumnNumber(CurLoc),
Chris Lattnere541d012010-04-02 20:21:43 +00001340 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001341}
1342
1343/// EmitRegionStart- Constructs the debug code for entering a declarative
1344/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +00001345void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001346 llvm::DIDescriptor D =
1347 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1348 llvm::DIDescriptor() :
Devang Pateld19429f2010-02-16 21:41:20 +00001349 llvm::DIDescriptor(RegionStack.back()),
Devang Patel8ab870d2010-05-12 23:46:38 +00001350 getLineNumber(CurLoc),
1351 getColumnNumber(CurLoc));
Devang Patelab699792010-05-07 18:12:35 +00001352 llvm::MDNode *DN = D;
1353 RegionStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001354}
1355
1356/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1357/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +00001358void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001359 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1360
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001361 // Provide an region stop point.
1362 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001363
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001364 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001365}
1366
Devang Patel809b9bb2010-02-10 18:49:08 +00001367// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1368// See BuildByRefType.
1369llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1370 uint64_t *XOffset) {
1371
1372 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1373
1374 QualType FType;
1375 uint64_t FieldSize, FieldOffset;
1376 unsigned FieldAlign;
1377
Devang Patel17800552010-03-09 00:44:50 +00001378 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001379 QualType Type = VD->getType();
1380
1381 FieldOffset = 0;
1382 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001383 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1384 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001385 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001386 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1387 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1388
Devang Patel809b9bb2010-02-10 18:49:08 +00001389 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1390 if (HasCopyAndDispose) {
1391 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001392 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1393 &FieldOffset));
1394 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1395 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001396 }
1397
1398 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1399 if (Align > CharUnits::fromQuantity(
1400 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1401 unsigned AlignedOffsetInBytes
1402 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1403 unsigned NumPaddingBytes
1404 = AlignedOffsetInBytes - FieldOffset/8;
1405
1406 if (NumPaddingBytes > 0) {
1407 llvm::APInt pad(32, NumPaddingBytes);
1408 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1409 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001410 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001411 }
1412 }
1413
1414 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001415 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00001416 FieldSize = CGM.getContext().getTypeSize(FType);
1417 FieldAlign = Align.getQuantity()*8;
1418
1419 *XOffset = FieldOffset;
1420 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001421 VD->getName(), Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001422 0, FieldSize, FieldAlign,
1423 FieldOffset, 0, FieldTy);
1424 EltTys.push_back(FieldTy);
1425 FieldOffset += FieldSize;
1426
1427 llvm::DIArray Elements =
1428 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1429
1430 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1431
1432 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001433 Unit, "", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001434 0, FieldOffset, 0, 0, Flags,
1435 llvm::DIType(), Elements);
1436
1437}
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001438/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001439void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001440 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001441 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1442
Devang Patel17800552010-03-09 00:44:50 +00001443 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001444 llvm::DIType Ty;
1445 uint64_t XOffset = 0;
1446 if (VD->hasAttr<BlocksAttr>())
1447 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1448 else
1449 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001450
Devang Patelf4e54a22010-05-07 23:05:55 +00001451 // If there is not any debug info for type then do not emit debug info
1452 // for this variable.
1453 if (!Ty)
1454 return;
1455
Chris Lattner9c85ba32008-11-10 06:08:34 +00001456 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001457 unsigned Line = getLineNumber(VD->getLocation());
1458 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001459
Chris Lattner9c85ba32008-11-10 06:08:34 +00001460 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001461 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001462 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel239cec62010-02-01 21:39:52 +00001463 VD->getName(),
Chris Lattner650cea92009-05-05 04:57:08 +00001464 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001465 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001466 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001467 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001468
Chris Lattnerc6034632010-04-01 06:31:43 +00001469 llvm::MDNode *Scope = RegionStack.back();
Chris Lattnere541d012010-04-02 20:21:43 +00001470 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001471}
1472
Mike Stumpb1a6e682009-09-30 02:43:10 +00001473/// EmitDeclare - Emit local variable declaration debug info.
1474void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1475 llvm::Value *Storage, CGBuilderTy &Builder,
1476 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001477 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001478 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1479
Devang Patel2b594b92010-04-26 23:28:46 +00001480 if (Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001481 return;
1482
1483 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001484 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001485 llvm::DIType Ty;
1486 if (VD->hasAttr<BlocksAttr>())
1487 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1488 else
1489 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001490
1491 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001492 unsigned Line = getLineNumber(VD->getLocation());
1493 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001494
Devang Pateld6c5a262010-02-01 21:52:22 +00001495 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001496 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001497 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1498 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1499 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1500 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001501 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001502 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1503 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001504 // offset of __forwarding field
1505 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001506 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1507 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1508 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001509 // offset of x field
1510 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001511 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001512 }
1513
1514 // Create the descriptor for the variable.
1515 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001516 DebugFactory.CreateComplexVariable(Tag,
1517 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001518 VD->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001519 addr);
1520 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001521 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001522 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattnerd5b89022009-12-28 21:44:41 +00001523
Chris Lattnerc6034632010-04-01 06:31:43 +00001524 llvm::MDNode *Scope = RegionStack.back();
Devang Patelf8e10a52010-05-10 23:48:38 +00001525 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001526}
1527
Devang Pateld6c5a262010-02-01 21:52:22 +00001528void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001529 llvm::Value *Storage,
1530 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001531 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001532}
1533
Mike Stumpb1a6e682009-09-30 02:43:10 +00001534void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1535 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1536 CodeGenFunction *CGF) {
1537 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1538}
1539
Chris Lattner9c85ba32008-11-10 06:08:34 +00001540/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1541/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001542void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001543 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001544 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001545}
1546
1547
1548
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001549/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001550void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001551 const VarDecl *D) {
1552
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001553 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001554 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001555 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +00001556
Devang Pateleb6d79b2010-02-01 21:34:11 +00001557 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001558 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001559
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001560 // CodeGen turns int[] into int[1] so we'll do the same here.
1561 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001562
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001563 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001564 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001565
Anders Carlsson20f12a22009-12-06 18:00:51 +00001566 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001567 ArrayType::Normal, 0);
1568 }
Devang Patel5d822f02010-04-29 17:48:37 +00001569 llvm::StringRef DeclName = D->getName();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001570 llvm::DIDescriptor DContext =
1571 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
1572 DebugFactory.CreateGlobalVariable(DContext, DeclName,
Devang Patel33583052010-01-28 23:15:27 +00001573 DeclName, llvm::StringRef(), Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001574 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001575 Var->hasInternalLinkage(),
1576 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001577}
1578
Devang Patel9ca36b62009-02-26 21:10:26 +00001579/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001580void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001581 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001582 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001583 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001584 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patel9ca36b62009-02-26 21:10:26 +00001585
Devang Pateld6c5a262010-02-01 21:52:22 +00001586 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001587
Devang Pateld6c5a262010-02-01 21:52:22 +00001588 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001589 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001590
Devang Patel9ca36b62009-02-26 21:10:26 +00001591 // CodeGen turns int[] into int[1] so we'll do the same here.
1592 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001593
Devang Patel9ca36b62009-02-26 21:10:26 +00001594 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001595 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001596
Anders Carlsson20f12a22009-12-06 18:00:51 +00001597 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001598 ArrayType::Normal, 0);
1599 }
1600
Devang Patelf6a39b72009-10-20 18:26:30 +00001601 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001602 getOrCreateType(T, Unit),
1603 Var->hasInternalLinkage(),
1604 true/*definition*/, Var);
1605}
Devang Patelabb485f2010-02-01 19:16:32 +00001606
1607/// getOrCreateNamesSpace - Return namespace descriptor for the given
1608/// namespace decl.
1609llvm::DINameSpace
1610CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1611 llvm::DIDescriptor Unit) {
1612 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1613 NameSpaceCache.find(NSDecl);
1614 if (I != NameSpaceCache.end())
1615 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1616
Devang Patel8ab870d2010-05-12 23:46:38 +00001617 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patelabb485f2010-02-01 19:16:32 +00001618
1619 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00001620 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00001621 llvm::DINameSpace NS =
1622 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
Devang Patelab699792010-05-07 18:12:35 +00001623 llvm::DIFile(Unit), LineNo);
1624 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00001625 return NS;
1626}