blob: bf5f9746da835cd425f5e175b38269d1c0e91b3e [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 Patel2ed8f002010-08-27 17:47:47 +000018#include "clang/AST/DeclFriend.h"
Devang Patel9ca36b62009-02-26 21:10:26 +000019#include "clang/AST/DeclObjC.h"
Devang Patel700a1cb2010-07-20 20:24:18 +000020#include "clang/AST/DeclTemplate.h"
Chris Lattner3cc5c402008-11-11 07:01:36 +000021#include "clang/AST/Expr.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000022#include "clang/AST/RecordLayout.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000023#include "clang/Basic/SourceManager.h"
24#include "clang/Basic/FileManager.h"
Mike Stump5a862172009-09-15 21:48:34 +000025#include "clang/Basic/Version.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000026#include "clang/Frontend/CodeGenOptions.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000027#include "llvm/Constants.h"
28#include "llvm/DerivedTypes.h"
29#include "llvm/Instructions.h"
30#include "llvm/Intrinsics.h"
31#include "llvm/Module.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000032#include "llvm/ADT/StringExtras.h"
33#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000034#include "llvm/Support/Dwarf.h"
Devang Patel446c6192009-04-17 21:06:59 +000035#include "llvm/System/Path.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000036#include "llvm/Target/TargetMachine.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000037using namespace clang;
38using namespace clang::CodeGen;
39
Anders Carlsson20f12a22009-12-06 18:00:51 +000040CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Devang Patel17800552010-03-09 00:44:50 +000041 : CGM(CGM), DebugFactory(CGM.getModule()),
Dan Gohman4cac5b42010-08-20 22:02:57 +000042 BlockLiteralGenericSet(false) {
Devang Patel17800552010-03-09 00:44:50 +000043 CreateCompileUnit();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000044}
45
Chris Lattner9c85ba32008-11-10 06:08:34 +000046CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar66031a52008-10-17 16:15:48 +000047 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000048}
49
Chris Lattner9c85ba32008-11-10 06:08:34 +000050void CGDebugInfo::setLocation(SourceLocation Loc) {
51 if (Loc.isValid())
Anders Carlsson20f12a22009-12-06 18:00:51 +000052 CurLoc = CGM.getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000053}
54
Devang Patel33583052010-01-28 23:15:27 +000055/// getContextDescriptor - Get context info for the decl.
Devang Pateleb6d79b2010-02-01 21:34:11 +000056llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context,
Devang Patel33583052010-01-28 23:15:27 +000057 llvm::DIDescriptor &CompileUnit) {
Devang Pateleb6d79b2010-02-01 21:34:11 +000058 if (!Context)
59 return CompileUnit;
60
61 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
62 I = RegionMap.find(Context);
63 if (I != RegionMap.end())
Gabor Greif38c9b172010-09-18 13:00:17 +000064 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(&*I->second));
Devang Patel411894b2010-02-01 22:40:08 +000065
Devang Pateleb6d79b2010-02-01 21:34:11 +000066 // Check namespace.
67 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
68 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl, CompileUnit));
Devang Patel8b90a782010-05-13 23:52:37 +000069
70 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context)) {
71 if (!RDecl->isDependentType()) {
72 llvm::DIType Ty = getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
73 llvm::DIFile(CompileUnit));
74 return llvm::DIDescriptor(Ty);
75 }
76 }
Devang Patel979ec2e2009-10-06 00:35:31 +000077 return CompileUnit;
78}
79
Devang Patel9c6c3a02010-01-14 00:36:21 +000080/// getFunctionName - Get function name for the given FunctionDecl. If the
81/// name is constructred on demand (e.g. C++ destructor) then the name
82/// is stored on the side.
83llvm::StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
84 assert (FD && "Invalid FunctionDecl!");
85 IdentifierInfo *FII = FD->getIdentifier();
86 if (FII)
87 return FII->getName();
88
89 // Otherwise construct human readable name for debug info.
90 std::string NS = FD->getNameAsString();
91
92 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +000093 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer1b627dc2010-01-23 18:16:07 +000094 memcpy(StrPtr, NS.data(), NS.length());
95 return llvm::StringRef(StrPtr, NS.length());
Devang Patel9c6c3a02010-01-14 00:36:21 +000096}
97
David Chisnall52044a22010-09-02 18:01:51 +000098llvm::StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
99 llvm::SmallString<256> MethodName;
100 llvm::raw_svector_ostream OS(MethodName);
101 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
102 const DeclContext *DC = OMD->getDeclContext();
103 if (const ObjCImplementationDecl *OID = dyn_cast<const ObjCImplementationDecl>(DC)) {
104 OS << OID->getName();
105 } else if (const ObjCCategoryImplDecl *OCD = dyn_cast<const ObjCCategoryImplDecl>(DC)){
106 OS << ((NamedDecl *)OCD)->getIdentifier()->getNameStart() << '(' <<
107 OCD->getIdentifier()->getNameStart() << ')';
108 }
109 OS << ' ' << OMD->getSelector().getAsString() << ']';
110
111 char *StrPtr = DebugInfoNames.Allocate<char>(OS.tell());
112 memcpy(StrPtr, MethodName.begin(), OS.tell());
113 return llvm::StringRef(StrPtr, OS.tell());
114}
115
Devang Patel700a1cb2010-07-20 20:24:18 +0000116/// getClassName - Get class name including template argument list.
117llvm::StringRef
118CGDebugInfo::getClassName(RecordDecl *RD) {
119 ClassTemplateSpecializationDecl *Spec
120 = dyn_cast<ClassTemplateSpecializationDecl>(RD);
121 if (!Spec)
122 return RD->getName();
123
124 const TemplateArgument *Args;
125 unsigned NumArgs;
126 std::string Buffer;
127 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
128 const TemplateSpecializationType *TST =
129 cast<TemplateSpecializationType>(TAW->getType());
130 Args = TST->getArgs();
131 NumArgs = TST->getNumArgs();
132 } else {
133 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
134 Args = TemplateArgs.getFlatArgumentList();
135 NumArgs = TemplateArgs.flat_size();
136 }
137 Buffer = RD->getIdentifier()->getNameStart();
138 PrintingPolicy Policy(CGM.getLangOptions());
139 Buffer += TemplateSpecializationType::PrintTemplateArgumentList(Args,
140 NumArgs,
141 Policy);
142
143 // Copy this name on the side and use its reference.
144 char *StrPtr = DebugInfoNames.Allocate<char>(Buffer.length());
145 memcpy(StrPtr, Buffer.data(), Buffer.length());
146 return llvm::StringRef(StrPtr, Buffer.length());
147
148}
149
Devang Patel17800552010-03-09 00:44:50 +0000150/// getOrCreateFile - Get the file debug info descriptor for the input location.
151llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Ted Kremenek9c250392010-03-30 00:27:51 +0000152 if (!Loc.isValid())
Devang Patel17800552010-03-09 00:44:50 +0000153 // If Location is not valid then use main input file.
154 return DebugFactory.CreateFile(TheCU.getFilename(), TheCU.getDirectory(),
155 TheCU);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000156 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel17800552010-03-09 00:44:50 +0000157 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Ted Kremenek9c250392010-03-30 00:27:51 +0000158
159 // Cache the results.
160 const char *fname = PLoc.getFilename();
161 llvm::DenseMap<const char *, llvm::WeakVH>::iterator it =
162 DIFileCache.find(fname);
163
164 if (it != DIFileCache.end()) {
165 // Verify that the information still exists.
166 if (&*it->second)
167 return llvm::DIFile(cast<llvm::MDNode>(it->second));
168 }
169
Devang Patelac4d13c2010-07-27 15:17:16 +0000170 llvm::DIFile F = DebugFactory.CreateFile(PLoc.getFilename(),
171 getCurrentDirname(), TheCU);
Ted Kremenek9c250392010-03-30 00:27:51 +0000172
Devang Patelab699792010-05-07 18:12:35 +0000173 DIFileCache[fname] = F;
Ted Kremenek9c250392010-03-30 00:27:51 +0000174 return F;
175
Devang Patel17800552010-03-09 00:44:50 +0000176}
Devang Patel8ab870d2010-05-12 23:46:38 +0000177
178/// getLineNumber - Get line number for the location. If location is invalid
179/// then use current location.
180unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
181 assert (CurLoc.isValid() && "Invalid current location!");
182 SourceManager &SM = CGM.getContext().getSourceManager();
183 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
184 return PLoc.getLine();
185}
186
187/// getColumnNumber - Get column number for the location. If location is
188/// invalid then use current location.
189unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) {
190 assert (CurLoc.isValid() && "Invalid current location!");
191 SourceManager &SM = CGM.getContext().getSourceManager();
192 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
193 return PLoc.getColumn();
194}
195
Devang Patelac4d13c2010-07-27 15:17:16 +0000196llvm::StringRef CGDebugInfo::getCurrentDirname() {
197 if (!CWDName.empty())
198 return CWDName;
199 char *CompDirnamePtr = NULL;
200 llvm::sys::Path CWD = llvm::sys::Path::GetCurrentDirectory();
201 CompDirnamePtr = DebugInfoNames.Allocate<char>(CWD.size());
202 memcpy(CompDirnamePtr, CWD.c_str(), CWD.size());
203 return CWDName = llvm::StringRef(CompDirnamePtr, CWD.size());
204}
205
Devang Patel17800552010-03-09 00:44:50 +0000206/// CreateCompileUnit - Create new compile unit.
207void CGDebugInfo::CreateCompileUnit() {
208
209 // Get absolute path name.
Douglas Gregorac91b4c2010-03-18 23:46:43 +0000210 SourceManager &SM = CGM.getContext().getSourceManager();
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000211 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
212 if (MainFileName.empty())
Devang Patel22fe5852010-03-12 21:04:27 +0000213 MainFileName = "<unknown>";
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000214
Douglas Gregorf6728fc2010-03-22 21:28:29 +0000215 // The main file name provided via the "-main-file-name" option contains just
216 // the file name itself with no path information. This file name may have had
217 // a relative path, so we look into the actual file entry for the main
218 // file to determine the real absolute path for the file.
Devang Patel6e6bc392010-07-23 23:04:28 +0000219 std::string MainFileDir;
Devang Patelac4d13c2010-07-27 15:17:16 +0000220 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000221 MainFileDir = MainFile->getDir()->getName();
Devang Patelac4d13c2010-07-27 15:17:16 +0000222 if (MainFileDir != ".")
223 MainFileName = MainFileDir + "/" + MainFileName;
224 }
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000225
Devang Patelac4d13c2010-07-27 15:17:16 +0000226 // Save filename string.
227 char *FilenamePtr = DebugInfoNames.Allocate<char>(MainFileName.length());
228 memcpy(FilenamePtr, MainFileName.c_str(), MainFileName.length());
229 llvm::StringRef Filename(FilenamePtr, MainFileName.length());
230
Chris Lattner515455a2009-03-25 03:28:08 +0000231 unsigned LangTag;
Devang Patel17800552010-03-09 00:44:50 +0000232 const LangOptions &LO = CGM.getLangOptions();
Chris Lattner515455a2009-03-25 03:28:08 +0000233 if (LO.CPlusPlus) {
234 if (LO.ObjC1)
235 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
236 else
237 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
238 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000239 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000240 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000241 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000242 } else {
243 LangTag = llvm::dwarf::DW_LANG_C89;
244 }
Devang Patel446c6192009-04-17 21:06:59 +0000245
Daniel Dunbar19f19832010-08-24 17:41:09 +0000246 std::string Producer = getClangFullVersion();
Chris Lattner4c2577a2009-05-02 01:00:04 +0000247
248 // Figure out which version of the ObjC runtime we have.
249 unsigned RuntimeVers = 0;
250 if (LO.ObjC1)
251 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000253 // Create new compile unit.
Devang Patel17800552010-03-09 00:44:50 +0000254 TheCU = DebugFactory.CreateCompileUnit(
Devang Patel58115002010-07-27 20:49:59 +0000255 LangTag, Filename, getCurrentDirname(),
Devang Patelac4d13c2010-07-27 15:17:16 +0000256 Producer, true,
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000257 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000258}
259
Devang Patel65e99f22009-02-25 01:36:11 +0000260/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000261/// one if necessary.
262llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel17800552010-03-09 00:44:50 +0000263 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000264 unsigned Encoding = 0;
Devang Patel05127ca2010-07-28 23:23:29 +0000265 const char *BTName = NULL;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000266 switch (BT->getKind()) {
267 default:
268 case BuiltinType::Void:
269 return llvm::DIType();
Devang Patelc8972c62010-07-28 01:33:15 +0000270 case BuiltinType::ObjCClass:
Devang Pateleaf5ee92010-07-21 22:41:25 +0000271 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Patelc8972c62010-07-28 01:33:15 +0000272 Unit, "objc_class", Unit, 0, 0, 0, 0,
Devang Pateleaf5ee92010-07-21 22:41:25 +0000273 llvm::DIType::FlagFwdDecl,
274 llvm::DIType(), llvm::DIArray());
Devang Patelc8972c62010-07-28 01:33:15 +0000275 case BuiltinType::ObjCId: {
276 // typedef struct objc_class *Class;
277 // typedef struct objc_object {
278 // Class isa;
279 // } *id;
280
281 llvm::DIType OCTy =
282 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
283 Unit, "objc_class", Unit, 0, 0, 0, 0,
284 llvm::DIType::FlagFwdDecl,
285 llvm::DIType(), llvm::DIArray());
286 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
287
288 llvm::DIType ISATy =
289 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
290 Unit, "", Unit,
291 0, Size, 0, 0, 0, OCTy);
292
293 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
294
295 llvm::DIType FieldTy =
296 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
297 "isa", Unit,
298 0,Size, 0, 0, 0, ISATy);
299 EltTys.push_back(FieldTy);
300 llvm::DIArray Elements =
301 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
302
303 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
304 Unit, "objc_object", Unit, 0, 0, 0, 0,
305 0,
306 llvm::DIType(), Elements);
307 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000308 case BuiltinType::UChar:
309 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
310 case BuiltinType::Char_S:
311 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
312 case BuiltinType::UShort:
313 case BuiltinType::UInt:
314 case BuiltinType::ULong:
315 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
316 case BuiltinType::Short:
317 case BuiltinType::Int:
318 case BuiltinType::Long:
319 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
320 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
321 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000322 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000323 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000324 }
Devang Patel05127ca2010-07-28 23:23:29 +0000325
326 switch (BT->getKind()) {
327 case BuiltinType::Long: BTName = "long int"; break;
328 case BuiltinType::LongLong: BTName = "long long int"; break;
329 case BuiltinType::ULong: BTName = "long unsigned int"; break;
330 case BuiltinType::ULongLong: BTName = "long long unsigned int"; break;
331 default:
332 BTName = BT->getName(CGM.getContext().getLangOptions());
333 break;
334 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000335 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000336 uint64_t Size = CGM.getContext().getTypeSize(BT);
337 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000338 uint64_t Offset = 0;
Devang Patel05127ca2010-07-28 23:23:29 +0000339
Devang Patelca80a5f2009-10-20 19:55:01 +0000340 llvm::DIType DbgTy =
Devang Patel05127ca2010-07-28 23:23:29 +0000341 DebugFactory.CreateBasicType(Unit, BTName,
Devang Patelca80a5f2009-10-20 19:55:01 +0000342 Unit, 0, Size, Align,
343 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000344 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000345}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000346
Chris Lattnerb7003772009-04-23 06:13:01 +0000347llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000348 llvm::DIFile Unit) {
Chris Lattnerb7003772009-04-23 06:13:01 +0000349 // Bit size, align and offset of the type.
350 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
351 if (Ty->isComplexIntegerType())
352 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Anders Carlsson20f12a22009-12-06 18:00:51 +0000354 uint64_t Size = CGM.getContext().getTypeSize(Ty);
355 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattnerb7003772009-04-23 06:13:01 +0000356 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Devang Patelca80a5f2009-10-20 19:55:01 +0000358 llvm::DIType DbgTy =
359 DebugFactory.CreateBasicType(Unit, "complex",
360 Unit, 0, Size, Align,
361 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000362 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000363}
364
John McCalla1805292009-09-25 01:40:47 +0000365/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000366/// a new one if necessary.
Devang Patel17800552010-03-09 00:44:50 +0000367llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +0000368 QualifierCollector Qc;
369 const Type *T = Qc.strip(Ty);
370
371 // Ignore these qualifiers for now.
372 Qc.removeObjCGCAttr();
373 Qc.removeAddressSpace();
374
Chris Lattner9c85ba32008-11-10 06:08:34 +0000375 // We will create one Derived type for one qualifier and recurse to handle any
376 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000377 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000378 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000379 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000380 Qc.removeConst();
381 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000382 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000383 Qc.removeVolatile();
384 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000385 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000386 Qc.removeRestrict();
387 } else {
388 assert(Qc.empty() && "Unknown type qualifier for debug info");
389 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000390 }
Mike Stump1eb44332009-09-09 15:08:12 +0000391
John McCalla1805292009-09-25 01:40:47 +0000392 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
393
Daniel Dunbar3845f862008-10-31 03:54:29 +0000394 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
395 // CVR derived types.
Devang Patelca80a5f2009-10-20 19:55:01 +0000396 llvm::DIType DbgTy =
Devang Pateld58562e2010-03-09 22:49:11 +0000397 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000398 0, 0, 0, 0, 0, FromTy);
Devang Patelca80a5f2009-10-20 19:55:01 +0000399 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000400}
401
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000402llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000403 llvm::DIFile Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000404 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000405 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
406 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000407 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000408}
409
Chris Lattner9c85ba32008-11-10 06:08:34 +0000410llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000411 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000412 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
413 Ty->getPointeeType(), Unit);
414}
415
416llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
417 const Type *Ty,
418 QualType PointeeTy,
Devang Patel17800552010-03-09 00:44:50 +0000419 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000420 llvm::DIType EltTy = getOrCreateType(PointeeTy, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000422 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000423
424 // Size is always the size of a pointer. We can't use getTypeSize here
425 // because that does not return the correct value for references.
426 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000427 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
428 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Devang Patelca80a5f2009-10-20 19:55:01 +0000430 return
Devang Pateld58562e2010-03-09 22:49:11 +0000431 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000432 0, Size, Align, 0, 0, EltTy);
Anders Carlssona031b352009-11-06 19:19:55 +0000433
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000434}
435
Mike Stump9bc093c2009-05-14 02:03:51 +0000436llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000437 llvm::DIFile Unit) {
Mike Stump9bc093c2009-05-14 02:03:51 +0000438 if (BlockLiteralGenericSet)
439 return BlockLiteralGeneric;
440
Mike Stump9bc093c2009-05-14 02:03:51 +0000441 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
442
443 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
444
445 llvm::DIType FieldTy;
446
447 QualType FType;
448 uint64_t FieldSize, FieldOffset;
449 unsigned FieldAlign;
450
451 llvm::DIArray Elements;
452 llvm::DIType EltTy, DescTy;
453
454 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000455 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000456 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
457 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000458
Daniel Dunbarca308df2009-05-26 19:40:20 +0000459 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000460 EltTys.clear();
461
Mike Stump3d363c52009-10-02 02:30:50 +0000462 unsigned Flags = llvm::DIType::FlagAppleBlock;
Devang Patel8ab870d2010-05-12 23:46:38 +0000463 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump3d363c52009-10-02 02:30:50 +0000464
Mike Stump9bc093c2009-05-14 02:03:51 +0000465 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Devang Patel8ab870d2010-05-12 23:46:38 +0000466 Unit, LineNo, FieldOffset, 0, 0,
467 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Mike Stump9bc093c2009-05-14 02:03:51 +0000469 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000470 uint64_t Size = CGM.getContext().getTypeSize(Ty);
471 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Mike Stump9bc093c2009-05-14 02:03:51 +0000473 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000474 Unit, "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000475 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000476
477 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000478 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000479 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson20f12a22009-12-06 18:00:51 +0000480 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000481 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
482 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramerd3651cc2010-04-24 20:26:20 +0000483 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000484 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000485
Anders Carlsson20f12a22009-12-06 18:00:51 +0000486 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000487 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000488 FieldSize = CGM.getContext().getTypeSize(Ty);
489 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000490 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000491 "__descriptor", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000492 LineNo, FieldSize, FieldAlign,
Mike Stump9bc093c2009-05-14 02:03:51 +0000493 FieldOffset, 0, FieldTy);
494 EltTys.push_back(FieldTy);
495
496 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000497 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000498
499 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Devang Patel8ab870d2010-05-12 23:46:38 +0000500 Unit, LineNo, FieldOffset, 0, 0,
501 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Mike Stump9bc093c2009-05-14 02:03:51 +0000503 BlockLiteralGenericSet = true;
504 BlockLiteralGeneric
505 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000506 "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000507 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000508 return BlockLiteralGeneric;
509}
510
Chris Lattner9c85ba32008-11-10 06:08:34 +0000511llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000512 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000513 // Typedefs are derived from some other type. If we have a typedef of a
514 // typedef, make sure to emit the whole chain.
515 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Chris Lattner9c85ba32008-11-10 06:08:34 +0000517 // We don't set size information, but do specify where the typedef was
518 // declared.
Devang Patel8ab870d2010-05-12 23:46:38 +0000519 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000520
Devang Pateleb6d79b2010-02-01 21:34:11 +0000521 llvm::DIDescriptor TyContext
522 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
523 Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000524 llvm::DIType DbgTy =
Devang Pateld5289052010-01-29 22:29:31 +0000525 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Pateleb6d79b2010-02-01 21:34:11 +0000526 TyContext,
Devang Pateld5289052010-01-29 22:29:31 +0000527 Ty->getDecl()->getName(), Unit,
528 Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000529 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000530}
531
Chris Lattner9c85ba32008-11-10 06:08:34 +0000532llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000533 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000534 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000535
Chris Lattner9c85ba32008-11-10 06:08:34 +0000536 // Add the result type at least.
537 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Chris Lattner9c85ba32008-11-10 06:08:34 +0000539 // Set up remainder of arguments if there is a prototype.
540 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000541 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000542 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
543 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
544 } else {
545 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000546 }
547
Chris Lattner9c85ba32008-11-10 06:08:34 +0000548 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000549 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Devang Patelca80a5f2009-10-20 19:55:01 +0000551 llvm::DIType DbgTy =
552 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000553 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000554 0, 0, 0, 0, 0,
555 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000556 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000557}
558
Devang Patel428deb52010-01-19 00:00:59 +0000559/// CollectRecordFields - A helper function to collect debug info for
560/// record fields. This is used while creating debug info entry for a Record.
561void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000562CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit,
Devang Patel428deb52010-01-19 00:00:59 +0000563 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
564 unsigned FieldNo = 0;
Devang Patel239cec62010-02-01 21:39:52 +0000565 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
566 for (RecordDecl::field_iterator I = RD->field_begin(),
567 E = RD->field_end();
Devang Patel428deb52010-01-19 00:00:59 +0000568 I != E; ++I, ++FieldNo) {
569 FieldDecl *Field = *I;
570 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Devang Patel428deb52010-01-19 00:00:59 +0000571 llvm::StringRef FieldName = Field->getName();
572
Devang Patel4835fdd2010-02-12 01:31:06 +0000573 // Ignore unnamed fields. Do not ignore unnamed records.
574 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel428deb52010-01-19 00:00:59 +0000575 continue;
576
577 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +0000578 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
579 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel428deb52010-01-19 00:00:59 +0000580 QualType FType = Field->getType();
581 uint64_t FieldSize = 0;
582 unsigned FieldAlign = 0;
583 if (!FType->isIncompleteArrayType()) {
584
585 // Bit size, align and offset of the type.
586 FieldSize = CGM.getContext().getTypeSize(FType);
587 Expr *BitWidth = Field->getBitWidth();
588 if (BitWidth)
589 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Devang Patel428deb52010-01-19 00:00:59 +0000590 FieldAlign = CGM.getContext().getTypeAlign(FType);
591 }
592
593 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
594
Devang Patel71f4ff62010-04-21 23:12:37 +0000595 unsigned Flags = 0;
596 AccessSpecifier Access = I->getAccess();
597 if (Access == clang::AS_private)
598 Flags |= llvm::DIType::FlagPrivate;
599 else if (Access == clang::AS_protected)
600 Flags |= llvm::DIType::FlagProtected;
601
Devang Patel428deb52010-01-19 00:00:59 +0000602 // Create a DW_TAG_member node to remember the offset of this field in the
603 // struct. FIXME: This is an absolutely insane way to capture this
604 // information. When we gut debug info, this should be fixed.
605 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
606 FieldName, FieldDefUnit,
607 FieldLine, FieldSize, FieldAlign,
Devang Patel71f4ff62010-04-21 23:12:37 +0000608 FieldOffset, Flags, FieldTy);
Devang Patel428deb52010-01-19 00:00:59 +0000609 EltTys.push_back(FieldTy);
610 }
611}
612
Devang Patela6da1922010-01-28 00:28:01 +0000613/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
614/// function type is not updated to include implicit "this" pointer. Use this
615/// routine to get a method type which includes "this" pointer.
616llvm::DIType
617CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000618 llvm::DIFile Unit) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +0000619 llvm::DIType FnTy
620 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
621 0),
622 Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000623
Devang Patel2ed8f002010-08-27 17:47:47 +0000624 unsigned BFlags=0;
625 AccessSpecifier Access = Method->getAccess();
626 if (Access == clang::AS_private)
627 BFlags |= llvm::DIType::FlagPrivate;
628 else if (Access == clang::AS_protected)
629 BFlags |= llvm::DIType::FlagProtected;
Devang Pateld774d1e2010-01-28 21:43:50 +0000630
Devang Patela6da1922010-01-28 00:28:01 +0000631 // Add "this" pointer.
632
Devang Patelab699792010-05-07 18:12:35 +0000633 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patela6da1922010-01-28 00:28:01 +0000634 assert (Args.getNumElements() && "Invalid number of arguments!");
635
636 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
637
638 // First element is always return type. For 'void' functions it is NULL.
639 Elts.push_back(Args.getElement(0));
640
Devang Patel2ed8f002010-08-27 17:47:47 +0000641 if (!Method->isStatic())
642 {
643 // "this" pointer is always first argument.
644 ASTContext &Context = CGM.getContext();
645 QualType ThisPtr =
646 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
647 llvm::DIType ThisPtrType =
648 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
Devang Patel769640e2010-07-13 00:24:30 +0000649
Devang Patel2ed8f002010-08-27 17:47:47 +0000650 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
651 Elts.push_back(ThisPtrType);
652 }
Devang Patela6da1922010-01-28 00:28:01 +0000653
654 // Copy rest of the arguments.
655 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
656 Elts.push_back(Args.getElement(i));
657
658 llvm::DIArray EltTypeArray =
659 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
660
661 return
662 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000663 Unit, "", Unit,
Devang Patela6da1922010-01-28 00:28:01 +0000664 0, 0, 0, 0, 0,
665 llvm::DIType(), EltTypeArray);
666}
667
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000668/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
669/// a single member function GlobalDecl.
670llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000671CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000672 llvm::DIFile Unit,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000673 llvm::DIType RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000674 bool IsCtorOrDtor =
675 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
676
677 llvm::StringRef MethodName = getFunctionName(Method);
Devang Patela6da1922010-01-28 00:28:01 +0000678 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000679
680 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
681 // make sense to give a single ctor/dtor a linkage name.
Anders Carlsson9a20d552010-06-22 16:16:50 +0000682 llvm::StringRef MethodLinkageName;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000683 if (!IsCtorOrDtor)
Anders Carlsson9a20d552010-06-22 16:16:50 +0000684 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000685
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000686 // Get the location for the method.
Devang Patel8ab870d2010-05-12 23:46:38 +0000687 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
688 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000689
690 // Collect virtual method info.
691 llvm::DIType ContainingType;
692 unsigned Virtuality = 0;
693 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000694
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000695 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000696 if (Method->isPure())
697 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
698 else
699 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
700
701 // It doesn't make sense to give a virtual destructor a vtable index,
702 // since a single destructor has two entries in the vtable.
703 if (!isa<CXXDestructorDecl>(Method))
Anders Carlsson046c2942010-04-17 20:15:18 +0000704 VIndex = CGM.getVTables().getMethodVTableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000705 ContainingType = RecordTy;
706 }
707
708 llvm::DISubprogram SP =
709 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
710 MethodLinkageName,
711 MethodDefUnit, MethodLine,
712 MethodTy, /*isLocalToUnit=*/false,
Devang Patela5c5bab2010-07-12 22:54:41 +0000713 /* isDefintion=*/ false,
Devang Patel40894912010-07-15 22:57:00 +0000714 Virtuality, VIndex, ContainingType,
Devang Patel15a3d7d2010-07-15 23:09:46 +0000715 Method->isImplicit(),
716 CGM.getLangOptions().Optimize);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000717
718 // Don't cache ctors or dtors since we have to emit multiple functions for
719 // a single ctor or dtor.
720 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
Devang Patelab699792010-05-07 18:12:35 +0000721 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000722
723 return SP;
724}
725
Devang Patel4125fd22010-01-19 01:54:44 +0000726/// CollectCXXMemberFunctions - A helper function to collect debug info for
727/// C++ member functions.This is used while creating debug info entry for
728/// a Record.
729void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000730CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000731 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000732 llvm::DIType RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000733 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
734 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000735 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000736
Devang Pateld5322da2010-02-09 19:09:28 +0000737 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000738 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000739
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000740 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000741 }
742}
743
Devang Patel2ed8f002010-08-27 17:47:47 +0000744/// CollectCXXFriends - A helper function to collect debug info for
745/// C++ base classes. This is used while creating debug info entry for
746/// a Record.
747void CGDebugInfo::
748CollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit,
749 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
750 llvm::DIType RecordTy) {
751
752 for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(),
753 BE = RD->friend_end(); BI != BE; ++BI) {
754
755 TypeSourceInfo *TInfo = (*BI)->getFriendType();
756 if(TInfo)
757 {
758 llvm::DIType Ty = getOrCreateType(TInfo->getType(), Unit);
759
760 llvm::DIType DTy =
761 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_friend,
762 RecordTy, llvm::StringRef(),
763 Unit, 0, 0, 0,
764 0, 0, Ty);
765
766 EltTys.push_back(DTy);
767 }
768
769 }
770}
771
Devang Patela245c5b2010-01-25 23:32:18 +0000772/// CollectCXXBases - A helper function to collect debug info for
773/// C++ base classes. This is used while creating debug info entry for
774/// a Record.
775void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000776CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000777 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000778 llvm::DIType RecordTy) {
Devang Patela245c5b2010-01-25 23:32:18 +0000779
Devang Patel239cec62010-02-01 21:39:52 +0000780 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
781 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
782 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000783 unsigned BFlags = 0;
784 uint64_t BaseOffset;
785
786 const CXXRecordDecl *Base =
787 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
788
789 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000790 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000791 // expression where it expects +ve number.
Anders Carlssonaf440352010-03-23 04:11:45 +0000792 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base);
Devang Patelca7daed2010-01-28 21:54:15 +0000793 BFlags = llvm::DIType::FlagVirtual;
794 } else
795 BaseOffset = RL.getBaseClassOffset(Base);
796
797 AccessSpecifier Access = BI->getAccessSpecifier();
798 if (Access == clang::AS_private)
799 BFlags |= llvm::DIType::FlagPrivate;
800 else if (Access == clang::AS_protected)
801 BFlags |= llvm::DIType::FlagProtected;
802
803 llvm::DIType DTy =
804 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
805 RecordTy, llvm::StringRef(),
Devang Pateld58562e2010-03-09 22:49:11 +0000806 Unit, 0, 0, 0,
Devang Patelca7daed2010-01-28 21:54:15 +0000807 BaseOffset, BFlags,
808 getOrCreateType(BI->getType(),
809 Unit));
810 EltTys.push_back(DTy);
811 }
Devang Patela245c5b2010-01-25 23:32:18 +0000812}
813
Devang Patel4ce3f202010-01-28 18:11:52 +0000814/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000815llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000816 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000817 return VTablePtrType;
818
819 ASTContext &Context = CGM.getContext();
820
821 /* Function type */
Benjamin Kramerad468862010-03-30 11:36:44 +0000822 llvm::DIDescriptor STy = getOrCreateType(Context.IntTy, Unit);
823 llvm::DIArray SElements = DebugFactory.GetOrCreateArray(&STy, 1);
Devang Patel4ce3f202010-01-28 18:11:52 +0000824 llvm::DIType SubTy =
825 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000826 Unit, "", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000827 0, 0, 0, 0, 0, llvm::DIType(), SElements);
828
829 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
830 llvm::DIType vtbl_ptr_type
831 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000832 Unit, "__vtbl_ptr_type", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000833 0, Size, 0, 0, 0, SubTy);
834
Devang Pateld58562e2010-03-09 22:49:11 +0000835 VTablePtrType =
836 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
837 Unit, "", Unit,
838 0, Size, 0, 0, 0, vtbl_ptr_type);
Devang Patel4ce3f202010-01-28 18:11:52 +0000839 return VTablePtrType;
840}
841
Anders Carlsson046c2942010-04-17 20:15:18 +0000842/// getVTableName - Get vtable name for the given Class.
843llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000844 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000845 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000846
847 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000848 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000849 memcpy(StrPtr, Name.data(), Name.length());
850 return llvm::StringRef(StrPtr, Name.length());
851}
852
853
Anders Carlsson046c2942010-04-17 20:15:18 +0000854/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel4ce3f202010-01-28 18:11:52 +0000855/// debug info entry in EltTys vector.
856void CGDebugInfo::
Anders Carlsson046c2942010-04-17 20:15:18 +0000857CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000858 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000859 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000860
861 // If there is a primary base then it will hold vtable info.
862 if (RL.getPrimaryBase())
863 return;
864
865 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000866 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000867 return;
868
869 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
870 llvm::DIType VPTR
871 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Anders Carlsson046c2942010-04-17 20:15:18 +0000872 getVTableName(RD), Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000873 0, Size, 0, 0, 0,
874 getOrCreateVTablePtrType(Unit));
875 EltTys.push_back(VPTR);
876}
877
Devang Patel65e99f22009-02-25 01:36:11 +0000878/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000879llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000880 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000881 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000882
Chris Lattner9c85ba32008-11-10 06:08:34 +0000883 unsigned Tag;
Devang Pateld6c5a262010-02-01 21:52:22 +0000884 if (RD->isStruct())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000885 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Pateld6c5a262010-02-01 21:52:22 +0000886 else if (RD->isUnion())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000887 Tag = llvm::dwarf::DW_TAG_union_type;
888 else {
Devang Pateld6c5a262010-02-01 21:52:22 +0000889 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000890 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000891 }
892
Chris Lattner9c85ba32008-11-10 06:08:34 +0000893 // Get overall information about the record type for the debug info.
Devang Patel8ab870d2010-05-12 23:46:38 +0000894 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
895 unsigned Line = getLineNumber(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000896
Chris Lattner9c85ba32008-11-10 06:08:34 +0000897 // Records and classes and unions can all be recursive. To handle them, we
898 // first generate a debug descriptor for the struct as a forward declaration.
899 // Then (if it is a definition) we go through and get debug info for all of
900 // its members. Finally, we create a descriptor for the complete type (which
901 // may refer to the forward decl if the struct is recursive) and replace all
902 // uses of the forward declaration with the final definition.
Devang Patel0b897992010-07-08 19:56:29 +0000903 llvm::DIDescriptor FDContext =
904 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
905
906 // If this is just a forward declaration, construct an appropriately
907 // marked node and just return it.
908 if (!RD->getDefinition()) {
909 llvm::DICompositeType FwdDecl =
910 DebugFactory.CreateCompositeType(Tag, FDContext, RD->getName(),
911 DefUnit, Line, 0, 0, 0,
912 llvm::DIType::FlagFwdDecl,
913 llvm::DIType(), llvm::DIArray());
914
915 return FwdDecl;
916 }
Devang Pateld0f251b2010-01-20 23:56:40 +0000917
Dan Gohman86968372010-08-20 22:39:57 +0000918 llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType();
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Devang Patelab699792010-05-07 18:12:35 +0000920 llvm::MDNode *MN = FwdDecl;
921 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000922 // Otherwise, insert it into the TypeCache so that recursive uses will find
923 // it.
Devang Patelab699792010-05-07 18:12:35 +0000924 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000925 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000926 RegionStack.push_back(FwdDeclNode);
927 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000928
929 // Convert all the elements.
930 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
931
Devang Pateld6c5a262010-02-01 21:52:22 +0000932 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000933 if (CXXDecl) {
934 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson046c2942010-04-17 20:15:18 +0000935 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000936 }
Devang Pateldabc3e92010-08-12 00:02:44 +0000937
938 // Collect static variables with initializers.
939 for (RecordDecl::decl_iterator I = RD->decls_begin(), E = RD->decls_end();
940 I != E; ++I)
941 if (const VarDecl *V = dyn_cast<VarDecl>(*I)) {
942 if (const Expr *Init = V->getInit()) {
943 Expr::EvalResult Result;
944 if (Init->Evaluate(Result, CGM.getContext()) && Result.Val.isInt()) {
945 llvm::ConstantInt *CI
946 = llvm::ConstantInt::get(CGM.getLLVMContext(), Result.Val.getInt());
947
948 // Create the descriptor for static variable.
949 llvm::DIFile VUnit = getOrCreateFile(V->getLocation());
950 llvm::StringRef VName = V->getName();
951 llvm::DIType VTy = getOrCreateType(V->getType(), VUnit);
952 // Do not use DIGlobalVariable for enums.
953 if (VTy.getTag() != llvm::dwarf::DW_TAG_enumeration_type) {
954 DebugFactory.CreateGlobalVariable(FwdDecl, VName, VName, VName, VUnit,
955 getLineNumber(V->getLocation()),
956 VTy, true, true, CI);
957 }
958 }
959 }
960 }
961
Devang Pateld6c5a262010-02-01 21:52:22 +0000962 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +0000963 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +0000964 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +0000965 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel2ed8f002010-08-27 17:47:47 +0000966 CollectCXXFriends(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +0000967
968 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +0000969 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel0ac8f312010-01-28 00:54:21 +0000970 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
971 ContainingType =
Devang Patelab699792010-05-07 18:12:35 +0000972 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patel0ac8f312010-01-28 00:54:21 +0000973 else if (CXXDecl->isDynamicClass())
Devang Patelab699792010-05-07 18:12:35 +0000974 ContainingType = FwdDecl;
Devang Patela245c5b2010-01-25 23:32:18 +0000975 }
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Chris Lattner9c85ba32008-11-10 06:08:34 +0000977 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000978 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000979
980 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000981 uint64_t Size = CGM.getContext().getTypeSize(Ty);
982 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000983
Devang Patele4c1ea02010-03-11 20:01:48 +0000984 RegionStack.pop_back();
985 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
986 RegionMap.find(Ty->getDecl());
987 if (RI != RegionMap.end())
988 RegionMap.erase(RI);
989
Devang Patel411894b2010-02-01 22:40:08 +0000990 llvm::DIDescriptor RDContext =
991 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel700a1cb2010-07-20 20:24:18 +0000992
993 llvm::StringRef RDName = RD->getName();
994 // If this is a class, include the template arguments also.
995 if (Tag == llvm::dwarf::DW_TAG_class_type)
996 RDName = getClassName(RD);
997
Devang Patel0ce73f62009-07-22 18:57:00 +0000998 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +0000999 DebugFactory.CreateCompositeType(Tag, RDContext,
Devang Patel700a1cb2010-07-20 20:24:18 +00001000 RDName,
Devang Patelab71ff52009-11-12 00:51:46 +00001001 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +00001002 llvm::DIType(), Elements,
1003 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001004
1005 // Now that we have a real decl for the struct, replace anything using the
1006 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001007 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001008 RegionMap[RD] = llvm::WeakVH(RealDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001009 return RealDecl;
1010}
1011
John McCallc12c5bb2010-05-15 11:32:37 +00001012/// CreateType - get objective-c object type.
1013llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1014 llvm::DIFile Unit) {
1015 // Ignore protocols.
1016 return getOrCreateType(Ty->getBaseType(), Unit);
1017}
1018
Devang Patel9ca36b62009-02-26 21:10:26 +00001019/// CreateType - get objective-c interface type.
1020llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001021 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001022 ObjCInterfaceDecl *ID = Ty->getDecl();
Devang Patel9ca36b62009-02-26 21:10:26 +00001023 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Patel9ca36b62009-02-26 21:10:26 +00001024
1025 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +00001026 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001027 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel17800552010-03-09 00:44:50 +00001028 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +00001029
Dan Gohman45f7c782010-08-23 21:15:56 +00001030 // If this is just a forward declaration, return a special forward-declaration
1031 // debug type.
1032 if (ID->isForwardDecl()) {
1033 llvm::DICompositeType FwdDecl =
1034 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
1035 DefUnit, Line, 0, 0, 0, 0,
1036 llvm::DIType(), llvm::DIArray(),
1037 RuntimeLang);
1038 return FwdDecl;
1039 }
1040
Devang Patel9ca36b62009-02-26 21:10:26 +00001041 // To handle recursive interface, we
1042 // first generate a debug descriptor for the struct as a forward declaration.
1043 // Then (if it is a definition) we go through and get debug info for all of
1044 // its members. Finally, we create a descriptor for the complete type (which
1045 // may refer to the forward decl if the struct is recursive) and replace all
1046 // uses of the forward declaration with the final definition.
Dan Gohman86968372010-08-20 22:39:57 +00001047 llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType();
Mike Stump1eb44332009-09-09 15:08:12 +00001048
Devang Patelab699792010-05-07 18:12:35 +00001049 llvm::MDNode *MN = FwdDecl;
1050 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patel9ca36b62009-02-26 21:10:26 +00001051 // Otherwise, insert it into the TypeCache so that recursive uses will find
1052 // it.
Devang Patelab699792010-05-07 18:12:35 +00001053 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +00001054 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001055 RegionStack.push_back(FwdDeclNode);
1056 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001057
1058 // Convert all the elements.
1059 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
1060
Devang Pateld6c5a262010-02-01 21:52:22 +00001061 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +00001062 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +00001063 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001064 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001065 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +00001066 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Pateld58562e2010-03-09 22:49:11 +00001067 Unit, "", Unit, 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +00001068 0 /* offset */, 0, SClassTy);
1069 EltTys.push_back(InhTag);
1070 }
1071
Devang Pateld6c5a262010-02-01 21:52:22 +00001072 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001073
1074 unsigned FieldNo = 0;
Devang Pateld6c5a262010-02-01 21:52:22 +00001075 for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(),
1076 E = ID->ivar_end(); I != E; ++I, ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001077 ObjCIvarDecl *Field = *I;
1078 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1079
Devang Patel73621622009-11-25 17:37:31 +00001080 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001081
Devang Patelde135022009-04-27 22:40:36 +00001082 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +00001083 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +00001084 continue;
1085
Devang Patel9ca36b62009-02-26 21:10:26 +00001086 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +00001087 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1088 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel99c20eb2009-03-20 18:24:39 +00001089 QualType FType = Field->getType();
1090 uint64_t FieldSize = 0;
1091 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +00001092
Devang Patel99c20eb2009-03-20 18:24:39 +00001093 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Devang Patel99c20eb2009-03-20 18:24:39 +00001095 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001096 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001097 Expr *BitWidth = Field->getBitWidth();
1098 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +00001099 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +00001100
Anders Carlsson20f12a22009-12-06 18:00:51 +00001101 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001102 }
1103
Mike Stump1eb44332009-09-09 15:08:12 +00001104 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
1105
Devang Patelc20482b2009-03-19 00:23:53 +00001106 unsigned Flags = 0;
1107 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
1108 Flags = llvm::DIType::FlagProtected;
1109 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
1110 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +00001111
Devang Patel9ca36b62009-02-26 21:10:26 +00001112 // Create a DW_TAG_member node to remember the offset of this field in the
1113 // struct. FIXME: This is an absolutely insane way to capture this
1114 // information. When we gut debug info, this should be fixed.
1115 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1116 FieldName, FieldDefUnit,
1117 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +00001118 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +00001119 EltTys.push_back(FieldTy);
1120 }
Mike Stump1eb44332009-09-09 15:08:12 +00001121
Devang Patel9ca36b62009-02-26 21:10:26 +00001122 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +00001123 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +00001124
Devang Patele4c1ea02010-03-11 20:01:48 +00001125 RegionStack.pop_back();
1126 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1127 RegionMap.find(Ty->getDecl());
1128 if (RI != RegionMap.end())
1129 RegionMap.erase(RI);
1130
Devang Patel9ca36b62009-02-26 21:10:26 +00001131 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001132 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1133 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001134
Devang Patel6c1fddf2009-07-27 18:42:03 +00001135 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +00001136 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +00001137 Line, Size, Align, 0, 0, llvm::DIType(),
1138 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +00001139
1140 // Now that we have a real decl for the struct, replace anything using the
1141 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001142 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001143 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +00001144
Devang Patel9ca36b62009-02-26 21:10:26 +00001145 return RealDecl;
1146}
1147
Chris Lattner9c85ba32008-11-10 06:08:34 +00001148llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001149 llvm::DIFile Unit) {
Devang Patel6237cea2010-08-23 22:07:25 +00001150 return CreateEnumType(Ty->getDecl(), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001151
Chris Lattner9c85ba32008-11-10 06:08:34 +00001152}
1153
1154llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001155 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001156 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1157 return CreateType(RT, Unit);
1158 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1159 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001160
Chris Lattner9c85ba32008-11-10 06:08:34 +00001161 return llvm::DIType();
1162}
1163
Devang Patel70c23cd2010-02-23 22:59:39 +00001164llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Eli Friedmana7e68452010-08-22 01:00:03 +00001165 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001166 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1167 uint64_t NumElems = Ty->getNumElements();
1168 if (NumElems > 0)
1169 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001170
Benjamin Kramerad468862010-03-30 11:36:44 +00001171 llvm::DIDescriptor Subscript = DebugFactory.GetOrCreateSubrange(0, NumElems);
1172 llvm::DIArray SubscriptArray = DebugFactory.GetOrCreateArray(&Subscript, 1);
Devang Patel70c23cd2010-02-23 22:59:39 +00001173
1174 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1175 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1176
1177 return
1178 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001179 Unit, "", Unit,
Devang Patel70c23cd2010-02-23 22:59:39 +00001180 0, Size, Align, 0, 0,
Eli Friedmana7e68452010-08-22 01:00:03 +00001181 ElementTy, SubscriptArray);
Devang Patel70c23cd2010-02-23 22:59:39 +00001182}
1183
Chris Lattner9c85ba32008-11-10 06:08:34 +00001184llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001185 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001186 uint64_t Size;
1187 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001188
1189
Nuno Lopes010d5142009-01-28 00:35:17 +00001190 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001191 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001192 Size = 0;
1193 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001194 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001195 } else if (Ty->isIncompleteArrayType()) {
1196 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001197 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001198 } else {
1199 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001200 Size = CGM.getContext().getTypeSize(Ty);
1201 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001202 }
Mike Stump1eb44332009-09-09 15:08:12 +00001203
Chris Lattner9c85ba32008-11-10 06:08:34 +00001204 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1205 // interior arrays, do we care? Why aren't nested arrays represented the
1206 // obvious/recursive way?
1207 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1208 QualType EltTy(Ty, 0);
1209 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +00001210 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001211 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +00001212 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +00001213 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001214 // FIXME: Verify this is right for VLAs.
1215 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1216 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +00001217 }
Mike Stump1eb44332009-09-09 15:08:12 +00001218
Chris Lattner9c85ba32008-11-10 06:08:34 +00001219 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001220 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001221
Devang Patelca80a5f2009-10-20 19:55:01 +00001222 llvm::DIType DbgTy =
1223 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001224 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +00001225 0, Size, Align, 0, 0,
1226 getOrCreateType(EltTy, Unit),
1227 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001228 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001229}
1230
Anders Carlssona031b352009-11-06 19:19:55 +00001231llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001232 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001233 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1234 Ty, Ty->getPointeeType(), Unit);
1235}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001236
Anders Carlsson20f12a22009-12-06 18:00:51 +00001237llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001238 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001239 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1240 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1241
1242 if (!Ty->getPointeeType()->isFunctionType()) {
1243 // We have a data member pointer type.
1244 return PointerDiffDITy;
1245 }
1246
1247 // We have a member function pointer type. Treat it as a struct with two
1248 // ptrdiff_t members.
1249 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1250
1251 uint64_t FieldOffset = 0;
1252 llvm::DIDescriptor ElementTypes[2];
1253
1254 // FIXME: This should probably be a function type instead.
1255 ElementTypes[0] =
1256 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001257 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001258 Info.first, Info.second, FieldOffset, 0,
1259 PointerDiffDITy);
1260 FieldOffset += Info.first;
1261
1262 ElementTypes[1] =
1263 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001264 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001265 Info.first, Info.second, FieldOffset, 0,
1266 PointerDiffDITy);
1267
1268 llvm::DIArray Elements =
1269 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1270 llvm::array_lengthof(ElementTypes));
1271
1272 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1273 U, llvm::StringRef("test"),
Devang Pateld58562e2010-03-09 22:49:11 +00001274 U, 0, FieldOffset,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001275 0, 0, 0, llvm::DIType(), Elements);
1276}
1277
Devang Patel6237cea2010-08-23 22:07:25 +00001278/// CreateEnumType - get enumeration type.
1279llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED, llvm::DIFile Unit){
1280 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
1281
1282 // Create DIEnumerator elements for each enumerator.
1283 for (EnumDecl::enumerator_iterator
1284 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
1285 Enum != EnumEnd; ++Enum) {
1286 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
1287 Enum->getInitVal().getZExtValue()));
1288 }
1289
1290 // Return a CompositeType for the enum itself.
1291 llvm::DIArray EltArray =
1292 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
1293
1294 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1295 unsigned Line = getLineNumber(ED->getLocation());
1296 uint64_t Size = 0;
Devang Patelffc52e72010-08-24 18:14:06 +00001297 uint64_t Align = 0;
1298 if (!ED->getTypeForDecl()->isIncompleteType()) {
1299 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1300 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1301 }
Devang Patel6237cea2010-08-23 22:07:25 +00001302 llvm::DIType DbgTy =
1303 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
1304 Unit, ED->getName(), DefUnit, Line,
Devang Patelffc52e72010-08-24 18:14:06 +00001305 Size, Align, 0, 0,
Devang Patel6237cea2010-08-23 22:07:25 +00001306 llvm::DIType(), EltArray);
1307 return DbgTy;
1308}
1309
Douglas Gregor840943d2009-12-21 20:18:30 +00001310static QualType UnwrapTypeForDebugInfo(QualType T) {
1311 do {
1312 QualType LastT = T;
1313 switch (T->getTypeClass()) {
1314 default:
1315 return T;
1316 case Type::TemplateSpecialization:
1317 T = cast<TemplateSpecializationType>(T)->desugar();
1318 break;
1319 case Type::TypeOfExpr: {
1320 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1321 T = Ty->getUnderlyingExpr()->getType();
1322 break;
1323 }
1324 case Type::TypeOf:
1325 T = cast<TypeOfType>(T)->getUnderlyingType();
1326 break;
1327 case Type::Decltype:
1328 T = cast<DecltypeType>(T)->getUnderlyingType();
1329 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001330 case Type::Elaborated:
1331 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001332 break;
1333 case Type::SubstTemplateTypeParm:
1334 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1335 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001336 }
1337
1338 assert(T != LastT && "Type unwrapping failed to unwrap!");
1339 if (T == LastT)
1340 return T;
1341 } while (true);
1342
1343 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001344}
1345
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001346/// getOrCreateType - Get the type from the cache or create a new
1347/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001348llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001349 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001350 if (Ty.isNull())
1351 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001352
Douglas Gregor840943d2009-12-21 20:18:30 +00001353 // Unwrap the type as needed for debug information.
1354 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001355
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001356 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001357 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001358 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001359 if (it != TypeCache.end()) {
1360 // Verify that the debug info still exists.
1361 if (&*it->second)
1362 return llvm::DIType(cast<llvm::MDNode>(it->second));
1363 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001364
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001365 // Otherwise create the type.
1366 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001367
1368 // And update the type cache.
Devang Patelab699792010-05-07 18:12:35 +00001369 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001370 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001371}
1372
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001373/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001374llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001375 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001376 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001377 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001378 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001379
Douglas Gregor2101a822009-12-21 19:57:21 +00001380 const char *Diag = 0;
1381
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001382 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001383 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001384#define TYPE(Class, Base)
1385#define ABSTRACT_TYPE(Class, Base)
1386#define NON_CANONICAL_TYPE(Class, Base)
1387#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1388#include "clang/AST/TypeNodes.def"
1389 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001390
Anders Carlssonbfe69952009-11-06 18:24:04 +00001391 // FIXME: Handle these.
1392 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001393 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001394
1395 case Type::Vector:
1396 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001397 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001398 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCallc12c5bb2010-05-15 11:32:37 +00001399 case Type::ObjCObject:
1400 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001401 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001402 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1403 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1404 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1405 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001406 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001407 return CreateType(cast<BlockPointerType>(Ty), Unit);
1408 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001409 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001410 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001411 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001412 case Type::FunctionProto:
1413 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001414 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001415 case Type::ConstantArray:
1416 case Type::VariableArray:
1417 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001418 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001419
1420 case Type::LValueReference:
1421 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1422
Anders Carlsson20f12a22009-12-06 18:00:51 +00001423 case Type::MemberPointer:
1424 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001425
1426 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001427 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001428 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001429 case Type::TypeOfExpr:
1430 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001431 case Type::Decltype:
1432 llvm_unreachable("type should have been unwrapped!");
1433 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001434
1435 case Type::RValueReference:
1436 // FIXME: Implement!
1437 Diag = "rvalue references";
1438 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001439 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001440
1441 assert(Diag && "Fall through without a diagnostic?");
1442 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1443 "debug information for %0 is not yet supported");
1444 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1445 << Diag;
1446 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001447}
1448
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001449/// CreateMemberType - Create new member and increase Offset by FType's size.
1450llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
1451 llvm::StringRef Name,
1452 uint64_t *Offset) {
1453 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1454 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1455 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
1456 llvm::DIType Ty = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1457 Unit, Name, Unit, 0,
1458 FieldSize, FieldAlign,
1459 *Offset, 0, FieldTy);
1460 *Offset += FieldSize;
1461 return Ty;
1462}
1463
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001464/// EmitFunctionStart - Constructs the debug code for entering a function -
1465/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001466void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001467 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001468 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001469
Devang Patel9c6c3a02010-01-14 00:36:21 +00001470 llvm::StringRef Name;
Anders Carlsson9a20d552010-06-22 16:16:50 +00001471 llvm::StringRef LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001472
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001473 FnBeginRegionCount.push_back(RegionStack.size());
1474
Devang Patel9c6c3a02010-01-14 00:36:21 +00001475 const Decl *D = GD.getDecl();
1476 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001477 // If there is a DISubprogram for this function available then use it.
1478 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1479 FI = SPCache.find(FD);
1480 if (FI != SPCache.end()) {
Gabor Greif38c9b172010-09-18 13:00:17 +00001481 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(&*FI->second));
Devang Patelab699792010-05-07 18:12:35 +00001482 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1483 llvm::MDNode *SPN = SP;
1484 RegionStack.push_back(SPN);
1485 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00001486 return;
1487 }
1488 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001489 Name = getFunctionName(FD);
1490 // Use mangled name as linkage name for c/c++ functions.
Anders Carlsson9a20d552010-06-22 16:16:50 +00001491 LinkageName = CGM.getMangledName(GD);
David Chisnall70b9b442010-09-02 17:16:32 +00001492 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
David Chisnall52044a22010-09-02 18:01:51 +00001493 Name = getObjCMethodName(OMD);
David Chisnall70b9b442010-09-02 17:16:32 +00001494 LinkageName = Name;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001495 } else {
1496 // Use llvm function name as linkage name.
1497 Name = Fn->getName();
Anders Carlsson9a20d552010-06-22 16:16:50 +00001498 LinkageName = Name;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001499 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001500 if (!Name.empty() && Name[0] == '\01')
1501 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001502
Devang Patel970c6182010-04-24 00:49:16 +00001503 // It is expected that CurLoc is set before using EmitFunctionStart.
1504 // Usually, CurLoc points to the left bracket location of compound
1505 // statement representing function body.
1506 llvm::DIFile Unit = getOrCreateFile(CurLoc);
Devang Patel8ab870d2010-05-12 23:46:38 +00001507 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001508
Chris Lattner9c85ba32008-11-10 06:08:34 +00001509 llvm::DISubprogram SP =
Devang Patel970c6182010-04-24 00:49:16 +00001510 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001511 getOrCreateType(FnType, Unit),
Devang Patel15a3d7d2010-07-15 23:09:46 +00001512 Fn->hasInternalLinkage(), true/*definition*/,
1513 0, 0, llvm::DIType(),
1514 D->isImplicit(),
1515 CGM.getLangOptions().Optimize, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +00001516
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001517 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001518 llvm::MDNode *SPN = SP;
1519 RegionStack.push_back(SPN);
1520 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001521
1522 // Clear stack used to keep track of #line directives.
1523 LineDirectiveFiles.clear();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001524}
1525
1526
Devang Patel4d939e62010-07-20 22:20:10 +00001527void CGDebugInfo::EmitStopPoint(CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001528 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001529
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001530 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001531 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001532 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001533 || (SM.getInstantiationLineNumber(CurLoc) ==
1534 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001535 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patel4800ea62010-04-05 21:09:15 +00001536 // New Builder may not be in sync with CGDebugInfo.
1537 if (!Builder.getCurrentDebugLocation().isUnknown())
1538 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001539
1540 // Update last state.
1541 PrevLoc = CurLoc;
1542
Chris Lattnerc6034632010-04-01 06:31:43 +00001543 llvm::MDNode *Scope = RegionStack.back();
Devang Patel8ab870d2010-05-12 23:46:38 +00001544 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1545 getColumnNumber(CurLoc),
Chris Lattnere541d012010-04-02 20:21:43 +00001546 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001547}
1548
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001549/// UpdateLineDirectiveRegion - Update region stack only if #line directive
1550/// has introduced scope change.
1551void CGDebugInfo::UpdateLineDirectiveRegion(CGBuilderTy &Builder) {
1552 if (CurLoc.isInvalid() || CurLoc.isMacroID() ||
1553 PrevLoc.isInvalid() || PrevLoc.isMacroID())
1554 return;
1555 SourceManager &SM = CGM.getContext().getSourceManager();
1556 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
1557 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc);
1558
1559 if (!strcmp(PPLoc.getFilename(), PCLoc.getFilename()))
1560 return;
1561
1562 // If #line directive stack is empty then we are entering a new scope.
1563 if (LineDirectiveFiles.empty()) {
1564 EmitRegionStart(Builder);
1565 LineDirectiveFiles.push_back(PCLoc.getFilename());
1566 return;
1567 }
1568
1569 assert (RegionStack.size() >= LineDirectiveFiles.size()
1570 && "error handling #line regions!");
1571
1572 bool SeenThisFile = false;
Devang Patel424a5c62010-09-15 20:50:40 +00001573 // Chek if current file is already seen earlier.
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001574 for(std::vector<const char *>::iterator I = LineDirectiveFiles.begin(),
1575 E = LineDirectiveFiles.end(); I != E; ++I)
Devang Patel424a5c62010-09-15 20:50:40 +00001576 if (!strcmp(PCLoc.getFilename(), *I)) {
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001577 SeenThisFile = true;
1578 break;
1579 }
1580
1581 // If #line for this file is seen earlier then pop out #line regions.
1582 if (SeenThisFile) {
1583 while (!LineDirectiveFiles.empty()) {
1584 const char *LastFile = LineDirectiveFiles.back();
1585 RegionStack.pop_back();
1586 LineDirectiveFiles.pop_back();
1587 if (!strcmp(PPLoc.getFilename(), LastFile))
1588 break;
1589 }
1590 return;
1591 }
1592
1593 // .. otherwise insert new #line region.
1594 EmitRegionStart(Builder);
1595 LineDirectiveFiles.push_back(PCLoc.getFilename());
1596
1597 return;
1598}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001599/// EmitRegionStart- Constructs the debug code for entering a declarative
1600/// region - "llvm.dbg.region.start.".
Devang Patel4d939e62010-07-20 22:20:10 +00001601void CGDebugInfo::EmitRegionStart(CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001602 llvm::DIDescriptor D =
1603 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1604 llvm::DIDescriptor() :
Devang Pateld19429f2010-02-16 21:41:20 +00001605 llvm::DIDescriptor(RegionStack.back()),
Stuart Hastings257d1d32010-07-19 23:56:31 +00001606 getOrCreateFile(CurLoc),
Devang Patel8ab870d2010-05-12 23:46:38 +00001607 getLineNumber(CurLoc),
1608 getColumnNumber(CurLoc));
Devang Patelab699792010-05-07 18:12:35 +00001609 llvm::MDNode *DN = D;
1610 RegionStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001611}
1612
1613/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1614/// region - "llvm.dbg.region.end."
Devang Patel4d939e62010-07-20 22:20:10 +00001615void CGDebugInfo::EmitRegionEnd(CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001616 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1617
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001618 // Provide an region stop point.
Devang Patel4d939e62010-07-20 22:20:10 +00001619 EmitStopPoint(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001620
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001621 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001622}
1623
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001624/// EmitFunctionEnd - Constructs the debug code for exiting a function.
1625void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
1626 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1627 unsigned RCount = FnBeginRegionCount.back();
1628 assert(RCount <= RegionStack.size() && "Region stack mismatch");
1629
1630 // Pop all regions for this function.
1631 while (RegionStack.size() != RCount)
1632 EmitRegionEnd(Builder);
1633 FnBeginRegionCount.pop_back();
1634}
1635
Devang Patel809b9bb2010-02-10 18:49:08 +00001636// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1637// See BuildByRefType.
1638llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1639 uint64_t *XOffset) {
1640
1641 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1642
1643 QualType FType;
1644 uint64_t FieldSize, FieldOffset;
1645 unsigned FieldAlign;
1646
Devang Patel17800552010-03-09 00:44:50 +00001647 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001648 QualType Type = VD->getType();
1649
1650 FieldOffset = 0;
1651 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001652 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1653 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001654 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001655 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1656 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1657
Devang Patel809b9bb2010-02-10 18:49:08 +00001658 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1659 if (HasCopyAndDispose) {
1660 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001661 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1662 &FieldOffset));
1663 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1664 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001665 }
1666
1667 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1668 if (Align > CharUnits::fromQuantity(
1669 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1670 unsigned AlignedOffsetInBytes
1671 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1672 unsigned NumPaddingBytes
1673 = AlignedOffsetInBytes - FieldOffset/8;
1674
1675 if (NumPaddingBytes > 0) {
1676 llvm::APInt pad(32, NumPaddingBytes);
1677 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1678 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001679 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001680 }
1681 }
1682
1683 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001684 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00001685 FieldSize = CGM.getContext().getTypeSize(FType);
1686 FieldAlign = Align.getQuantity()*8;
1687
1688 *XOffset = FieldOffset;
1689 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001690 VD->getName(), Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001691 0, FieldSize, FieldAlign,
1692 FieldOffset, 0, FieldTy);
1693 EltTys.push_back(FieldTy);
1694 FieldOffset += FieldSize;
1695
1696 llvm::DIArray Elements =
1697 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1698
1699 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1700
1701 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001702 Unit, "", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001703 0, FieldOffset, 0, 0, Flags,
1704 llvm::DIType(), Elements);
1705
1706}
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001707/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001708void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001709 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001710 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1711
Devang Patel17800552010-03-09 00:44:50 +00001712 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001713 llvm::DIType Ty;
1714 uint64_t XOffset = 0;
1715 if (VD->hasAttr<BlocksAttr>())
1716 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1717 else
1718 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001719
Devang Patelf4e54a22010-05-07 23:05:55 +00001720 // If there is not any debug info for type then do not emit debug info
1721 // for this variable.
1722 if (!Ty)
1723 return;
1724
Chris Lattner9c85ba32008-11-10 06:08:34 +00001725 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001726 unsigned Line = getLineNumber(VD->getLocation());
1727 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001728
Chris Lattner9c85ba32008-11-10 06:08:34 +00001729 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001730 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001731 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel239cec62010-02-01 21:39:52 +00001732 VD->getName(),
Devang Patel44eeeba2010-06-05 01:14:40 +00001733 Unit, Line, Ty, CGM.getLangOptions().Optimize);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001734 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001735 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001736 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001737
Chris Lattnerc6034632010-04-01 06:31:43 +00001738 llvm::MDNode *Scope = RegionStack.back();
Chris Lattnere541d012010-04-02 20:21:43 +00001739 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001740}
1741
Mike Stumpb1a6e682009-09-30 02:43:10 +00001742/// EmitDeclare - Emit local variable declaration debug info.
1743void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1744 llvm::Value *Storage, CGBuilderTy &Builder,
1745 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001746 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001747 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1748
Devang Patel2b594b92010-04-26 23:28:46 +00001749 if (Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001750 return;
1751
1752 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001753 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001754 llvm::DIType Ty;
1755 if (VD->hasAttr<BlocksAttr>())
1756 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1757 else
1758 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001759
1760 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001761 unsigned Line = getLineNumber(VD->getLocation());
1762 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001763
Devang Pateld6c5a262010-02-01 21:52:22 +00001764 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001765 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001766 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1767 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1768 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1769 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001770 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001771 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1772 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001773 // offset of __forwarding field
1774 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001775 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1776 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1777 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001778 // offset of x field
1779 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001780 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001781 }
1782
1783 // Create the descriptor for the variable.
1784 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001785 DebugFactory.CreateComplexVariable(Tag,
1786 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001787 VD->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001788 addr);
1789 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001790 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001791 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattnerd5b89022009-12-28 21:44:41 +00001792
Chris Lattnerc6034632010-04-01 06:31:43 +00001793 llvm::MDNode *Scope = RegionStack.back();
Devang Patelf8e10a52010-05-10 23:48:38 +00001794 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001795}
1796
Devang Pateld6c5a262010-02-01 21:52:22 +00001797void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001798 llvm::Value *Storage,
1799 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001800 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001801}
1802
Mike Stumpb1a6e682009-09-30 02:43:10 +00001803void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1804 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1805 CodeGenFunction *CGF) {
1806 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1807}
1808
Chris Lattner9c85ba32008-11-10 06:08:34 +00001809/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1810/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001811void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001812 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001813 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001814}
1815
1816
1817
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001818/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001819void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001820 const VarDecl *D) {
1821
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001822 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001823 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001824 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +00001825
Devang Pateleb6d79b2010-02-01 21:34:11 +00001826 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001827 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001828
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001829 // CodeGen turns int[] into int[1] so we'll do the same here.
1830 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001831
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001832 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001833 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001834
Anders Carlsson20f12a22009-12-06 18:00:51 +00001835 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001836 ArrayType::Normal, 0);
1837 }
Devang Patel5d822f02010-04-29 17:48:37 +00001838 llvm::StringRef DeclName = D->getName();
Devang Patel8b90a782010-05-13 23:52:37 +00001839 llvm::StringRef LinkageName;
Devang Patel0fd3d1f2010-05-14 16:55:25 +00001840 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()))
Devang Patel8b90a782010-05-13 23:52:37 +00001841 LinkageName = Var->getName();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001842 llvm::DIDescriptor DContext =
1843 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
Devang Patel8b90a782010-05-13 23:52:37 +00001844 DebugFactory.CreateGlobalVariable(DContext, DeclName, DeclName, LinkageName,
1845 Unit, LineNo, getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001846 Var->hasInternalLinkage(),
1847 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001848}
1849
Devang Patel9ca36b62009-02-26 21:10:26 +00001850/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001851void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001852 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001853 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001854 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001855 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patel9ca36b62009-02-26 21:10:26 +00001856
Devang Pateld6c5a262010-02-01 21:52:22 +00001857 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001858
Devang Pateld6c5a262010-02-01 21:52:22 +00001859 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001860 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001861
Devang Patel9ca36b62009-02-26 21:10:26 +00001862 // CodeGen turns int[] into int[1] so we'll do the same here.
1863 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001864
Devang Patel9ca36b62009-02-26 21:10:26 +00001865 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001866 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001867
Anders Carlsson20f12a22009-12-06 18:00:51 +00001868 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001869 ArrayType::Normal, 0);
1870 }
1871
Devang Patelf6a39b72009-10-20 18:26:30 +00001872 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001873 getOrCreateType(T, Unit),
1874 Var->hasInternalLinkage(),
1875 true/*definition*/, Var);
1876}
Devang Patelabb485f2010-02-01 19:16:32 +00001877
Devang Patel25c2c8f2010-08-10 17:53:33 +00001878/// EmitGlobalVariable - Emit global variable's debug info.
1879void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
1880 llvm::ConstantInt *Init,
Devang Patel8d308382010-08-10 07:24:25 +00001881 CGBuilderTy &Builder) {
1882 // Create the descriptor for the variable.
1883 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
1884 llvm::StringRef Name = VD->getName();
Devang Patel0317ab02010-08-10 18:27:15 +00001885 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
Devang Patel6237cea2010-08-23 22:07:25 +00001886 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
1887 if (const EnumDecl *ED = dyn_cast<EnumDecl>(ECD->getDeclContext()))
1888 Ty = CreateEnumType(ED, Unit);
1889 }
Devang Patel0317ab02010-08-10 18:27:15 +00001890 // Do not use DIGlobalVariable for enums.
1891 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
1892 return;
Devang Patel8d308382010-08-10 07:24:25 +00001893 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit,
1894 getLineNumber(VD->getLocation()),
Devang Patel4c4acc02010-08-10 20:16:57 +00001895 Ty, true, true, Init);
Devang Patel8d308382010-08-10 07:24:25 +00001896}
1897
Devang Patelabb485f2010-02-01 19:16:32 +00001898/// getOrCreateNamesSpace - Return namespace descriptor for the given
1899/// namespace decl.
1900llvm::DINameSpace
1901CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1902 llvm::DIDescriptor Unit) {
1903 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1904 NameSpaceCache.find(NSDecl);
1905 if (I != NameSpaceCache.end())
1906 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1907
Devang Patel8ab870d2010-05-12 23:46:38 +00001908 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patelabb485f2010-02-01 19:16:32 +00001909
1910 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00001911 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00001912 llvm::DINameSpace NS =
1913 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
Devang Patel2ed8f002010-08-27 17:47:47 +00001914 llvm::DIFile(Unit), LineNo);
Devang Patelab699792010-05-07 18:12:35 +00001915 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00001916 return NS;
1917}