blob: 8e9f416d4fbda7cdca71c48b17b450af40f3d312 [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"
Devang Patel700a1cb2010-07-20 20:24:18 +000019#include "clang/AST/DeclTemplate.h"
Chris Lattner3cc5c402008-11-11 07:01:36 +000020#include "clang/AST/Expr.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000022#include "clang/Basic/SourceManager.h"
23#include "clang/Basic/FileManager.h"
Mike Stump5a862172009-09-15 21:48:34 +000024#include "clang/Basic/Version.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000025#include "clang/Frontend/CodeGenOptions.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000026#include "llvm/Constants.h"
27#include "llvm/DerivedTypes.h"
28#include "llvm/Instructions.h"
29#include "llvm/Intrinsics.h"
30#include "llvm/Module.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000031#include "llvm/ADT/StringExtras.h"
32#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000033#include "llvm/Support/Dwarf.h"
Devang Patel446c6192009-04-17 21:06:59 +000034#include "llvm/System/Path.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000035#include "llvm/Target/TargetMachine.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000036using namespace clang;
37using namespace clang::CodeGen;
38
Anders Carlsson20f12a22009-12-06 18:00:51 +000039CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Devang Patel17800552010-03-09 00:44:50 +000040 : CGM(CGM), DebugFactory(CGM.getModule()),
Devang Patel7573f8b2010-03-09 21:32:27 +000041 FwdDeclCount(0), BlockLiteralGenericSet(false) {
Devang Patel17800552010-03-09 00:44:50 +000042 CreateCompileUnit();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000043}
44
Chris Lattner9c85ba32008-11-10 06:08:34 +000045CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar66031a52008-10-17 16:15:48 +000046 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000047}
48
Chris Lattner9c85ba32008-11-10 06:08:34 +000049void CGDebugInfo::setLocation(SourceLocation Loc) {
50 if (Loc.isValid())
Anders Carlsson20f12a22009-12-06 18:00:51 +000051 CurLoc = CGM.getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000052}
53
Devang Patel33583052010-01-28 23:15:27 +000054/// getContextDescriptor - Get context info for the decl.
Devang Pateleb6d79b2010-02-01 21:34:11 +000055llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context,
Devang Patel33583052010-01-28 23:15:27 +000056 llvm::DIDescriptor &CompileUnit) {
Devang Pateleb6d79b2010-02-01 21:34:11 +000057 if (!Context)
58 return CompileUnit;
59
60 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
61 I = RegionMap.find(Context);
62 if (I != RegionMap.end())
63 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(I->second));
Devang Patel411894b2010-02-01 22:40:08 +000064
Devang Pateleb6d79b2010-02-01 21:34:11 +000065 // Check namespace.
66 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
67 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl, CompileUnit));
Devang Patel8b90a782010-05-13 23:52:37 +000068
69 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context)) {
70 if (!RDecl->isDependentType()) {
71 llvm::DIType Ty = getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
72 llvm::DIFile(CompileUnit));
73 return llvm::DIDescriptor(Ty);
74 }
75 }
Devang Patel979ec2e2009-10-06 00:35:31 +000076 return CompileUnit;
77}
78
Devang Patel9c6c3a02010-01-14 00:36:21 +000079/// getFunctionName - Get function name for the given FunctionDecl. If the
80/// name is constructred on demand (e.g. C++ destructor) then the name
81/// is stored on the side.
82llvm::StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
83 assert (FD && "Invalid FunctionDecl!");
84 IdentifierInfo *FII = FD->getIdentifier();
85 if (FII)
86 return FII->getName();
87
88 // Otherwise construct human readable name for debug info.
89 std::string NS = FD->getNameAsString();
90
91 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +000092 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer1b627dc2010-01-23 18:16:07 +000093 memcpy(StrPtr, NS.data(), NS.length());
94 return llvm::StringRef(StrPtr, NS.length());
Devang Patel9c6c3a02010-01-14 00:36:21 +000095}
96
Devang Patel700a1cb2010-07-20 20:24:18 +000097/// getClassName - Get class name including template argument list.
98llvm::StringRef
99CGDebugInfo::getClassName(RecordDecl *RD) {
100 ClassTemplateSpecializationDecl *Spec
101 = dyn_cast<ClassTemplateSpecializationDecl>(RD);
102 if (!Spec)
103 return RD->getName();
104
105 const TemplateArgument *Args;
106 unsigned NumArgs;
107 std::string Buffer;
108 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
109 const TemplateSpecializationType *TST =
110 cast<TemplateSpecializationType>(TAW->getType());
111 Args = TST->getArgs();
112 NumArgs = TST->getNumArgs();
113 } else {
114 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
115 Args = TemplateArgs.getFlatArgumentList();
116 NumArgs = TemplateArgs.flat_size();
117 }
118 Buffer = RD->getIdentifier()->getNameStart();
119 PrintingPolicy Policy(CGM.getLangOptions());
120 Buffer += TemplateSpecializationType::PrintTemplateArgumentList(Args,
121 NumArgs,
122 Policy);
123
124 // Copy this name on the side and use its reference.
125 char *StrPtr = DebugInfoNames.Allocate<char>(Buffer.length());
126 memcpy(StrPtr, Buffer.data(), Buffer.length());
127 return llvm::StringRef(StrPtr, Buffer.length());
128
129}
130
Devang Patel17800552010-03-09 00:44:50 +0000131/// getOrCreateFile - Get the file debug info descriptor for the input location.
132llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Ted Kremenek9c250392010-03-30 00:27:51 +0000133 if (!Loc.isValid())
Devang Patel17800552010-03-09 00:44:50 +0000134 // If Location is not valid then use main input file.
135 return DebugFactory.CreateFile(TheCU.getFilename(), TheCU.getDirectory(),
136 TheCU);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000137 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel17800552010-03-09 00:44:50 +0000138 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Ted Kremenek9c250392010-03-30 00:27:51 +0000139
140 // Cache the results.
141 const char *fname = PLoc.getFilename();
142 llvm::DenseMap<const char *, llvm::WeakVH>::iterator it =
143 DIFileCache.find(fname);
144
145 if (it != DIFileCache.end()) {
146 // Verify that the information still exists.
147 if (&*it->second)
148 return llvm::DIFile(cast<llvm::MDNode>(it->second));
149 }
150
Devang Patelac4d13c2010-07-27 15:17:16 +0000151 llvm::DIFile F = DebugFactory.CreateFile(PLoc.getFilename(),
152 getCurrentDirname(), TheCU);
Ted Kremenek9c250392010-03-30 00:27:51 +0000153
Devang Patelab699792010-05-07 18:12:35 +0000154 DIFileCache[fname] = F;
Ted Kremenek9c250392010-03-30 00:27:51 +0000155 return F;
156
Devang Patel17800552010-03-09 00:44:50 +0000157}
Devang Patel8ab870d2010-05-12 23:46:38 +0000158
159/// getLineNumber - Get line number for the location. If location is invalid
160/// then use current location.
161unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
162 assert (CurLoc.isValid() && "Invalid current location!");
163 SourceManager &SM = CGM.getContext().getSourceManager();
164 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
165 return PLoc.getLine();
166}
167
168/// getColumnNumber - Get column number for the location. If location is
169/// invalid then use current location.
170unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) {
171 assert (CurLoc.isValid() && "Invalid current location!");
172 SourceManager &SM = CGM.getContext().getSourceManager();
173 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
174 return PLoc.getColumn();
175}
176
Devang Patelac4d13c2010-07-27 15:17:16 +0000177llvm::StringRef CGDebugInfo::getCurrentDirname() {
178 if (!CWDName.empty())
179 return CWDName;
180 char *CompDirnamePtr = NULL;
181 llvm::sys::Path CWD = llvm::sys::Path::GetCurrentDirectory();
182 CompDirnamePtr = DebugInfoNames.Allocate<char>(CWD.size());
183 memcpy(CompDirnamePtr, CWD.c_str(), CWD.size());
184 return CWDName = llvm::StringRef(CompDirnamePtr, CWD.size());
185}
186
Devang Patel17800552010-03-09 00:44:50 +0000187/// CreateCompileUnit - Create new compile unit.
188void CGDebugInfo::CreateCompileUnit() {
189
190 // Get absolute path name.
Douglas Gregorac91b4c2010-03-18 23:46:43 +0000191 SourceManager &SM = CGM.getContext().getSourceManager();
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000192 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
193 if (MainFileName.empty())
Devang Patel22fe5852010-03-12 21:04:27 +0000194 MainFileName = "<unknown>";
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000195
Douglas Gregorf6728fc2010-03-22 21:28:29 +0000196 // The main file name provided via the "-main-file-name" option contains just
197 // the file name itself with no path information. This file name may have had
198 // a relative path, so we look into the actual file entry for the main
199 // file to determine the real absolute path for the file.
Devang Patel6e6bc392010-07-23 23:04:28 +0000200 std::string MainFileDir;
Devang Patelac4d13c2010-07-27 15:17:16 +0000201 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000202 MainFileDir = MainFile->getDir()->getName();
Devang Patelac4d13c2010-07-27 15:17:16 +0000203 if (MainFileDir != ".")
204 MainFileName = MainFileDir + "/" + MainFileName;
205 }
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000206
Devang Patelac4d13c2010-07-27 15:17:16 +0000207 // Save filename string.
208 char *FilenamePtr = DebugInfoNames.Allocate<char>(MainFileName.length());
209 memcpy(FilenamePtr, MainFileName.c_str(), MainFileName.length());
210 llvm::StringRef Filename(FilenamePtr, MainFileName.length());
211
Chris Lattner515455a2009-03-25 03:28:08 +0000212 unsigned LangTag;
Devang Patel17800552010-03-09 00:44:50 +0000213 const LangOptions &LO = CGM.getLangOptions();
Chris Lattner515455a2009-03-25 03:28:08 +0000214 if (LO.CPlusPlus) {
215 if (LO.ObjC1)
216 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
217 else
218 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
219 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000220 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000221 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000222 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000223 } else {
224 LangTag = llvm::dwarf::DW_LANG_C89;
225 }
Devang Patel446c6192009-04-17 21:06:59 +0000226
Benjamin Kramer47daf682009-12-08 11:02:29 +0000227 const char *Producer =
Mike Stumpd8945d62009-10-09 18:38:12 +0000228#ifdef CLANG_VENDOR
229 CLANG_VENDOR
230#endif
231 "clang " CLANG_VERSION_STRING;
Chris Lattner4c2577a2009-05-02 01:00:04 +0000232
233 // Figure out which version of the ObjC runtime we have.
234 unsigned RuntimeVers = 0;
235 if (LO.ObjC1)
236 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000238 // Create new compile unit.
Devang Patel17800552010-03-09 00:44:50 +0000239 TheCU = DebugFactory.CreateCompileUnit(
Devang Patel58115002010-07-27 20:49:59 +0000240 LangTag, Filename, getCurrentDirname(),
Devang Patelac4d13c2010-07-27 15:17:16 +0000241 Producer, true,
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000242 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000243}
244
Devang Patel65e99f22009-02-25 01:36:11 +0000245/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000246/// one if necessary.
247llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel17800552010-03-09 00:44:50 +0000248 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000249 unsigned Encoding = 0;
250 switch (BT->getKind()) {
251 default:
252 case BuiltinType::Void:
253 return llvm::DIType();
Devang Pateleaf5ee92010-07-21 22:41:25 +0000254 case BuiltinType::ObjCId:
255 // id is struct objc_object *.
256 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
257 Unit, "objc_object", Unit, 0, 0, 0, 0,
258 llvm::DIType::FlagFwdDecl,
259 llvm::DIType(), llvm::DIArray());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000260 case BuiltinType::UChar:
261 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
262 case BuiltinType::Char_S:
263 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
264 case BuiltinType::UShort:
265 case BuiltinType::UInt:
266 case BuiltinType::ULong:
267 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
268 case BuiltinType::Short:
269 case BuiltinType::Int:
270 case BuiltinType::Long:
271 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
272 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
273 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000274 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000275 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000276 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000277 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000278 uint64_t Size = CGM.getContext().getTypeSize(BT);
279 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000280 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Devang Patelca80a5f2009-10-20 19:55:01 +0000282 llvm::DIType DbgTy =
283 DebugFactory.CreateBasicType(Unit,
Anders Carlsson20f12a22009-12-06 18:00:51 +0000284 BT->getName(CGM.getContext().getLangOptions()),
Devang Patelca80a5f2009-10-20 19:55:01 +0000285 Unit, 0, Size, Align,
286 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000287 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000288}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000289
Chris Lattnerb7003772009-04-23 06:13:01 +0000290llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000291 llvm::DIFile Unit) {
Chris Lattnerb7003772009-04-23 06:13:01 +0000292 // Bit size, align and offset of the type.
293 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
294 if (Ty->isComplexIntegerType())
295 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Anders Carlsson20f12a22009-12-06 18:00:51 +0000297 uint64_t Size = CGM.getContext().getTypeSize(Ty);
298 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattnerb7003772009-04-23 06:13:01 +0000299 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Devang Patelca80a5f2009-10-20 19:55:01 +0000301 llvm::DIType DbgTy =
302 DebugFactory.CreateBasicType(Unit, "complex",
303 Unit, 0, Size, Align,
304 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000305 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000306}
307
John McCalla1805292009-09-25 01:40:47 +0000308/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000309/// a new one if necessary.
Devang Patel17800552010-03-09 00:44:50 +0000310llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +0000311 QualifierCollector Qc;
312 const Type *T = Qc.strip(Ty);
313
314 // Ignore these qualifiers for now.
315 Qc.removeObjCGCAttr();
316 Qc.removeAddressSpace();
317
Chris Lattner9c85ba32008-11-10 06:08:34 +0000318 // We will create one Derived type for one qualifier and recurse to handle any
319 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000320 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000321 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000322 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000323 Qc.removeConst();
324 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000325 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000326 Qc.removeVolatile();
327 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000328 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000329 Qc.removeRestrict();
330 } else {
331 assert(Qc.empty() && "Unknown type qualifier for debug info");
332 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000333 }
Mike Stump1eb44332009-09-09 15:08:12 +0000334
John McCalla1805292009-09-25 01:40:47 +0000335 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
336
Daniel Dunbar3845f862008-10-31 03:54:29 +0000337 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
338 // CVR derived types.
Devang Patelca80a5f2009-10-20 19:55:01 +0000339 llvm::DIType DbgTy =
Devang Pateld58562e2010-03-09 22:49:11 +0000340 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000341 0, 0, 0, 0, 0, FromTy);
Devang Patelca80a5f2009-10-20 19:55:01 +0000342 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000343}
344
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000345llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000346 llvm::DIFile Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000347 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000348 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
349 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000350 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000351}
352
Chris Lattner9c85ba32008-11-10 06:08:34 +0000353llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000354 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000355 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
356 Ty->getPointeeType(), Unit);
357}
358
359llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
360 const Type *Ty,
361 QualType PointeeTy,
Devang Patel17800552010-03-09 00:44:50 +0000362 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000363 llvm::DIType EltTy = getOrCreateType(PointeeTy, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000365 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000366
367 // Size is always the size of a pointer. We can't use getTypeSize here
368 // because that does not return the correct value for references.
369 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000370 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
371 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Devang Patelca80a5f2009-10-20 19:55:01 +0000373 return
Devang Pateld58562e2010-03-09 22:49:11 +0000374 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000375 0, Size, Align, 0, 0, EltTy);
Anders Carlssona031b352009-11-06 19:19:55 +0000376
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000377}
378
Mike Stump9bc093c2009-05-14 02:03:51 +0000379llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000380 llvm::DIFile Unit) {
Mike Stump9bc093c2009-05-14 02:03:51 +0000381 if (BlockLiteralGenericSet)
382 return BlockLiteralGeneric;
383
Mike Stump9bc093c2009-05-14 02:03:51 +0000384 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
385
386 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
387
388 llvm::DIType FieldTy;
389
390 QualType FType;
391 uint64_t FieldSize, FieldOffset;
392 unsigned FieldAlign;
393
394 llvm::DIArray Elements;
395 llvm::DIType EltTy, DescTy;
396
397 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000398 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000399 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
400 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000401
Daniel Dunbarca308df2009-05-26 19:40:20 +0000402 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000403 EltTys.clear();
404
Mike Stump3d363c52009-10-02 02:30:50 +0000405 unsigned Flags = llvm::DIType::FlagAppleBlock;
Devang Patel8ab870d2010-05-12 23:46:38 +0000406 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump3d363c52009-10-02 02:30:50 +0000407
Mike Stump9bc093c2009-05-14 02:03:51 +0000408 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Devang Patel8ab870d2010-05-12 23:46:38 +0000409 Unit, LineNo, FieldOffset, 0, 0,
410 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Mike Stump9bc093c2009-05-14 02:03:51 +0000412 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000413 uint64_t Size = CGM.getContext().getTypeSize(Ty);
414 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Mike Stump9bc093c2009-05-14 02:03:51 +0000416 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000417 Unit, "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000418 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000419
420 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000421 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000422 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson20f12a22009-12-06 18:00:51 +0000423 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000424 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
425 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramerd3651cc2010-04-24 20:26:20 +0000426 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000427 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000428
Anders Carlsson20f12a22009-12-06 18:00:51 +0000429 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000430 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000431 FieldSize = CGM.getContext().getTypeSize(Ty);
432 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000433 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000434 "__descriptor", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000435 LineNo, FieldSize, FieldAlign,
Mike Stump9bc093c2009-05-14 02:03:51 +0000436 FieldOffset, 0, FieldTy);
437 EltTys.push_back(FieldTy);
438
439 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000440 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000441
442 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Devang Patel8ab870d2010-05-12 23:46:38 +0000443 Unit, LineNo, FieldOffset, 0, 0,
444 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Mike Stump9bc093c2009-05-14 02:03:51 +0000446 BlockLiteralGenericSet = true;
447 BlockLiteralGeneric
448 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000449 "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000450 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000451 return BlockLiteralGeneric;
452}
453
Chris Lattner9c85ba32008-11-10 06:08:34 +0000454llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000455 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000456 // Typedefs are derived from some other type. If we have a typedef of a
457 // typedef, make sure to emit the whole chain.
458 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Chris Lattner9c85ba32008-11-10 06:08:34 +0000460 // We don't set size information, but do specify where the typedef was
461 // declared.
Devang Patel8ab870d2010-05-12 23:46:38 +0000462 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000463
Devang Pateleb6d79b2010-02-01 21:34:11 +0000464 llvm::DIDescriptor TyContext
465 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
466 Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000467 llvm::DIType DbgTy =
Devang Pateld5289052010-01-29 22:29:31 +0000468 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Pateleb6d79b2010-02-01 21:34:11 +0000469 TyContext,
Devang Pateld5289052010-01-29 22:29:31 +0000470 Ty->getDecl()->getName(), Unit,
471 Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000472 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000473}
474
Chris Lattner9c85ba32008-11-10 06:08:34 +0000475llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000476 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000477 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000478
Chris Lattner9c85ba32008-11-10 06:08:34 +0000479 // Add the result type at least.
480 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Chris Lattner9c85ba32008-11-10 06:08:34 +0000482 // Set up remainder of arguments if there is a prototype.
483 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000484 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000485 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
486 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
487 } else {
488 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000489 }
490
Chris Lattner9c85ba32008-11-10 06:08:34 +0000491 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000492 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Devang Patelca80a5f2009-10-20 19:55:01 +0000494 llvm::DIType DbgTy =
495 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000496 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000497 0, 0, 0, 0, 0,
498 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000499 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000500}
501
Devang Patel428deb52010-01-19 00:00:59 +0000502/// CollectRecordFields - A helper function to collect debug info for
503/// record fields. This is used while creating debug info entry for a Record.
504void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000505CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit,
Devang Patel428deb52010-01-19 00:00:59 +0000506 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
507 unsigned FieldNo = 0;
Devang Patel239cec62010-02-01 21:39:52 +0000508 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
509 for (RecordDecl::field_iterator I = RD->field_begin(),
510 E = RD->field_end();
Devang Patel428deb52010-01-19 00:00:59 +0000511 I != E; ++I, ++FieldNo) {
512 FieldDecl *Field = *I;
513 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
514
515 llvm::StringRef FieldName = Field->getName();
516
Devang Patel4835fdd2010-02-12 01:31:06 +0000517 // Ignore unnamed fields. Do not ignore unnamed records.
518 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel428deb52010-01-19 00:00:59 +0000519 continue;
520
521 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +0000522 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
523 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel428deb52010-01-19 00:00:59 +0000524 QualType FType = Field->getType();
525 uint64_t FieldSize = 0;
526 unsigned FieldAlign = 0;
527 if (!FType->isIncompleteArrayType()) {
528
529 // Bit size, align and offset of the type.
530 FieldSize = CGM.getContext().getTypeSize(FType);
531 Expr *BitWidth = Field->getBitWidth();
532 if (BitWidth)
533 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
534
535 FieldAlign = CGM.getContext().getTypeAlign(FType);
536 }
537
538 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
539
Devang Patel71f4ff62010-04-21 23:12:37 +0000540 unsigned Flags = 0;
541 AccessSpecifier Access = I->getAccess();
542 if (Access == clang::AS_private)
543 Flags |= llvm::DIType::FlagPrivate;
544 else if (Access == clang::AS_protected)
545 Flags |= llvm::DIType::FlagProtected;
546
Devang Patel428deb52010-01-19 00:00:59 +0000547 // Create a DW_TAG_member node to remember the offset of this field in the
548 // struct. FIXME: This is an absolutely insane way to capture this
549 // information. When we gut debug info, this should be fixed.
550 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
551 FieldName, FieldDefUnit,
552 FieldLine, FieldSize, FieldAlign,
Devang Patel71f4ff62010-04-21 23:12:37 +0000553 FieldOffset, Flags, FieldTy);
Devang Patel428deb52010-01-19 00:00:59 +0000554 EltTys.push_back(FieldTy);
555 }
556}
557
Devang Patela6da1922010-01-28 00:28:01 +0000558/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
559/// function type is not updated to include implicit "this" pointer. Use this
560/// routine to get a method type which includes "this" pointer.
561llvm::DIType
562CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000563 llvm::DIFile Unit) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +0000564 llvm::DIType FnTy
565 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
566 0),
567 Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000568
569 // Static methods do not need "this" pointer argument.
570 if (Method->isStatic())
571 return FnTy;
572
Devang Patela6da1922010-01-28 00:28:01 +0000573 // Add "this" pointer.
574
Devang Patelab699792010-05-07 18:12:35 +0000575 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patela6da1922010-01-28 00:28:01 +0000576 assert (Args.getNumElements() && "Invalid number of arguments!");
577
578 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
579
580 // First element is always return type. For 'void' functions it is NULL.
581 Elts.push_back(Args.getElement(0));
582
583 // "this" pointer is always first argument.
584 ASTContext &Context = CGM.getContext();
585 QualType ThisPtr =
586 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
Devang Patel337472d2010-02-09 17:57:50 +0000587 llvm::DIType ThisPtrType =
588 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
Devang Patel769640e2010-07-13 00:24:30 +0000589
Devang Patelab699792010-05-07 18:12:35 +0000590 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
Devang Patel337472d2010-02-09 17:57:50 +0000591 Elts.push_back(ThisPtrType);
Devang Patela6da1922010-01-28 00:28:01 +0000592
593 // Copy rest of the arguments.
594 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
595 Elts.push_back(Args.getElement(i));
596
597 llvm::DIArray EltTypeArray =
598 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
599
600 return
601 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000602 Unit, "", Unit,
Devang Patela6da1922010-01-28 00:28:01 +0000603 0, 0, 0, 0, 0,
604 llvm::DIType(), EltTypeArray);
605}
606
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000607/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
608/// a single member function GlobalDecl.
609llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000610CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000611 llvm::DIFile Unit,
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000612 llvm::DICompositeType &RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000613 bool IsCtorOrDtor =
614 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
615
616 llvm::StringRef MethodName = getFunctionName(Method);
Devang Patela6da1922010-01-28 00:28:01 +0000617 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000618
619 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
620 // make sense to give a single ctor/dtor a linkage name.
Anders Carlsson9a20d552010-06-22 16:16:50 +0000621 llvm::StringRef MethodLinkageName;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000622 if (!IsCtorOrDtor)
Anders Carlsson9a20d552010-06-22 16:16:50 +0000623 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000624
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000625 // Get the location for the method.
Devang Patel8ab870d2010-05-12 23:46:38 +0000626 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
627 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000628
629 // Collect virtual method info.
630 llvm::DIType ContainingType;
631 unsigned Virtuality = 0;
632 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000633
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000634 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000635 if (Method->isPure())
636 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
637 else
638 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
639
640 // It doesn't make sense to give a virtual destructor a vtable index,
641 // since a single destructor has two entries in the vtable.
642 if (!isa<CXXDestructorDecl>(Method))
Anders Carlsson046c2942010-04-17 20:15:18 +0000643 VIndex = CGM.getVTables().getMethodVTableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000644 ContainingType = RecordTy;
645 }
646
647 llvm::DISubprogram SP =
648 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
649 MethodLinkageName,
650 MethodDefUnit, MethodLine,
651 MethodTy, /*isLocalToUnit=*/false,
Devang Patela5c5bab2010-07-12 22:54:41 +0000652 /* isDefintion=*/ false,
Devang Patel40894912010-07-15 22:57:00 +0000653 Virtuality, VIndex, ContainingType,
Devang Patel15a3d7d2010-07-15 23:09:46 +0000654 Method->isImplicit(),
655 CGM.getLangOptions().Optimize);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000656
657 // Don't cache ctors or dtors since we have to emit multiple functions for
658 // a single ctor or dtor.
659 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
Devang Patelab699792010-05-07 18:12:35 +0000660 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000661
662 return SP;
663}
664
Devang Patel4125fd22010-01-19 01:54:44 +0000665/// CollectCXXMemberFunctions - A helper function to collect debug info for
666/// C++ member functions.This is used while creating debug info entry for
667/// a Record.
668void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000669CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000670 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
671 llvm::DICompositeType &RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000672 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
673 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000674 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000675
Devang Pateld5322da2010-02-09 19:09:28 +0000676 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000677 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000678
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000679 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000680 }
681}
682
Devang Patela245c5b2010-01-25 23:32:18 +0000683/// CollectCXXBases - A helper function to collect debug info for
684/// C++ base classes. This is used while creating debug info entry for
685/// a Record.
686void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000687CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000688 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
689 llvm::DICompositeType &RecordTy) {
690
Devang Patel239cec62010-02-01 21:39:52 +0000691 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
692 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
693 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000694 unsigned BFlags = 0;
695 uint64_t BaseOffset;
696
697 const CXXRecordDecl *Base =
698 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
699
700 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000701 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000702 // expression where it expects +ve number.
Anders Carlssonaf440352010-03-23 04:11:45 +0000703 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base);
Devang Patelca7daed2010-01-28 21:54:15 +0000704 BFlags = llvm::DIType::FlagVirtual;
705 } else
706 BaseOffset = RL.getBaseClassOffset(Base);
707
708 AccessSpecifier Access = BI->getAccessSpecifier();
709 if (Access == clang::AS_private)
710 BFlags |= llvm::DIType::FlagPrivate;
711 else if (Access == clang::AS_protected)
712 BFlags |= llvm::DIType::FlagProtected;
713
714 llvm::DIType DTy =
715 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
716 RecordTy, llvm::StringRef(),
Devang Pateld58562e2010-03-09 22:49:11 +0000717 Unit, 0, 0, 0,
Devang Patelca7daed2010-01-28 21:54:15 +0000718 BaseOffset, BFlags,
719 getOrCreateType(BI->getType(),
720 Unit));
721 EltTys.push_back(DTy);
722 }
Devang Patela245c5b2010-01-25 23:32:18 +0000723}
724
Devang Patel4ce3f202010-01-28 18:11:52 +0000725/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000726llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000727 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000728 return VTablePtrType;
729
730 ASTContext &Context = CGM.getContext();
731
732 /* Function type */
Benjamin Kramerad468862010-03-30 11:36:44 +0000733 llvm::DIDescriptor STy = getOrCreateType(Context.IntTy, Unit);
734 llvm::DIArray SElements = DebugFactory.GetOrCreateArray(&STy, 1);
Devang Patel4ce3f202010-01-28 18:11:52 +0000735 llvm::DIType SubTy =
736 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000737 Unit, "", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000738 0, 0, 0, 0, 0, llvm::DIType(), SElements);
739
740 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
741 llvm::DIType vtbl_ptr_type
742 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000743 Unit, "__vtbl_ptr_type", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000744 0, Size, 0, 0, 0, SubTy);
745
Devang Pateld58562e2010-03-09 22:49:11 +0000746 VTablePtrType =
747 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
748 Unit, "", Unit,
749 0, Size, 0, 0, 0, vtbl_ptr_type);
Devang Patel4ce3f202010-01-28 18:11:52 +0000750 return VTablePtrType;
751}
752
Anders Carlsson046c2942010-04-17 20:15:18 +0000753/// getVTableName - Get vtable name for the given Class.
754llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000755 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000756 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000757
758 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000759 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000760 memcpy(StrPtr, Name.data(), Name.length());
761 return llvm::StringRef(StrPtr, Name.length());
762}
763
764
Anders Carlsson046c2942010-04-17 20:15:18 +0000765/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel4ce3f202010-01-28 18:11:52 +0000766/// debug info entry in EltTys vector.
767void CGDebugInfo::
Anders Carlsson046c2942010-04-17 20:15:18 +0000768CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000769 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000770 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000771
772 // If there is a primary base then it will hold vtable info.
773 if (RL.getPrimaryBase())
774 return;
775
776 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000777 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000778 return;
779
780 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
781 llvm::DIType VPTR
782 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Anders Carlsson046c2942010-04-17 20:15:18 +0000783 getVTableName(RD), Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000784 0, Size, 0, 0, 0,
785 getOrCreateVTablePtrType(Unit));
786 EltTys.push_back(VPTR);
787}
788
Devang Patel65e99f22009-02-25 01:36:11 +0000789/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000790llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000791 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000792 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Chris Lattner9c85ba32008-11-10 06:08:34 +0000794 unsigned Tag;
Devang Pateld6c5a262010-02-01 21:52:22 +0000795 if (RD->isStruct())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000796 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Pateld6c5a262010-02-01 21:52:22 +0000797 else if (RD->isUnion())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000798 Tag = llvm::dwarf::DW_TAG_union_type;
799 else {
Devang Pateld6c5a262010-02-01 21:52:22 +0000800 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000801 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000802 }
803
Chris Lattner9c85ba32008-11-10 06:08:34 +0000804 // Get overall information about the record type for the debug info.
Devang Patel8ab870d2010-05-12 23:46:38 +0000805 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
806 unsigned Line = getLineNumber(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000807
Chris Lattner9c85ba32008-11-10 06:08:34 +0000808 // Records and classes and unions can all be recursive. To handle them, we
809 // first generate a debug descriptor for the struct as a forward declaration.
810 // Then (if it is a definition) we go through and get debug info for all of
811 // its members. Finally, we create a descriptor for the complete type (which
812 // may refer to the forward decl if the struct is recursive) and replace all
813 // uses of the forward declaration with the final definition.
Devang Patel0b897992010-07-08 19:56:29 +0000814 llvm::DIDescriptor FDContext =
815 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
816
817 // If this is just a forward declaration, construct an appropriately
818 // marked node and just return it.
819 if (!RD->getDefinition()) {
820 llvm::DICompositeType FwdDecl =
821 DebugFactory.CreateCompositeType(Tag, FDContext, RD->getName(),
822 DefUnit, Line, 0, 0, 0,
823 llvm::DIType::FlagFwdDecl,
824 llvm::DIType(), llvm::DIArray());
825
826 return FwdDecl;
827 }
Devang Pateld0f251b2010-01-20 23:56:40 +0000828
Devang Pateld6c5a262010-02-01 21:52:22 +0000829 // A RD->getName() is not unique. However, the debug info descriptors
Devang Patelce78c972010-02-01 22:51:29 +0000830 // are uniqued so use type name to ensure uniquness.
Benjamin Kramerfea3d4d2010-03-13 12:06:51 +0000831 llvm::SmallString<128> FwdDeclName;
832 llvm::raw_svector_ostream(FwdDeclName) << "fwd.type." << FwdDeclCount++;
Devang Patel0ce73f62009-07-22 18:57:00 +0000833 llvm::DICompositeType FwdDecl =
Devang Patel7573f8b2010-03-09 21:32:27 +0000834 DebugFactory.CreateCompositeType(Tag, FDContext, FwdDeclName,
Devang Patelab71ff52009-11-12 00:51:46 +0000835 DefUnit, Line, 0, 0, 0, 0,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000836 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Devang Patelab699792010-05-07 18:12:35 +0000838 llvm::MDNode *MN = FwdDecl;
839 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000840 // Otherwise, insert it into the TypeCache so that recursive uses will find
841 // it.
Devang Patelab699792010-05-07 18:12:35 +0000842 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000843 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000844 RegionStack.push_back(FwdDeclNode);
845 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000846
847 // Convert all the elements.
848 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
849
Devang Pateld6c5a262010-02-01 21:52:22 +0000850 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000851 if (CXXDecl) {
852 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson046c2942010-04-17 20:15:18 +0000853 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000854 }
Devang Pateld6c5a262010-02-01 21:52:22 +0000855 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +0000856 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +0000857 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +0000858 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +0000859
860 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +0000861 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel0ac8f312010-01-28 00:54:21 +0000862 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
863 ContainingType =
Devang Patelab699792010-05-07 18:12:35 +0000864 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patel0ac8f312010-01-28 00:54:21 +0000865 else if (CXXDecl->isDynamicClass())
Devang Patelab699792010-05-07 18:12:35 +0000866 ContainingType = FwdDecl;
Devang Patela245c5b2010-01-25 23:32:18 +0000867 }
Mike Stump1eb44332009-09-09 15:08:12 +0000868
Chris Lattner9c85ba32008-11-10 06:08:34 +0000869 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000870 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000871
872 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000873 uint64_t Size = CGM.getContext().getTypeSize(Ty);
874 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Devang Patele4c1ea02010-03-11 20:01:48 +0000876 RegionStack.pop_back();
877 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
878 RegionMap.find(Ty->getDecl());
879 if (RI != RegionMap.end())
880 RegionMap.erase(RI);
881
Devang Patel411894b2010-02-01 22:40:08 +0000882 llvm::DIDescriptor RDContext =
883 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel700a1cb2010-07-20 20:24:18 +0000884
885 llvm::StringRef RDName = RD->getName();
886 // If this is a class, include the template arguments also.
887 if (Tag == llvm::dwarf::DW_TAG_class_type)
888 RDName = getClassName(RD);
889
Devang Patel0ce73f62009-07-22 18:57:00 +0000890 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +0000891 DebugFactory.CreateCompositeType(Tag, RDContext,
Devang Patel700a1cb2010-07-20 20:24:18 +0000892 RDName,
Devang Patelab71ff52009-11-12 00:51:46 +0000893 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +0000894 llvm::DIType(), Elements,
895 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000896
897 // Now that we have a real decl for the struct, replace anything using the
898 // old decl with the new one. This will recursively update the debug info.
Eli Friedman14d63652009-11-16 21:04:30 +0000899 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +0000900 RegionMap[RD] = llvm::WeakVH(RealDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000901 return RealDecl;
902}
903
John McCallc12c5bb2010-05-15 11:32:37 +0000904/// CreateType - get objective-c object type.
905llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
906 llvm::DIFile Unit) {
907 // Ignore protocols.
908 return getOrCreateType(Ty->getBaseType(), Unit);
909}
910
Devang Patel9ca36b62009-02-26 21:10:26 +0000911/// CreateType - get objective-c interface type.
912llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000913 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000914 ObjCInterfaceDecl *ID = Ty->getDecl();
Devang Patel9ca36b62009-02-26 21:10:26 +0000915 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Patel9ca36b62009-02-26 21:10:26 +0000916
917 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +0000918 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +0000919 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel17800552010-03-09 00:44:50 +0000920 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000921
Devang Patel9ca36b62009-02-26 21:10:26 +0000922 // To handle recursive interface, we
923 // first generate a debug descriptor for the struct as a forward declaration.
924 // Then (if it is a definition) we go through and get debug info for all of
925 // its members. Finally, we create a descriptor for the complete type (which
926 // may refer to the forward decl if the struct is recursive) and replace all
927 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000928 llvm::DICompositeType FwdDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +0000929 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000930 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000931 llvm::DIType(), llvm::DIArray(),
932 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Devang Patel9ca36b62009-02-26 21:10:26 +0000934 // If this is just a forward declaration, return it.
Devang Pateld6c5a262010-02-01 21:52:22 +0000935 if (ID->isForwardDecl())
Devang Patel9ca36b62009-02-26 21:10:26 +0000936 return FwdDecl;
937
Devang Patelab699792010-05-07 18:12:35 +0000938 llvm::MDNode *MN = FwdDecl;
939 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patel9ca36b62009-02-26 21:10:26 +0000940 // Otherwise, insert it into the TypeCache so that recursive uses will find
941 // it.
Devang Patelab699792010-05-07 18:12:35 +0000942 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000943 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000944 RegionStack.push_back(FwdDeclNode);
945 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000946
947 // Convert all the elements.
948 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
949
Devang Pateld6c5a262010-02-01 21:52:22 +0000950 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +0000951 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000952 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000953 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000954 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000955 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Pateld58562e2010-03-09 22:49:11 +0000956 Unit, "", Unit, 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000957 0 /* offset */, 0, SClassTy);
958 EltTys.push_back(InhTag);
959 }
960
Devang Pateld6c5a262010-02-01 21:52:22 +0000961 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +0000962
963 unsigned FieldNo = 0;
Devang Pateld6c5a262010-02-01 21:52:22 +0000964 for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(),
965 E = ID->ivar_end(); I != E; ++I, ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +0000966 ObjCIvarDecl *Field = *I;
967 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
968
Devang Patel73621622009-11-25 17:37:31 +0000969 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +0000970
Devang Patelde135022009-04-27 22:40:36 +0000971 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +0000972 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +0000973 continue;
974
Devang Patel9ca36b62009-02-26 21:10:26 +0000975 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +0000976 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
977 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel99c20eb2009-03-20 18:24:39 +0000978 QualType FType = Field->getType();
979 uint64_t FieldSize = 0;
980 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000981
Devang Patel99c20eb2009-03-20 18:24:39 +0000982 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000983
Devang Patel99c20eb2009-03-20 18:24:39 +0000984 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000985 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000986 Expr *BitWidth = Field->getBitWidth();
987 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +0000988 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000989
Anders Carlsson20f12a22009-12-06 18:00:51 +0000990 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000991 }
992
Mike Stump1eb44332009-09-09 15:08:12 +0000993 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
994
Devang Patelc20482b2009-03-19 00:23:53 +0000995 unsigned Flags = 0;
996 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
997 Flags = llvm::DIType::FlagProtected;
998 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
999 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +00001000
Devang Patel9ca36b62009-02-26 21:10:26 +00001001 // Create a DW_TAG_member node to remember the offset of this field in the
1002 // struct. FIXME: This is an absolutely insane way to capture this
1003 // information. When we gut debug info, this should be fixed.
1004 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1005 FieldName, FieldDefUnit,
1006 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +00001007 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +00001008 EltTys.push_back(FieldTy);
1009 }
Mike Stump1eb44332009-09-09 15:08:12 +00001010
Devang Patel9ca36b62009-02-26 21:10:26 +00001011 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +00001012 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +00001013
Devang Patele4c1ea02010-03-11 20:01:48 +00001014 RegionStack.pop_back();
1015 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1016 RegionMap.find(Ty->getDecl());
1017 if (RI != RegionMap.end())
1018 RegionMap.erase(RI);
1019
Devang Patel9ca36b62009-02-26 21:10:26 +00001020 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001021 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1022 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001023
Devang Patel6c1fddf2009-07-27 18:42:03 +00001024 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +00001025 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +00001026 Line, Size, Align, 0, 0, llvm::DIType(),
1027 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +00001028
1029 // Now that we have a real decl for the struct, replace anything using the
1030 // old decl with the new one. This will recursively update the debug info.
Devang Patelffffb032009-11-16 20:09:38 +00001031 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001032 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +00001033
Devang Patel9ca36b62009-02-26 21:10:26 +00001034 return RealDecl;
1035}
1036
Chris Lattner9c85ba32008-11-10 06:08:34 +00001037llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001038 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001039 EnumDecl *ED = Ty->getDecl();
Chris Lattner9c85ba32008-11-10 06:08:34 +00001040
1041 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
1042
1043 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +00001044 for (EnumDecl::enumerator_iterator
Devang Pateld6c5a262010-02-01 21:52:22 +00001045 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +00001046 Enum != EnumEnd; ++Enum) {
Devang Patel73621622009-11-25 17:37:31 +00001047 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001048 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +00001049 }
Mike Stump1eb44332009-09-09 15:08:12 +00001050
Chris Lattner9c85ba32008-11-10 06:08:34 +00001051 // Return a CompositeType for the enum itself.
1052 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +00001053 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001054
Devang Patel8ab870d2010-05-12 23:46:38 +00001055 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1056 unsigned Line = getLineNumber(ED->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001057
Chris Lattner9c85ba32008-11-10 06:08:34 +00001058 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +00001059 uint64_t Size = 0;
1060 unsigned Align = 0;
1061 if (!Ty->isIncompleteType()) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001062 Size = CGM.getContext().getTypeSize(Ty);
1063 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman3189e4b2009-05-04 04:39:55 +00001064 }
Mike Stump1eb44332009-09-09 15:08:12 +00001065
Devang Patelca80a5f2009-10-20 19:55:01 +00001066 llvm::DIType DbgTy =
1067 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Pateld6c5a262010-02-01 21:52:22 +00001068 Unit, ED->getName(), DefUnit, Line,
Devang Patelca80a5f2009-10-20 19:55:01 +00001069 Size, Align, 0, 0,
1070 llvm::DIType(), EltArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001071 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001072}
1073
1074llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001075 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001076 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1077 return CreateType(RT, Unit);
1078 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1079 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001080
Chris Lattner9c85ba32008-11-10 06:08:34 +00001081 return llvm::DIType();
1082}
1083
Devang Patel70c23cd2010-02-23 22:59:39 +00001084llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001085 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001086 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1087 uint64_t NumElems = Ty->getNumElements();
1088 if (NumElems > 0)
1089 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001090
Benjamin Kramerad468862010-03-30 11:36:44 +00001091 llvm::DIDescriptor Subscript = DebugFactory.GetOrCreateSubrange(0, NumElems);
1092 llvm::DIArray SubscriptArray = DebugFactory.GetOrCreateArray(&Subscript, 1);
Devang Patel70c23cd2010-02-23 22:59:39 +00001093
1094 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1095 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1096
1097 return
1098 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001099 Unit, "", Unit,
Devang Patel70c23cd2010-02-23 22:59:39 +00001100 0, Size, Align, 0, 0,
1101 ElementTy, SubscriptArray);
1102}
1103
Chris Lattner9c85ba32008-11-10 06:08:34 +00001104llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001105 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001106 uint64_t Size;
1107 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001108
1109
Nuno Lopes010d5142009-01-28 00:35:17 +00001110 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001111 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001112 Size = 0;
1113 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001114 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001115 } else if (Ty->isIncompleteArrayType()) {
1116 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001117 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001118 } else {
1119 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001120 Size = CGM.getContext().getTypeSize(Ty);
1121 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001122 }
Mike Stump1eb44332009-09-09 15:08:12 +00001123
Chris Lattner9c85ba32008-11-10 06:08:34 +00001124 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1125 // interior arrays, do we care? Why aren't nested arrays represented the
1126 // obvious/recursive way?
1127 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1128 QualType EltTy(Ty, 0);
1129 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +00001130 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001131 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +00001132 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +00001133 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001134 // FIXME: Verify this is right for VLAs.
1135 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1136 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +00001137 }
Mike Stump1eb44332009-09-09 15:08:12 +00001138
Chris Lattner9c85ba32008-11-10 06:08:34 +00001139 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001140 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001141
Devang Patelca80a5f2009-10-20 19:55:01 +00001142 llvm::DIType DbgTy =
1143 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001144 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +00001145 0, Size, Align, 0, 0,
1146 getOrCreateType(EltTy, Unit),
1147 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001148 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001149}
1150
Anders Carlssona031b352009-11-06 19:19:55 +00001151llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001152 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001153 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1154 Ty, Ty->getPointeeType(), Unit);
1155}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001156
Anders Carlsson20f12a22009-12-06 18:00:51 +00001157llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001158 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001159 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1160 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1161
1162 if (!Ty->getPointeeType()->isFunctionType()) {
1163 // We have a data member pointer type.
1164 return PointerDiffDITy;
1165 }
1166
1167 // We have a member function pointer type. Treat it as a struct with two
1168 // ptrdiff_t members.
1169 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1170
1171 uint64_t FieldOffset = 0;
1172 llvm::DIDescriptor ElementTypes[2];
1173
1174 // FIXME: This should probably be a function type instead.
1175 ElementTypes[0] =
1176 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001177 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001178 Info.first, Info.second, FieldOffset, 0,
1179 PointerDiffDITy);
1180 FieldOffset += Info.first;
1181
1182 ElementTypes[1] =
1183 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001184 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001185 Info.first, Info.second, FieldOffset, 0,
1186 PointerDiffDITy);
1187
1188 llvm::DIArray Elements =
1189 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1190 llvm::array_lengthof(ElementTypes));
1191
1192 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1193 U, llvm::StringRef("test"),
Devang Pateld58562e2010-03-09 22:49:11 +00001194 U, 0, FieldOffset,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001195 0, 0, 0, llvm::DIType(), Elements);
1196}
1197
Douglas Gregor840943d2009-12-21 20:18:30 +00001198static QualType UnwrapTypeForDebugInfo(QualType T) {
1199 do {
1200 QualType LastT = T;
1201 switch (T->getTypeClass()) {
1202 default:
1203 return T;
1204 case Type::TemplateSpecialization:
1205 T = cast<TemplateSpecializationType>(T)->desugar();
1206 break;
1207 case Type::TypeOfExpr: {
1208 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1209 T = Ty->getUnderlyingExpr()->getType();
1210 break;
1211 }
1212 case Type::TypeOf:
1213 T = cast<TypeOfType>(T)->getUnderlyingType();
1214 break;
1215 case Type::Decltype:
1216 T = cast<DecltypeType>(T)->getUnderlyingType();
1217 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001218 case Type::Elaborated:
1219 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001220 break;
1221 case Type::SubstTemplateTypeParm:
1222 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1223 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001224 }
1225
1226 assert(T != LastT && "Type unwrapping failed to unwrap!");
1227 if (T == LastT)
1228 return T;
1229 } while (true);
1230
1231 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001232}
1233
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001234/// getOrCreateType - Get the type from the cache or create a new
1235/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001236llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001237 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001238 if (Ty.isNull())
1239 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001240
Douglas Gregor840943d2009-12-21 20:18:30 +00001241 // Unwrap the type as needed for debug information.
1242 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001243
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001244 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001245 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001246 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001247 if (it != TypeCache.end()) {
1248 // Verify that the debug info still exists.
1249 if (&*it->second)
1250 return llvm::DIType(cast<llvm::MDNode>(it->second));
1251 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001252
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001253 // Otherwise create the type.
1254 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001255
1256 // And update the type cache.
Devang Patelab699792010-05-07 18:12:35 +00001257 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001258 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001259}
1260
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001261/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001262llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001263 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001264 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001265 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001266 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001267
Douglas Gregor2101a822009-12-21 19:57:21 +00001268 const char *Diag = 0;
1269
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001270 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001271 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001272#define TYPE(Class, Base)
1273#define ABSTRACT_TYPE(Class, Base)
1274#define NON_CANONICAL_TYPE(Class, Base)
1275#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1276#include "clang/AST/TypeNodes.def"
1277 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001278
Anders Carlssonbfe69952009-11-06 18:24:04 +00001279 // FIXME: Handle these.
1280 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001281 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001282
1283 case Type::Vector:
1284 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001285 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001286 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCallc12c5bb2010-05-15 11:32:37 +00001287 case Type::ObjCObject:
1288 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001289 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001290 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1291 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1292 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1293 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001294 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001295 return CreateType(cast<BlockPointerType>(Ty), Unit);
1296 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001297 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001298 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001299 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001300 case Type::FunctionProto:
1301 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001302 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001303 case Type::ConstantArray:
1304 case Type::VariableArray:
1305 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001306 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001307
1308 case Type::LValueReference:
1309 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1310
Anders Carlsson20f12a22009-12-06 18:00:51 +00001311 case Type::MemberPointer:
1312 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001313
1314 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001315 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001316 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001317 case Type::TypeOfExpr:
1318 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001319 case Type::Decltype:
1320 llvm_unreachable("type should have been unwrapped!");
1321 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001322
1323 case Type::RValueReference:
1324 // FIXME: Implement!
1325 Diag = "rvalue references";
1326 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001327 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001328
1329 assert(Diag && "Fall through without a diagnostic?");
1330 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1331 "debug information for %0 is not yet supported");
1332 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1333 << Diag;
1334 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001335}
1336
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001337/// CreateMemberType - Create new member and increase Offset by FType's size.
1338llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
1339 llvm::StringRef Name,
1340 uint64_t *Offset) {
1341 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1342 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1343 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
1344 llvm::DIType Ty = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1345 Unit, Name, Unit, 0,
1346 FieldSize, FieldAlign,
1347 *Offset, 0, FieldTy);
1348 *Offset += FieldSize;
1349 return Ty;
1350}
1351
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001352/// EmitFunctionStart - Constructs the debug code for entering a function -
1353/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001354void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001355 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001356 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001357
Devang Patel9c6c3a02010-01-14 00:36:21 +00001358 llvm::StringRef Name;
Anders Carlsson9a20d552010-06-22 16:16:50 +00001359 llvm::StringRef LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001360
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001361 FnBeginRegionCount.push_back(RegionStack.size());
1362
Devang Patel9c6c3a02010-01-14 00:36:21 +00001363 const Decl *D = GD.getDecl();
1364 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001365 // If there is a DISubprogram for this function available then use it.
1366 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1367 FI = SPCache.find(FD);
1368 if (FI != SPCache.end()) {
Devang Patel0804e6e2010-03-08 20:53:17 +00001369 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
Devang Patelab699792010-05-07 18:12:35 +00001370 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1371 llvm::MDNode *SPN = SP;
1372 RegionStack.push_back(SPN);
1373 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00001374 return;
1375 }
1376 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001377 Name = getFunctionName(FD);
1378 // Use mangled name as linkage name for c/c++ functions.
Anders Carlsson9a20d552010-06-22 16:16:50 +00001379 LinkageName = CGM.getMangledName(GD);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001380 } else {
1381 // Use llvm function name as linkage name.
1382 Name = Fn->getName();
Anders Carlsson9a20d552010-06-22 16:16:50 +00001383 LinkageName = Name;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001384 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001385 if (!Name.empty() && Name[0] == '\01')
1386 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001387
Devang Patel970c6182010-04-24 00:49:16 +00001388 // It is expected that CurLoc is set before using EmitFunctionStart.
1389 // Usually, CurLoc points to the left bracket location of compound
1390 // statement representing function body.
1391 llvm::DIFile Unit = getOrCreateFile(CurLoc);
Devang Patel8ab870d2010-05-12 23:46:38 +00001392 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001393
Chris Lattner9c85ba32008-11-10 06:08:34 +00001394 llvm::DISubprogram SP =
Devang Patel970c6182010-04-24 00:49:16 +00001395 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001396 getOrCreateType(FnType, Unit),
Devang Patel15a3d7d2010-07-15 23:09:46 +00001397 Fn->hasInternalLinkage(), true/*definition*/,
1398 0, 0, llvm::DIType(),
1399 D->isImplicit(),
1400 CGM.getLangOptions().Optimize, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +00001401
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001402 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001403 llvm::MDNode *SPN = SP;
1404 RegionStack.push_back(SPN);
1405 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001406
1407 // Clear stack used to keep track of #line directives.
1408 LineDirectiveFiles.clear();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001409}
1410
1411
Devang Patel4d939e62010-07-20 22:20:10 +00001412void CGDebugInfo::EmitStopPoint(CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001413 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001414
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001415 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001416 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001417 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001418 || (SM.getInstantiationLineNumber(CurLoc) ==
1419 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001420 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patel4800ea62010-04-05 21:09:15 +00001421 // New Builder may not be in sync with CGDebugInfo.
1422 if (!Builder.getCurrentDebugLocation().isUnknown())
1423 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001424
1425 // Update last state.
1426 PrevLoc = CurLoc;
1427
Chris Lattnerc6034632010-04-01 06:31:43 +00001428 llvm::MDNode *Scope = RegionStack.back();
Devang Patel8ab870d2010-05-12 23:46:38 +00001429 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1430 getColumnNumber(CurLoc),
Chris Lattnere541d012010-04-02 20:21:43 +00001431 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001432}
1433
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001434/// UpdateLineDirectiveRegion - Update region stack only if #line directive
1435/// has introduced scope change.
1436void CGDebugInfo::UpdateLineDirectiveRegion(CGBuilderTy &Builder) {
1437 if (CurLoc.isInvalid() || CurLoc.isMacroID() ||
1438 PrevLoc.isInvalid() || PrevLoc.isMacroID())
1439 return;
1440 SourceManager &SM = CGM.getContext().getSourceManager();
1441 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
1442 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc);
1443
1444 if (!strcmp(PPLoc.getFilename(), PCLoc.getFilename()))
1445 return;
1446
1447 // If #line directive stack is empty then we are entering a new scope.
1448 if (LineDirectiveFiles.empty()) {
1449 EmitRegionStart(Builder);
1450 LineDirectiveFiles.push_back(PCLoc.getFilename());
1451 return;
1452 }
1453
1454 assert (RegionStack.size() >= LineDirectiveFiles.size()
1455 && "error handling #line regions!");
1456
1457 bool SeenThisFile = false;
1458 for(std::vector<const char *>::iterator I = LineDirectiveFiles.begin(),
1459 E = LineDirectiveFiles.end(); I != E; ++I)
1460 if (!strcmp(PPLoc.getFilename(), *I)) {
1461 SeenThisFile = true;
1462 break;
1463 }
1464
1465 // If #line for this file is seen earlier then pop out #line regions.
1466 if (SeenThisFile) {
1467 while (!LineDirectiveFiles.empty()) {
1468 const char *LastFile = LineDirectiveFiles.back();
1469 RegionStack.pop_back();
1470 LineDirectiveFiles.pop_back();
1471 if (!strcmp(PPLoc.getFilename(), LastFile))
1472 break;
1473 }
1474 return;
1475 }
1476
1477 // .. otherwise insert new #line region.
1478 EmitRegionStart(Builder);
1479 LineDirectiveFiles.push_back(PCLoc.getFilename());
1480
1481 return;
1482}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001483/// EmitRegionStart- Constructs the debug code for entering a declarative
1484/// region - "llvm.dbg.region.start.".
Devang Patel4d939e62010-07-20 22:20:10 +00001485void CGDebugInfo::EmitRegionStart(CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001486 llvm::DIDescriptor D =
1487 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1488 llvm::DIDescriptor() :
Devang Pateld19429f2010-02-16 21:41:20 +00001489 llvm::DIDescriptor(RegionStack.back()),
Stuart Hastings257d1d32010-07-19 23:56:31 +00001490 getOrCreateFile(CurLoc),
Devang Patel8ab870d2010-05-12 23:46:38 +00001491 getLineNumber(CurLoc),
1492 getColumnNumber(CurLoc));
Devang Patelab699792010-05-07 18:12:35 +00001493 llvm::MDNode *DN = D;
1494 RegionStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001495}
1496
1497/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1498/// region - "llvm.dbg.region.end."
Devang Patel4d939e62010-07-20 22:20:10 +00001499void CGDebugInfo::EmitRegionEnd(CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001500 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1501
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001502 // Provide an region stop point.
Devang Patel4d939e62010-07-20 22:20:10 +00001503 EmitStopPoint(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001504
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001505 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001506}
1507
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001508/// EmitFunctionEnd - Constructs the debug code for exiting a function.
1509void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
1510 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1511 unsigned RCount = FnBeginRegionCount.back();
1512 assert(RCount <= RegionStack.size() && "Region stack mismatch");
1513
1514 // Pop all regions for this function.
1515 while (RegionStack.size() != RCount)
1516 EmitRegionEnd(Builder);
1517 FnBeginRegionCount.pop_back();
1518}
1519
Devang Patel809b9bb2010-02-10 18:49:08 +00001520// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1521// See BuildByRefType.
1522llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1523 uint64_t *XOffset) {
1524
1525 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1526
1527 QualType FType;
1528 uint64_t FieldSize, FieldOffset;
1529 unsigned FieldAlign;
1530
Devang Patel17800552010-03-09 00:44:50 +00001531 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001532 QualType Type = VD->getType();
1533
1534 FieldOffset = 0;
1535 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001536 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1537 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001538 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001539 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1540 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1541
Devang Patel809b9bb2010-02-10 18:49:08 +00001542 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1543 if (HasCopyAndDispose) {
1544 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001545 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1546 &FieldOffset));
1547 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1548 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001549 }
1550
1551 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1552 if (Align > CharUnits::fromQuantity(
1553 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1554 unsigned AlignedOffsetInBytes
1555 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1556 unsigned NumPaddingBytes
1557 = AlignedOffsetInBytes - FieldOffset/8;
1558
1559 if (NumPaddingBytes > 0) {
1560 llvm::APInt pad(32, NumPaddingBytes);
1561 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1562 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001563 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001564 }
1565 }
1566
1567 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001568 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00001569 FieldSize = CGM.getContext().getTypeSize(FType);
1570 FieldAlign = Align.getQuantity()*8;
1571
1572 *XOffset = FieldOffset;
1573 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001574 VD->getName(), Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001575 0, FieldSize, FieldAlign,
1576 FieldOffset, 0, FieldTy);
1577 EltTys.push_back(FieldTy);
1578 FieldOffset += FieldSize;
1579
1580 llvm::DIArray Elements =
1581 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1582
1583 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1584
1585 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001586 Unit, "", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001587 0, FieldOffset, 0, 0, Flags,
1588 llvm::DIType(), Elements);
1589
1590}
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001591/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001592void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001593 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001594 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1595
Devang Patel17800552010-03-09 00:44:50 +00001596 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001597 llvm::DIType Ty;
1598 uint64_t XOffset = 0;
1599 if (VD->hasAttr<BlocksAttr>())
1600 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1601 else
1602 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001603
Devang Patelf4e54a22010-05-07 23:05:55 +00001604 // If there is not any debug info for type then do not emit debug info
1605 // for this variable.
1606 if (!Ty)
1607 return;
1608
Chris Lattner9c85ba32008-11-10 06:08:34 +00001609 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001610 unsigned Line = getLineNumber(VD->getLocation());
1611 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001612
Chris Lattner9c85ba32008-11-10 06:08:34 +00001613 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001614 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001615 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel239cec62010-02-01 21:39:52 +00001616 VD->getName(),
Devang Patel44eeeba2010-06-05 01:14:40 +00001617 Unit, Line, Ty, CGM.getLangOptions().Optimize);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001618 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001619 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001620 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001621
Chris Lattnerc6034632010-04-01 06:31:43 +00001622 llvm::MDNode *Scope = RegionStack.back();
Chris Lattnere541d012010-04-02 20:21:43 +00001623 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001624}
1625
Mike Stumpb1a6e682009-09-30 02:43:10 +00001626/// EmitDeclare - Emit local variable declaration debug info.
1627void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1628 llvm::Value *Storage, CGBuilderTy &Builder,
1629 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001630 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001631 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1632
Devang Patel2b594b92010-04-26 23:28:46 +00001633 if (Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001634 return;
1635
1636 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001637 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001638 llvm::DIType Ty;
1639 if (VD->hasAttr<BlocksAttr>())
1640 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1641 else
1642 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001643
1644 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001645 unsigned Line = getLineNumber(VD->getLocation());
1646 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001647
Devang Pateld6c5a262010-02-01 21:52:22 +00001648 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001649 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001650 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1651 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1652 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1653 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001654 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001655 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1656 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001657 // offset of __forwarding field
1658 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001659 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1660 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1661 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001662 // offset of x field
1663 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001664 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001665 }
1666
1667 // Create the descriptor for the variable.
1668 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001669 DebugFactory.CreateComplexVariable(Tag,
1670 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001671 VD->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001672 addr);
1673 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001674 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001675 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattnerd5b89022009-12-28 21:44:41 +00001676
Chris Lattnerc6034632010-04-01 06:31:43 +00001677 llvm::MDNode *Scope = RegionStack.back();
Devang Patelf8e10a52010-05-10 23:48:38 +00001678 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001679}
1680
Devang Pateld6c5a262010-02-01 21:52:22 +00001681void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001682 llvm::Value *Storage,
1683 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001684 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001685}
1686
Mike Stumpb1a6e682009-09-30 02:43:10 +00001687void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1688 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1689 CodeGenFunction *CGF) {
1690 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1691}
1692
Chris Lattner9c85ba32008-11-10 06:08:34 +00001693/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1694/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001695void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001696 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001697 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001698}
1699
1700
1701
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001702/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001703void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001704 const VarDecl *D) {
1705
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001706 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001707 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001708 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +00001709
Devang Pateleb6d79b2010-02-01 21:34:11 +00001710 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001711 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001712
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001713 // CodeGen turns int[] into int[1] so we'll do the same here.
1714 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001715
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001716 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001717 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001718
Anders Carlsson20f12a22009-12-06 18:00:51 +00001719 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001720 ArrayType::Normal, 0);
1721 }
Devang Patel5d822f02010-04-29 17:48:37 +00001722 llvm::StringRef DeclName = D->getName();
Devang Patel8b90a782010-05-13 23:52:37 +00001723 llvm::StringRef LinkageName;
Devang Patel0fd3d1f2010-05-14 16:55:25 +00001724 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()))
Devang Patel8b90a782010-05-13 23:52:37 +00001725 LinkageName = Var->getName();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001726 llvm::DIDescriptor DContext =
1727 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
Devang Patel8b90a782010-05-13 23:52:37 +00001728 DebugFactory.CreateGlobalVariable(DContext, DeclName, DeclName, LinkageName,
1729 Unit, LineNo, getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001730 Var->hasInternalLinkage(),
1731 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001732}
1733
Devang Patel9ca36b62009-02-26 21:10:26 +00001734/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001735void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001736 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001737 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001738 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001739 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patel9ca36b62009-02-26 21:10:26 +00001740
Devang Pateld6c5a262010-02-01 21:52:22 +00001741 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001742
Devang Pateld6c5a262010-02-01 21:52:22 +00001743 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001744 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001745
Devang Patel9ca36b62009-02-26 21:10:26 +00001746 // CodeGen turns int[] into int[1] so we'll do the same here.
1747 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001748
Devang Patel9ca36b62009-02-26 21:10:26 +00001749 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001750 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001751
Anders Carlsson20f12a22009-12-06 18:00:51 +00001752 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001753 ArrayType::Normal, 0);
1754 }
1755
Devang Patelf6a39b72009-10-20 18:26:30 +00001756 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001757 getOrCreateType(T, Unit),
1758 Var->hasInternalLinkage(),
1759 true/*definition*/, Var);
1760}
Devang Patelabb485f2010-02-01 19:16:32 +00001761
1762/// getOrCreateNamesSpace - Return namespace descriptor for the given
1763/// namespace decl.
1764llvm::DINameSpace
1765CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1766 llvm::DIDescriptor Unit) {
1767 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1768 NameSpaceCache.find(NSDecl);
1769 if (I != NameSpaceCache.end())
1770 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1771
Devang Patel8ab870d2010-05-12 23:46:38 +00001772 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patelabb485f2010-02-01 19:16:32 +00001773
1774 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00001775 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00001776 llvm::DINameSpace NS =
1777 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
Devang Patelab699792010-05-07 18:12:35 +00001778 llvm::DIFile(Unit), LineNo);
1779 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00001780 return NS;
1781}