blob: 0309c6e09ba6485d1b23597a6830fd5e615e1877 [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
187/// getCompDirname - AT_comp_dir is empty if filename is absulte otherwise
188/// it points to compilation directory.
189llvm::StringRef CGDebugInfo::getCompDirname(llvm::StringRef Filename) {
190 llvm::sys::Path FilePath(Filename);
191 if (FilePath.isAbsolute())
192 return llvm::StringRef();
193 return getCurrentDirname();
194}
195
Devang Patel17800552010-03-09 00:44:50 +0000196/// CreateCompileUnit - Create new compile unit.
197void CGDebugInfo::CreateCompileUnit() {
198
199 // Get absolute path name.
Douglas Gregorac91b4c2010-03-18 23:46:43 +0000200 SourceManager &SM = CGM.getContext().getSourceManager();
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000201 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
202 if (MainFileName.empty())
Devang Patel22fe5852010-03-12 21:04:27 +0000203 MainFileName = "<unknown>";
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000204
Douglas Gregorf6728fc2010-03-22 21:28:29 +0000205 // The main file name provided via the "-main-file-name" option contains just
206 // the file name itself with no path information. This file name may have had
207 // a relative path, so we look into the actual file entry for the main
208 // file to determine the real absolute path for the file.
Devang Patel6e6bc392010-07-23 23:04:28 +0000209 std::string MainFileDir;
Devang Patelac4d13c2010-07-27 15:17:16 +0000210 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000211 MainFileDir = MainFile->getDir()->getName();
Devang Patelac4d13c2010-07-27 15:17:16 +0000212 if (MainFileDir != ".")
213 MainFileName = MainFileDir + "/" + MainFileName;
214 }
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000215
Devang Patelac4d13c2010-07-27 15:17:16 +0000216 // Save filename string.
217 char *FilenamePtr = DebugInfoNames.Allocate<char>(MainFileName.length());
218 memcpy(FilenamePtr, MainFileName.c_str(), MainFileName.length());
219 llvm::StringRef Filename(FilenamePtr, MainFileName.length());
220
Chris Lattner515455a2009-03-25 03:28:08 +0000221 unsigned LangTag;
Devang Patel17800552010-03-09 00:44:50 +0000222 const LangOptions &LO = CGM.getLangOptions();
Chris Lattner515455a2009-03-25 03:28:08 +0000223 if (LO.CPlusPlus) {
224 if (LO.ObjC1)
225 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
226 else
227 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
228 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000229 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000230 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000231 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000232 } else {
233 LangTag = llvm::dwarf::DW_LANG_C89;
234 }
Devang Patel446c6192009-04-17 21:06:59 +0000235
Benjamin Kramer47daf682009-12-08 11:02:29 +0000236 const char *Producer =
Mike Stumpd8945d62009-10-09 18:38:12 +0000237#ifdef CLANG_VENDOR
238 CLANG_VENDOR
239#endif
240 "clang " CLANG_VERSION_STRING;
Chris Lattner4c2577a2009-05-02 01:00:04 +0000241
242 // Figure out which version of the ObjC runtime we have.
243 unsigned RuntimeVers = 0;
244 if (LO.ObjC1)
245 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000247 // Create new compile unit.
Devang Patel17800552010-03-09 00:44:50 +0000248 TheCU = DebugFactory.CreateCompileUnit(
Devang Patelac4d13c2010-07-27 15:17:16 +0000249 LangTag, Filename, getCompDirname(Filename),
250 Producer, true,
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000251 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000252}
253
Devang Patel65e99f22009-02-25 01:36:11 +0000254/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000255/// one if necessary.
256llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel17800552010-03-09 00:44:50 +0000257 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000258 unsigned Encoding = 0;
259 switch (BT->getKind()) {
260 default:
261 case BuiltinType::Void:
262 return llvm::DIType();
Devang Pateleaf5ee92010-07-21 22:41:25 +0000263 case BuiltinType::ObjCId:
264 // id is struct objc_object *.
265 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
266 Unit, "objc_object", Unit, 0, 0, 0, 0,
267 llvm::DIType::FlagFwdDecl,
268 llvm::DIType(), llvm::DIArray());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000269 case BuiltinType::UChar:
270 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
271 case BuiltinType::Char_S:
272 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
273 case BuiltinType::UShort:
274 case BuiltinType::UInt:
275 case BuiltinType::ULong:
276 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
277 case BuiltinType::Short:
278 case BuiltinType::Int:
279 case BuiltinType::Long:
280 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
281 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
282 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000283 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000284 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000285 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000286 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000287 uint64_t Size = CGM.getContext().getTypeSize(BT);
288 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000289 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Devang Patelca80a5f2009-10-20 19:55:01 +0000291 llvm::DIType DbgTy =
292 DebugFactory.CreateBasicType(Unit,
Anders Carlsson20f12a22009-12-06 18:00:51 +0000293 BT->getName(CGM.getContext().getLangOptions()),
Devang Patelca80a5f2009-10-20 19:55:01 +0000294 Unit, 0, Size, Align,
295 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000296 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000297}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000298
Chris Lattnerb7003772009-04-23 06:13:01 +0000299llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000300 llvm::DIFile Unit) {
Chris Lattnerb7003772009-04-23 06:13:01 +0000301 // Bit size, align and offset of the type.
302 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
303 if (Ty->isComplexIntegerType())
304 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Anders Carlsson20f12a22009-12-06 18:00:51 +0000306 uint64_t Size = CGM.getContext().getTypeSize(Ty);
307 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattnerb7003772009-04-23 06:13:01 +0000308 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000309
Devang Patelca80a5f2009-10-20 19:55:01 +0000310 llvm::DIType DbgTy =
311 DebugFactory.CreateBasicType(Unit, "complex",
312 Unit, 0, Size, Align,
313 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000314 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000315}
316
John McCalla1805292009-09-25 01:40:47 +0000317/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000318/// a new one if necessary.
Devang Patel17800552010-03-09 00:44:50 +0000319llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +0000320 QualifierCollector Qc;
321 const Type *T = Qc.strip(Ty);
322
323 // Ignore these qualifiers for now.
324 Qc.removeObjCGCAttr();
325 Qc.removeAddressSpace();
326
Chris Lattner9c85ba32008-11-10 06:08:34 +0000327 // We will create one Derived type for one qualifier and recurse to handle any
328 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000329 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000330 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000331 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000332 Qc.removeConst();
333 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000334 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000335 Qc.removeVolatile();
336 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000337 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000338 Qc.removeRestrict();
339 } else {
340 assert(Qc.empty() && "Unknown type qualifier for debug info");
341 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000342 }
Mike Stump1eb44332009-09-09 15:08:12 +0000343
John McCalla1805292009-09-25 01:40:47 +0000344 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
345
Daniel Dunbar3845f862008-10-31 03:54:29 +0000346 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
347 // CVR derived types.
Devang Patelca80a5f2009-10-20 19:55:01 +0000348 llvm::DIType DbgTy =
Devang Pateld58562e2010-03-09 22:49:11 +0000349 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000350 0, 0, 0, 0, 0, FromTy);
Devang Patelca80a5f2009-10-20 19:55:01 +0000351 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000352}
353
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000354llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000355 llvm::DIFile Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000356 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000357 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
358 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000359 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000360}
361
Chris Lattner9c85ba32008-11-10 06:08:34 +0000362llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000363 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000364 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
365 Ty->getPointeeType(), Unit);
366}
367
368llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
369 const Type *Ty,
370 QualType PointeeTy,
Devang Patel17800552010-03-09 00:44:50 +0000371 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000372 llvm::DIType EltTy = getOrCreateType(PointeeTy, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000374 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000375
376 // Size is always the size of a pointer. We can't use getTypeSize here
377 // because that does not return the correct value for references.
378 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000379 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
380 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Devang Patelca80a5f2009-10-20 19:55:01 +0000382 return
Devang Pateld58562e2010-03-09 22:49:11 +0000383 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000384 0, Size, Align, 0, 0, EltTy);
Anders Carlssona031b352009-11-06 19:19:55 +0000385
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000386}
387
Mike Stump9bc093c2009-05-14 02:03:51 +0000388llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000389 llvm::DIFile Unit) {
Mike Stump9bc093c2009-05-14 02:03:51 +0000390 if (BlockLiteralGenericSet)
391 return BlockLiteralGeneric;
392
Mike Stump9bc093c2009-05-14 02:03:51 +0000393 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
394
395 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
396
397 llvm::DIType FieldTy;
398
399 QualType FType;
400 uint64_t FieldSize, FieldOffset;
401 unsigned FieldAlign;
402
403 llvm::DIArray Elements;
404 llvm::DIType EltTy, DescTy;
405
406 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000407 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000408 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
409 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000410
Daniel Dunbarca308df2009-05-26 19:40:20 +0000411 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000412 EltTys.clear();
413
Mike Stump3d363c52009-10-02 02:30:50 +0000414 unsigned Flags = llvm::DIType::FlagAppleBlock;
Devang Patel8ab870d2010-05-12 23:46:38 +0000415 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump3d363c52009-10-02 02:30:50 +0000416
Mike Stump9bc093c2009-05-14 02:03:51 +0000417 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Devang Patel8ab870d2010-05-12 23:46:38 +0000418 Unit, LineNo, FieldOffset, 0, 0,
419 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Mike Stump9bc093c2009-05-14 02:03:51 +0000421 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000422 uint64_t Size = CGM.getContext().getTypeSize(Ty);
423 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Mike Stump9bc093c2009-05-14 02:03:51 +0000425 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000426 Unit, "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000427 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000428
429 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000430 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000431 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson20f12a22009-12-06 18:00:51 +0000432 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000433 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
434 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramerd3651cc2010-04-24 20:26:20 +0000435 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000436 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000437
Anders Carlsson20f12a22009-12-06 18:00:51 +0000438 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000439 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000440 FieldSize = CGM.getContext().getTypeSize(Ty);
441 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000442 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000443 "__descriptor", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000444 LineNo, FieldSize, FieldAlign,
Mike Stump9bc093c2009-05-14 02:03:51 +0000445 FieldOffset, 0, FieldTy);
446 EltTys.push_back(FieldTy);
447
448 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000449 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000450
451 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Devang Patel8ab870d2010-05-12 23:46:38 +0000452 Unit, LineNo, FieldOffset, 0, 0,
453 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Mike Stump9bc093c2009-05-14 02:03:51 +0000455 BlockLiteralGenericSet = true;
456 BlockLiteralGeneric
457 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000458 "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000459 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000460 return BlockLiteralGeneric;
461}
462
Chris Lattner9c85ba32008-11-10 06:08:34 +0000463llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000464 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000465 // Typedefs are derived from some other type. If we have a typedef of a
466 // typedef, make sure to emit the whole chain.
467 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Chris Lattner9c85ba32008-11-10 06:08:34 +0000469 // We don't set size information, but do specify where the typedef was
470 // declared.
Devang Patel8ab870d2010-05-12 23:46:38 +0000471 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000472
Devang Pateleb6d79b2010-02-01 21:34:11 +0000473 llvm::DIDescriptor TyContext
474 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
475 Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000476 llvm::DIType DbgTy =
Devang Pateld5289052010-01-29 22:29:31 +0000477 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Pateleb6d79b2010-02-01 21:34:11 +0000478 TyContext,
Devang Pateld5289052010-01-29 22:29:31 +0000479 Ty->getDecl()->getName(), Unit,
480 Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000481 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000482}
483
Chris Lattner9c85ba32008-11-10 06:08:34 +0000484llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000485 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000486 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000487
Chris Lattner9c85ba32008-11-10 06:08:34 +0000488 // Add the result type at least.
489 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Chris Lattner9c85ba32008-11-10 06:08:34 +0000491 // Set up remainder of arguments if there is a prototype.
492 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000493 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000494 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
495 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
496 } else {
497 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000498 }
499
Chris Lattner9c85ba32008-11-10 06:08:34 +0000500 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000501 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Devang Patelca80a5f2009-10-20 19:55:01 +0000503 llvm::DIType DbgTy =
504 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000505 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000506 0, 0, 0, 0, 0,
507 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000508 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000509}
510
Devang Patel428deb52010-01-19 00:00:59 +0000511/// CollectRecordFields - A helper function to collect debug info for
512/// record fields. This is used while creating debug info entry for a Record.
513void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000514CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit,
Devang Patel428deb52010-01-19 00:00:59 +0000515 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
516 unsigned FieldNo = 0;
Devang Patel239cec62010-02-01 21:39:52 +0000517 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
518 for (RecordDecl::field_iterator I = RD->field_begin(),
519 E = RD->field_end();
Devang Patel428deb52010-01-19 00:00:59 +0000520 I != E; ++I, ++FieldNo) {
521 FieldDecl *Field = *I;
522 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
523
524 llvm::StringRef FieldName = Field->getName();
525
Devang Patel4835fdd2010-02-12 01:31:06 +0000526 // Ignore unnamed fields. Do not ignore unnamed records.
527 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel428deb52010-01-19 00:00:59 +0000528 continue;
529
530 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +0000531 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
532 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel428deb52010-01-19 00:00:59 +0000533 QualType FType = Field->getType();
534 uint64_t FieldSize = 0;
535 unsigned FieldAlign = 0;
536 if (!FType->isIncompleteArrayType()) {
537
538 // Bit size, align and offset of the type.
539 FieldSize = CGM.getContext().getTypeSize(FType);
540 Expr *BitWidth = Field->getBitWidth();
541 if (BitWidth)
542 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
543
544 FieldAlign = CGM.getContext().getTypeAlign(FType);
545 }
546
547 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
548
Devang Patel71f4ff62010-04-21 23:12:37 +0000549 unsigned Flags = 0;
550 AccessSpecifier Access = I->getAccess();
551 if (Access == clang::AS_private)
552 Flags |= llvm::DIType::FlagPrivate;
553 else if (Access == clang::AS_protected)
554 Flags |= llvm::DIType::FlagProtected;
555
Devang Patel428deb52010-01-19 00:00:59 +0000556 // Create a DW_TAG_member node to remember the offset of this field in the
557 // struct. FIXME: This is an absolutely insane way to capture this
558 // information. When we gut debug info, this should be fixed.
559 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
560 FieldName, FieldDefUnit,
561 FieldLine, FieldSize, FieldAlign,
Devang Patel71f4ff62010-04-21 23:12:37 +0000562 FieldOffset, Flags, FieldTy);
Devang Patel428deb52010-01-19 00:00:59 +0000563 EltTys.push_back(FieldTy);
564 }
565}
566
Devang Patela6da1922010-01-28 00:28:01 +0000567/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
568/// function type is not updated to include implicit "this" pointer. Use this
569/// routine to get a method type which includes "this" pointer.
570llvm::DIType
571CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000572 llvm::DIFile Unit) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +0000573 llvm::DIType FnTy
574 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
575 0),
576 Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000577
578 // Static methods do not need "this" pointer argument.
579 if (Method->isStatic())
580 return FnTy;
581
Devang Patela6da1922010-01-28 00:28:01 +0000582 // Add "this" pointer.
583
Devang Patelab699792010-05-07 18:12:35 +0000584 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patela6da1922010-01-28 00:28:01 +0000585 assert (Args.getNumElements() && "Invalid number of arguments!");
586
587 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
588
589 // First element is always return type. For 'void' functions it is NULL.
590 Elts.push_back(Args.getElement(0));
591
592 // "this" pointer is always first argument.
593 ASTContext &Context = CGM.getContext();
594 QualType ThisPtr =
595 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
Devang Patel337472d2010-02-09 17:57:50 +0000596 llvm::DIType ThisPtrType =
597 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
Devang Patel769640e2010-07-13 00:24:30 +0000598
Devang Patelab699792010-05-07 18:12:35 +0000599 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
Devang Patel337472d2010-02-09 17:57:50 +0000600 Elts.push_back(ThisPtrType);
Devang Patela6da1922010-01-28 00:28:01 +0000601
602 // Copy rest of the arguments.
603 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
604 Elts.push_back(Args.getElement(i));
605
606 llvm::DIArray EltTypeArray =
607 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
608
609 return
610 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000611 Unit, "", Unit,
Devang Patela6da1922010-01-28 00:28:01 +0000612 0, 0, 0, 0, 0,
613 llvm::DIType(), EltTypeArray);
614}
615
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000616/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
617/// a single member function GlobalDecl.
618llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000619CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000620 llvm::DIFile Unit,
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000621 llvm::DICompositeType &RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000622 bool IsCtorOrDtor =
623 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
624
625 llvm::StringRef MethodName = getFunctionName(Method);
Devang Patela6da1922010-01-28 00:28:01 +0000626 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000627
628 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
629 // make sense to give a single ctor/dtor a linkage name.
Anders Carlsson9a20d552010-06-22 16:16:50 +0000630 llvm::StringRef MethodLinkageName;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000631 if (!IsCtorOrDtor)
Anders Carlsson9a20d552010-06-22 16:16:50 +0000632 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000633
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000634 // Get the location for the method.
Devang Patel8ab870d2010-05-12 23:46:38 +0000635 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
636 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000637
638 // Collect virtual method info.
639 llvm::DIType ContainingType;
640 unsigned Virtuality = 0;
641 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000642
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000643 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000644 if (Method->isPure())
645 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
646 else
647 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
648
649 // It doesn't make sense to give a virtual destructor a vtable index,
650 // since a single destructor has two entries in the vtable.
651 if (!isa<CXXDestructorDecl>(Method))
Anders Carlsson046c2942010-04-17 20:15:18 +0000652 VIndex = CGM.getVTables().getMethodVTableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000653 ContainingType = RecordTy;
654 }
655
656 llvm::DISubprogram SP =
657 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
658 MethodLinkageName,
659 MethodDefUnit, MethodLine,
660 MethodTy, /*isLocalToUnit=*/false,
Devang Patela5c5bab2010-07-12 22:54:41 +0000661 /* isDefintion=*/ false,
Devang Patel40894912010-07-15 22:57:00 +0000662 Virtuality, VIndex, ContainingType,
Devang Patel15a3d7d2010-07-15 23:09:46 +0000663 Method->isImplicit(),
664 CGM.getLangOptions().Optimize);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000665
666 // Don't cache ctors or dtors since we have to emit multiple functions for
667 // a single ctor or dtor.
668 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
Devang Patelab699792010-05-07 18:12:35 +0000669 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000670
671 return SP;
672}
673
Devang Patel4125fd22010-01-19 01:54:44 +0000674/// CollectCXXMemberFunctions - A helper function to collect debug info for
675/// C++ member functions.This is used while creating debug info entry for
676/// a Record.
677void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000678CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000679 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
680 llvm::DICompositeType &RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000681 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
682 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000683 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000684
Devang Pateld5322da2010-02-09 19:09:28 +0000685 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000686 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000687
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000688 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000689 }
690}
691
Devang Patela245c5b2010-01-25 23:32:18 +0000692/// CollectCXXBases - A helper function to collect debug info for
693/// C++ base classes. This is used while creating debug info entry for
694/// a Record.
695void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000696CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000697 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
698 llvm::DICompositeType &RecordTy) {
699
Devang Patel239cec62010-02-01 21:39:52 +0000700 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
701 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
702 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000703 unsigned BFlags = 0;
704 uint64_t BaseOffset;
705
706 const CXXRecordDecl *Base =
707 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
708
709 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000710 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000711 // expression where it expects +ve number.
Anders Carlssonaf440352010-03-23 04:11:45 +0000712 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base);
Devang Patelca7daed2010-01-28 21:54:15 +0000713 BFlags = llvm::DIType::FlagVirtual;
714 } else
715 BaseOffset = RL.getBaseClassOffset(Base);
716
717 AccessSpecifier Access = BI->getAccessSpecifier();
718 if (Access == clang::AS_private)
719 BFlags |= llvm::DIType::FlagPrivate;
720 else if (Access == clang::AS_protected)
721 BFlags |= llvm::DIType::FlagProtected;
722
723 llvm::DIType DTy =
724 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
725 RecordTy, llvm::StringRef(),
Devang Pateld58562e2010-03-09 22:49:11 +0000726 Unit, 0, 0, 0,
Devang Patelca7daed2010-01-28 21:54:15 +0000727 BaseOffset, BFlags,
728 getOrCreateType(BI->getType(),
729 Unit));
730 EltTys.push_back(DTy);
731 }
Devang Patela245c5b2010-01-25 23:32:18 +0000732}
733
Devang Patel4ce3f202010-01-28 18:11:52 +0000734/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000735llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000736 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000737 return VTablePtrType;
738
739 ASTContext &Context = CGM.getContext();
740
741 /* Function type */
Benjamin Kramerad468862010-03-30 11:36:44 +0000742 llvm::DIDescriptor STy = getOrCreateType(Context.IntTy, Unit);
743 llvm::DIArray SElements = DebugFactory.GetOrCreateArray(&STy, 1);
Devang Patel4ce3f202010-01-28 18:11:52 +0000744 llvm::DIType SubTy =
745 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000746 Unit, "", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000747 0, 0, 0, 0, 0, llvm::DIType(), SElements);
748
749 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
750 llvm::DIType vtbl_ptr_type
751 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000752 Unit, "__vtbl_ptr_type", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000753 0, Size, 0, 0, 0, SubTy);
754
Devang Pateld58562e2010-03-09 22:49:11 +0000755 VTablePtrType =
756 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
757 Unit, "", Unit,
758 0, Size, 0, 0, 0, vtbl_ptr_type);
Devang Patel4ce3f202010-01-28 18:11:52 +0000759 return VTablePtrType;
760}
761
Anders Carlsson046c2942010-04-17 20:15:18 +0000762/// getVTableName - Get vtable name for the given Class.
763llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000764 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000765 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000766
767 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000768 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000769 memcpy(StrPtr, Name.data(), Name.length());
770 return llvm::StringRef(StrPtr, Name.length());
771}
772
773
Anders Carlsson046c2942010-04-17 20:15:18 +0000774/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel4ce3f202010-01-28 18:11:52 +0000775/// debug info entry in EltTys vector.
776void CGDebugInfo::
Anders Carlsson046c2942010-04-17 20:15:18 +0000777CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000778 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000779 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000780
781 // If there is a primary base then it will hold vtable info.
782 if (RL.getPrimaryBase())
783 return;
784
785 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000786 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000787 return;
788
789 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
790 llvm::DIType VPTR
791 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Anders Carlsson046c2942010-04-17 20:15:18 +0000792 getVTableName(RD), Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000793 0, Size, 0, 0, 0,
794 getOrCreateVTablePtrType(Unit));
795 EltTys.push_back(VPTR);
796}
797
Devang Patel65e99f22009-02-25 01:36:11 +0000798/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000799llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000800 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000801 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Chris Lattner9c85ba32008-11-10 06:08:34 +0000803 unsigned Tag;
Devang Pateld6c5a262010-02-01 21:52:22 +0000804 if (RD->isStruct())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000805 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Pateld6c5a262010-02-01 21:52:22 +0000806 else if (RD->isUnion())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000807 Tag = llvm::dwarf::DW_TAG_union_type;
808 else {
Devang Pateld6c5a262010-02-01 21:52:22 +0000809 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000810 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000811 }
812
Chris Lattner9c85ba32008-11-10 06:08:34 +0000813 // Get overall information about the record type for the debug info.
Devang Patel8ab870d2010-05-12 23:46:38 +0000814 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
815 unsigned Line = getLineNumber(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000816
Chris Lattner9c85ba32008-11-10 06:08:34 +0000817 // Records and classes and unions can all be recursive. To handle them, we
818 // first generate a debug descriptor for the struct as a forward declaration.
819 // Then (if it is a definition) we go through and get debug info for all of
820 // its members. Finally, we create a descriptor for the complete type (which
821 // may refer to the forward decl if the struct is recursive) and replace all
822 // uses of the forward declaration with the final definition.
Devang Patel0b897992010-07-08 19:56:29 +0000823 llvm::DIDescriptor FDContext =
824 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
825
826 // If this is just a forward declaration, construct an appropriately
827 // marked node and just return it.
828 if (!RD->getDefinition()) {
829 llvm::DICompositeType FwdDecl =
830 DebugFactory.CreateCompositeType(Tag, FDContext, RD->getName(),
831 DefUnit, Line, 0, 0, 0,
832 llvm::DIType::FlagFwdDecl,
833 llvm::DIType(), llvm::DIArray());
834
835 return FwdDecl;
836 }
Devang Pateld0f251b2010-01-20 23:56:40 +0000837
Devang Pateld6c5a262010-02-01 21:52:22 +0000838 // A RD->getName() is not unique. However, the debug info descriptors
Devang Patelce78c972010-02-01 22:51:29 +0000839 // are uniqued so use type name to ensure uniquness.
Benjamin Kramerfea3d4d2010-03-13 12:06:51 +0000840 llvm::SmallString<128> FwdDeclName;
841 llvm::raw_svector_ostream(FwdDeclName) << "fwd.type." << FwdDeclCount++;
Devang Patel0ce73f62009-07-22 18:57:00 +0000842 llvm::DICompositeType FwdDecl =
Devang Patel7573f8b2010-03-09 21:32:27 +0000843 DebugFactory.CreateCompositeType(Tag, FDContext, FwdDeclName,
Devang Patelab71ff52009-11-12 00:51:46 +0000844 DefUnit, Line, 0, 0, 0, 0,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000845 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000846
Devang Patelab699792010-05-07 18:12:35 +0000847 llvm::MDNode *MN = FwdDecl;
848 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000849 // Otherwise, insert it into the TypeCache so that recursive uses will find
850 // it.
Devang Patelab699792010-05-07 18:12:35 +0000851 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000852 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000853 RegionStack.push_back(FwdDeclNode);
854 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000855
856 // Convert all the elements.
857 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
858
Devang Pateld6c5a262010-02-01 21:52:22 +0000859 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000860 if (CXXDecl) {
861 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson046c2942010-04-17 20:15:18 +0000862 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000863 }
Devang Pateld6c5a262010-02-01 21:52:22 +0000864 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +0000865 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +0000866 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +0000867 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +0000868
869 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +0000870 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel0ac8f312010-01-28 00:54:21 +0000871 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
872 ContainingType =
Devang Patelab699792010-05-07 18:12:35 +0000873 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patel0ac8f312010-01-28 00:54:21 +0000874 else if (CXXDecl->isDynamicClass())
Devang Patelab699792010-05-07 18:12:35 +0000875 ContainingType = FwdDecl;
Devang Patela245c5b2010-01-25 23:32:18 +0000876 }
Mike Stump1eb44332009-09-09 15:08:12 +0000877
Chris Lattner9c85ba32008-11-10 06:08:34 +0000878 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000879 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000880
881 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000882 uint64_t Size = CGM.getContext().getTypeSize(Ty);
883 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000884
Devang Patele4c1ea02010-03-11 20:01:48 +0000885 RegionStack.pop_back();
886 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
887 RegionMap.find(Ty->getDecl());
888 if (RI != RegionMap.end())
889 RegionMap.erase(RI);
890
Devang Patel411894b2010-02-01 22:40:08 +0000891 llvm::DIDescriptor RDContext =
892 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel700a1cb2010-07-20 20:24:18 +0000893
894 llvm::StringRef RDName = RD->getName();
895 // If this is a class, include the template arguments also.
896 if (Tag == llvm::dwarf::DW_TAG_class_type)
897 RDName = getClassName(RD);
898
Devang Patel0ce73f62009-07-22 18:57:00 +0000899 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +0000900 DebugFactory.CreateCompositeType(Tag, RDContext,
Devang Patel700a1cb2010-07-20 20:24:18 +0000901 RDName,
Devang Patelab71ff52009-11-12 00:51:46 +0000902 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +0000903 llvm::DIType(), Elements,
904 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000905
906 // Now that we have a real decl for the struct, replace anything using the
907 // old decl with the new one. This will recursively update the debug info.
Eli Friedman14d63652009-11-16 21:04:30 +0000908 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +0000909 RegionMap[RD] = llvm::WeakVH(RealDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000910 return RealDecl;
911}
912
John McCallc12c5bb2010-05-15 11:32:37 +0000913/// CreateType - get objective-c object type.
914llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
915 llvm::DIFile Unit) {
916 // Ignore protocols.
917 return getOrCreateType(Ty->getBaseType(), Unit);
918}
919
Devang Patel9ca36b62009-02-26 21:10:26 +0000920/// CreateType - get objective-c interface type.
921llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000922 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000923 ObjCInterfaceDecl *ID = Ty->getDecl();
Devang Patel9ca36b62009-02-26 21:10:26 +0000924 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Patel9ca36b62009-02-26 21:10:26 +0000925
926 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +0000927 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +0000928 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel17800552010-03-09 00:44:50 +0000929 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000930
Devang Patel9ca36b62009-02-26 21:10:26 +0000931 // To handle recursive interface, we
932 // first generate a debug descriptor for the struct as a forward declaration.
933 // Then (if it is a definition) we go through and get debug info for all of
934 // its members. Finally, we create a descriptor for the complete type (which
935 // may refer to the forward decl if the struct is recursive) and replace all
936 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000937 llvm::DICompositeType FwdDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +0000938 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000939 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000940 llvm::DIType(), llvm::DIArray(),
941 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000942
Devang Patel9ca36b62009-02-26 21:10:26 +0000943 // If this is just a forward declaration, return it.
Devang Pateld6c5a262010-02-01 21:52:22 +0000944 if (ID->isForwardDecl())
Devang Patel9ca36b62009-02-26 21:10:26 +0000945 return FwdDecl;
946
Devang Patelab699792010-05-07 18:12:35 +0000947 llvm::MDNode *MN = FwdDecl;
948 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patel9ca36b62009-02-26 21:10:26 +0000949 // Otherwise, insert it into the TypeCache so that recursive uses will find
950 // it.
Devang Patelab699792010-05-07 18:12:35 +0000951 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000952 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000953 RegionStack.push_back(FwdDeclNode);
954 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000955
956 // Convert all the elements.
957 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
958
Devang Pateld6c5a262010-02-01 21:52:22 +0000959 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +0000960 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000961 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000962 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000963 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000964 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Pateld58562e2010-03-09 22:49:11 +0000965 Unit, "", Unit, 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000966 0 /* offset */, 0, SClassTy);
967 EltTys.push_back(InhTag);
968 }
969
Devang Pateld6c5a262010-02-01 21:52:22 +0000970 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +0000971
972 unsigned FieldNo = 0;
Devang Pateld6c5a262010-02-01 21:52:22 +0000973 for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(),
974 E = ID->ivar_end(); I != E; ++I, ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +0000975 ObjCIvarDecl *Field = *I;
976 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
977
Devang Patel73621622009-11-25 17:37:31 +0000978 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +0000979
Devang Patelde135022009-04-27 22:40:36 +0000980 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +0000981 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +0000982 continue;
983
Devang Patel9ca36b62009-02-26 21:10:26 +0000984 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +0000985 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
986 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel99c20eb2009-03-20 18:24:39 +0000987 QualType FType = Field->getType();
988 uint64_t FieldSize = 0;
989 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000990
Devang Patel99c20eb2009-03-20 18:24:39 +0000991 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Devang Patel99c20eb2009-03-20 18:24:39 +0000993 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000994 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000995 Expr *BitWidth = Field->getBitWidth();
996 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +0000997 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000998
Anders Carlsson20f12a22009-12-06 18:00:51 +0000999 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001000 }
1001
Mike Stump1eb44332009-09-09 15:08:12 +00001002 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
1003
Devang Patelc20482b2009-03-19 00:23:53 +00001004 unsigned Flags = 0;
1005 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
1006 Flags = llvm::DIType::FlagProtected;
1007 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
1008 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Devang Patel9ca36b62009-02-26 21:10:26 +00001010 // Create a DW_TAG_member node to remember the offset of this field in the
1011 // struct. FIXME: This is an absolutely insane way to capture this
1012 // information. When we gut debug info, this should be fixed.
1013 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1014 FieldName, FieldDefUnit,
1015 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +00001016 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +00001017 EltTys.push_back(FieldTy);
1018 }
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Devang Patel9ca36b62009-02-26 21:10:26 +00001020 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +00001021 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +00001022
Devang Patele4c1ea02010-03-11 20:01:48 +00001023 RegionStack.pop_back();
1024 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1025 RegionMap.find(Ty->getDecl());
1026 if (RI != RegionMap.end())
1027 RegionMap.erase(RI);
1028
Devang Patel9ca36b62009-02-26 21:10:26 +00001029 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001030 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1031 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001032
Devang Patel6c1fddf2009-07-27 18:42:03 +00001033 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +00001034 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +00001035 Line, Size, Align, 0, 0, llvm::DIType(),
1036 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +00001037
1038 // Now that we have a real decl for the struct, replace anything using the
1039 // old decl with the new one. This will recursively update the debug info.
Devang Patelffffb032009-11-16 20:09:38 +00001040 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001041 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +00001042
Devang Patel9ca36b62009-02-26 21:10:26 +00001043 return RealDecl;
1044}
1045
Chris Lattner9c85ba32008-11-10 06:08:34 +00001046llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001047 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001048 EnumDecl *ED = Ty->getDecl();
Chris Lattner9c85ba32008-11-10 06:08:34 +00001049
1050 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
1051
1052 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +00001053 for (EnumDecl::enumerator_iterator
Devang Pateld6c5a262010-02-01 21:52:22 +00001054 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +00001055 Enum != EnumEnd; ++Enum) {
Devang Patel73621622009-11-25 17:37:31 +00001056 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001057 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +00001058 }
Mike Stump1eb44332009-09-09 15:08:12 +00001059
Chris Lattner9c85ba32008-11-10 06:08:34 +00001060 // Return a CompositeType for the enum itself.
1061 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +00001062 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001063
Devang Patel8ab870d2010-05-12 23:46:38 +00001064 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1065 unsigned Line = getLineNumber(ED->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001066
Chris Lattner9c85ba32008-11-10 06:08:34 +00001067 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +00001068 uint64_t Size = 0;
1069 unsigned Align = 0;
1070 if (!Ty->isIncompleteType()) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001071 Size = CGM.getContext().getTypeSize(Ty);
1072 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman3189e4b2009-05-04 04:39:55 +00001073 }
Mike Stump1eb44332009-09-09 15:08:12 +00001074
Devang Patelca80a5f2009-10-20 19:55:01 +00001075 llvm::DIType DbgTy =
1076 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Pateld6c5a262010-02-01 21:52:22 +00001077 Unit, ED->getName(), DefUnit, Line,
Devang Patelca80a5f2009-10-20 19:55:01 +00001078 Size, Align, 0, 0,
1079 llvm::DIType(), EltArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001080 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001081}
1082
1083llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001084 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001085 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1086 return CreateType(RT, Unit);
1087 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1088 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001089
Chris Lattner9c85ba32008-11-10 06:08:34 +00001090 return llvm::DIType();
1091}
1092
Devang Patel70c23cd2010-02-23 22:59:39 +00001093llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001094 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001095 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1096 uint64_t NumElems = Ty->getNumElements();
1097 if (NumElems > 0)
1098 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001099
Benjamin Kramerad468862010-03-30 11:36:44 +00001100 llvm::DIDescriptor Subscript = DebugFactory.GetOrCreateSubrange(0, NumElems);
1101 llvm::DIArray SubscriptArray = DebugFactory.GetOrCreateArray(&Subscript, 1);
Devang Patel70c23cd2010-02-23 22:59:39 +00001102
1103 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1104 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1105
1106 return
1107 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001108 Unit, "", Unit,
Devang Patel70c23cd2010-02-23 22:59:39 +00001109 0, Size, Align, 0, 0,
1110 ElementTy, SubscriptArray);
1111}
1112
Chris Lattner9c85ba32008-11-10 06:08:34 +00001113llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001114 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001115 uint64_t Size;
1116 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001117
1118
Nuno Lopes010d5142009-01-28 00:35:17 +00001119 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001120 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001121 Size = 0;
1122 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001123 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001124 } else if (Ty->isIncompleteArrayType()) {
1125 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001126 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001127 } else {
1128 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001129 Size = CGM.getContext().getTypeSize(Ty);
1130 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001131 }
Mike Stump1eb44332009-09-09 15:08:12 +00001132
Chris Lattner9c85ba32008-11-10 06:08:34 +00001133 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1134 // interior arrays, do we care? Why aren't nested arrays represented the
1135 // obvious/recursive way?
1136 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1137 QualType EltTy(Ty, 0);
1138 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +00001139 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001140 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +00001141 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +00001142 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001143 // FIXME: Verify this is right for VLAs.
1144 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1145 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +00001146 }
Mike Stump1eb44332009-09-09 15:08:12 +00001147
Chris Lattner9c85ba32008-11-10 06:08:34 +00001148 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001149 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001150
Devang Patelca80a5f2009-10-20 19:55:01 +00001151 llvm::DIType DbgTy =
1152 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001153 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +00001154 0, Size, Align, 0, 0,
1155 getOrCreateType(EltTy, Unit),
1156 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001157 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001158}
1159
Anders Carlssona031b352009-11-06 19:19:55 +00001160llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001161 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001162 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1163 Ty, Ty->getPointeeType(), Unit);
1164}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001165
Anders Carlsson20f12a22009-12-06 18:00:51 +00001166llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001167 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001168 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1169 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1170
1171 if (!Ty->getPointeeType()->isFunctionType()) {
1172 // We have a data member pointer type.
1173 return PointerDiffDITy;
1174 }
1175
1176 // We have a member function pointer type. Treat it as a struct with two
1177 // ptrdiff_t members.
1178 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1179
1180 uint64_t FieldOffset = 0;
1181 llvm::DIDescriptor ElementTypes[2];
1182
1183 // FIXME: This should probably be a function type instead.
1184 ElementTypes[0] =
1185 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001186 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001187 Info.first, Info.second, FieldOffset, 0,
1188 PointerDiffDITy);
1189 FieldOffset += Info.first;
1190
1191 ElementTypes[1] =
1192 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001193 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001194 Info.first, Info.second, FieldOffset, 0,
1195 PointerDiffDITy);
1196
1197 llvm::DIArray Elements =
1198 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1199 llvm::array_lengthof(ElementTypes));
1200
1201 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1202 U, llvm::StringRef("test"),
Devang Pateld58562e2010-03-09 22:49:11 +00001203 U, 0, FieldOffset,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001204 0, 0, 0, llvm::DIType(), Elements);
1205}
1206
Douglas Gregor840943d2009-12-21 20:18:30 +00001207static QualType UnwrapTypeForDebugInfo(QualType T) {
1208 do {
1209 QualType LastT = T;
1210 switch (T->getTypeClass()) {
1211 default:
1212 return T;
1213 case Type::TemplateSpecialization:
1214 T = cast<TemplateSpecializationType>(T)->desugar();
1215 break;
1216 case Type::TypeOfExpr: {
1217 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1218 T = Ty->getUnderlyingExpr()->getType();
1219 break;
1220 }
1221 case Type::TypeOf:
1222 T = cast<TypeOfType>(T)->getUnderlyingType();
1223 break;
1224 case Type::Decltype:
1225 T = cast<DecltypeType>(T)->getUnderlyingType();
1226 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001227 case Type::Elaborated:
1228 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001229 break;
1230 case Type::SubstTemplateTypeParm:
1231 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1232 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001233 }
1234
1235 assert(T != LastT && "Type unwrapping failed to unwrap!");
1236 if (T == LastT)
1237 return T;
1238 } while (true);
1239
1240 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001241}
1242
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001243/// getOrCreateType - Get the type from the cache or create a new
1244/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001245llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001246 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001247 if (Ty.isNull())
1248 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001249
Douglas Gregor840943d2009-12-21 20:18:30 +00001250 // Unwrap the type as needed for debug information.
1251 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001252
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001253 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001254 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001255 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001256 if (it != TypeCache.end()) {
1257 // Verify that the debug info still exists.
1258 if (&*it->second)
1259 return llvm::DIType(cast<llvm::MDNode>(it->second));
1260 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001261
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001262 // Otherwise create the type.
1263 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001264
1265 // And update the type cache.
Devang Patelab699792010-05-07 18:12:35 +00001266 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001267 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001268}
1269
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001270/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001271llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001272 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001273 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001274 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001275 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001276
Douglas Gregor2101a822009-12-21 19:57:21 +00001277 const char *Diag = 0;
1278
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001279 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001280 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001281#define TYPE(Class, Base)
1282#define ABSTRACT_TYPE(Class, Base)
1283#define NON_CANONICAL_TYPE(Class, Base)
1284#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1285#include "clang/AST/TypeNodes.def"
1286 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001287
Anders Carlssonbfe69952009-11-06 18:24:04 +00001288 // FIXME: Handle these.
1289 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001290 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001291
1292 case Type::Vector:
1293 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001294 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001295 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCallc12c5bb2010-05-15 11:32:37 +00001296 case Type::ObjCObject:
1297 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001298 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001299 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1300 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1301 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1302 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001303 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001304 return CreateType(cast<BlockPointerType>(Ty), Unit);
1305 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001306 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001307 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001308 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001309 case Type::FunctionProto:
1310 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001311 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001312 case Type::ConstantArray:
1313 case Type::VariableArray:
1314 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001315 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001316
1317 case Type::LValueReference:
1318 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1319
Anders Carlsson20f12a22009-12-06 18:00:51 +00001320 case Type::MemberPointer:
1321 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001322
1323 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001324 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001325 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001326 case Type::TypeOfExpr:
1327 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001328 case Type::Decltype:
1329 llvm_unreachable("type should have been unwrapped!");
1330 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001331
1332 case Type::RValueReference:
1333 // FIXME: Implement!
1334 Diag = "rvalue references";
1335 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001336 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001337
1338 assert(Diag && "Fall through without a diagnostic?");
1339 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1340 "debug information for %0 is not yet supported");
1341 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1342 << Diag;
1343 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001344}
1345
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001346/// CreateMemberType - Create new member and increase Offset by FType's size.
1347llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
1348 llvm::StringRef Name,
1349 uint64_t *Offset) {
1350 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1351 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1352 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
1353 llvm::DIType Ty = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1354 Unit, Name, Unit, 0,
1355 FieldSize, FieldAlign,
1356 *Offset, 0, FieldTy);
1357 *Offset += FieldSize;
1358 return Ty;
1359}
1360
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001361/// EmitFunctionStart - Constructs the debug code for entering a function -
1362/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001363void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001364 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001365 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001366
Devang Patel9c6c3a02010-01-14 00:36:21 +00001367 llvm::StringRef Name;
Anders Carlsson9a20d552010-06-22 16:16:50 +00001368 llvm::StringRef LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001369
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001370 FnBeginRegionCount.push_back(RegionStack.size());
1371
Devang Patel9c6c3a02010-01-14 00:36:21 +00001372 const Decl *D = GD.getDecl();
1373 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001374 // If there is a DISubprogram for this function available then use it.
1375 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1376 FI = SPCache.find(FD);
1377 if (FI != SPCache.end()) {
Devang Patel0804e6e2010-03-08 20:53:17 +00001378 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
Devang Patelab699792010-05-07 18:12:35 +00001379 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1380 llvm::MDNode *SPN = SP;
1381 RegionStack.push_back(SPN);
1382 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00001383 return;
1384 }
1385 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001386 Name = getFunctionName(FD);
1387 // Use mangled name as linkage name for c/c++ functions.
Anders Carlsson9a20d552010-06-22 16:16:50 +00001388 LinkageName = CGM.getMangledName(GD);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001389 } else {
1390 // Use llvm function name as linkage name.
1391 Name = Fn->getName();
Anders Carlsson9a20d552010-06-22 16:16:50 +00001392 LinkageName = Name;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001393 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001394 if (!Name.empty() && Name[0] == '\01')
1395 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001396
Devang Patel970c6182010-04-24 00:49:16 +00001397 // It is expected that CurLoc is set before using EmitFunctionStart.
1398 // Usually, CurLoc points to the left bracket location of compound
1399 // statement representing function body.
1400 llvm::DIFile Unit = getOrCreateFile(CurLoc);
Devang Patel8ab870d2010-05-12 23:46:38 +00001401 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001402
Chris Lattner9c85ba32008-11-10 06:08:34 +00001403 llvm::DISubprogram SP =
Devang Patel970c6182010-04-24 00:49:16 +00001404 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001405 getOrCreateType(FnType, Unit),
Devang Patel15a3d7d2010-07-15 23:09:46 +00001406 Fn->hasInternalLinkage(), true/*definition*/,
1407 0, 0, llvm::DIType(),
1408 D->isImplicit(),
1409 CGM.getLangOptions().Optimize, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +00001410
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001411 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001412 llvm::MDNode *SPN = SP;
1413 RegionStack.push_back(SPN);
1414 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001415
1416 // Clear stack used to keep track of #line directives.
1417 LineDirectiveFiles.clear();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001418}
1419
1420
Devang Patel4d939e62010-07-20 22:20:10 +00001421void CGDebugInfo::EmitStopPoint(CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001422 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001423
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001424 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001425 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001426 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001427 || (SM.getInstantiationLineNumber(CurLoc) ==
1428 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001429 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patel4800ea62010-04-05 21:09:15 +00001430 // New Builder may not be in sync with CGDebugInfo.
1431 if (!Builder.getCurrentDebugLocation().isUnknown())
1432 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001433
1434 // Update last state.
1435 PrevLoc = CurLoc;
1436
Chris Lattnerc6034632010-04-01 06:31:43 +00001437 llvm::MDNode *Scope = RegionStack.back();
Devang Patel8ab870d2010-05-12 23:46:38 +00001438 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1439 getColumnNumber(CurLoc),
Chris Lattnere541d012010-04-02 20:21:43 +00001440 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001441}
1442
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001443/// UpdateLineDirectiveRegion - Update region stack only if #line directive
1444/// has introduced scope change.
1445void CGDebugInfo::UpdateLineDirectiveRegion(CGBuilderTy &Builder) {
1446 if (CurLoc.isInvalid() || CurLoc.isMacroID() ||
1447 PrevLoc.isInvalid() || PrevLoc.isMacroID())
1448 return;
1449 SourceManager &SM = CGM.getContext().getSourceManager();
1450 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
1451 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc);
1452
1453 if (!strcmp(PPLoc.getFilename(), PCLoc.getFilename()))
1454 return;
1455
1456 // If #line directive stack is empty then we are entering a new scope.
1457 if (LineDirectiveFiles.empty()) {
1458 EmitRegionStart(Builder);
1459 LineDirectiveFiles.push_back(PCLoc.getFilename());
1460 return;
1461 }
1462
1463 assert (RegionStack.size() >= LineDirectiveFiles.size()
1464 && "error handling #line regions!");
1465
1466 bool SeenThisFile = false;
1467 for(std::vector<const char *>::iterator I = LineDirectiveFiles.begin(),
1468 E = LineDirectiveFiles.end(); I != E; ++I)
1469 if (!strcmp(PPLoc.getFilename(), *I)) {
1470 SeenThisFile = true;
1471 break;
1472 }
1473
1474 // If #line for this file is seen earlier then pop out #line regions.
1475 if (SeenThisFile) {
1476 while (!LineDirectiveFiles.empty()) {
1477 const char *LastFile = LineDirectiveFiles.back();
1478 RegionStack.pop_back();
1479 LineDirectiveFiles.pop_back();
1480 if (!strcmp(PPLoc.getFilename(), LastFile))
1481 break;
1482 }
1483 return;
1484 }
1485
1486 // .. otherwise insert new #line region.
1487 EmitRegionStart(Builder);
1488 LineDirectiveFiles.push_back(PCLoc.getFilename());
1489
1490 return;
1491}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001492/// EmitRegionStart- Constructs the debug code for entering a declarative
1493/// region - "llvm.dbg.region.start.".
Devang Patel4d939e62010-07-20 22:20:10 +00001494void CGDebugInfo::EmitRegionStart(CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001495 llvm::DIDescriptor D =
1496 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1497 llvm::DIDescriptor() :
Devang Pateld19429f2010-02-16 21:41:20 +00001498 llvm::DIDescriptor(RegionStack.back()),
Stuart Hastings257d1d32010-07-19 23:56:31 +00001499 getOrCreateFile(CurLoc),
Devang Patel8ab870d2010-05-12 23:46:38 +00001500 getLineNumber(CurLoc),
1501 getColumnNumber(CurLoc));
Devang Patelab699792010-05-07 18:12:35 +00001502 llvm::MDNode *DN = D;
1503 RegionStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001504}
1505
1506/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1507/// region - "llvm.dbg.region.end."
Devang Patel4d939e62010-07-20 22:20:10 +00001508void CGDebugInfo::EmitRegionEnd(CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001509 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1510
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001511 // Provide an region stop point.
Devang Patel4d939e62010-07-20 22:20:10 +00001512 EmitStopPoint(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001513
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001514 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001515}
1516
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001517/// EmitFunctionEnd - Constructs the debug code for exiting a function.
1518void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
1519 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1520 unsigned RCount = FnBeginRegionCount.back();
1521 assert(RCount <= RegionStack.size() && "Region stack mismatch");
1522
1523 // Pop all regions for this function.
1524 while (RegionStack.size() != RCount)
1525 EmitRegionEnd(Builder);
1526 FnBeginRegionCount.pop_back();
1527}
1528
Devang Patel809b9bb2010-02-10 18:49:08 +00001529// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1530// See BuildByRefType.
1531llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1532 uint64_t *XOffset) {
1533
1534 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1535
1536 QualType FType;
1537 uint64_t FieldSize, FieldOffset;
1538 unsigned FieldAlign;
1539
Devang Patel17800552010-03-09 00:44:50 +00001540 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001541 QualType Type = VD->getType();
1542
1543 FieldOffset = 0;
1544 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001545 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1546 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001547 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001548 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1549 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1550
Devang Patel809b9bb2010-02-10 18:49:08 +00001551 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1552 if (HasCopyAndDispose) {
1553 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001554 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1555 &FieldOffset));
1556 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1557 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001558 }
1559
1560 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1561 if (Align > CharUnits::fromQuantity(
1562 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1563 unsigned AlignedOffsetInBytes
1564 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1565 unsigned NumPaddingBytes
1566 = AlignedOffsetInBytes - FieldOffset/8;
1567
1568 if (NumPaddingBytes > 0) {
1569 llvm::APInt pad(32, NumPaddingBytes);
1570 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1571 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001572 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001573 }
1574 }
1575
1576 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001577 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00001578 FieldSize = CGM.getContext().getTypeSize(FType);
1579 FieldAlign = Align.getQuantity()*8;
1580
1581 *XOffset = FieldOffset;
1582 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001583 VD->getName(), Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001584 0, FieldSize, FieldAlign,
1585 FieldOffset, 0, FieldTy);
1586 EltTys.push_back(FieldTy);
1587 FieldOffset += FieldSize;
1588
1589 llvm::DIArray Elements =
1590 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1591
1592 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1593
1594 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001595 Unit, "", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001596 0, FieldOffset, 0, 0, Flags,
1597 llvm::DIType(), Elements);
1598
1599}
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001600/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001601void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001602 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001603 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1604
Devang Patel17800552010-03-09 00:44:50 +00001605 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001606 llvm::DIType Ty;
1607 uint64_t XOffset = 0;
1608 if (VD->hasAttr<BlocksAttr>())
1609 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1610 else
1611 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001612
Devang Patelf4e54a22010-05-07 23:05:55 +00001613 // If there is not any debug info for type then do not emit debug info
1614 // for this variable.
1615 if (!Ty)
1616 return;
1617
Chris Lattner9c85ba32008-11-10 06:08:34 +00001618 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001619 unsigned Line = getLineNumber(VD->getLocation());
1620 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001621
Chris Lattner9c85ba32008-11-10 06:08:34 +00001622 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001623 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001624 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel239cec62010-02-01 21:39:52 +00001625 VD->getName(),
Devang Patel44eeeba2010-06-05 01:14:40 +00001626 Unit, Line, Ty, CGM.getLangOptions().Optimize);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001627 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001628 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001629 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001630
Chris Lattnerc6034632010-04-01 06:31:43 +00001631 llvm::MDNode *Scope = RegionStack.back();
Chris Lattnere541d012010-04-02 20:21:43 +00001632 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001633}
1634
Mike Stumpb1a6e682009-09-30 02:43:10 +00001635/// EmitDeclare - Emit local variable declaration debug info.
1636void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1637 llvm::Value *Storage, CGBuilderTy &Builder,
1638 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001639 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001640 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1641
Devang Patel2b594b92010-04-26 23:28:46 +00001642 if (Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001643 return;
1644
1645 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001646 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001647 llvm::DIType Ty;
1648 if (VD->hasAttr<BlocksAttr>())
1649 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1650 else
1651 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001652
1653 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001654 unsigned Line = getLineNumber(VD->getLocation());
1655 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001656
Devang Pateld6c5a262010-02-01 21:52:22 +00001657 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001658 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001659 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1660 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1661 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1662 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001663 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001664 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1665 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001666 // offset of __forwarding field
1667 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001668 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1669 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1670 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001671 // offset of x field
1672 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001673 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001674 }
1675
1676 // Create the descriptor for the variable.
1677 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001678 DebugFactory.CreateComplexVariable(Tag,
1679 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001680 VD->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001681 addr);
1682 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001683 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001684 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattnerd5b89022009-12-28 21:44:41 +00001685
Chris Lattnerc6034632010-04-01 06:31:43 +00001686 llvm::MDNode *Scope = RegionStack.back();
Devang Patelf8e10a52010-05-10 23:48:38 +00001687 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001688}
1689
Devang Pateld6c5a262010-02-01 21:52:22 +00001690void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001691 llvm::Value *Storage,
1692 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001693 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001694}
1695
Mike Stumpb1a6e682009-09-30 02:43:10 +00001696void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1697 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1698 CodeGenFunction *CGF) {
1699 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1700}
1701
Chris Lattner9c85ba32008-11-10 06:08:34 +00001702/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1703/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001704void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001705 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001706 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001707}
1708
1709
1710
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001711/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001712void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001713 const VarDecl *D) {
1714
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001715 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001716 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001717 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +00001718
Devang Pateleb6d79b2010-02-01 21:34:11 +00001719 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001720 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001721
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001722 // CodeGen turns int[] into int[1] so we'll do the same here.
1723 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001724
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001725 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001726 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001727
Anders Carlsson20f12a22009-12-06 18:00:51 +00001728 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001729 ArrayType::Normal, 0);
1730 }
Devang Patel5d822f02010-04-29 17:48:37 +00001731 llvm::StringRef DeclName = D->getName();
Devang Patel8b90a782010-05-13 23:52:37 +00001732 llvm::StringRef LinkageName;
Devang Patel0fd3d1f2010-05-14 16:55:25 +00001733 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()))
Devang Patel8b90a782010-05-13 23:52:37 +00001734 LinkageName = Var->getName();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001735 llvm::DIDescriptor DContext =
1736 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
Devang Patel8b90a782010-05-13 23:52:37 +00001737 DebugFactory.CreateGlobalVariable(DContext, DeclName, DeclName, LinkageName,
1738 Unit, LineNo, getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001739 Var->hasInternalLinkage(),
1740 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001741}
1742
Devang Patel9ca36b62009-02-26 21:10:26 +00001743/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001744void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001745 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001746 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001747 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001748 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patel9ca36b62009-02-26 21:10:26 +00001749
Devang Pateld6c5a262010-02-01 21:52:22 +00001750 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001751
Devang Pateld6c5a262010-02-01 21:52:22 +00001752 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001753 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001754
Devang Patel9ca36b62009-02-26 21:10:26 +00001755 // CodeGen turns int[] into int[1] so we'll do the same here.
1756 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001757
Devang Patel9ca36b62009-02-26 21:10:26 +00001758 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001759 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001760
Anders Carlsson20f12a22009-12-06 18:00:51 +00001761 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001762 ArrayType::Normal, 0);
1763 }
1764
Devang Patelf6a39b72009-10-20 18:26:30 +00001765 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001766 getOrCreateType(T, Unit),
1767 Var->hasInternalLinkage(),
1768 true/*definition*/, Var);
1769}
Devang Patelabb485f2010-02-01 19:16:32 +00001770
1771/// getOrCreateNamesSpace - Return namespace descriptor for the given
1772/// namespace decl.
1773llvm::DINameSpace
1774CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1775 llvm::DIDescriptor Unit) {
1776 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1777 NameSpaceCache.find(NSDecl);
1778 if (I != NameSpaceCache.end())
1779 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1780
Devang Patel8ab870d2010-05-12 23:46:38 +00001781 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patelabb485f2010-02-01 19:16:32 +00001782
1783 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00001784 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00001785 llvm::DINameSpace NS =
1786 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
Devang Patelab699792010-05-07 18:12:35 +00001787 llvm::DIFile(Unit), LineNo);
1788 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00001789 return NS;
1790}