blob: 45a887b1bbb80770c35908ef288b4b1b76521c95 [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;
Devang Patel10a7a6a2010-09-29 21:46:16 +0000704 AccessSpecifier Access = Method->getAccess();
705 if (Access == clang::AS_private)
706 Flags |= llvm::DIDescriptor::FlagPrivate;
707 else if (Access == clang::AS_protected)
708 Flags |= llvm::DIDescriptor::FlagProtected;
709
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000710 llvm::DISubprogram SP =
711 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
712 MethodLinkageName,
713 MethodDefUnit, MethodLine,
714 MethodTy, /*isLocalToUnit=*/false,
Devang Patela5c5bab2010-07-12 22:54:41 +0000715 /* isDefintion=*/ false,
Devang Patel40894912010-07-15 22:57:00 +0000716 Virtuality, VIndex, ContainingType,
Devang Patele2472482010-09-29 21:05:52 +0000717 Flags,
Devang Patel15a3d7d2010-07-15 23:09:46 +0000718 CGM.getLangOptions().Optimize);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000719
720 // Don't cache ctors or dtors since we have to emit multiple functions for
721 // a single ctor or dtor.
722 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
Devang Patelab699792010-05-07 18:12:35 +0000723 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000724
725 return SP;
726}
727
Devang Patel4125fd22010-01-19 01:54:44 +0000728/// CollectCXXMemberFunctions - A helper function to collect debug info for
729/// C++ member functions.This is used while creating debug info entry for
730/// a Record.
731void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000732CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000733 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000734 llvm::DIType RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000735 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
736 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000737 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000738
Devang Pateld5322da2010-02-09 19:09:28 +0000739 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000740 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000741
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000742 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000743 }
744}
745
Devang Patel2ed8f002010-08-27 17:47:47 +0000746/// CollectCXXFriends - A helper function to collect debug info for
747/// C++ base classes. This is used while creating debug info entry for
748/// a Record.
749void CGDebugInfo::
750CollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit,
751 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
752 llvm::DIType RecordTy) {
753
754 for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(),
755 BE = RD->friend_end(); BI != BE; ++BI) {
756
757 TypeSourceInfo *TInfo = (*BI)->getFriendType();
758 if(TInfo)
759 {
760 llvm::DIType Ty = getOrCreateType(TInfo->getType(), Unit);
761
762 llvm::DIType DTy =
763 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_friend,
764 RecordTy, llvm::StringRef(),
765 Unit, 0, 0, 0,
766 0, 0, Ty);
767
768 EltTys.push_back(DTy);
769 }
770
771 }
772}
773
Devang Patela245c5b2010-01-25 23:32:18 +0000774/// CollectCXXBases - A helper function to collect debug info for
775/// C++ base classes. This is used while creating debug info entry for
776/// a Record.
777void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000778CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000779 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000780 llvm::DIType RecordTy) {
Devang Patela245c5b2010-01-25 23:32:18 +0000781
Devang Patel239cec62010-02-01 21:39:52 +0000782 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
783 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
784 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000785 unsigned BFlags = 0;
786 uint64_t BaseOffset;
787
788 const CXXRecordDecl *Base =
789 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
790
791 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000792 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000793 // expression where it expects +ve number.
Anders Carlssonaf440352010-03-23 04:11:45 +0000794 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base);
Devang Patele2472482010-09-29 21:05:52 +0000795 BFlags = llvm::DIDescriptor::FlagVirtual;
Devang Patelca7daed2010-01-28 21:54:15 +0000796 } else
797 BaseOffset = RL.getBaseClassOffset(Base);
798
799 AccessSpecifier Access = BI->getAccessSpecifier();
800 if (Access == clang::AS_private)
Devang Patele2472482010-09-29 21:05:52 +0000801 BFlags |= llvm::DIDescriptor::FlagPrivate;
Devang Patelca7daed2010-01-28 21:54:15 +0000802 else if (Access == clang::AS_protected)
Devang Patele2472482010-09-29 21:05:52 +0000803 BFlags |= llvm::DIDescriptor::FlagProtected;
Devang Patelca7daed2010-01-28 21:54:15 +0000804
805 llvm::DIType DTy =
806 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
807 RecordTy, llvm::StringRef(),
Devang Pateld58562e2010-03-09 22:49:11 +0000808 Unit, 0, 0, 0,
Devang Patelca7daed2010-01-28 21:54:15 +0000809 BaseOffset, BFlags,
810 getOrCreateType(BI->getType(),
811 Unit));
812 EltTys.push_back(DTy);
813 }
Devang Patela245c5b2010-01-25 23:32:18 +0000814}
815
Devang Patel4ce3f202010-01-28 18:11:52 +0000816/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000817llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000818 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000819 return VTablePtrType;
820
821 ASTContext &Context = CGM.getContext();
822
823 /* Function type */
Benjamin Kramerad468862010-03-30 11:36:44 +0000824 llvm::DIDescriptor STy = getOrCreateType(Context.IntTy, Unit);
825 llvm::DIArray SElements = DebugFactory.GetOrCreateArray(&STy, 1);
Devang Patel4ce3f202010-01-28 18:11:52 +0000826 llvm::DIType SubTy =
827 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000828 Unit, "", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000829 0, 0, 0, 0, 0, llvm::DIType(), SElements);
830
831 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
832 llvm::DIType vtbl_ptr_type
833 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000834 Unit, "__vtbl_ptr_type", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000835 0, Size, 0, 0, 0, SubTy);
836
Devang Pateld58562e2010-03-09 22:49:11 +0000837 VTablePtrType =
838 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
839 Unit, "", Unit,
840 0, Size, 0, 0, 0, vtbl_ptr_type);
Devang Patel4ce3f202010-01-28 18:11:52 +0000841 return VTablePtrType;
842}
843
Anders Carlsson046c2942010-04-17 20:15:18 +0000844/// getVTableName - Get vtable name for the given Class.
845llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000846 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000847 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000848
849 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000850 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000851 memcpy(StrPtr, Name.data(), Name.length());
852 return llvm::StringRef(StrPtr, Name.length());
853}
854
855
Anders Carlsson046c2942010-04-17 20:15:18 +0000856/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel4ce3f202010-01-28 18:11:52 +0000857/// debug info entry in EltTys vector.
858void CGDebugInfo::
Anders Carlsson046c2942010-04-17 20:15:18 +0000859CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000860 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000861 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000862
863 // If there is a primary base then it will hold vtable info.
864 if (RL.getPrimaryBase())
865 return;
866
867 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000868 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000869 return;
870
871 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
872 llvm::DIType VPTR
873 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Anders Carlsson046c2942010-04-17 20:15:18 +0000874 getVTableName(RD), Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000875 0, Size, 0, 0, 0,
876 getOrCreateVTablePtrType(Unit));
877 EltTys.push_back(VPTR);
878}
879
Devang Patel65e99f22009-02-25 01:36:11 +0000880/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000881llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000882 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000883 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000884
Chris Lattner9c85ba32008-11-10 06:08:34 +0000885 unsigned Tag;
Devang Pateld6c5a262010-02-01 21:52:22 +0000886 if (RD->isStruct())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000887 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Pateld6c5a262010-02-01 21:52:22 +0000888 else if (RD->isUnion())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000889 Tag = llvm::dwarf::DW_TAG_union_type;
890 else {
Devang Pateld6c5a262010-02-01 21:52:22 +0000891 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000892 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000893 }
894
Chris Lattner9c85ba32008-11-10 06:08:34 +0000895 // Get overall information about the record type for the debug info.
Devang Patel8ab870d2010-05-12 23:46:38 +0000896 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
897 unsigned Line = getLineNumber(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000898
Chris Lattner9c85ba32008-11-10 06:08:34 +0000899 // Records and classes and unions can all be recursive. To handle them, we
900 // first generate a debug descriptor for the struct as a forward declaration.
901 // Then (if it is a definition) we go through and get debug info for all of
902 // its members. Finally, we create a descriptor for the complete type (which
903 // may refer to the forward decl if the struct is recursive) and replace all
904 // uses of the forward declaration with the final definition.
Devang Patel0b897992010-07-08 19:56:29 +0000905 llvm::DIDescriptor FDContext =
906 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
907
908 // If this is just a forward declaration, construct an appropriately
909 // marked node and just return it.
910 if (!RD->getDefinition()) {
911 llvm::DICompositeType FwdDecl =
912 DebugFactory.CreateCompositeType(Tag, FDContext, RD->getName(),
913 DefUnit, Line, 0, 0, 0,
Devang Patele2472482010-09-29 21:05:52 +0000914 llvm::DIDescriptor::FlagFwdDecl,
Devang Patel0b897992010-07-08 19:56:29 +0000915 llvm::DIType(), llvm::DIArray());
916
917 return FwdDecl;
918 }
Devang Pateld0f251b2010-01-20 23:56:40 +0000919
Dan Gohman86968372010-08-20 22:39:57 +0000920 llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType();
Mike Stump1eb44332009-09-09 15:08:12 +0000921
Devang Patelab699792010-05-07 18:12:35 +0000922 llvm::MDNode *MN = FwdDecl;
923 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000924 // Otherwise, insert it into the TypeCache so that recursive uses will find
925 // it.
Devang Patelab699792010-05-07 18:12:35 +0000926 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000927 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000928 RegionStack.push_back(FwdDeclNode);
929 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000930
931 // Convert all the elements.
932 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
933
Devang Pateld6c5a262010-02-01 21:52:22 +0000934 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000935 if (CXXDecl) {
936 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson046c2942010-04-17 20:15:18 +0000937 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000938 }
Devang Pateldabc3e92010-08-12 00:02:44 +0000939
940 // Collect static variables with initializers.
941 for (RecordDecl::decl_iterator I = RD->decls_begin(), E = RD->decls_end();
942 I != E; ++I)
943 if (const VarDecl *V = dyn_cast<VarDecl>(*I)) {
944 if (const Expr *Init = V->getInit()) {
945 Expr::EvalResult Result;
946 if (Init->Evaluate(Result, CGM.getContext()) && Result.Val.isInt()) {
947 llvm::ConstantInt *CI
948 = llvm::ConstantInt::get(CGM.getLLVMContext(), Result.Val.getInt());
949
950 // Create the descriptor for static variable.
951 llvm::DIFile VUnit = getOrCreateFile(V->getLocation());
952 llvm::StringRef VName = V->getName();
953 llvm::DIType VTy = getOrCreateType(V->getType(), VUnit);
954 // Do not use DIGlobalVariable for enums.
955 if (VTy.getTag() != llvm::dwarf::DW_TAG_enumeration_type) {
956 DebugFactory.CreateGlobalVariable(FwdDecl, VName, VName, VName, VUnit,
957 getLineNumber(V->getLocation()),
958 VTy, true, true, CI);
959 }
960 }
961 }
962 }
963
Devang Pateld6c5a262010-02-01 21:52:22 +0000964 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +0000965 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +0000966 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +0000967 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel2ed8f002010-08-27 17:47:47 +0000968 CollectCXXFriends(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +0000969
970 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +0000971 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel0ac8f312010-01-28 00:54:21 +0000972 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
973 ContainingType =
Devang Patelab699792010-05-07 18:12:35 +0000974 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patel0ac8f312010-01-28 00:54:21 +0000975 else if (CXXDecl->isDynamicClass())
Devang Patelab699792010-05-07 18:12:35 +0000976 ContainingType = FwdDecl;
Devang Patela245c5b2010-01-25 23:32:18 +0000977 }
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Chris Lattner9c85ba32008-11-10 06:08:34 +0000979 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000980 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000981
982 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000983 uint64_t Size = CGM.getContext().getTypeSize(Ty);
984 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000985
Devang Patele4c1ea02010-03-11 20:01:48 +0000986 RegionStack.pop_back();
987 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
988 RegionMap.find(Ty->getDecl());
989 if (RI != RegionMap.end())
990 RegionMap.erase(RI);
991
Devang Patel411894b2010-02-01 22:40:08 +0000992 llvm::DIDescriptor RDContext =
993 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel700a1cb2010-07-20 20:24:18 +0000994
995 llvm::StringRef RDName = RD->getName();
996 // If this is a class, include the template arguments also.
997 if (Tag == llvm::dwarf::DW_TAG_class_type)
998 RDName = getClassName(RD);
999
Devang Patel0ce73f62009-07-22 18:57:00 +00001000 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +00001001 DebugFactory.CreateCompositeType(Tag, RDContext,
Devang Patel700a1cb2010-07-20 20:24:18 +00001002 RDName,
Devang Patelab71ff52009-11-12 00:51:46 +00001003 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +00001004 llvm::DIType(), Elements,
1005 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001006
1007 // Now that we have a real decl for the struct, replace anything using the
1008 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001009 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001010 RegionMap[RD] = llvm::WeakVH(RealDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001011 return RealDecl;
1012}
1013
John McCallc12c5bb2010-05-15 11:32:37 +00001014/// CreateType - get objective-c object type.
1015llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1016 llvm::DIFile Unit) {
1017 // Ignore protocols.
1018 return getOrCreateType(Ty->getBaseType(), Unit);
1019}
1020
Devang Patel9ca36b62009-02-26 21:10:26 +00001021/// CreateType - get objective-c interface type.
1022llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001023 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001024 ObjCInterfaceDecl *ID = Ty->getDecl();
Devang Patel9ca36b62009-02-26 21:10:26 +00001025 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Patel9ca36b62009-02-26 21:10:26 +00001026
1027 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +00001028 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001029 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel17800552010-03-09 00:44:50 +00001030 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +00001031
Dan Gohman45f7c782010-08-23 21:15:56 +00001032 // If this is just a forward declaration, return a special forward-declaration
1033 // debug type.
1034 if (ID->isForwardDecl()) {
1035 llvm::DICompositeType FwdDecl =
1036 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
1037 DefUnit, Line, 0, 0, 0, 0,
1038 llvm::DIType(), llvm::DIArray(),
1039 RuntimeLang);
1040 return FwdDecl;
1041 }
1042
Devang Patel9ca36b62009-02-26 21:10:26 +00001043 // To handle recursive interface, we
1044 // first generate a debug descriptor for the struct as a forward declaration.
1045 // Then (if it is a definition) we go through and get debug info for all of
1046 // its members. Finally, we create a descriptor for the complete type (which
1047 // may refer to the forward decl if the struct is recursive) and replace all
1048 // uses of the forward declaration with the final definition.
Dan Gohman86968372010-08-20 22:39:57 +00001049 llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType();
Mike Stump1eb44332009-09-09 15:08:12 +00001050
Devang Patelab699792010-05-07 18:12:35 +00001051 llvm::MDNode *MN = FwdDecl;
1052 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patel9ca36b62009-02-26 21:10:26 +00001053 // Otherwise, insert it into the TypeCache so that recursive uses will find
1054 // it.
Devang Patelab699792010-05-07 18:12:35 +00001055 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +00001056 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001057 RegionStack.push_back(FwdDeclNode);
1058 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001059
1060 // Convert all the elements.
1061 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
1062
Devang Pateld6c5a262010-02-01 21:52:22 +00001063 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +00001064 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +00001065 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001066 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001067 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +00001068 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Pateld58562e2010-03-09 22:49:11 +00001069 Unit, "", Unit, 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +00001070 0 /* offset */, 0, SClassTy);
1071 EltTys.push_back(InhTag);
1072 }
1073
Devang Pateld6c5a262010-02-01 21:52:22 +00001074 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001075
1076 unsigned FieldNo = 0;
Devang Pateld6c5a262010-02-01 21:52:22 +00001077 for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(),
1078 E = ID->ivar_end(); I != E; ++I, ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001079 ObjCIvarDecl *Field = *I;
1080 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1081
Devang Patel73621622009-11-25 17:37:31 +00001082 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001083
Devang Patelde135022009-04-27 22:40:36 +00001084 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +00001085 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +00001086 continue;
1087
Devang Patel9ca36b62009-02-26 21:10:26 +00001088 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +00001089 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1090 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel99c20eb2009-03-20 18:24:39 +00001091 QualType FType = Field->getType();
1092 uint64_t FieldSize = 0;
1093 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +00001094
Devang Patel99c20eb2009-03-20 18:24:39 +00001095 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001096
Devang Patel99c20eb2009-03-20 18:24:39 +00001097 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001098 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001099 Expr *BitWidth = Field->getBitWidth();
1100 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +00001101 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +00001102
Anders Carlsson20f12a22009-12-06 18:00:51 +00001103 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001104 }
1105
Mike Stump1eb44332009-09-09 15:08:12 +00001106 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
1107
Devang Patelc20482b2009-03-19 00:23:53 +00001108 unsigned Flags = 0;
1109 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Devang Patele2472482010-09-29 21:05:52 +00001110 Flags = llvm::DIDescriptor::FlagProtected;
Devang Patelc20482b2009-03-19 00:23:53 +00001111 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Devang Patele2472482010-09-29 21:05:52 +00001112 Flags = llvm::DIDescriptor::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +00001113
Devang Patel9ca36b62009-02-26 21:10:26 +00001114 // Create a DW_TAG_member node to remember the offset of this field in the
1115 // struct. FIXME: This is an absolutely insane way to capture this
1116 // information. When we gut debug info, this should be fixed.
1117 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1118 FieldName, FieldDefUnit,
1119 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +00001120 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +00001121 EltTys.push_back(FieldTy);
1122 }
Mike Stump1eb44332009-09-09 15:08:12 +00001123
Devang Patel9ca36b62009-02-26 21:10:26 +00001124 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +00001125 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +00001126
Devang Patele4c1ea02010-03-11 20:01:48 +00001127 RegionStack.pop_back();
1128 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1129 RegionMap.find(Ty->getDecl());
1130 if (RI != RegionMap.end())
1131 RegionMap.erase(RI);
1132
Devang Patel9ca36b62009-02-26 21:10:26 +00001133 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001134 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1135 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001136
Devang Patel6c1fddf2009-07-27 18:42:03 +00001137 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +00001138 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +00001139 Line, Size, Align, 0, 0, llvm::DIType(),
1140 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +00001141
1142 // Now that we have a real decl for the struct, replace anything using the
1143 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001144 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001145 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +00001146
Devang Patel9ca36b62009-02-26 21:10:26 +00001147 return RealDecl;
1148}
1149
Chris Lattner9c85ba32008-11-10 06:08:34 +00001150llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001151 llvm::DIFile Unit) {
Devang Patel6237cea2010-08-23 22:07:25 +00001152 return CreateEnumType(Ty->getDecl(), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001153
Chris Lattner9c85ba32008-11-10 06:08:34 +00001154}
1155
1156llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001157 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001158 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1159 return CreateType(RT, Unit);
1160 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1161 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001162
Chris Lattner9c85ba32008-11-10 06:08:34 +00001163 return llvm::DIType();
1164}
1165
Devang Patel70c23cd2010-02-23 22:59:39 +00001166llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Eli Friedmana7e68452010-08-22 01:00:03 +00001167 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001168 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1169 uint64_t NumElems = Ty->getNumElements();
1170 if (NumElems > 0)
1171 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001172
Benjamin Kramerad468862010-03-30 11:36:44 +00001173 llvm::DIDescriptor Subscript = DebugFactory.GetOrCreateSubrange(0, NumElems);
1174 llvm::DIArray SubscriptArray = DebugFactory.GetOrCreateArray(&Subscript, 1);
Devang Patel70c23cd2010-02-23 22:59:39 +00001175
1176 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1177 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1178
1179 return
1180 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001181 Unit, "", Unit,
Devang Patel70c23cd2010-02-23 22:59:39 +00001182 0, Size, Align, 0, 0,
Eli Friedmana7e68452010-08-22 01:00:03 +00001183 ElementTy, SubscriptArray);
Devang Patel70c23cd2010-02-23 22:59:39 +00001184}
1185
Chris Lattner9c85ba32008-11-10 06:08:34 +00001186llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001187 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001188 uint64_t Size;
1189 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001190
1191
Nuno Lopes010d5142009-01-28 00:35:17 +00001192 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001193 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001194 Size = 0;
1195 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001196 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001197 } else if (Ty->isIncompleteArrayType()) {
1198 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001199 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001200 } else {
1201 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001202 Size = CGM.getContext().getTypeSize(Ty);
1203 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001204 }
Mike Stump1eb44332009-09-09 15:08:12 +00001205
Chris Lattner9c85ba32008-11-10 06:08:34 +00001206 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1207 // interior arrays, do we care? Why aren't nested arrays represented the
1208 // obvious/recursive way?
1209 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1210 QualType EltTy(Ty, 0);
1211 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +00001212 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001213 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +00001214 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +00001215 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001216 // FIXME: Verify this is right for VLAs.
1217 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1218 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +00001219 }
Mike Stump1eb44332009-09-09 15:08:12 +00001220
Chris Lattner9c85ba32008-11-10 06:08:34 +00001221 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001222 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001223
Devang Patelca80a5f2009-10-20 19:55:01 +00001224 llvm::DIType DbgTy =
1225 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001226 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +00001227 0, Size, Align, 0, 0,
1228 getOrCreateType(EltTy, Unit),
1229 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001230 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001231}
1232
Anders Carlssona031b352009-11-06 19:19:55 +00001233llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001234 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001235 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1236 Ty, Ty->getPointeeType(), Unit);
1237}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001238
Anders Carlsson20f12a22009-12-06 18:00:51 +00001239llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001240 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001241 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1242 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1243
1244 if (!Ty->getPointeeType()->isFunctionType()) {
1245 // We have a data member pointer type.
1246 return PointerDiffDITy;
1247 }
1248
1249 // We have a member function pointer type. Treat it as a struct with two
1250 // ptrdiff_t members.
1251 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1252
1253 uint64_t FieldOffset = 0;
1254 llvm::DIDescriptor ElementTypes[2];
1255
1256 // FIXME: This should probably be a function type instead.
1257 ElementTypes[0] =
1258 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001259 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001260 Info.first, Info.second, FieldOffset, 0,
1261 PointerDiffDITy);
1262 FieldOffset += Info.first;
1263
1264 ElementTypes[1] =
1265 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001266 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001267 Info.first, Info.second, FieldOffset, 0,
1268 PointerDiffDITy);
1269
1270 llvm::DIArray Elements =
1271 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1272 llvm::array_lengthof(ElementTypes));
1273
1274 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1275 U, llvm::StringRef("test"),
Devang Pateld58562e2010-03-09 22:49:11 +00001276 U, 0, FieldOffset,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001277 0, 0, 0, llvm::DIType(), Elements);
1278}
1279
Devang Patel6237cea2010-08-23 22:07:25 +00001280/// CreateEnumType - get enumeration type.
1281llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED, llvm::DIFile Unit){
1282 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
1283
1284 // Create DIEnumerator elements for each enumerator.
1285 for (EnumDecl::enumerator_iterator
1286 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
1287 Enum != EnumEnd; ++Enum) {
1288 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
1289 Enum->getInitVal().getZExtValue()));
1290 }
1291
1292 // Return a CompositeType for the enum itself.
1293 llvm::DIArray EltArray =
1294 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
1295
1296 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1297 unsigned Line = getLineNumber(ED->getLocation());
1298 uint64_t Size = 0;
Devang Patelffc52e72010-08-24 18:14:06 +00001299 uint64_t Align = 0;
1300 if (!ED->getTypeForDecl()->isIncompleteType()) {
1301 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1302 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1303 }
Devang Patel6237cea2010-08-23 22:07:25 +00001304 llvm::DIType DbgTy =
1305 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
1306 Unit, ED->getName(), DefUnit, Line,
Devang Patelffc52e72010-08-24 18:14:06 +00001307 Size, Align, 0, 0,
Devang Patel6237cea2010-08-23 22:07:25 +00001308 llvm::DIType(), EltArray);
1309 return DbgTy;
1310}
1311
Douglas Gregor840943d2009-12-21 20:18:30 +00001312static QualType UnwrapTypeForDebugInfo(QualType T) {
1313 do {
1314 QualType LastT = T;
1315 switch (T->getTypeClass()) {
1316 default:
1317 return T;
1318 case Type::TemplateSpecialization:
1319 T = cast<TemplateSpecializationType>(T)->desugar();
1320 break;
1321 case Type::TypeOfExpr: {
1322 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1323 T = Ty->getUnderlyingExpr()->getType();
1324 break;
1325 }
1326 case Type::TypeOf:
1327 T = cast<TypeOfType>(T)->getUnderlyingType();
1328 break;
1329 case Type::Decltype:
1330 T = cast<DecltypeType>(T)->getUnderlyingType();
1331 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001332 case Type::Elaborated:
1333 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001334 break;
1335 case Type::SubstTemplateTypeParm:
1336 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1337 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001338 }
1339
1340 assert(T != LastT && "Type unwrapping failed to unwrap!");
1341 if (T == LastT)
1342 return T;
1343 } while (true);
1344
1345 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001346}
1347
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001348/// getOrCreateType - Get the type from the cache or create a new
1349/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001350llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001351 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001352 if (Ty.isNull())
1353 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001354
Douglas Gregor840943d2009-12-21 20:18:30 +00001355 // Unwrap the type as needed for debug information.
1356 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001357
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001358 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001359 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001360 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001361 if (it != TypeCache.end()) {
1362 // Verify that the debug info still exists.
1363 if (&*it->second)
1364 return llvm::DIType(cast<llvm::MDNode>(it->second));
1365 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001366
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001367 // Otherwise create the type.
1368 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001369
1370 // And update the type cache.
Devang Patelab699792010-05-07 18:12:35 +00001371 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001372 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001373}
1374
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001375/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001376llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001377 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001378 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001379 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001380 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001381
Douglas Gregor2101a822009-12-21 19:57:21 +00001382 const char *Diag = 0;
1383
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001384 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001385 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001386#define TYPE(Class, Base)
1387#define ABSTRACT_TYPE(Class, Base)
1388#define NON_CANONICAL_TYPE(Class, Base)
1389#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1390#include "clang/AST/TypeNodes.def"
1391 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001392
Anders Carlssonbfe69952009-11-06 18:24:04 +00001393 // FIXME: Handle these.
1394 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001395 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001396
1397 case Type::Vector:
1398 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001399 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001400 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCallc12c5bb2010-05-15 11:32:37 +00001401 case Type::ObjCObject:
1402 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001403 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001404 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1405 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1406 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1407 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001408 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001409 return CreateType(cast<BlockPointerType>(Ty), Unit);
1410 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001411 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001412 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001413 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001414 case Type::FunctionProto:
1415 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001416 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001417 case Type::ConstantArray:
1418 case Type::VariableArray:
1419 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001420 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001421
1422 case Type::LValueReference:
1423 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1424
Anders Carlsson20f12a22009-12-06 18:00:51 +00001425 case Type::MemberPointer:
1426 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001427
1428 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001429 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001430 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001431 case Type::TypeOfExpr:
1432 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001433 case Type::Decltype:
1434 llvm_unreachable("type should have been unwrapped!");
1435 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001436
1437 case Type::RValueReference:
1438 // FIXME: Implement!
1439 Diag = "rvalue references";
1440 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001441 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001442
1443 assert(Diag && "Fall through without a diagnostic?");
1444 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1445 "debug information for %0 is not yet supported");
1446 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1447 << Diag;
1448 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001449}
1450
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001451/// CreateMemberType - Create new member and increase Offset by FType's size.
1452llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
1453 llvm::StringRef Name,
1454 uint64_t *Offset) {
1455 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1456 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1457 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
1458 llvm::DIType Ty = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1459 Unit, Name, Unit, 0,
1460 FieldSize, FieldAlign,
1461 *Offset, 0, FieldTy);
1462 *Offset += FieldSize;
1463 return Ty;
1464}
1465
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001466/// EmitFunctionStart - Constructs the debug code for entering a function -
1467/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001468void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001469 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001470 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001471
Devang Patel9c6c3a02010-01-14 00:36:21 +00001472 llvm::StringRef Name;
Anders Carlsson9a20d552010-06-22 16:16:50 +00001473 llvm::StringRef LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001474
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001475 FnBeginRegionCount.push_back(RegionStack.size());
1476
Devang Patel9c6c3a02010-01-14 00:36:21 +00001477 const Decl *D = GD.getDecl();
1478 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001479 // If there is a DISubprogram for this function available then use it.
1480 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1481 FI = SPCache.find(FD);
1482 if (FI != SPCache.end()) {
Gabor Greif38c9b172010-09-18 13:00:17 +00001483 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(&*FI->second));
Devang Patelab699792010-05-07 18:12:35 +00001484 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1485 llvm::MDNode *SPN = SP;
1486 RegionStack.push_back(SPN);
1487 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00001488 return;
1489 }
1490 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001491 Name = getFunctionName(FD);
1492 // Use mangled name as linkage name for c/c++ functions.
Anders Carlsson9a20d552010-06-22 16:16:50 +00001493 LinkageName = CGM.getMangledName(GD);
David Chisnall70b9b442010-09-02 17:16:32 +00001494 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
David Chisnall52044a22010-09-02 18:01:51 +00001495 Name = getObjCMethodName(OMD);
David Chisnall70b9b442010-09-02 17:16:32 +00001496 LinkageName = Name;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001497 } else {
1498 // Use llvm function name as linkage name.
1499 Name = Fn->getName();
Anders Carlsson9a20d552010-06-22 16:16:50 +00001500 LinkageName = Name;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001501 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001502 if (!Name.empty() && Name[0] == '\01')
1503 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001504
Devang Patel970c6182010-04-24 00:49:16 +00001505 // It is expected that CurLoc is set before using EmitFunctionStart.
1506 // Usually, CurLoc points to the left bracket location of compound
1507 // statement representing function body.
1508 llvm::DIFile Unit = getOrCreateFile(CurLoc);
Devang Patel8ab870d2010-05-12 23:46:38 +00001509 unsigned LineNo = getLineNumber(CurLoc);
Devang Patele2472482010-09-29 21:05:52 +00001510 unsigned Flags = 0;
1511 if (D->isImplicit())
1512 Flags |= llvm::DIDescriptor::FlagArtificial;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001513 llvm::DISubprogram SP =
Devang Patel970c6182010-04-24 00:49:16 +00001514 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001515 getOrCreateType(FnType, Unit),
Devang Patel15a3d7d2010-07-15 23:09:46 +00001516 Fn->hasInternalLinkage(), true/*definition*/,
1517 0, 0, llvm::DIType(),
Devang Patele2472482010-09-29 21:05:52 +00001518 Flags,
Devang Patel15a3d7d2010-07-15 23:09:46 +00001519 CGM.getLangOptions().Optimize, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +00001520
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001521 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001522 llvm::MDNode *SPN = SP;
1523 RegionStack.push_back(SPN);
1524 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001525
1526 // Clear stack used to keep track of #line directives.
1527 LineDirectiveFiles.clear();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001528}
1529
1530
Devang Patel4d939e62010-07-20 22:20:10 +00001531void CGDebugInfo::EmitStopPoint(CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001532 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001533
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001534 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001535 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001536 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001537 || (SM.getInstantiationLineNumber(CurLoc) ==
1538 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001539 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patel4800ea62010-04-05 21:09:15 +00001540 // New Builder may not be in sync with CGDebugInfo.
1541 if (!Builder.getCurrentDebugLocation().isUnknown())
1542 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001543
1544 // Update last state.
1545 PrevLoc = CurLoc;
1546
Chris Lattnerc6034632010-04-01 06:31:43 +00001547 llvm::MDNode *Scope = RegionStack.back();
Devang Patel8ab870d2010-05-12 23:46:38 +00001548 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1549 getColumnNumber(CurLoc),
Chris Lattnere541d012010-04-02 20:21:43 +00001550 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001551}
1552
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001553/// UpdateLineDirectiveRegion - Update region stack only if #line directive
1554/// has introduced scope change.
1555void CGDebugInfo::UpdateLineDirectiveRegion(CGBuilderTy &Builder) {
1556 if (CurLoc.isInvalid() || CurLoc.isMacroID() ||
1557 PrevLoc.isInvalid() || PrevLoc.isMacroID())
1558 return;
1559 SourceManager &SM = CGM.getContext().getSourceManager();
1560 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
1561 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc);
1562
1563 if (!strcmp(PPLoc.getFilename(), PCLoc.getFilename()))
1564 return;
1565
1566 // If #line directive stack is empty then we are entering a new scope.
1567 if (LineDirectiveFiles.empty()) {
1568 EmitRegionStart(Builder);
1569 LineDirectiveFiles.push_back(PCLoc.getFilename());
1570 return;
1571 }
1572
1573 assert (RegionStack.size() >= LineDirectiveFiles.size()
1574 && "error handling #line regions!");
1575
1576 bool SeenThisFile = false;
Devang Patel424a5c62010-09-15 20:50:40 +00001577 // Chek if current file is already seen earlier.
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001578 for(std::vector<const char *>::iterator I = LineDirectiveFiles.begin(),
1579 E = LineDirectiveFiles.end(); I != E; ++I)
Devang Patel424a5c62010-09-15 20:50:40 +00001580 if (!strcmp(PCLoc.getFilename(), *I)) {
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001581 SeenThisFile = true;
1582 break;
1583 }
1584
1585 // If #line for this file is seen earlier then pop out #line regions.
1586 if (SeenThisFile) {
1587 while (!LineDirectiveFiles.empty()) {
1588 const char *LastFile = LineDirectiveFiles.back();
1589 RegionStack.pop_back();
1590 LineDirectiveFiles.pop_back();
1591 if (!strcmp(PPLoc.getFilename(), LastFile))
1592 break;
1593 }
1594 return;
1595 }
1596
1597 // .. otherwise insert new #line region.
1598 EmitRegionStart(Builder);
1599 LineDirectiveFiles.push_back(PCLoc.getFilename());
1600
1601 return;
1602}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001603/// EmitRegionStart- Constructs the debug code for entering a declarative
1604/// region - "llvm.dbg.region.start.".
Devang Patel4d939e62010-07-20 22:20:10 +00001605void CGDebugInfo::EmitRegionStart(CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001606 llvm::DIDescriptor D =
1607 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1608 llvm::DIDescriptor() :
Devang Pateld19429f2010-02-16 21:41:20 +00001609 llvm::DIDescriptor(RegionStack.back()),
Stuart Hastings257d1d32010-07-19 23:56:31 +00001610 getOrCreateFile(CurLoc),
Devang Patel8ab870d2010-05-12 23:46:38 +00001611 getLineNumber(CurLoc),
1612 getColumnNumber(CurLoc));
Devang Patelab699792010-05-07 18:12:35 +00001613 llvm::MDNode *DN = D;
1614 RegionStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001615}
1616
1617/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1618/// region - "llvm.dbg.region.end."
Devang Patel4d939e62010-07-20 22:20:10 +00001619void CGDebugInfo::EmitRegionEnd(CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001620 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1621
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001622 // Provide an region stop point.
Devang Patel4d939e62010-07-20 22:20:10 +00001623 EmitStopPoint(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001624
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001625 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001626}
1627
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001628/// EmitFunctionEnd - Constructs the debug code for exiting a function.
1629void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
1630 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1631 unsigned RCount = FnBeginRegionCount.back();
1632 assert(RCount <= RegionStack.size() && "Region stack mismatch");
1633
1634 // Pop all regions for this function.
1635 while (RegionStack.size() != RCount)
1636 EmitRegionEnd(Builder);
1637 FnBeginRegionCount.pop_back();
1638}
1639
Devang Patel809b9bb2010-02-10 18:49:08 +00001640// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1641// See BuildByRefType.
1642llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1643 uint64_t *XOffset) {
1644
1645 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1646
1647 QualType FType;
1648 uint64_t FieldSize, FieldOffset;
1649 unsigned FieldAlign;
1650
Devang Patel17800552010-03-09 00:44:50 +00001651 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001652 QualType Type = VD->getType();
1653
1654 FieldOffset = 0;
1655 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001656 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1657 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001658 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001659 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1660 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1661
Devang Patel809b9bb2010-02-10 18:49:08 +00001662 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1663 if (HasCopyAndDispose) {
1664 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001665 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1666 &FieldOffset));
1667 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1668 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001669 }
1670
1671 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1672 if (Align > CharUnits::fromQuantity(
1673 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1674 unsigned AlignedOffsetInBytes
1675 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1676 unsigned NumPaddingBytes
1677 = AlignedOffsetInBytes - FieldOffset/8;
1678
1679 if (NumPaddingBytes > 0) {
1680 llvm::APInt pad(32, NumPaddingBytes);
1681 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1682 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001683 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001684 }
1685 }
1686
1687 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001688 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00001689 FieldSize = CGM.getContext().getTypeSize(FType);
1690 FieldAlign = Align.getQuantity()*8;
1691
1692 *XOffset = FieldOffset;
1693 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001694 VD->getName(), Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001695 0, FieldSize, FieldAlign,
1696 FieldOffset, 0, FieldTy);
1697 EltTys.push_back(FieldTy);
1698 FieldOffset += FieldSize;
1699
1700 llvm::DIArray Elements =
1701 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1702
Devang Patele2472482010-09-29 21:05:52 +00001703 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Devang Patel809b9bb2010-02-10 18:49:08 +00001704
1705 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001706 Unit, "", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001707 0, FieldOffset, 0, 0, Flags,
1708 llvm::DIType(), Elements);
1709
1710}
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001711/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001712void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001713 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001714 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1715
Devang Patel17800552010-03-09 00:44:50 +00001716 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001717 llvm::DIType Ty;
1718 uint64_t XOffset = 0;
1719 if (VD->hasAttr<BlocksAttr>())
1720 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1721 else
1722 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001723
Devang Patelf4e54a22010-05-07 23:05:55 +00001724 // If there is not any debug info for type then do not emit debug info
1725 // for this variable.
1726 if (!Ty)
1727 return;
1728
Chris Lattner9c85ba32008-11-10 06:08:34 +00001729 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001730 unsigned Line = getLineNumber(VD->getLocation());
1731 unsigned Column = getColumnNumber(VD->getLocation());
Devang Patelaca745b2010-09-29 23:09:21 +00001732 unsigned Flags = 0;
1733 if (VD->isImplicit())
1734 Flags |= llvm::DIDescriptor::FlagArtificial;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001735 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001736 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001737 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patelaca745b2010-09-29 23:09:21 +00001738 VD->getName(), Unit, Line, Ty,
1739 CGM.getLangOptions().Optimize, Flags);
1740
Chris Lattner9c85ba32008-11-10 06:08:34 +00001741 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001742 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001743 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001744
Chris Lattnerc6034632010-04-01 06:31:43 +00001745 llvm::MDNode *Scope = RegionStack.back();
Chris Lattnere541d012010-04-02 20:21:43 +00001746 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001747}
1748
Mike Stumpb1a6e682009-09-30 02:43:10 +00001749/// EmitDeclare - Emit local variable declaration debug info.
1750void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1751 llvm::Value *Storage, CGBuilderTy &Builder,
1752 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001753 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001754 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1755
Devang Patel2b594b92010-04-26 23:28:46 +00001756 if (Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001757 return;
1758
1759 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001760 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001761 llvm::DIType Ty;
1762 if (VD->hasAttr<BlocksAttr>())
1763 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1764 else
1765 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001766
1767 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001768 unsigned Line = getLineNumber(VD->getLocation());
1769 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001770
Devang Pateld6c5a262010-02-01 21:52:22 +00001771 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001772 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001773 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1774 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1775 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1776 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001777 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001778 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1779 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001780 // offset of __forwarding field
1781 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001782 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1783 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1784 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001785 // offset of x field
1786 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001787 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001788 }
1789
1790 // Create the descriptor for the variable.
1791 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001792 DebugFactory.CreateComplexVariable(Tag,
1793 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001794 VD->getName(), Unit, Line, Ty,
Benjamin Kramer3475cfe2010-09-21 15:59:59 +00001795 addr.data(), addr.size());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001796 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001797 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001798 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattnerd5b89022009-12-28 21:44:41 +00001799
Chris Lattnerc6034632010-04-01 06:31:43 +00001800 llvm::MDNode *Scope = RegionStack.back();
Devang Patelf8e10a52010-05-10 23:48:38 +00001801 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001802}
1803
Devang Pateld6c5a262010-02-01 21:52:22 +00001804void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001805 llvm::Value *Storage,
1806 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001807 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001808}
1809
Mike Stumpb1a6e682009-09-30 02:43:10 +00001810void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1811 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1812 CodeGenFunction *CGF) {
1813 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1814}
1815
Chris Lattner9c85ba32008-11-10 06:08:34 +00001816/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1817/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001818void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001819 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001820 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001821}
1822
1823
1824
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001825/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001826void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001827 const VarDecl *D) {
1828
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001829 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001830 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001831 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +00001832
Devang Pateleb6d79b2010-02-01 21:34:11 +00001833 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001834 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001835
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001836 // CodeGen turns int[] into int[1] so we'll do the same here.
1837 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001838
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001839 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001840 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001841
Anders Carlsson20f12a22009-12-06 18:00:51 +00001842 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001843 ArrayType::Normal, 0);
1844 }
Devang Patel5d822f02010-04-29 17:48:37 +00001845 llvm::StringRef DeclName = D->getName();
Devang Patel8b90a782010-05-13 23:52:37 +00001846 llvm::StringRef LinkageName;
Devang Patel0fd3d1f2010-05-14 16:55:25 +00001847 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()))
Devang Patel8b90a782010-05-13 23:52:37 +00001848 LinkageName = Var->getName();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001849 llvm::DIDescriptor DContext =
1850 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
Devang Patel8b90a782010-05-13 23:52:37 +00001851 DebugFactory.CreateGlobalVariable(DContext, DeclName, DeclName, LinkageName,
1852 Unit, LineNo, getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001853 Var->hasInternalLinkage(),
1854 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001855}
1856
Devang Patel9ca36b62009-02-26 21:10:26 +00001857/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001858void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001859 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001860 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001861 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001862 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patel9ca36b62009-02-26 21:10:26 +00001863
Devang Pateld6c5a262010-02-01 21:52:22 +00001864 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001865
Devang Pateld6c5a262010-02-01 21:52:22 +00001866 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001867 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001868
Devang Patel9ca36b62009-02-26 21:10:26 +00001869 // CodeGen turns int[] into int[1] so we'll do the same here.
1870 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001871
Devang Patel9ca36b62009-02-26 21:10:26 +00001872 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001873 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001874
Anders Carlsson20f12a22009-12-06 18:00:51 +00001875 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001876 ArrayType::Normal, 0);
1877 }
1878
Devang Patelf6a39b72009-10-20 18:26:30 +00001879 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001880 getOrCreateType(T, Unit),
1881 Var->hasInternalLinkage(),
1882 true/*definition*/, Var);
1883}
Devang Patelabb485f2010-02-01 19:16:32 +00001884
Devang Patel25c2c8f2010-08-10 17:53:33 +00001885/// EmitGlobalVariable - Emit global variable's debug info.
1886void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
1887 llvm::ConstantInt *Init,
Devang Patel8d308382010-08-10 07:24:25 +00001888 CGBuilderTy &Builder) {
1889 // Create the descriptor for the variable.
1890 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
1891 llvm::StringRef Name = VD->getName();
Devang Patel0317ab02010-08-10 18:27:15 +00001892 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
Devang Patel6237cea2010-08-23 22:07:25 +00001893 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
1894 if (const EnumDecl *ED = dyn_cast<EnumDecl>(ECD->getDeclContext()))
1895 Ty = CreateEnumType(ED, Unit);
1896 }
Devang Patel0317ab02010-08-10 18:27:15 +00001897 // Do not use DIGlobalVariable for enums.
1898 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
1899 return;
Devang Patel8d308382010-08-10 07:24:25 +00001900 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit,
1901 getLineNumber(VD->getLocation()),
Devang Patel4c4acc02010-08-10 20:16:57 +00001902 Ty, true, true, Init);
Devang Patel8d308382010-08-10 07:24:25 +00001903}
1904
Devang Patelabb485f2010-02-01 19:16:32 +00001905/// getOrCreateNamesSpace - Return namespace descriptor for the given
1906/// namespace decl.
1907llvm::DINameSpace
1908CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1909 llvm::DIDescriptor Unit) {
1910 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1911 NameSpaceCache.find(NSDecl);
1912 if (I != NameSpaceCache.end())
1913 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1914
Devang Patel8ab870d2010-05-12 23:46:38 +00001915 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patelabb485f2010-02-01 19:16:32 +00001916
1917 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00001918 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00001919 llvm::DINameSpace NS =
1920 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
Devang Patel2ed8f002010-08-27 17:47:47 +00001921 llvm::DIFile(Unit), LineNo);
Devang Patelab699792010-05-07 18:12:35 +00001922 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00001923 return NS;
1924}