blob: 9e8dfb0825c78c449accf73305d75864e7753009 [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 Patele2472482010-09-29 21:05:52 +0000273 llvm::DIDescriptor::FlagFwdDecl,
Devang Pateleaf5ee92010-07-21 22:41:25 +0000274 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,
Devang Patele2472482010-09-29 21:05:52 +0000284 llvm::DIDescriptor::FlagFwdDecl,
Devang Patelc8972c62010-07-28 01:33:15 +0000285 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
Devang Patele2472482010-09-29 21:05:52 +0000462 unsigned Flags = llvm::DIDescriptor::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)
Devang Patele2472482010-09-29 21:05:52 +0000598 Flags |= llvm::DIDescriptor::FlagPrivate;
Devang Patel71f4ff62010-04-21 23:12:37 +0000599 else if (Access == clang::AS_protected)
Devang Patele2472482010-09-29 21:05:52 +0000600 Flags |= llvm::DIDescriptor::FlagProtected;
Devang Patel71f4ff62010-04-21 23:12:37 +0000601
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 Patela6da1922010-01-28 00:28:01 +0000624 // Add "this" pointer.
625
Devang Patelab699792010-05-07 18:12:35 +0000626 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patela6da1922010-01-28 00:28:01 +0000627 assert (Args.getNumElements() && "Invalid number of arguments!");
628
629 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
630
631 // First element is always return type. For 'void' functions it is NULL.
632 Elts.push_back(Args.getElement(0));
633
Devang Patel2ed8f002010-08-27 17:47:47 +0000634 if (!Method->isStatic())
635 {
636 // "this" pointer is always first argument.
637 ASTContext &Context = CGM.getContext();
638 QualType ThisPtr =
639 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
640 llvm::DIType ThisPtrType =
641 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
Devang Patel769640e2010-07-13 00:24:30 +0000642
Devang Patel2ed8f002010-08-27 17:47:47 +0000643 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
644 Elts.push_back(ThisPtrType);
645 }
Devang Patela6da1922010-01-28 00:28:01 +0000646
647 // Copy rest of the arguments.
648 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
649 Elts.push_back(Args.getElement(i));
650
651 llvm::DIArray EltTypeArray =
652 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
653
654 return
655 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000656 Unit, "", Unit,
Devang Patela6da1922010-01-28 00:28:01 +0000657 0, 0, 0, 0, 0,
658 llvm::DIType(), EltTypeArray);
659}
660
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000661/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
662/// a single member function GlobalDecl.
663llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000664CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000665 llvm::DIFile Unit,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000666 llvm::DIType RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000667 bool IsCtorOrDtor =
668 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
669
670 llvm::StringRef MethodName = getFunctionName(Method);
Devang Patela6da1922010-01-28 00:28:01 +0000671 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000672
673 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
674 // make sense to give a single ctor/dtor a linkage name.
Anders Carlsson9a20d552010-06-22 16:16:50 +0000675 llvm::StringRef MethodLinkageName;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000676 if (!IsCtorOrDtor)
Anders Carlsson9a20d552010-06-22 16:16:50 +0000677 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000678
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000679 // Get the location for the method.
Devang Patel8ab870d2010-05-12 23:46:38 +0000680 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
681 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000682
683 // Collect virtual method info.
684 llvm::DIType ContainingType;
685 unsigned Virtuality = 0;
686 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000687
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000688 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000689 if (Method->isPure())
690 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
691 else
692 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
693
694 // It doesn't make sense to give a virtual destructor a vtable index,
695 // since a single destructor has two entries in the vtable.
696 if (!isa<CXXDestructorDecl>(Method))
Anders Carlsson046c2942010-04-17 20:15:18 +0000697 VIndex = CGM.getVTables().getMethodVTableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000698 ContainingType = RecordTy;
699 }
700
Devang Patele2472482010-09-29 21:05:52 +0000701 unsigned Flags = 0;
702 if (Method->isImplicit())
703 Flags |= llvm::DIDescriptor::FlagArtificial;
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000704 llvm::DISubprogram SP =
705 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
706 MethodLinkageName,
707 MethodDefUnit, MethodLine,
708 MethodTy, /*isLocalToUnit=*/false,
Devang Patela5c5bab2010-07-12 22:54:41 +0000709 /* isDefintion=*/ false,
Devang Patel40894912010-07-15 22:57:00 +0000710 Virtuality, VIndex, ContainingType,
Devang Patele2472482010-09-29 21:05:52 +0000711 Flags,
Devang Patel15a3d7d2010-07-15 23:09:46 +0000712 CGM.getLangOptions().Optimize);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000713
714 // Don't cache ctors or dtors since we have to emit multiple functions for
715 // a single ctor or dtor.
716 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
Devang Patelab699792010-05-07 18:12:35 +0000717 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000718
719 return SP;
720}
721
Devang Patel4125fd22010-01-19 01:54:44 +0000722/// CollectCXXMemberFunctions - A helper function to collect debug info for
723/// C++ member functions.This is used while creating debug info entry for
724/// a Record.
725void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000726CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000727 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000728 llvm::DIType RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000729 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
730 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000731 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000732
Devang Pateld5322da2010-02-09 19:09:28 +0000733 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000734 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000735
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000736 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000737 }
738}
739
Devang Patel2ed8f002010-08-27 17:47:47 +0000740/// CollectCXXFriends - A helper function to collect debug info for
741/// C++ base classes. This is used while creating debug info entry for
742/// a Record.
743void CGDebugInfo::
744CollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit,
745 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
746 llvm::DIType RecordTy) {
747
748 for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(),
749 BE = RD->friend_end(); BI != BE; ++BI) {
750
751 TypeSourceInfo *TInfo = (*BI)->getFriendType();
752 if(TInfo)
753 {
754 llvm::DIType Ty = getOrCreateType(TInfo->getType(), Unit);
755
756 llvm::DIType DTy =
757 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_friend,
758 RecordTy, llvm::StringRef(),
759 Unit, 0, 0, 0,
760 0, 0, Ty);
761
762 EltTys.push_back(DTy);
763 }
764
765 }
766}
767
Devang Patela245c5b2010-01-25 23:32:18 +0000768/// CollectCXXBases - A helper function to collect debug info for
769/// C++ base classes. This is used while creating debug info entry for
770/// a Record.
771void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000772CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000773 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000774 llvm::DIType RecordTy) {
Devang Patela245c5b2010-01-25 23:32:18 +0000775
Devang Patel239cec62010-02-01 21:39:52 +0000776 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
777 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
778 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000779 unsigned BFlags = 0;
780 uint64_t BaseOffset;
781
782 const CXXRecordDecl *Base =
783 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
784
785 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000786 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000787 // expression where it expects +ve number.
Anders Carlssonaf440352010-03-23 04:11:45 +0000788 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base);
Devang Patele2472482010-09-29 21:05:52 +0000789 BFlags = llvm::DIDescriptor::FlagVirtual;
Devang Patelca7daed2010-01-28 21:54:15 +0000790 } else
791 BaseOffset = RL.getBaseClassOffset(Base);
792
793 AccessSpecifier Access = BI->getAccessSpecifier();
794 if (Access == clang::AS_private)
Devang Patele2472482010-09-29 21:05:52 +0000795 BFlags |= llvm::DIDescriptor::FlagPrivate;
Devang Patelca7daed2010-01-28 21:54:15 +0000796 else if (Access == clang::AS_protected)
Devang Patele2472482010-09-29 21:05:52 +0000797 BFlags |= llvm::DIDescriptor::FlagProtected;
Devang Patelca7daed2010-01-28 21:54:15 +0000798
799 llvm::DIType DTy =
800 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
801 RecordTy, llvm::StringRef(),
Devang Pateld58562e2010-03-09 22:49:11 +0000802 Unit, 0, 0, 0,
Devang Patelca7daed2010-01-28 21:54:15 +0000803 BaseOffset, BFlags,
804 getOrCreateType(BI->getType(),
805 Unit));
806 EltTys.push_back(DTy);
807 }
Devang Patela245c5b2010-01-25 23:32:18 +0000808}
809
Devang Patel4ce3f202010-01-28 18:11:52 +0000810/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000811llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000812 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000813 return VTablePtrType;
814
815 ASTContext &Context = CGM.getContext();
816
817 /* Function type */
Benjamin Kramerad468862010-03-30 11:36:44 +0000818 llvm::DIDescriptor STy = getOrCreateType(Context.IntTy, Unit);
819 llvm::DIArray SElements = DebugFactory.GetOrCreateArray(&STy, 1);
Devang Patel4ce3f202010-01-28 18:11:52 +0000820 llvm::DIType SubTy =
821 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000822 Unit, "", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000823 0, 0, 0, 0, 0, llvm::DIType(), SElements);
824
825 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
826 llvm::DIType vtbl_ptr_type
827 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000828 Unit, "__vtbl_ptr_type", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000829 0, Size, 0, 0, 0, SubTy);
830
Devang Pateld58562e2010-03-09 22:49:11 +0000831 VTablePtrType =
832 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
833 Unit, "", Unit,
834 0, Size, 0, 0, 0, vtbl_ptr_type);
Devang Patel4ce3f202010-01-28 18:11:52 +0000835 return VTablePtrType;
836}
837
Anders Carlsson046c2942010-04-17 20:15:18 +0000838/// getVTableName - Get vtable name for the given Class.
839llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000840 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000841 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000842
843 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000844 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000845 memcpy(StrPtr, Name.data(), Name.length());
846 return llvm::StringRef(StrPtr, Name.length());
847}
848
849
Anders Carlsson046c2942010-04-17 20:15:18 +0000850/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel4ce3f202010-01-28 18:11:52 +0000851/// debug info entry in EltTys vector.
852void CGDebugInfo::
Anders Carlsson046c2942010-04-17 20:15:18 +0000853CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000854 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000855 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000856
857 // If there is a primary base then it will hold vtable info.
858 if (RL.getPrimaryBase())
859 return;
860
861 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000862 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000863 return;
864
865 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
866 llvm::DIType VPTR
867 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Anders Carlsson046c2942010-04-17 20:15:18 +0000868 getVTableName(RD), Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000869 0, Size, 0, 0, 0,
870 getOrCreateVTablePtrType(Unit));
871 EltTys.push_back(VPTR);
872}
873
Devang Patel65e99f22009-02-25 01:36:11 +0000874/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000875llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000876 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000877 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Chris Lattner9c85ba32008-11-10 06:08:34 +0000879 unsigned Tag;
Devang Pateld6c5a262010-02-01 21:52:22 +0000880 if (RD->isStruct())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000881 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Pateld6c5a262010-02-01 21:52:22 +0000882 else if (RD->isUnion())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000883 Tag = llvm::dwarf::DW_TAG_union_type;
884 else {
Devang Pateld6c5a262010-02-01 21:52:22 +0000885 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000886 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000887 }
888
Chris Lattner9c85ba32008-11-10 06:08:34 +0000889 // Get overall information about the record type for the debug info.
Devang Patel8ab870d2010-05-12 23:46:38 +0000890 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
891 unsigned Line = getLineNumber(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000892
Chris Lattner9c85ba32008-11-10 06:08:34 +0000893 // Records and classes and unions can all be recursive. To handle them, we
894 // first generate a debug descriptor for the struct as a forward declaration.
895 // Then (if it is a definition) we go through and get debug info for all of
896 // its members. Finally, we create a descriptor for the complete type (which
897 // may refer to the forward decl if the struct is recursive) and replace all
898 // uses of the forward declaration with the final definition.
Devang Patel0b897992010-07-08 19:56:29 +0000899 llvm::DIDescriptor FDContext =
900 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
901
902 // If this is just a forward declaration, construct an appropriately
903 // marked node and just return it.
904 if (!RD->getDefinition()) {
905 llvm::DICompositeType FwdDecl =
906 DebugFactory.CreateCompositeType(Tag, FDContext, RD->getName(),
907 DefUnit, Line, 0, 0, 0,
Devang Patele2472482010-09-29 21:05:52 +0000908 llvm::DIDescriptor::FlagFwdDecl,
Devang Patel0b897992010-07-08 19:56:29 +0000909 llvm::DIType(), llvm::DIArray());
910
911 return FwdDecl;
912 }
Devang Pateld0f251b2010-01-20 23:56:40 +0000913
Dan Gohman86968372010-08-20 22:39:57 +0000914 llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType();
Mike Stump1eb44332009-09-09 15:08:12 +0000915
Devang Patelab699792010-05-07 18:12:35 +0000916 llvm::MDNode *MN = FwdDecl;
917 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000918 // Otherwise, insert it into the TypeCache so that recursive uses will find
919 // it.
Devang Patelab699792010-05-07 18:12:35 +0000920 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000921 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000922 RegionStack.push_back(FwdDeclNode);
923 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000924
925 // Convert all the elements.
926 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
927
Devang Pateld6c5a262010-02-01 21:52:22 +0000928 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000929 if (CXXDecl) {
930 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson046c2942010-04-17 20:15:18 +0000931 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000932 }
Devang Pateldabc3e92010-08-12 00:02:44 +0000933
934 // Collect static variables with initializers.
935 for (RecordDecl::decl_iterator I = RD->decls_begin(), E = RD->decls_end();
936 I != E; ++I)
937 if (const VarDecl *V = dyn_cast<VarDecl>(*I)) {
938 if (const Expr *Init = V->getInit()) {
939 Expr::EvalResult Result;
940 if (Init->Evaluate(Result, CGM.getContext()) && Result.Val.isInt()) {
941 llvm::ConstantInt *CI
942 = llvm::ConstantInt::get(CGM.getLLVMContext(), Result.Val.getInt());
943
944 // Create the descriptor for static variable.
945 llvm::DIFile VUnit = getOrCreateFile(V->getLocation());
946 llvm::StringRef VName = V->getName();
947 llvm::DIType VTy = getOrCreateType(V->getType(), VUnit);
948 // Do not use DIGlobalVariable for enums.
949 if (VTy.getTag() != llvm::dwarf::DW_TAG_enumeration_type) {
950 DebugFactory.CreateGlobalVariable(FwdDecl, VName, VName, VName, VUnit,
951 getLineNumber(V->getLocation()),
952 VTy, true, true, CI);
953 }
954 }
955 }
956 }
957
Devang Pateld6c5a262010-02-01 21:52:22 +0000958 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +0000959 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +0000960 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +0000961 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel2ed8f002010-08-27 17:47:47 +0000962 CollectCXXFriends(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +0000963
964 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +0000965 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel0ac8f312010-01-28 00:54:21 +0000966 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
967 ContainingType =
Devang Patelab699792010-05-07 18:12:35 +0000968 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patel0ac8f312010-01-28 00:54:21 +0000969 else if (CXXDecl->isDynamicClass())
Devang Patelab699792010-05-07 18:12:35 +0000970 ContainingType = FwdDecl;
Devang Patela245c5b2010-01-25 23:32:18 +0000971 }
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Chris Lattner9c85ba32008-11-10 06:08:34 +0000973 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000974 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000975
976 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000977 uint64_t Size = CGM.getContext().getTypeSize(Ty);
978 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000979
Devang Patele4c1ea02010-03-11 20:01:48 +0000980 RegionStack.pop_back();
981 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
982 RegionMap.find(Ty->getDecl());
983 if (RI != RegionMap.end())
984 RegionMap.erase(RI);
985
Devang Patel411894b2010-02-01 22:40:08 +0000986 llvm::DIDescriptor RDContext =
987 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel700a1cb2010-07-20 20:24:18 +0000988
989 llvm::StringRef RDName = RD->getName();
990 // If this is a class, include the template arguments also.
991 if (Tag == llvm::dwarf::DW_TAG_class_type)
992 RDName = getClassName(RD);
993
Devang Patel0ce73f62009-07-22 18:57:00 +0000994 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +0000995 DebugFactory.CreateCompositeType(Tag, RDContext,
Devang Patel700a1cb2010-07-20 20:24:18 +0000996 RDName,
Devang Patelab71ff52009-11-12 00:51:46 +0000997 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +0000998 llvm::DIType(), Elements,
999 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001000
1001 // Now that we have a real decl for the struct, replace anything using the
1002 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001003 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001004 RegionMap[RD] = llvm::WeakVH(RealDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001005 return RealDecl;
1006}
1007
John McCallc12c5bb2010-05-15 11:32:37 +00001008/// CreateType - get objective-c object type.
1009llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1010 llvm::DIFile Unit) {
1011 // Ignore protocols.
1012 return getOrCreateType(Ty->getBaseType(), Unit);
1013}
1014
Devang Patel9ca36b62009-02-26 21:10:26 +00001015/// CreateType - get objective-c interface type.
1016llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001017 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001018 ObjCInterfaceDecl *ID = Ty->getDecl();
Devang Patel9ca36b62009-02-26 21:10:26 +00001019 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Patel9ca36b62009-02-26 21:10:26 +00001020
1021 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +00001022 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001023 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel17800552010-03-09 00:44:50 +00001024 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +00001025
Dan Gohman45f7c782010-08-23 21:15:56 +00001026 // If this is just a forward declaration, return a special forward-declaration
1027 // debug type.
1028 if (ID->isForwardDecl()) {
1029 llvm::DICompositeType FwdDecl =
1030 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
1031 DefUnit, Line, 0, 0, 0, 0,
1032 llvm::DIType(), llvm::DIArray(),
1033 RuntimeLang);
1034 return FwdDecl;
1035 }
1036
Devang Patel9ca36b62009-02-26 21:10:26 +00001037 // To handle recursive interface, we
1038 // first generate a debug descriptor for the struct as a forward declaration.
1039 // Then (if it is a definition) we go through and get debug info for all of
1040 // its members. Finally, we create a descriptor for the complete type (which
1041 // may refer to the forward decl if the struct is recursive) and replace all
1042 // uses of the forward declaration with the final definition.
Dan Gohman86968372010-08-20 22:39:57 +00001043 llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType();
Mike Stump1eb44332009-09-09 15:08:12 +00001044
Devang Patelab699792010-05-07 18:12:35 +00001045 llvm::MDNode *MN = FwdDecl;
1046 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patel9ca36b62009-02-26 21:10:26 +00001047 // Otherwise, insert it into the TypeCache so that recursive uses will find
1048 // it.
Devang Patelab699792010-05-07 18:12:35 +00001049 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +00001050 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001051 RegionStack.push_back(FwdDeclNode);
1052 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001053
1054 // Convert all the elements.
1055 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
1056
Devang Pateld6c5a262010-02-01 21:52:22 +00001057 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +00001058 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +00001059 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001060 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001061 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +00001062 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Pateld58562e2010-03-09 22:49:11 +00001063 Unit, "", Unit, 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +00001064 0 /* offset */, 0, SClassTy);
1065 EltTys.push_back(InhTag);
1066 }
1067
Devang Pateld6c5a262010-02-01 21:52:22 +00001068 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001069
1070 unsigned FieldNo = 0;
Devang Pateld6c5a262010-02-01 21:52:22 +00001071 for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(),
1072 E = ID->ivar_end(); I != E; ++I, ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001073 ObjCIvarDecl *Field = *I;
1074 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1075
Devang Patel73621622009-11-25 17:37:31 +00001076 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001077
Devang Patelde135022009-04-27 22:40:36 +00001078 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +00001079 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +00001080 continue;
1081
Devang Patel9ca36b62009-02-26 21:10:26 +00001082 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +00001083 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1084 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel99c20eb2009-03-20 18:24:39 +00001085 QualType FType = Field->getType();
1086 uint64_t FieldSize = 0;
1087 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +00001088
Devang Patel99c20eb2009-03-20 18:24:39 +00001089 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001090
Devang Patel99c20eb2009-03-20 18:24:39 +00001091 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001092 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001093 Expr *BitWidth = Field->getBitWidth();
1094 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +00001095 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +00001096
Anders Carlsson20f12a22009-12-06 18:00:51 +00001097 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001098 }
1099
Mike Stump1eb44332009-09-09 15:08:12 +00001100 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
1101
Devang Patelc20482b2009-03-19 00:23:53 +00001102 unsigned Flags = 0;
1103 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Devang Patele2472482010-09-29 21:05:52 +00001104 Flags = llvm::DIDescriptor::FlagProtected;
Devang Patelc20482b2009-03-19 00:23:53 +00001105 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Devang Patele2472482010-09-29 21:05:52 +00001106 Flags = llvm::DIDescriptor::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +00001107
Devang Patel9ca36b62009-02-26 21:10:26 +00001108 // Create a DW_TAG_member node to remember the offset of this field in the
1109 // struct. FIXME: This is an absolutely insane way to capture this
1110 // information. When we gut debug info, this should be fixed.
1111 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1112 FieldName, FieldDefUnit,
1113 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +00001114 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +00001115 EltTys.push_back(FieldTy);
1116 }
Mike Stump1eb44332009-09-09 15:08:12 +00001117
Devang Patel9ca36b62009-02-26 21:10:26 +00001118 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +00001119 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +00001120
Devang Patele4c1ea02010-03-11 20:01:48 +00001121 RegionStack.pop_back();
1122 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1123 RegionMap.find(Ty->getDecl());
1124 if (RI != RegionMap.end())
1125 RegionMap.erase(RI);
1126
Devang Patel9ca36b62009-02-26 21:10:26 +00001127 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001128 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1129 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001130
Devang Patel6c1fddf2009-07-27 18:42:03 +00001131 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +00001132 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +00001133 Line, Size, Align, 0, 0, llvm::DIType(),
1134 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +00001135
1136 // Now that we have a real decl for the struct, replace anything using the
1137 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001138 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001139 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +00001140
Devang Patel9ca36b62009-02-26 21:10:26 +00001141 return RealDecl;
1142}
1143
Chris Lattner9c85ba32008-11-10 06:08:34 +00001144llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001145 llvm::DIFile Unit) {
Devang Patel6237cea2010-08-23 22:07:25 +00001146 return CreateEnumType(Ty->getDecl(), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001147
Chris Lattner9c85ba32008-11-10 06:08:34 +00001148}
1149
1150llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001151 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001152 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1153 return CreateType(RT, Unit);
1154 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1155 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001156
Chris Lattner9c85ba32008-11-10 06:08:34 +00001157 return llvm::DIType();
1158}
1159
Devang Patel70c23cd2010-02-23 22:59:39 +00001160llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Eli Friedmana7e68452010-08-22 01:00:03 +00001161 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001162 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1163 uint64_t NumElems = Ty->getNumElements();
1164 if (NumElems > 0)
1165 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001166
Benjamin Kramerad468862010-03-30 11:36:44 +00001167 llvm::DIDescriptor Subscript = DebugFactory.GetOrCreateSubrange(0, NumElems);
1168 llvm::DIArray SubscriptArray = DebugFactory.GetOrCreateArray(&Subscript, 1);
Devang Patel70c23cd2010-02-23 22:59:39 +00001169
1170 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1171 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1172
1173 return
1174 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001175 Unit, "", Unit,
Devang Patel70c23cd2010-02-23 22:59:39 +00001176 0, Size, Align, 0, 0,
Eli Friedmana7e68452010-08-22 01:00:03 +00001177 ElementTy, SubscriptArray);
Devang Patel70c23cd2010-02-23 22:59:39 +00001178}
1179
Chris Lattner9c85ba32008-11-10 06:08:34 +00001180llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001181 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001182 uint64_t Size;
1183 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001184
1185
Nuno Lopes010d5142009-01-28 00:35:17 +00001186 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001187 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001188 Size = 0;
1189 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001190 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001191 } else if (Ty->isIncompleteArrayType()) {
1192 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001193 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001194 } else {
1195 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001196 Size = CGM.getContext().getTypeSize(Ty);
1197 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001198 }
Mike Stump1eb44332009-09-09 15:08:12 +00001199
Chris Lattner9c85ba32008-11-10 06:08:34 +00001200 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1201 // interior arrays, do we care? Why aren't nested arrays represented the
1202 // obvious/recursive way?
1203 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1204 QualType EltTy(Ty, 0);
1205 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +00001206 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001207 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +00001208 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +00001209 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001210 // FIXME: Verify this is right for VLAs.
1211 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1212 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +00001213 }
Mike Stump1eb44332009-09-09 15:08:12 +00001214
Chris Lattner9c85ba32008-11-10 06:08:34 +00001215 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001216 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001217
Devang Patelca80a5f2009-10-20 19:55:01 +00001218 llvm::DIType DbgTy =
1219 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001220 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +00001221 0, Size, Align, 0, 0,
1222 getOrCreateType(EltTy, Unit),
1223 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001224 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001225}
1226
Anders Carlssona031b352009-11-06 19:19:55 +00001227llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001228 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001229 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1230 Ty, Ty->getPointeeType(), Unit);
1231}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001232
Anders Carlsson20f12a22009-12-06 18:00:51 +00001233llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001234 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001235 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1236 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1237
1238 if (!Ty->getPointeeType()->isFunctionType()) {
1239 // We have a data member pointer type.
1240 return PointerDiffDITy;
1241 }
1242
1243 // We have a member function pointer type. Treat it as a struct with two
1244 // ptrdiff_t members.
1245 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1246
1247 uint64_t FieldOffset = 0;
1248 llvm::DIDescriptor ElementTypes[2];
1249
1250 // FIXME: This should probably be a function type instead.
1251 ElementTypes[0] =
1252 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001253 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001254 Info.first, Info.second, FieldOffset, 0,
1255 PointerDiffDITy);
1256 FieldOffset += Info.first;
1257
1258 ElementTypes[1] =
1259 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001260 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001261 Info.first, Info.second, FieldOffset, 0,
1262 PointerDiffDITy);
1263
1264 llvm::DIArray Elements =
1265 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1266 llvm::array_lengthof(ElementTypes));
1267
1268 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1269 U, llvm::StringRef("test"),
Devang Pateld58562e2010-03-09 22:49:11 +00001270 U, 0, FieldOffset,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001271 0, 0, 0, llvm::DIType(), Elements);
1272}
1273
Devang Patel6237cea2010-08-23 22:07:25 +00001274/// CreateEnumType - get enumeration type.
1275llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED, llvm::DIFile Unit){
1276 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
1277
1278 // Create DIEnumerator elements for each enumerator.
1279 for (EnumDecl::enumerator_iterator
1280 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
1281 Enum != EnumEnd; ++Enum) {
1282 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
1283 Enum->getInitVal().getZExtValue()));
1284 }
1285
1286 // Return a CompositeType for the enum itself.
1287 llvm::DIArray EltArray =
1288 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
1289
1290 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1291 unsigned Line = getLineNumber(ED->getLocation());
1292 uint64_t Size = 0;
Devang Patelffc52e72010-08-24 18:14:06 +00001293 uint64_t Align = 0;
1294 if (!ED->getTypeForDecl()->isIncompleteType()) {
1295 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1296 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1297 }
Devang Patel6237cea2010-08-23 22:07:25 +00001298 llvm::DIType DbgTy =
1299 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
1300 Unit, ED->getName(), DefUnit, Line,
Devang Patelffc52e72010-08-24 18:14:06 +00001301 Size, Align, 0, 0,
Devang Patel6237cea2010-08-23 22:07:25 +00001302 llvm::DIType(), EltArray);
1303 return DbgTy;
1304}
1305
Douglas Gregor840943d2009-12-21 20:18:30 +00001306static QualType UnwrapTypeForDebugInfo(QualType T) {
1307 do {
1308 QualType LastT = T;
1309 switch (T->getTypeClass()) {
1310 default:
1311 return T;
1312 case Type::TemplateSpecialization:
1313 T = cast<TemplateSpecializationType>(T)->desugar();
1314 break;
1315 case Type::TypeOfExpr: {
1316 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1317 T = Ty->getUnderlyingExpr()->getType();
1318 break;
1319 }
1320 case Type::TypeOf:
1321 T = cast<TypeOfType>(T)->getUnderlyingType();
1322 break;
1323 case Type::Decltype:
1324 T = cast<DecltypeType>(T)->getUnderlyingType();
1325 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001326 case Type::Elaborated:
1327 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001328 break;
1329 case Type::SubstTemplateTypeParm:
1330 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1331 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001332 }
1333
1334 assert(T != LastT && "Type unwrapping failed to unwrap!");
1335 if (T == LastT)
1336 return T;
1337 } while (true);
1338
1339 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001340}
1341
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001342/// getOrCreateType - Get the type from the cache or create a new
1343/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001344llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001345 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001346 if (Ty.isNull())
1347 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001348
Douglas Gregor840943d2009-12-21 20:18:30 +00001349 // Unwrap the type as needed for debug information.
1350 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001351
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001352 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001353 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001354 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001355 if (it != TypeCache.end()) {
1356 // Verify that the debug info still exists.
1357 if (&*it->second)
1358 return llvm::DIType(cast<llvm::MDNode>(it->second));
1359 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001360
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001361 // Otherwise create the type.
1362 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001363
1364 // And update the type cache.
Devang Patelab699792010-05-07 18:12:35 +00001365 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001366 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001367}
1368
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001369/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001370llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001371 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001372 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001373 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001374 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001375
Douglas Gregor2101a822009-12-21 19:57:21 +00001376 const char *Diag = 0;
1377
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001378 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001379 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001380#define TYPE(Class, Base)
1381#define ABSTRACT_TYPE(Class, Base)
1382#define NON_CANONICAL_TYPE(Class, Base)
1383#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1384#include "clang/AST/TypeNodes.def"
1385 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001386
Anders Carlssonbfe69952009-11-06 18:24:04 +00001387 // FIXME: Handle these.
1388 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001389 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001390
1391 case Type::Vector:
1392 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001393 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001394 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCallc12c5bb2010-05-15 11:32:37 +00001395 case Type::ObjCObject:
1396 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001397 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001398 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1399 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1400 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1401 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001402 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001403 return CreateType(cast<BlockPointerType>(Ty), Unit);
1404 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001405 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001406 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001407 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001408 case Type::FunctionProto:
1409 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001410 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001411 case Type::ConstantArray:
1412 case Type::VariableArray:
1413 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001414 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001415
1416 case Type::LValueReference:
1417 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1418
Anders Carlsson20f12a22009-12-06 18:00:51 +00001419 case Type::MemberPointer:
1420 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001421
1422 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001423 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001424 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001425 case Type::TypeOfExpr:
1426 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001427 case Type::Decltype:
1428 llvm_unreachable("type should have been unwrapped!");
1429 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001430
1431 case Type::RValueReference:
1432 // FIXME: Implement!
1433 Diag = "rvalue references";
1434 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001435 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001436
1437 assert(Diag && "Fall through without a diagnostic?");
1438 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1439 "debug information for %0 is not yet supported");
1440 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1441 << Diag;
1442 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001443}
1444
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001445/// CreateMemberType - Create new member and increase Offset by FType's size.
1446llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
1447 llvm::StringRef Name,
1448 uint64_t *Offset) {
1449 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1450 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1451 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
1452 llvm::DIType Ty = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1453 Unit, Name, Unit, 0,
1454 FieldSize, FieldAlign,
1455 *Offset, 0, FieldTy);
1456 *Offset += FieldSize;
1457 return Ty;
1458}
1459
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001460/// EmitFunctionStart - Constructs the debug code for entering a function -
1461/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001462void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001463 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001464 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001465
Devang Patel9c6c3a02010-01-14 00:36:21 +00001466 llvm::StringRef Name;
Anders Carlsson9a20d552010-06-22 16:16:50 +00001467 llvm::StringRef LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001468
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001469 FnBeginRegionCount.push_back(RegionStack.size());
1470
Devang Patel9c6c3a02010-01-14 00:36:21 +00001471 const Decl *D = GD.getDecl();
1472 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001473 // If there is a DISubprogram for this function available then use it.
1474 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1475 FI = SPCache.find(FD);
1476 if (FI != SPCache.end()) {
Gabor Greif38c9b172010-09-18 13:00:17 +00001477 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(&*FI->second));
Devang Patelab699792010-05-07 18:12:35 +00001478 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1479 llvm::MDNode *SPN = SP;
1480 RegionStack.push_back(SPN);
1481 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00001482 return;
1483 }
1484 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001485 Name = getFunctionName(FD);
1486 // Use mangled name as linkage name for c/c++ functions.
Anders Carlsson9a20d552010-06-22 16:16:50 +00001487 LinkageName = CGM.getMangledName(GD);
David Chisnall70b9b442010-09-02 17:16:32 +00001488 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
David Chisnall52044a22010-09-02 18:01:51 +00001489 Name = getObjCMethodName(OMD);
David Chisnall70b9b442010-09-02 17:16:32 +00001490 LinkageName = Name;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001491 } else {
1492 // Use llvm function name as linkage name.
1493 Name = Fn->getName();
Anders Carlsson9a20d552010-06-22 16:16:50 +00001494 LinkageName = Name;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001495 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001496 if (!Name.empty() && Name[0] == '\01')
1497 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001498
Devang Patel970c6182010-04-24 00:49:16 +00001499 // It is expected that CurLoc is set before using EmitFunctionStart.
1500 // Usually, CurLoc points to the left bracket location of compound
1501 // statement representing function body.
1502 llvm::DIFile Unit = getOrCreateFile(CurLoc);
Devang Patel8ab870d2010-05-12 23:46:38 +00001503 unsigned LineNo = getLineNumber(CurLoc);
Devang Patele2472482010-09-29 21:05:52 +00001504 unsigned Flags = 0;
1505 if (D->isImplicit())
1506 Flags |= llvm::DIDescriptor::FlagArtificial;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001507 llvm::DISubprogram SP =
Devang Patel970c6182010-04-24 00:49:16 +00001508 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001509 getOrCreateType(FnType, Unit),
Devang Patel15a3d7d2010-07-15 23:09:46 +00001510 Fn->hasInternalLinkage(), true/*definition*/,
1511 0, 0, llvm::DIType(),
Devang Patele2472482010-09-29 21:05:52 +00001512 Flags,
Devang Patel15a3d7d2010-07-15 23:09:46 +00001513 CGM.getLangOptions().Optimize, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001515 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001516 llvm::MDNode *SPN = SP;
1517 RegionStack.push_back(SPN);
1518 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001519
1520 // Clear stack used to keep track of #line directives.
1521 LineDirectiveFiles.clear();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001522}
1523
1524
Devang Patel4d939e62010-07-20 22:20:10 +00001525void CGDebugInfo::EmitStopPoint(CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001526 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001527
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001528 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001529 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001530 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001531 || (SM.getInstantiationLineNumber(CurLoc) ==
1532 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001533 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patel4800ea62010-04-05 21:09:15 +00001534 // New Builder may not be in sync with CGDebugInfo.
1535 if (!Builder.getCurrentDebugLocation().isUnknown())
1536 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001537
1538 // Update last state.
1539 PrevLoc = CurLoc;
1540
Chris Lattnerc6034632010-04-01 06:31:43 +00001541 llvm::MDNode *Scope = RegionStack.back();
Devang Patel8ab870d2010-05-12 23:46:38 +00001542 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1543 getColumnNumber(CurLoc),
Chris Lattnere541d012010-04-02 20:21:43 +00001544 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001545}
1546
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001547/// UpdateLineDirectiveRegion - Update region stack only if #line directive
1548/// has introduced scope change.
1549void CGDebugInfo::UpdateLineDirectiveRegion(CGBuilderTy &Builder) {
1550 if (CurLoc.isInvalid() || CurLoc.isMacroID() ||
1551 PrevLoc.isInvalid() || PrevLoc.isMacroID())
1552 return;
1553 SourceManager &SM = CGM.getContext().getSourceManager();
1554 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
1555 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc);
1556
1557 if (!strcmp(PPLoc.getFilename(), PCLoc.getFilename()))
1558 return;
1559
1560 // If #line directive stack is empty then we are entering a new scope.
1561 if (LineDirectiveFiles.empty()) {
1562 EmitRegionStart(Builder);
1563 LineDirectiveFiles.push_back(PCLoc.getFilename());
1564 return;
1565 }
1566
1567 assert (RegionStack.size() >= LineDirectiveFiles.size()
1568 && "error handling #line regions!");
1569
1570 bool SeenThisFile = false;
Devang Patel424a5c62010-09-15 20:50:40 +00001571 // Chek if current file is already seen earlier.
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001572 for(std::vector<const char *>::iterator I = LineDirectiveFiles.begin(),
1573 E = LineDirectiveFiles.end(); I != E; ++I)
Devang Patel424a5c62010-09-15 20:50:40 +00001574 if (!strcmp(PCLoc.getFilename(), *I)) {
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001575 SeenThisFile = true;
1576 break;
1577 }
1578
1579 // If #line for this file is seen earlier then pop out #line regions.
1580 if (SeenThisFile) {
1581 while (!LineDirectiveFiles.empty()) {
1582 const char *LastFile = LineDirectiveFiles.back();
1583 RegionStack.pop_back();
1584 LineDirectiveFiles.pop_back();
1585 if (!strcmp(PPLoc.getFilename(), LastFile))
1586 break;
1587 }
1588 return;
1589 }
1590
1591 // .. otherwise insert new #line region.
1592 EmitRegionStart(Builder);
1593 LineDirectiveFiles.push_back(PCLoc.getFilename());
1594
1595 return;
1596}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001597/// EmitRegionStart- Constructs the debug code for entering a declarative
1598/// region - "llvm.dbg.region.start.".
Devang Patel4d939e62010-07-20 22:20:10 +00001599void CGDebugInfo::EmitRegionStart(CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001600 llvm::DIDescriptor D =
1601 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1602 llvm::DIDescriptor() :
Devang Pateld19429f2010-02-16 21:41:20 +00001603 llvm::DIDescriptor(RegionStack.back()),
Stuart Hastings257d1d32010-07-19 23:56:31 +00001604 getOrCreateFile(CurLoc),
Devang Patel8ab870d2010-05-12 23:46:38 +00001605 getLineNumber(CurLoc),
1606 getColumnNumber(CurLoc));
Devang Patelab699792010-05-07 18:12:35 +00001607 llvm::MDNode *DN = D;
1608 RegionStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001609}
1610
1611/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1612/// region - "llvm.dbg.region.end."
Devang Patel4d939e62010-07-20 22:20:10 +00001613void CGDebugInfo::EmitRegionEnd(CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001614 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1615
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001616 // Provide an region stop point.
Devang Patel4d939e62010-07-20 22:20:10 +00001617 EmitStopPoint(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001618
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001619 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001620}
1621
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001622/// EmitFunctionEnd - Constructs the debug code for exiting a function.
1623void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
1624 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1625 unsigned RCount = FnBeginRegionCount.back();
1626 assert(RCount <= RegionStack.size() && "Region stack mismatch");
1627
1628 // Pop all regions for this function.
1629 while (RegionStack.size() != RCount)
1630 EmitRegionEnd(Builder);
1631 FnBeginRegionCount.pop_back();
1632}
1633
Devang Patel809b9bb2010-02-10 18:49:08 +00001634// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1635// See BuildByRefType.
1636llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1637 uint64_t *XOffset) {
1638
1639 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1640
1641 QualType FType;
1642 uint64_t FieldSize, FieldOffset;
1643 unsigned FieldAlign;
1644
Devang Patel17800552010-03-09 00:44:50 +00001645 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001646 QualType Type = VD->getType();
1647
1648 FieldOffset = 0;
1649 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001650 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1651 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001652 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001653 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1654 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1655
Devang Patel809b9bb2010-02-10 18:49:08 +00001656 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1657 if (HasCopyAndDispose) {
1658 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001659 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1660 &FieldOffset));
1661 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1662 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001663 }
1664
1665 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1666 if (Align > CharUnits::fromQuantity(
1667 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1668 unsigned AlignedOffsetInBytes
1669 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1670 unsigned NumPaddingBytes
1671 = AlignedOffsetInBytes - FieldOffset/8;
1672
1673 if (NumPaddingBytes > 0) {
1674 llvm::APInt pad(32, NumPaddingBytes);
1675 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1676 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001677 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001678 }
1679 }
1680
1681 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001682 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00001683 FieldSize = CGM.getContext().getTypeSize(FType);
1684 FieldAlign = Align.getQuantity()*8;
1685
1686 *XOffset = FieldOffset;
1687 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001688 VD->getName(), Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001689 0, FieldSize, FieldAlign,
1690 FieldOffset, 0, FieldTy);
1691 EltTys.push_back(FieldTy);
1692 FieldOffset += FieldSize;
1693
1694 llvm::DIArray Elements =
1695 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1696
Devang Patele2472482010-09-29 21:05:52 +00001697 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Devang Patel809b9bb2010-02-10 18:49:08 +00001698
1699 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001700 Unit, "", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001701 0, FieldOffset, 0, 0, Flags,
1702 llvm::DIType(), Elements);
1703
1704}
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001705/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001706void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001707 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001708 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1709
Devang Patel17800552010-03-09 00:44:50 +00001710 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001711 llvm::DIType Ty;
1712 uint64_t XOffset = 0;
1713 if (VD->hasAttr<BlocksAttr>())
1714 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1715 else
1716 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001717
Devang Patelf4e54a22010-05-07 23:05:55 +00001718 // If there is not any debug info for type then do not emit debug info
1719 // for this variable.
1720 if (!Ty)
1721 return;
1722
Chris Lattner9c85ba32008-11-10 06:08:34 +00001723 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001724 unsigned Line = getLineNumber(VD->getLocation());
1725 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001726
Chris Lattner9c85ba32008-11-10 06:08:34 +00001727 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001728 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001729 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel239cec62010-02-01 21:39:52 +00001730 VD->getName(),
Devang Patel44eeeba2010-06-05 01:14:40 +00001731 Unit, Line, Ty, CGM.getLangOptions().Optimize);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001732 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001733 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001734 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001735
Chris Lattnerc6034632010-04-01 06:31:43 +00001736 llvm::MDNode *Scope = RegionStack.back();
Chris Lattnere541d012010-04-02 20:21:43 +00001737 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001738}
1739
Mike Stumpb1a6e682009-09-30 02:43:10 +00001740/// EmitDeclare - Emit local variable declaration debug info.
1741void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1742 llvm::Value *Storage, CGBuilderTy &Builder,
1743 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001744 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001745 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1746
Devang Patel2b594b92010-04-26 23:28:46 +00001747 if (Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001748 return;
1749
1750 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001751 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001752 llvm::DIType Ty;
1753 if (VD->hasAttr<BlocksAttr>())
1754 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1755 else
1756 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001757
1758 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001759 unsigned Line = getLineNumber(VD->getLocation());
1760 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001761
Devang Pateld6c5a262010-02-01 21:52:22 +00001762 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001763 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001764 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1765 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1766 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1767 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001768 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001769 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1770 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001771 // offset of __forwarding field
1772 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001773 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1774 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1775 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001776 // offset of x field
1777 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001778 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001779 }
1780
1781 // Create the descriptor for the variable.
1782 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001783 DebugFactory.CreateComplexVariable(Tag,
1784 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001785 VD->getName(), Unit, Line, Ty,
Benjamin Kramer3475cfe2010-09-21 15:59:59 +00001786 addr.data(), addr.size());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001787 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001788 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001789 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattnerd5b89022009-12-28 21:44:41 +00001790
Chris Lattnerc6034632010-04-01 06:31:43 +00001791 llvm::MDNode *Scope = RegionStack.back();
Devang Patelf8e10a52010-05-10 23:48:38 +00001792 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001793}
1794
Devang Pateld6c5a262010-02-01 21:52:22 +00001795void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001796 llvm::Value *Storage,
1797 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001798 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001799}
1800
Mike Stumpb1a6e682009-09-30 02:43:10 +00001801void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1802 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1803 CodeGenFunction *CGF) {
1804 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1805}
1806
Chris Lattner9c85ba32008-11-10 06:08:34 +00001807/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1808/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001809void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001810 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001811 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001812}
1813
1814
1815
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001816/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001817void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001818 const VarDecl *D) {
1819
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001820 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001821 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001822 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +00001823
Devang Pateleb6d79b2010-02-01 21:34:11 +00001824 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001825 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001826
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001827 // CodeGen turns int[] into int[1] so we'll do the same here.
1828 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001829
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001830 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001831 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001832
Anders Carlsson20f12a22009-12-06 18:00:51 +00001833 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001834 ArrayType::Normal, 0);
1835 }
Devang Patel5d822f02010-04-29 17:48:37 +00001836 llvm::StringRef DeclName = D->getName();
Devang Patel8b90a782010-05-13 23:52:37 +00001837 llvm::StringRef LinkageName;
Devang Patel0fd3d1f2010-05-14 16:55:25 +00001838 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()))
Devang Patel8b90a782010-05-13 23:52:37 +00001839 LinkageName = Var->getName();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001840 llvm::DIDescriptor DContext =
1841 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
Devang Patel8b90a782010-05-13 23:52:37 +00001842 DebugFactory.CreateGlobalVariable(DContext, DeclName, DeclName, LinkageName,
1843 Unit, LineNo, getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001844 Var->hasInternalLinkage(),
1845 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001846}
1847
Devang Patel9ca36b62009-02-26 21:10:26 +00001848/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001849void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001850 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001851 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001852 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001853 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patel9ca36b62009-02-26 21:10:26 +00001854
Devang Pateld6c5a262010-02-01 21:52:22 +00001855 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001856
Devang Pateld6c5a262010-02-01 21:52:22 +00001857 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001858 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001859
Devang Patel9ca36b62009-02-26 21:10:26 +00001860 // CodeGen turns int[] into int[1] so we'll do the same here.
1861 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001862
Devang Patel9ca36b62009-02-26 21:10:26 +00001863 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001864 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001865
Anders Carlsson20f12a22009-12-06 18:00:51 +00001866 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001867 ArrayType::Normal, 0);
1868 }
1869
Devang Patelf6a39b72009-10-20 18:26:30 +00001870 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001871 getOrCreateType(T, Unit),
1872 Var->hasInternalLinkage(),
1873 true/*definition*/, Var);
1874}
Devang Patelabb485f2010-02-01 19:16:32 +00001875
Devang Patel25c2c8f2010-08-10 17:53:33 +00001876/// EmitGlobalVariable - Emit global variable's debug info.
1877void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
1878 llvm::ConstantInt *Init,
Devang Patel8d308382010-08-10 07:24:25 +00001879 CGBuilderTy &Builder) {
1880 // Create the descriptor for the variable.
1881 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
1882 llvm::StringRef Name = VD->getName();
Devang Patel0317ab02010-08-10 18:27:15 +00001883 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
Devang Patel6237cea2010-08-23 22:07:25 +00001884 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
1885 if (const EnumDecl *ED = dyn_cast<EnumDecl>(ECD->getDeclContext()))
1886 Ty = CreateEnumType(ED, Unit);
1887 }
Devang Patel0317ab02010-08-10 18:27:15 +00001888 // Do not use DIGlobalVariable for enums.
1889 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
1890 return;
Devang Patel8d308382010-08-10 07:24:25 +00001891 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit,
1892 getLineNumber(VD->getLocation()),
Devang Patel4c4acc02010-08-10 20:16:57 +00001893 Ty, true, true, Init);
Devang Patel8d308382010-08-10 07:24:25 +00001894}
1895
Devang Patelabb485f2010-02-01 19:16:32 +00001896/// getOrCreateNamesSpace - Return namespace descriptor for the given
1897/// namespace decl.
1898llvm::DINameSpace
1899CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1900 llvm::DIDescriptor Unit) {
1901 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1902 NameSpaceCache.find(NSDecl);
1903 if (I != NameSpaceCache.end())
1904 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1905
Devang Patel8ab870d2010-05-12 23:46:38 +00001906 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patelabb485f2010-02-01 19:16:32 +00001907
1908 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00001909 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00001910 llvm::DINameSpace NS =
1911 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
Devang Patel2ed8f002010-08-27 17:47:47 +00001912 llvm::DIFile(Unit), LineNo);
Devang Patelab699792010-05-07 18:12:35 +00001913 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00001914 return NS;
1915}