blob: 224710af3573a9dbb7283ec92b77916d94edf4f0 [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());
Devang Patel700a1cb2010-07-20 20:24:18 +0000147}
148
Devang Patel17800552010-03-09 00:44:50 +0000149/// getOrCreateFile - Get the file debug info descriptor for the input location.
150llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Ted Kremenek9c250392010-03-30 00:27:51 +0000151 if (!Loc.isValid())
Devang Patel17800552010-03-09 00:44:50 +0000152 // If Location is not valid then use main input file.
153 return DebugFactory.CreateFile(TheCU.getFilename(), TheCU.getDirectory(),
154 TheCU);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000155 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel17800552010-03-09 00:44:50 +0000156 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Ted Kremenek9c250392010-03-30 00:27:51 +0000157
158 // Cache the results.
159 const char *fname = PLoc.getFilename();
160 llvm::DenseMap<const char *, llvm::WeakVH>::iterator it =
161 DIFileCache.find(fname);
162
163 if (it != DIFileCache.end()) {
164 // Verify that the information still exists.
165 if (&*it->second)
166 return llvm::DIFile(cast<llvm::MDNode>(it->second));
167 }
168
Devang Patelac4d13c2010-07-27 15:17:16 +0000169 llvm::DIFile F = DebugFactory.CreateFile(PLoc.getFilename(),
170 getCurrentDirname(), TheCU);
Ted Kremenek9c250392010-03-30 00:27:51 +0000171
Devang Patelab699792010-05-07 18:12:35 +0000172 DIFileCache[fname] = F;
Ted Kremenek9c250392010-03-30 00:27:51 +0000173 return F;
174
Devang Patel17800552010-03-09 00:44:50 +0000175}
Devang Patel8ab870d2010-05-12 23:46:38 +0000176
177/// getLineNumber - Get line number for the location. If location is invalid
178/// then use current location.
179unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
180 assert (CurLoc.isValid() && "Invalid current location!");
181 SourceManager &SM = CGM.getContext().getSourceManager();
182 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
183 return PLoc.getLine();
184}
185
186/// getColumnNumber - Get column number for the location. If location is
187/// invalid then use current location.
188unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) {
189 assert (CurLoc.isValid() && "Invalid current location!");
190 SourceManager &SM = CGM.getContext().getSourceManager();
191 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
192 return PLoc.getColumn();
193}
194
Devang Patelac4d13c2010-07-27 15:17:16 +0000195llvm::StringRef CGDebugInfo::getCurrentDirname() {
196 if (!CWDName.empty())
197 return CWDName;
198 char *CompDirnamePtr = NULL;
199 llvm::sys::Path CWD = llvm::sys::Path::GetCurrentDirectory();
200 CompDirnamePtr = DebugInfoNames.Allocate<char>(CWD.size());
201 memcpy(CompDirnamePtr, CWD.c_str(), CWD.size());
202 return CWDName = llvm::StringRef(CompDirnamePtr, CWD.size());
203}
204
Devang Patel17800552010-03-09 00:44:50 +0000205/// CreateCompileUnit - Create new compile unit.
206void CGDebugInfo::CreateCompileUnit() {
207
208 // Get absolute path name.
Douglas Gregorac91b4c2010-03-18 23:46:43 +0000209 SourceManager &SM = CGM.getContext().getSourceManager();
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000210 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
211 if (MainFileName.empty())
Devang Patel22fe5852010-03-12 21:04:27 +0000212 MainFileName = "<unknown>";
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000213
Douglas Gregorf6728fc2010-03-22 21:28:29 +0000214 // The main file name provided via the "-main-file-name" option contains just
215 // the file name itself with no path information. This file name may have had
216 // a relative path, so we look into the actual file entry for the main
217 // file to determine the real absolute path for the file.
Devang Patel6e6bc392010-07-23 23:04:28 +0000218 std::string MainFileDir;
Devang Patelac4d13c2010-07-27 15:17:16 +0000219 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000220 MainFileDir = MainFile->getDir()->getName();
Devang Patelac4d13c2010-07-27 15:17:16 +0000221 if (MainFileDir != ".")
222 MainFileName = MainFileDir + "/" + MainFileName;
223 }
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000224
Devang Patelac4d13c2010-07-27 15:17:16 +0000225 // Save filename string.
226 char *FilenamePtr = DebugInfoNames.Allocate<char>(MainFileName.length());
227 memcpy(FilenamePtr, MainFileName.c_str(), MainFileName.length());
228 llvm::StringRef Filename(FilenamePtr, MainFileName.length());
229
Chris Lattner515455a2009-03-25 03:28:08 +0000230 unsigned LangTag;
Devang Patel17800552010-03-09 00:44:50 +0000231 const LangOptions &LO = CGM.getLangOptions();
Chris Lattner515455a2009-03-25 03:28:08 +0000232 if (LO.CPlusPlus) {
233 if (LO.ObjC1)
234 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
235 else
236 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
237 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000238 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000239 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000240 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000241 } else {
242 LangTag = llvm::dwarf::DW_LANG_C89;
243 }
Devang Patel446c6192009-04-17 21:06:59 +0000244
Daniel Dunbar19f19832010-08-24 17:41:09 +0000245 std::string Producer = getClangFullVersion();
Chris Lattner4c2577a2009-05-02 01:00:04 +0000246
247 // Figure out which version of the ObjC runtime we have.
248 unsigned RuntimeVers = 0;
249 if (LO.ObjC1)
250 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000252 // Create new compile unit.
Devang Patel17800552010-03-09 00:44:50 +0000253 TheCU = DebugFactory.CreateCompileUnit(
Devang Patel58115002010-07-27 20:49:59 +0000254 LangTag, Filename, getCurrentDirname(),
Devang Patelac4d13c2010-07-27 15:17:16 +0000255 Producer, true,
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000256 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000257}
258
Devang Patel65e99f22009-02-25 01:36:11 +0000259/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000260/// one if necessary.
261llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel17800552010-03-09 00:44:50 +0000262 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000263 unsigned Encoding = 0;
Devang Patel05127ca2010-07-28 23:23:29 +0000264 const char *BTName = NULL;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000265 switch (BT->getKind()) {
266 default:
267 case BuiltinType::Void:
268 return llvm::DIType();
Devang Patelc8972c62010-07-28 01:33:15 +0000269 case BuiltinType::ObjCClass:
Devang Pateleaf5ee92010-07-21 22:41:25 +0000270 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Patelc8972c62010-07-28 01:33:15 +0000271 Unit, "objc_class", Unit, 0, 0, 0, 0,
Devang Patele2472482010-09-29 21:05:52 +0000272 llvm::DIDescriptor::FlagFwdDecl,
Devang Pateleaf5ee92010-07-21 22:41:25 +0000273 llvm::DIType(), llvm::DIArray());
Devang Patelc8972c62010-07-28 01:33:15 +0000274 case BuiltinType::ObjCId: {
275 // typedef struct objc_class *Class;
276 // typedef struct objc_object {
277 // Class isa;
278 // } *id;
279
280 llvm::DIType OCTy =
281 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
282 Unit, "objc_class", Unit, 0, 0, 0, 0,
Devang Patele2472482010-09-29 21:05:52 +0000283 llvm::DIDescriptor::FlagFwdDecl,
Devang Patelc8972c62010-07-28 01:33:15 +0000284 llvm::DIType(), llvm::DIArray());
285 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
286
287 llvm::DIType ISATy =
288 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
289 Unit, "", Unit,
290 0, Size, 0, 0, 0, OCTy);
291
292 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
293
294 llvm::DIType FieldTy =
295 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
296 "isa", Unit,
297 0,Size, 0, 0, 0, ISATy);
298 EltTys.push_back(FieldTy);
299 llvm::DIArray Elements =
300 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
301
302 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
303 Unit, "objc_object", Unit, 0, 0, 0, 0,
304 0,
305 llvm::DIType(), Elements);
306 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000307 case BuiltinType::UChar:
308 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
309 case BuiltinType::Char_S:
310 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
311 case BuiltinType::UShort:
312 case BuiltinType::UInt:
313 case BuiltinType::ULong:
314 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
315 case BuiltinType::Short:
316 case BuiltinType::Int:
317 case BuiltinType::Long:
318 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
319 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
320 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000321 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000322 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000323 }
Devang Patel05127ca2010-07-28 23:23:29 +0000324
325 switch (BT->getKind()) {
326 case BuiltinType::Long: BTName = "long int"; break;
327 case BuiltinType::LongLong: BTName = "long long int"; break;
328 case BuiltinType::ULong: BTName = "long unsigned int"; break;
329 case BuiltinType::ULongLong: BTName = "long long unsigned int"; break;
330 default:
331 BTName = BT->getName(CGM.getContext().getLangOptions());
332 break;
333 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000334 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000335 uint64_t Size = CGM.getContext().getTypeSize(BT);
336 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000337 uint64_t Offset = 0;
Devang Patel05127ca2010-07-28 23:23:29 +0000338
Devang Patelca80a5f2009-10-20 19:55:01 +0000339 llvm::DIType DbgTy =
Devang Patel05127ca2010-07-28 23:23:29 +0000340 DebugFactory.CreateBasicType(Unit, BTName,
Devang Patelca80a5f2009-10-20 19:55:01 +0000341 Unit, 0, Size, Align,
342 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000343 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000344}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000345
Chris Lattnerb7003772009-04-23 06:13:01 +0000346llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000347 llvm::DIFile Unit) {
Chris Lattnerb7003772009-04-23 06:13:01 +0000348 // Bit size, align and offset of the type.
349 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
350 if (Ty->isComplexIntegerType())
351 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Anders Carlsson20f12a22009-12-06 18:00:51 +0000353 uint64_t Size = CGM.getContext().getTypeSize(Ty);
354 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattnerb7003772009-04-23 06:13:01 +0000355 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Devang Patelca80a5f2009-10-20 19:55:01 +0000357 llvm::DIType DbgTy =
358 DebugFactory.CreateBasicType(Unit, "complex",
359 Unit, 0, Size, Align,
360 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000361 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000362}
363
John McCalla1805292009-09-25 01:40:47 +0000364/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000365/// a new one if necessary.
Devang Patel17800552010-03-09 00:44:50 +0000366llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +0000367 QualifierCollector Qc;
368 const Type *T = Qc.strip(Ty);
369
370 // Ignore these qualifiers for now.
371 Qc.removeObjCGCAttr();
372 Qc.removeAddressSpace();
373
Chris Lattner9c85ba32008-11-10 06:08:34 +0000374 // We will create one Derived type for one qualifier and recurse to handle any
375 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000376 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000377 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000378 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000379 Qc.removeConst();
380 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000381 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000382 Qc.removeVolatile();
383 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000384 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000385 Qc.removeRestrict();
386 } else {
387 assert(Qc.empty() && "Unknown type qualifier for debug info");
388 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000389 }
Mike Stump1eb44332009-09-09 15:08:12 +0000390
John McCalla1805292009-09-25 01:40:47 +0000391 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
392
Daniel Dunbar3845f862008-10-31 03:54:29 +0000393 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
394 // CVR derived types.
Devang Patelca80a5f2009-10-20 19:55:01 +0000395 llvm::DIType DbgTy =
Devang Pateld58562e2010-03-09 22:49:11 +0000396 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000397 0, 0, 0, 0, 0, FromTy);
Devang Patelca80a5f2009-10-20 19:55:01 +0000398 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000399}
400
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000401llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000402 llvm::DIFile Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000403 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000404 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
405 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000406 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000407}
408
Chris Lattner9c85ba32008-11-10 06:08:34 +0000409llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000410 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000411 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
412 Ty->getPointeeType(), Unit);
413}
414
Devang Patelc69e1cf2010-09-30 19:05:55 +0000415/// CreatePointeeType - Create PointTee type. If Pointee is a record
416/// then emit record's fwd if debug info size reduction is enabled.
417llvm::DIType CGDebugInfo::CreatePointeeType(QualType PointeeTy,
418 llvm::DIFile Unit) {
419 if (!CGM.getCodeGenOpts().LimitDebugInfo)
420 return getOrCreateType(PointeeTy, Unit);
421
422 if (const RecordType *RTy = dyn_cast<RecordType>(PointeeTy)) {
423 RecordDecl *RD = RTy->getDecl();
424 unsigned RTag;
425 if (RD->isStruct())
426 RTag = llvm::dwarf::DW_TAG_structure_type;
427 else if (RD->isUnion())
428 RTag = llvm::dwarf::DW_TAG_union_type;
429 else {
430 assert(RD->isClass() && "Unknown RecordType!");
431 RTag = llvm::dwarf::DW_TAG_class_type;
432 }
433
434 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
435 unsigned Line = getLineNumber(RD->getLocation());
436 llvm::DIDescriptor FDContext =
437 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
438
439 return
440 DebugFactory.CreateCompositeType(RTag, FDContext, RD->getName(),
441 DefUnit, Line, 0, 0, 0,
442 llvm::DIType::FlagFwdDecl,
443 llvm::DIType(), llvm::DIArray());
444 }
445 return getOrCreateType(PointeeTy, Unit);
446
447}
448
Anders Carlssona031b352009-11-06 19:19:55 +0000449llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
450 const Type *Ty,
451 QualType PointeeTy,
Devang Patel17800552010-03-09 00:44:50 +0000452 llvm::DIFile Unit) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000453 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000454
455 // Size is always the size of a pointer. We can't use getTypeSize here
456 // because that does not return the correct value for references.
457 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000458 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
459 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Devang Patelc69e1cf2010-09-30 19:05:55 +0000461 return DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
462 0, Size, Align, 0, 0,
463 CreatePointeeType(PointeeTy, Unit));
464
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000465}
466
Mike Stump9bc093c2009-05-14 02:03:51 +0000467llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000468 llvm::DIFile Unit) {
Mike Stump9bc093c2009-05-14 02:03:51 +0000469 if (BlockLiteralGenericSet)
470 return BlockLiteralGeneric;
471
Mike Stump9bc093c2009-05-14 02:03:51 +0000472 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
473
474 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
475
476 llvm::DIType FieldTy;
477
478 QualType FType;
479 uint64_t FieldSize, FieldOffset;
480 unsigned FieldAlign;
481
482 llvm::DIArray Elements;
483 llvm::DIType EltTy, DescTy;
484
485 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000486 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000487 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
488 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000489
Daniel Dunbarca308df2009-05-26 19:40:20 +0000490 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000491 EltTys.clear();
492
Devang Patele2472482010-09-29 21:05:52 +0000493 unsigned Flags = llvm::DIDescriptor::FlagAppleBlock;
Devang Patel8ab870d2010-05-12 23:46:38 +0000494 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump3d363c52009-10-02 02:30:50 +0000495
Mike Stump9bc093c2009-05-14 02:03:51 +0000496 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Devang Patel8ab870d2010-05-12 23:46:38 +0000497 Unit, LineNo, FieldOffset, 0, 0,
498 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Mike Stump9bc093c2009-05-14 02:03:51 +0000500 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000501 uint64_t Size = CGM.getContext().getTypeSize(Ty);
502 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Mike Stump9bc093c2009-05-14 02:03:51 +0000504 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000505 Unit, "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000506 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000507
508 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000509 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000510 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson20f12a22009-12-06 18:00:51 +0000511 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000512 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
513 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramerd3651cc2010-04-24 20:26:20 +0000514 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000515 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000516
Anders Carlsson20f12a22009-12-06 18:00:51 +0000517 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000518 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000519 FieldSize = CGM.getContext().getTypeSize(Ty);
520 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000521 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000522 "__descriptor", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000523 LineNo, FieldSize, FieldAlign,
Mike Stump9bc093c2009-05-14 02:03:51 +0000524 FieldOffset, 0, FieldTy);
525 EltTys.push_back(FieldTy);
526
527 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000528 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000529
530 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Devang Patel8ab870d2010-05-12 23:46:38 +0000531 Unit, LineNo, FieldOffset, 0, 0,
532 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Mike Stump9bc093c2009-05-14 02:03:51 +0000534 BlockLiteralGenericSet = true;
535 BlockLiteralGeneric
536 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000537 "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000538 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000539 return BlockLiteralGeneric;
540}
541
Chris Lattner9c85ba32008-11-10 06:08:34 +0000542llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000543 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000544 // Typedefs are derived from some other type. If we have a typedef of a
545 // typedef, make sure to emit the whole chain.
546 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Chris Lattner9c85ba32008-11-10 06:08:34 +0000548 // We don't set size information, but do specify where the typedef was
549 // declared.
Devang Patel8ab870d2010-05-12 23:46:38 +0000550 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000551
Devang Pateleb6d79b2010-02-01 21:34:11 +0000552 llvm::DIDescriptor TyContext
553 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
554 Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000555 llvm::DIType DbgTy =
Devang Pateld5289052010-01-29 22:29:31 +0000556 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Pateleb6d79b2010-02-01 21:34:11 +0000557 TyContext,
Devang Pateld5289052010-01-29 22:29:31 +0000558 Ty->getDecl()->getName(), Unit,
559 Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000560 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000561}
562
Chris Lattner9c85ba32008-11-10 06:08:34 +0000563llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000564 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000565 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000566
Chris Lattner9c85ba32008-11-10 06:08:34 +0000567 // Add the result type at least.
568 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Chris Lattner9c85ba32008-11-10 06:08:34 +0000570 // Set up remainder of arguments if there is a prototype.
571 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Devang Patelaf164bb2010-10-06 20:51:45 +0000572 if (isa<FunctionNoProtoType>(Ty))
573 EltTys.push_back(DebugFactory.CreateUnspecifiedParameter());
574 else if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000575 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
576 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000577 }
578
Chris Lattner9c85ba32008-11-10 06:08:34 +0000579 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000580 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000581
Devang Patelca80a5f2009-10-20 19:55:01 +0000582 llvm::DIType DbgTy =
583 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000584 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000585 0, 0, 0, 0, 0,
586 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000587 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000588}
589
Devang Patel428deb52010-01-19 00:00:59 +0000590/// CollectRecordFields - A helper function to collect debug info for
591/// record fields. This is used while creating debug info entry for a Record.
592void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000593CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit,
Devang Patel428deb52010-01-19 00:00:59 +0000594 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
595 unsigned FieldNo = 0;
Devang Patel239cec62010-02-01 21:39:52 +0000596 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
597 for (RecordDecl::field_iterator I = RD->field_begin(),
598 E = RD->field_end();
Devang Patel428deb52010-01-19 00:00:59 +0000599 I != E; ++I, ++FieldNo) {
600 FieldDecl *Field = *I;
601 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Devang Patel428deb52010-01-19 00:00:59 +0000602 llvm::StringRef FieldName = Field->getName();
603
Devang Patel4835fdd2010-02-12 01:31:06 +0000604 // Ignore unnamed fields. Do not ignore unnamed records.
605 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel428deb52010-01-19 00:00:59 +0000606 continue;
607
608 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +0000609 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
610 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel428deb52010-01-19 00:00:59 +0000611 QualType FType = Field->getType();
612 uint64_t FieldSize = 0;
613 unsigned FieldAlign = 0;
614 if (!FType->isIncompleteArrayType()) {
615
616 // Bit size, align and offset of the type.
617 FieldSize = CGM.getContext().getTypeSize(FType);
618 Expr *BitWidth = Field->getBitWidth();
619 if (BitWidth)
620 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Devang Patel428deb52010-01-19 00:00:59 +0000621 FieldAlign = CGM.getContext().getTypeAlign(FType);
622 }
623
624 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
625
Devang Patel71f4ff62010-04-21 23:12:37 +0000626 unsigned Flags = 0;
627 AccessSpecifier Access = I->getAccess();
628 if (Access == clang::AS_private)
Devang Patele2472482010-09-29 21:05:52 +0000629 Flags |= llvm::DIDescriptor::FlagPrivate;
Devang Patel71f4ff62010-04-21 23:12:37 +0000630 else if (Access == clang::AS_protected)
Devang Patele2472482010-09-29 21:05:52 +0000631 Flags |= llvm::DIDescriptor::FlagProtected;
Devang Patel71f4ff62010-04-21 23:12:37 +0000632
Devang Patel428deb52010-01-19 00:00:59 +0000633 // Create a DW_TAG_member node to remember the offset of this field in the
634 // struct. FIXME: This is an absolutely insane way to capture this
635 // information. When we gut debug info, this should be fixed.
636 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
637 FieldName, FieldDefUnit,
638 FieldLine, FieldSize, FieldAlign,
Devang Patel71f4ff62010-04-21 23:12:37 +0000639 FieldOffset, Flags, FieldTy);
Devang Patel428deb52010-01-19 00:00:59 +0000640 EltTys.push_back(FieldTy);
641 }
642}
643
Devang Patela6da1922010-01-28 00:28:01 +0000644/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
645/// function type is not updated to include implicit "this" pointer. Use this
646/// routine to get a method type which includes "this" pointer.
647llvm::DIType
648CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000649 llvm::DIFile Unit) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +0000650 llvm::DIType FnTy
651 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
652 0),
653 Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000654
Devang Patela6da1922010-01-28 00:28:01 +0000655 // Add "this" pointer.
656
Devang Patelab699792010-05-07 18:12:35 +0000657 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patela6da1922010-01-28 00:28:01 +0000658 assert (Args.getNumElements() && "Invalid number of arguments!");
659
660 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
661
662 // First element is always return type. For 'void' functions it is NULL.
663 Elts.push_back(Args.getElement(0));
664
Devang Patel2ed8f002010-08-27 17:47:47 +0000665 if (!Method->isStatic())
666 {
667 // "this" pointer is always first argument.
668 ASTContext &Context = CGM.getContext();
669 QualType ThisPtr =
670 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
671 llvm::DIType ThisPtrType =
672 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
Devang Patel769640e2010-07-13 00:24:30 +0000673
Devang Patel2ed8f002010-08-27 17:47:47 +0000674 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
675 Elts.push_back(ThisPtrType);
676 }
Devang Patela6da1922010-01-28 00:28:01 +0000677
678 // Copy rest of the arguments.
679 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
680 Elts.push_back(Args.getElement(i));
681
682 llvm::DIArray EltTypeArray =
683 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
684
685 return
686 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000687 Unit, "", Unit,
Devang Patela6da1922010-01-28 00:28:01 +0000688 0, 0, 0, 0, 0,
689 llvm::DIType(), EltTypeArray);
690}
691
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000692/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
693/// a single member function GlobalDecl.
694llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000695CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000696 llvm::DIFile Unit,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000697 llvm::DIType RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000698 bool IsCtorOrDtor =
699 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
700
701 llvm::StringRef MethodName = getFunctionName(Method);
Devang Patela6da1922010-01-28 00:28:01 +0000702 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000703
704 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
705 // make sense to give a single ctor/dtor a linkage name.
Anders Carlsson9a20d552010-06-22 16:16:50 +0000706 llvm::StringRef MethodLinkageName;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000707 if (!IsCtorOrDtor)
Anders Carlsson9a20d552010-06-22 16:16:50 +0000708 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000709
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000710 // Get the location for the method.
Devang Patel8ab870d2010-05-12 23:46:38 +0000711 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
712 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000713
714 // Collect virtual method info.
715 llvm::DIType ContainingType;
716 unsigned Virtuality = 0;
717 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000718
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000719 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000720 if (Method->isPure())
721 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
722 else
723 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
724
725 // It doesn't make sense to give a virtual destructor a vtable index,
726 // since a single destructor has two entries in the vtable.
727 if (!isa<CXXDestructorDecl>(Method))
Anders Carlsson046c2942010-04-17 20:15:18 +0000728 VIndex = CGM.getVTables().getMethodVTableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000729 ContainingType = RecordTy;
730 }
731
Devang Patele2472482010-09-29 21:05:52 +0000732 unsigned Flags = 0;
733 if (Method->isImplicit())
734 Flags |= llvm::DIDescriptor::FlagArtificial;
Devang Patel10a7a6a2010-09-29 21:46:16 +0000735 AccessSpecifier Access = Method->getAccess();
736 if (Access == clang::AS_private)
737 Flags |= llvm::DIDescriptor::FlagPrivate;
738 else if (Access == clang::AS_protected)
739 Flags |= llvm::DIDescriptor::FlagProtected;
Devang Pateld78a0192010-10-01 23:32:17 +0000740 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
741 if (CXXC->isExplicit())
742 Flags |= llvm::DIDescriptor::FlagExplicit;
743 } else if (const CXXConversionDecl *CXXC =
744 dyn_cast<CXXConversionDecl>(Method)) {
745 if (CXXC->isExplicit())
746 Flags |= llvm::DIDescriptor::FlagExplicit;
747 }
Devang Patel3951e712010-10-07 22:03:49 +0000748 if (Method->hasPrototype())
749 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Pateld78a0192010-10-01 23:32:17 +0000750
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000751 llvm::DISubprogram SP =
752 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
753 MethodLinkageName,
754 MethodDefUnit, MethodLine,
755 MethodTy, /*isLocalToUnit=*/false,
Devang Patela5c5bab2010-07-12 22:54:41 +0000756 /* isDefintion=*/ false,
Devang Patel40894912010-07-15 22:57:00 +0000757 Virtuality, VIndex, ContainingType,
Devang Patele2472482010-09-29 21:05:52 +0000758 Flags,
Devang Patel15a3d7d2010-07-15 23:09:46 +0000759 CGM.getLangOptions().Optimize);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000760
761 // Don't cache ctors or dtors since we have to emit multiple functions for
762 // a single ctor or dtor.
763 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
Devang Patelab699792010-05-07 18:12:35 +0000764 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000765
766 return SP;
767}
768
Devang Patel4125fd22010-01-19 01:54:44 +0000769/// CollectCXXMemberFunctions - A helper function to collect debug info for
770/// C++ member functions.This is used while creating debug info entry for
771/// a Record.
772void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000773CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000774 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000775 llvm::DIType RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000776 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
777 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000778 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000779
Devang Pateld5322da2010-02-09 19:09:28 +0000780 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000781 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000782
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000783 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000784 }
785}
786
Devang Patel2ed8f002010-08-27 17:47:47 +0000787/// CollectCXXFriends - A helper function to collect debug info for
788/// C++ base classes. This is used while creating debug info entry for
789/// a Record.
790void CGDebugInfo::
791CollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit,
792 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
793 llvm::DIType RecordTy) {
794
795 for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(),
796 BE = RD->friend_end(); BI != BE; ++BI) {
797
798 TypeSourceInfo *TInfo = (*BI)->getFriendType();
799 if(TInfo)
800 {
801 llvm::DIType Ty = getOrCreateType(TInfo->getType(), Unit);
802
803 llvm::DIType DTy =
804 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_friend,
805 RecordTy, llvm::StringRef(),
806 Unit, 0, 0, 0,
807 0, 0, Ty);
808
809 EltTys.push_back(DTy);
810 }
811
812 }
813}
814
Devang Patela245c5b2010-01-25 23:32:18 +0000815/// CollectCXXBases - A helper function to collect debug info for
816/// C++ base classes. This is used while creating debug info entry for
817/// a Record.
818void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000819CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000820 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000821 llvm::DIType RecordTy) {
Devang Patela245c5b2010-01-25 23:32:18 +0000822
Devang Patel239cec62010-02-01 21:39:52 +0000823 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
824 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
825 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000826 unsigned BFlags = 0;
827 uint64_t BaseOffset;
828
829 const CXXRecordDecl *Base =
830 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
831
832 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000833 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000834 // expression where it expects +ve number.
Anders Carlssonaf440352010-03-23 04:11:45 +0000835 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base);
Devang Patele2472482010-09-29 21:05:52 +0000836 BFlags = llvm::DIDescriptor::FlagVirtual;
Devang Patelca7daed2010-01-28 21:54:15 +0000837 } else
838 BaseOffset = RL.getBaseClassOffset(Base);
839
840 AccessSpecifier Access = BI->getAccessSpecifier();
841 if (Access == clang::AS_private)
Devang Patele2472482010-09-29 21:05:52 +0000842 BFlags |= llvm::DIDescriptor::FlagPrivate;
Devang Patelca7daed2010-01-28 21:54:15 +0000843 else if (Access == clang::AS_protected)
Devang Patele2472482010-09-29 21:05:52 +0000844 BFlags |= llvm::DIDescriptor::FlagProtected;
Devang Patelca7daed2010-01-28 21:54:15 +0000845
846 llvm::DIType DTy =
847 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
848 RecordTy, llvm::StringRef(),
Devang Pateld58562e2010-03-09 22:49:11 +0000849 Unit, 0, 0, 0,
Devang Patelca7daed2010-01-28 21:54:15 +0000850 BaseOffset, BFlags,
851 getOrCreateType(BI->getType(),
852 Unit));
853 EltTys.push_back(DTy);
854 }
Devang Patela245c5b2010-01-25 23:32:18 +0000855}
856
Devang Patel4ce3f202010-01-28 18:11:52 +0000857/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000858llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000859 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000860 return VTablePtrType;
861
862 ASTContext &Context = CGM.getContext();
863
864 /* Function type */
Benjamin Kramerad468862010-03-30 11:36:44 +0000865 llvm::DIDescriptor STy = getOrCreateType(Context.IntTy, Unit);
866 llvm::DIArray SElements = DebugFactory.GetOrCreateArray(&STy, 1);
Devang Patel4ce3f202010-01-28 18:11:52 +0000867 llvm::DIType SubTy =
868 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000869 Unit, "", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000870 0, 0, 0, 0, 0, llvm::DIType(), SElements);
871
872 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
873 llvm::DIType vtbl_ptr_type
874 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000875 Unit, "__vtbl_ptr_type", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000876 0, Size, 0, 0, 0, SubTy);
877
Devang Pateld58562e2010-03-09 22:49:11 +0000878 VTablePtrType =
879 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
880 Unit, "", Unit,
881 0, Size, 0, 0, 0, vtbl_ptr_type);
Devang Patel4ce3f202010-01-28 18:11:52 +0000882 return VTablePtrType;
883}
884
Anders Carlsson046c2942010-04-17 20:15:18 +0000885/// getVTableName - Get vtable name for the given Class.
886llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000887 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000888 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000889
890 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000891 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000892 memcpy(StrPtr, Name.data(), Name.length());
893 return llvm::StringRef(StrPtr, Name.length());
894}
895
896
Anders Carlsson046c2942010-04-17 20:15:18 +0000897/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel4ce3f202010-01-28 18:11:52 +0000898/// debug info entry in EltTys vector.
899void CGDebugInfo::
Anders Carlsson046c2942010-04-17 20:15:18 +0000900CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000901 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000902 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000903
904 // If there is a primary base then it will hold vtable info.
905 if (RL.getPrimaryBase())
906 return;
907
908 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000909 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000910 return;
911
912 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
913 llvm::DIType VPTR
914 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Anders Carlsson046c2942010-04-17 20:15:18 +0000915 getVTableName(RD), Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000916 0, Size, 0, 0, 0,
917 getOrCreateVTablePtrType(Unit));
918 EltTys.push_back(VPTR);
919}
920
Devang Patelc69e1cf2010-09-30 19:05:55 +0000921/// getOrCreateRecordType - Emit record type's standalone debug info.
922llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
923 SourceLocation Loc) {
924 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
925 DebugFactory.RecordType(T);
926 return T;
927}
928
Devang Patel65e99f22009-02-25 01:36:11 +0000929/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000930llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000931 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000932 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Chris Lattner9c85ba32008-11-10 06:08:34 +0000934 unsigned Tag;
Devang Pateld6c5a262010-02-01 21:52:22 +0000935 if (RD->isStruct())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000936 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Pateld6c5a262010-02-01 21:52:22 +0000937 else if (RD->isUnion())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000938 Tag = llvm::dwarf::DW_TAG_union_type;
939 else {
Devang Pateld6c5a262010-02-01 21:52:22 +0000940 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000941 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000942 }
943
Chris Lattner9c85ba32008-11-10 06:08:34 +0000944 // Get overall information about the record type for the debug info.
Devang Patel8ab870d2010-05-12 23:46:38 +0000945 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
946 unsigned Line = getLineNumber(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000947
Chris Lattner9c85ba32008-11-10 06:08:34 +0000948 // Records and classes and unions can all be recursive. To handle them, we
949 // first generate a debug descriptor for the struct as a forward declaration.
950 // Then (if it is a definition) we go through and get debug info for all of
951 // its members. Finally, we create a descriptor for the complete type (which
952 // may refer to the forward decl if the struct is recursive) and replace all
953 // uses of the forward declaration with the final definition.
Devang Patel0b897992010-07-08 19:56:29 +0000954 llvm::DIDescriptor FDContext =
955 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
956
957 // If this is just a forward declaration, construct an appropriately
958 // marked node and just return it.
959 if (!RD->getDefinition()) {
960 llvm::DICompositeType FwdDecl =
961 DebugFactory.CreateCompositeType(Tag, FDContext, RD->getName(),
962 DefUnit, Line, 0, 0, 0,
Devang Patele2472482010-09-29 21:05:52 +0000963 llvm::DIDescriptor::FlagFwdDecl,
Devang Patel0b897992010-07-08 19:56:29 +0000964 llvm::DIType(), llvm::DIArray());
965
966 return FwdDecl;
967 }
Devang Pateld0f251b2010-01-20 23:56:40 +0000968
Dan Gohman86968372010-08-20 22:39:57 +0000969 llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType();
Mike Stump1eb44332009-09-09 15:08:12 +0000970
Devang Patelab699792010-05-07 18:12:35 +0000971 llvm::MDNode *MN = FwdDecl;
972 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000973 // Otherwise, insert it into the TypeCache so that recursive uses will find
974 // it.
Devang Patelab699792010-05-07 18:12:35 +0000975 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000976 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000977 RegionStack.push_back(FwdDeclNode);
978 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000979
980 // Convert all the elements.
981 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
982
Devang Pateld6c5a262010-02-01 21:52:22 +0000983 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000984 if (CXXDecl) {
985 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson046c2942010-04-17 20:15:18 +0000986 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000987 }
Devang Pateldabc3e92010-08-12 00:02:44 +0000988
989 // Collect static variables with initializers.
990 for (RecordDecl::decl_iterator I = RD->decls_begin(), E = RD->decls_end();
991 I != E; ++I)
992 if (const VarDecl *V = dyn_cast<VarDecl>(*I)) {
993 if (const Expr *Init = V->getInit()) {
994 Expr::EvalResult Result;
995 if (Init->Evaluate(Result, CGM.getContext()) && Result.Val.isInt()) {
996 llvm::ConstantInt *CI
997 = llvm::ConstantInt::get(CGM.getLLVMContext(), Result.Val.getInt());
998
999 // Create the descriptor for static variable.
1000 llvm::DIFile VUnit = getOrCreateFile(V->getLocation());
1001 llvm::StringRef VName = V->getName();
1002 llvm::DIType VTy = getOrCreateType(V->getType(), VUnit);
1003 // Do not use DIGlobalVariable for enums.
1004 if (VTy.getTag() != llvm::dwarf::DW_TAG_enumeration_type) {
1005 DebugFactory.CreateGlobalVariable(FwdDecl, VName, VName, VName, VUnit,
1006 getLineNumber(V->getLocation()),
1007 VTy, true, true, CI);
1008 }
1009 }
1010 }
1011 }
1012
Devang Pateld6c5a262010-02-01 21:52:22 +00001013 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +00001014 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +00001015 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +00001016 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel2ed8f002010-08-27 17:47:47 +00001017 CollectCXXFriends(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +00001018
1019 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +00001020 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel5bc794f2010-10-14 22:59:23 +00001021 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
1022 // Seek non virtual primary base root.
1023 while (1) {
1024 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
1025 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
1026 if (PBT && !BRL.getPrimaryBaseWasVirtual())
1027 PBase = PBT;
1028 else
1029 break;
1030 }
Devang Patel0ac8f312010-01-28 00:54:21 +00001031 ContainingType =
Devang Patelab699792010-05-07 18:12:35 +00001032 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patel5bc794f2010-10-14 22:59:23 +00001033 }
Devang Patel0ac8f312010-01-28 00:54:21 +00001034 else if (CXXDecl->isDynamicClass())
Devang Patelab699792010-05-07 18:12:35 +00001035 ContainingType = FwdDecl;
Devang Patela245c5b2010-01-25 23:32:18 +00001036 }
Mike Stump1eb44332009-09-09 15:08:12 +00001037
Chris Lattner9c85ba32008-11-10 06:08:34 +00001038 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001039 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001040
1041 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001042 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1043 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001044
Devang Patele4c1ea02010-03-11 20:01:48 +00001045 RegionStack.pop_back();
1046 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1047 RegionMap.find(Ty->getDecl());
1048 if (RI != RegionMap.end())
1049 RegionMap.erase(RI);
1050
Devang Patel411894b2010-02-01 22:40:08 +00001051 llvm::DIDescriptor RDContext =
1052 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel700a1cb2010-07-20 20:24:18 +00001053
1054 llvm::StringRef RDName = RD->getName();
1055 // If this is a class, include the template arguments also.
1056 if (Tag == llvm::dwarf::DW_TAG_class_type)
1057 RDName = getClassName(RD);
1058
Devang Patel0ce73f62009-07-22 18:57:00 +00001059 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +00001060 DebugFactory.CreateCompositeType(Tag, RDContext,
Devang Patel700a1cb2010-07-20 20:24:18 +00001061 RDName,
Devang Patelab71ff52009-11-12 00:51:46 +00001062 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +00001063 llvm::DIType(), Elements,
1064 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001065
1066 // Now that we have a real decl for the struct, replace anything using the
1067 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001068 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001069 RegionMap[RD] = llvm::WeakVH(RealDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001070 return RealDecl;
1071}
1072
John McCallc12c5bb2010-05-15 11:32:37 +00001073/// CreateType - get objective-c object type.
1074llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1075 llvm::DIFile Unit) {
1076 // Ignore protocols.
1077 return getOrCreateType(Ty->getBaseType(), Unit);
1078}
1079
Devang Patel9ca36b62009-02-26 21:10:26 +00001080/// CreateType - get objective-c interface type.
1081llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001082 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001083 ObjCInterfaceDecl *ID = Ty->getDecl();
Devang Patel9ca36b62009-02-26 21:10:26 +00001084 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Patel9ca36b62009-02-26 21:10:26 +00001085
1086 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +00001087 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001088 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel17800552010-03-09 00:44:50 +00001089 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +00001090
Dan Gohman45f7c782010-08-23 21:15:56 +00001091 // If this is just a forward declaration, return a special forward-declaration
1092 // debug type.
1093 if (ID->isForwardDecl()) {
1094 llvm::DICompositeType FwdDecl =
1095 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
1096 DefUnit, Line, 0, 0, 0, 0,
1097 llvm::DIType(), llvm::DIArray(),
1098 RuntimeLang);
1099 return FwdDecl;
1100 }
1101
Devang Patel9ca36b62009-02-26 21:10:26 +00001102 // To handle recursive interface, we
1103 // first generate a debug descriptor for the struct as a forward declaration.
1104 // Then (if it is a definition) we go through and get debug info for all of
1105 // its members. Finally, we create a descriptor for the complete type (which
1106 // may refer to the forward decl if the struct is recursive) and replace all
1107 // uses of the forward declaration with the final definition.
Dan Gohman86968372010-08-20 22:39:57 +00001108 llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType();
Mike Stump1eb44332009-09-09 15:08:12 +00001109
Devang Patelab699792010-05-07 18:12:35 +00001110 llvm::MDNode *MN = FwdDecl;
1111 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patel9ca36b62009-02-26 21:10:26 +00001112 // Otherwise, insert it into the TypeCache so that recursive uses will find
1113 // it.
Devang Patelab699792010-05-07 18:12:35 +00001114 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +00001115 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001116 RegionStack.push_back(FwdDeclNode);
1117 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001118
1119 // Convert all the elements.
1120 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
1121
Devang Pateld6c5a262010-02-01 21:52:22 +00001122 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +00001123 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +00001124 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001125 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001126 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +00001127 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Pateld58562e2010-03-09 22:49:11 +00001128 Unit, "", Unit, 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +00001129 0 /* offset */, 0, SClassTy);
1130 EltTys.push_back(InhTag);
1131 }
1132
Devang Pateld6c5a262010-02-01 21:52:22 +00001133 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001134
1135 unsigned FieldNo = 0;
Fariborz Jahanian97477392010-10-01 00:01:53 +00001136 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
Fariborz Jahanianfe8fdba2010-10-11 23:55:47 +00001137 Field = Field->getNextIvar(), ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001138 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1139
Devang Patel73621622009-11-25 17:37:31 +00001140 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001141
Devang Patelde135022009-04-27 22:40:36 +00001142 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +00001143 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +00001144 continue;
1145
Devang Patel9ca36b62009-02-26 21:10:26 +00001146 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +00001147 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1148 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel99c20eb2009-03-20 18:24:39 +00001149 QualType FType = Field->getType();
1150 uint64_t FieldSize = 0;
1151 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +00001152
Devang Patel99c20eb2009-03-20 18:24:39 +00001153 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001154
Devang Patel99c20eb2009-03-20 18:24:39 +00001155 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001156 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001157 Expr *BitWidth = Field->getBitWidth();
1158 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +00001159 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +00001160
Anders Carlsson20f12a22009-12-06 18:00:51 +00001161 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001162 }
1163
Mike Stump1eb44332009-09-09 15:08:12 +00001164 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
1165
Devang Patelc20482b2009-03-19 00:23:53 +00001166 unsigned Flags = 0;
1167 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Devang Patele2472482010-09-29 21:05:52 +00001168 Flags = llvm::DIDescriptor::FlagProtected;
Devang Patelc20482b2009-03-19 00:23:53 +00001169 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Devang Patele2472482010-09-29 21:05:52 +00001170 Flags = llvm::DIDescriptor::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +00001171
Devang Patel9ca36b62009-02-26 21:10:26 +00001172 // Create a DW_TAG_member node to remember the offset of this field in the
1173 // struct. FIXME: This is an absolutely insane way to capture this
1174 // information. When we gut debug info, this should be fixed.
1175 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1176 FieldName, FieldDefUnit,
1177 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +00001178 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +00001179 EltTys.push_back(FieldTy);
1180 }
Mike Stump1eb44332009-09-09 15:08:12 +00001181
Devang Patel9ca36b62009-02-26 21:10:26 +00001182 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +00001183 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +00001184
Devang Patele4c1ea02010-03-11 20:01:48 +00001185 RegionStack.pop_back();
1186 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1187 RegionMap.find(Ty->getDecl());
1188 if (RI != RegionMap.end())
1189 RegionMap.erase(RI);
1190
Devang Patel9ca36b62009-02-26 21:10:26 +00001191 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001192 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1193 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001194
Devang Patel6c1fddf2009-07-27 18:42:03 +00001195 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +00001196 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +00001197 Line, Size, Align, 0, 0, llvm::DIType(),
1198 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +00001199
1200 // Now that we have a real decl for the struct, replace anything using the
1201 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001202 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001203 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +00001204
Devang Patel9ca36b62009-02-26 21:10:26 +00001205 return RealDecl;
1206}
1207
Chris Lattner9c85ba32008-11-10 06:08:34 +00001208llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001209 llvm::DIFile Unit) {
Devang Patel6237cea2010-08-23 22:07:25 +00001210 return CreateEnumType(Ty->getDecl(), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001211
Chris Lattner9c85ba32008-11-10 06:08:34 +00001212}
1213
1214llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001215 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001216 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1217 return CreateType(RT, Unit);
1218 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1219 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001220
Chris Lattner9c85ba32008-11-10 06:08:34 +00001221 return llvm::DIType();
1222}
1223
Devang Patel70c23cd2010-02-23 22:59:39 +00001224llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Eli Friedmana7e68452010-08-22 01:00:03 +00001225 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001226 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1227 uint64_t NumElems = Ty->getNumElements();
1228 if (NumElems > 0)
1229 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001230
Benjamin Kramerad468862010-03-30 11:36:44 +00001231 llvm::DIDescriptor Subscript = DebugFactory.GetOrCreateSubrange(0, NumElems);
1232 llvm::DIArray SubscriptArray = DebugFactory.GetOrCreateArray(&Subscript, 1);
Devang Patel70c23cd2010-02-23 22:59:39 +00001233
1234 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1235 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1236
1237 return
1238 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001239 Unit, "", Unit,
Devang Patel70c23cd2010-02-23 22:59:39 +00001240 0, Size, Align, 0, 0,
Eli Friedmana7e68452010-08-22 01:00:03 +00001241 ElementTy, SubscriptArray);
Devang Patel70c23cd2010-02-23 22:59:39 +00001242}
1243
Chris Lattner9c85ba32008-11-10 06:08:34 +00001244llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001245 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001246 uint64_t Size;
1247 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001248
1249
Nuno Lopes010d5142009-01-28 00:35:17 +00001250 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001251 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001252 Size = 0;
1253 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001254 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001255 } else if (Ty->isIncompleteArrayType()) {
1256 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001257 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001258 } else {
1259 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001260 Size = CGM.getContext().getTypeSize(Ty);
1261 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001262 }
Mike Stump1eb44332009-09-09 15:08:12 +00001263
Chris Lattner9c85ba32008-11-10 06:08:34 +00001264 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1265 // interior arrays, do we care? Why aren't nested arrays represented the
1266 // obvious/recursive way?
1267 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1268 QualType EltTy(Ty, 0);
Devang Patelcdf523c2010-10-06 18:30:00 +00001269 if (Ty->isIncompleteArrayType())
Chris Lattner9c85ba32008-11-10 06:08:34 +00001270 EltTy = Ty->getElementType();
Devang Patelcdf523c2010-10-06 18:30:00 +00001271 else {
1272 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1273 uint64_t Upper = 0;
1274 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1275 if (CAT->getSize().getZExtValue())
1276 Upper = CAT->getSize().getZExtValue() - 1;
1277 // FIXME: Verify this is right for VLAs.
1278 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1279 EltTy = Ty->getElementType();
1280 }
Sanjiv Gupta507de852008-06-09 10:47:41 +00001281 }
Mike Stump1eb44332009-09-09 15:08:12 +00001282
Chris Lattner9c85ba32008-11-10 06:08:34 +00001283 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001284 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001285
Devang Patelca80a5f2009-10-20 19:55:01 +00001286 llvm::DIType DbgTy =
1287 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001288 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +00001289 0, Size, Align, 0, 0,
1290 getOrCreateType(EltTy, Unit),
1291 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001292 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001293}
1294
Anders Carlssona031b352009-11-06 19:19:55 +00001295llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001296 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001297 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1298 Ty, Ty->getPointeeType(), Unit);
1299}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001300
Anders Carlsson20f12a22009-12-06 18:00:51 +00001301llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001302 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001303 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1304 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1305
1306 if (!Ty->getPointeeType()->isFunctionType()) {
1307 // We have a data member pointer type.
1308 return PointerDiffDITy;
1309 }
1310
1311 // We have a member function pointer type. Treat it as a struct with two
1312 // ptrdiff_t members.
1313 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1314
1315 uint64_t FieldOffset = 0;
1316 llvm::DIDescriptor ElementTypes[2];
1317
1318 // FIXME: This should probably be a function type instead.
1319 ElementTypes[0] =
1320 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001321 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001322 Info.first, Info.second, FieldOffset, 0,
1323 PointerDiffDITy);
1324 FieldOffset += Info.first;
1325
1326 ElementTypes[1] =
1327 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001328 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001329 Info.first, Info.second, FieldOffset, 0,
1330 PointerDiffDITy);
1331
1332 llvm::DIArray Elements =
1333 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1334 llvm::array_lengthof(ElementTypes));
1335
1336 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1337 U, llvm::StringRef("test"),
Devang Pateld58562e2010-03-09 22:49:11 +00001338 U, 0, FieldOffset,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001339 0, 0, 0, llvm::DIType(), Elements);
1340}
1341
Devang Patel6237cea2010-08-23 22:07:25 +00001342/// CreateEnumType - get enumeration type.
1343llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED, llvm::DIFile Unit){
1344 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
1345
1346 // Create DIEnumerator elements for each enumerator.
1347 for (EnumDecl::enumerator_iterator
1348 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
1349 Enum != EnumEnd; ++Enum) {
1350 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
1351 Enum->getInitVal().getZExtValue()));
1352 }
1353
1354 // Return a CompositeType for the enum itself.
1355 llvm::DIArray EltArray =
1356 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
1357
1358 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1359 unsigned Line = getLineNumber(ED->getLocation());
1360 uint64_t Size = 0;
Devang Patelffc52e72010-08-24 18:14:06 +00001361 uint64_t Align = 0;
1362 if (!ED->getTypeForDecl()->isIncompleteType()) {
1363 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1364 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1365 }
Devang Patel6237cea2010-08-23 22:07:25 +00001366 llvm::DIType DbgTy =
1367 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
1368 Unit, ED->getName(), DefUnit, Line,
Devang Patelffc52e72010-08-24 18:14:06 +00001369 Size, Align, 0, 0,
Devang Patel6237cea2010-08-23 22:07:25 +00001370 llvm::DIType(), EltArray);
1371 return DbgTy;
1372}
1373
Douglas Gregor840943d2009-12-21 20:18:30 +00001374static QualType UnwrapTypeForDebugInfo(QualType T) {
1375 do {
1376 QualType LastT = T;
1377 switch (T->getTypeClass()) {
1378 default:
1379 return T;
1380 case Type::TemplateSpecialization:
1381 T = cast<TemplateSpecializationType>(T)->desugar();
1382 break;
1383 case Type::TypeOfExpr: {
1384 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1385 T = Ty->getUnderlyingExpr()->getType();
1386 break;
1387 }
1388 case Type::TypeOf:
1389 T = cast<TypeOfType>(T)->getUnderlyingType();
1390 break;
1391 case Type::Decltype:
1392 T = cast<DecltypeType>(T)->getUnderlyingType();
1393 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001394 case Type::Elaborated:
1395 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001396 break;
1397 case Type::SubstTemplateTypeParm:
1398 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1399 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001400 }
1401
1402 assert(T != LastT && "Type unwrapping failed to unwrap!");
1403 if (T == LastT)
1404 return T;
1405 } while (true);
1406
1407 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001408}
1409
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001410/// getOrCreateType - Get the type from the cache or create a new
1411/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001412llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001413 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001414 if (Ty.isNull())
1415 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001416
Douglas Gregor840943d2009-12-21 20:18:30 +00001417 // Unwrap the type as needed for debug information.
1418 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001419
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001420 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001421 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001422 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001423 if (it != TypeCache.end()) {
1424 // Verify that the debug info still exists.
1425 if (&*it->second)
1426 return llvm::DIType(cast<llvm::MDNode>(it->second));
1427 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001428
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001429 // Otherwise create the type.
1430 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001431
1432 // And update the type cache.
Devang Patelab699792010-05-07 18:12:35 +00001433 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001434 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001435}
1436
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001437/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001438llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001439 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001440 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001441 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001442 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001443
Douglas Gregor2101a822009-12-21 19:57:21 +00001444 const char *Diag = 0;
1445
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001446 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001447 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001448#define TYPE(Class, Base)
1449#define ABSTRACT_TYPE(Class, Base)
1450#define NON_CANONICAL_TYPE(Class, Base)
1451#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1452#include "clang/AST/TypeNodes.def"
1453 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001454
Anders Carlssonbfe69952009-11-06 18:24:04 +00001455 // FIXME: Handle these.
1456 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001457 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001458
1459 case Type::Vector:
1460 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001461 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001462 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCallc12c5bb2010-05-15 11:32:37 +00001463 case Type::ObjCObject:
1464 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001465 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001466 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1467 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1468 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1469 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001470 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001471 return CreateType(cast<BlockPointerType>(Ty), Unit);
1472 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001473 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001474 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001475 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001476 case Type::FunctionProto:
1477 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001478 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001479 case Type::ConstantArray:
1480 case Type::VariableArray:
1481 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001482 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001483
1484 case Type::LValueReference:
1485 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1486
Anders Carlsson20f12a22009-12-06 18:00:51 +00001487 case Type::MemberPointer:
1488 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001489
1490 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001491 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001492 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001493 case Type::TypeOfExpr:
1494 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001495 case Type::Decltype:
1496 llvm_unreachable("type should have been unwrapped!");
1497 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001498
1499 case Type::RValueReference:
1500 // FIXME: Implement!
1501 Diag = "rvalue references";
1502 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001503 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001504
1505 assert(Diag && "Fall through without a diagnostic?");
1506 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1507 "debug information for %0 is not yet supported");
1508 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1509 << Diag;
1510 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001511}
1512
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001513/// CreateMemberType - Create new member and increase Offset by FType's size.
1514llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
1515 llvm::StringRef Name,
1516 uint64_t *Offset) {
1517 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1518 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1519 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
1520 llvm::DIType Ty = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1521 Unit, Name, Unit, 0,
1522 FieldSize, FieldAlign,
1523 *Offset, 0, FieldTy);
1524 *Offset += FieldSize;
1525 return Ty;
1526}
1527
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001528/// EmitFunctionStart - Constructs the debug code for entering a function -
1529/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001530void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001531 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001532 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001533
Devang Patel9c6c3a02010-01-14 00:36:21 +00001534 llvm::StringRef Name;
Anders Carlsson9a20d552010-06-22 16:16:50 +00001535 llvm::StringRef LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001536
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001537 FnBeginRegionCount.push_back(RegionStack.size());
1538
Devang Patel9c6c3a02010-01-14 00:36:21 +00001539 const Decl *D = GD.getDecl();
Devang Patel3951e712010-10-07 22:03:49 +00001540 unsigned Flags = 0;
Devang Patel0692f832010-10-11 21:58:41 +00001541 llvm::DIFile Unit = getOrCreateFile(CurLoc);
1542 llvm::DIDescriptor FDContext(Unit);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001543 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001544 // If there is a DISubprogram for this function available then use it.
1545 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1546 FI = SPCache.find(FD);
1547 if (FI != SPCache.end()) {
Gabor Greif38c9b172010-09-18 13:00:17 +00001548 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(&*FI->second));
Devang Patelab699792010-05-07 18:12:35 +00001549 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1550 llvm::MDNode *SPN = SP;
1551 RegionStack.push_back(SPN);
1552 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00001553 return;
1554 }
1555 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001556 Name = getFunctionName(FD);
1557 // Use mangled name as linkage name for c/c++ functions.
Anders Carlsson9a20d552010-06-22 16:16:50 +00001558 LinkageName = CGM.getMangledName(GD);
Devang Patel3951e712010-10-07 22:03:49 +00001559 if (FD->hasPrototype())
1560 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel0692f832010-10-11 21:58:41 +00001561 if (const NamespaceDecl *NSDecl =
1562 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
1563 FDContext = getOrCreateNameSpace(NSDecl, Unit);
David Chisnall70b9b442010-09-02 17:16:32 +00001564 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
David Chisnall52044a22010-09-02 18:01:51 +00001565 Name = getObjCMethodName(OMD);
David Chisnall70b9b442010-09-02 17:16:32 +00001566 LinkageName = Name;
Devang Patel3951e712010-10-07 22:03:49 +00001567 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001568 } else {
1569 // Use llvm function name as linkage name.
1570 Name = Fn->getName();
Anders Carlsson9a20d552010-06-22 16:16:50 +00001571 LinkageName = Name;
Devang Patel3951e712010-10-07 22:03:49 +00001572 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001573 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001574 if (!Name.empty() && Name[0] == '\01')
1575 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001576
Devang Patel970c6182010-04-24 00:49:16 +00001577 // It is expected that CurLoc is set before using EmitFunctionStart.
1578 // Usually, CurLoc points to the left bracket location of compound
1579 // statement representing function body.
Devang Patel8ab870d2010-05-12 23:46:38 +00001580 unsigned LineNo = getLineNumber(CurLoc);
Devang Patele2472482010-09-29 21:05:52 +00001581 if (D->isImplicit())
1582 Flags |= llvm::DIDescriptor::FlagArtificial;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001583 llvm::DISubprogram SP =
Devang Patel0692f832010-10-11 21:58:41 +00001584 DebugFactory.CreateSubprogram(FDContext, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001585 getOrCreateType(FnType, Unit),
Devang Patel15a3d7d2010-07-15 23:09:46 +00001586 Fn->hasInternalLinkage(), true/*definition*/,
1587 0, 0, llvm::DIType(),
Devang Patele2472482010-09-29 21:05:52 +00001588 Flags,
Devang Patel15a3d7d2010-07-15 23:09:46 +00001589 CGM.getLangOptions().Optimize, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +00001590
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001591 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001592 llvm::MDNode *SPN = SP;
1593 RegionStack.push_back(SPN);
1594 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001595
1596 // Clear stack used to keep track of #line directives.
1597 LineDirectiveFiles.clear();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001598}
1599
1600
Devang Patel4d939e62010-07-20 22:20:10 +00001601void CGDebugInfo::EmitStopPoint(CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001602 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001603
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001604 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001605 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001606 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001607 || (SM.getInstantiationLineNumber(CurLoc) ==
1608 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001609 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patel4800ea62010-04-05 21:09:15 +00001610 // New Builder may not be in sync with CGDebugInfo.
1611 if (!Builder.getCurrentDebugLocation().isUnknown())
1612 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001613
1614 // Update last state.
1615 PrevLoc = CurLoc;
1616
Chris Lattnerc6034632010-04-01 06:31:43 +00001617 llvm::MDNode *Scope = RegionStack.back();
Devang Patel8ab870d2010-05-12 23:46:38 +00001618 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1619 getColumnNumber(CurLoc),
Chris Lattnere541d012010-04-02 20:21:43 +00001620 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001621}
1622
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001623/// UpdateLineDirectiveRegion - Update region stack only if #line directive
1624/// has introduced scope change.
1625void CGDebugInfo::UpdateLineDirectiveRegion(CGBuilderTy &Builder) {
1626 if (CurLoc.isInvalid() || CurLoc.isMacroID() ||
1627 PrevLoc.isInvalid() || PrevLoc.isMacroID())
1628 return;
1629 SourceManager &SM = CGM.getContext().getSourceManager();
1630 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
1631 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc);
1632
1633 if (!strcmp(PPLoc.getFilename(), PCLoc.getFilename()))
1634 return;
1635
1636 // If #line directive stack is empty then we are entering a new scope.
1637 if (LineDirectiveFiles.empty()) {
1638 EmitRegionStart(Builder);
1639 LineDirectiveFiles.push_back(PCLoc.getFilename());
1640 return;
1641 }
1642
1643 assert (RegionStack.size() >= LineDirectiveFiles.size()
1644 && "error handling #line regions!");
1645
1646 bool SeenThisFile = false;
Devang Patel424a5c62010-09-15 20:50:40 +00001647 // Chek if current file is already seen earlier.
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001648 for(std::vector<const char *>::iterator I = LineDirectiveFiles.begin(),
1649 E = LineDirectiveFiles.end(); I != E; ++I)
Devang Patel424a5c62010-09-15 20:50:40 +00001650 if (!strcmp(PCLoc.getFilename(), *I)) {
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001651 SeenThisFile = true;
1652 break;
1653 }
1654
1655 // If #line for this file is seen earlier then pop out #line regions.
1656 if (SeenThisFile) {
1657 while (!LineDirectiveFiles.empty()) {
1658 const char *LastFile = LineDirectiveFiles.back();
1659 RegionStack.pop_back();
1660 LineDirectiveFiles.pop_back();
1661 if (!strcmp(PPLoc.getFilename(), LastFile))
1662 break;
1663 }
1664 return;
1665 }
1666
1667 // .. otherwise insert new #line region.
1668 EmitRegionStart(Builder);
1669 LineDirectiveFiles.push_back(PCLoc.getFilename());
1670
1671 return;
1672}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001673/// EmitRegionStart- Constructs the debug code for entering a declarative
1674/// region - "llvm.dbg.region.start.".
Devang Patel4d939e62010-07-20 22:20:10 +00001675void CGDebugInfo::EmitRegionStart(CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001676 llvm::DIDescriptor D =
1677 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1678 llvm::DIDescriptor() :
Devang Pateld19429f2010-02-16 21:41:20 +00001679 llvm::DIDescriptor(RegionStack.back()),
Stuart Hastings257d1d32010-07-19 23:56:31 +00001680 getOrCreateFile(CurLoc),
Devang Patel8ab870d2010-05-12 23:46:38 +00001681 getLineNumber(CurLoc),
1682 getColumnNumber(CurLoc));
Devang Patelab699792010-05-07 18:12:35 +00001683 llvm::MDNode *DN = D;
1684 RegionStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001685}
1686
1687/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1688/// region - "llvm.dbg.region.end."
Devang Patel4d939e62010-07-20 22:20:10 +00001689void CGDebugInfo::EmitRegionEnd(CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001690 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1691
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001692 // Provide an region stop point.
Devang Patel4d939e62010-07-20 22:20:10 +00001693 EmitStopPoint(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001694
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001695 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001696}
1697
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001698/// EmitFunctionEnd - Constructs the debug code for exiting a function.
1699void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
1700 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1701 unsigned RCount = FnBeginRegionCount.back();
1702 assert(RCount <= RegionStack.size() && "Region stack mismatch");
1703
1704 // Pop all regions for this function.
1705 while (RegionStack.size() != RCount)
1706 EmitRegionEnd(Builder);
1707 FnBeginRegionCount.pop_back();
1708}
1709
Devang Patel809b9bb2010-02-10 18:49:08 +00001710// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1711// See BuildByRefType.
1712llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1713 uint64_t *XOffset) {
1714
1715 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1716
1717 QualType FType;
1718 uint64_t FieldSize, FieldOffset;
1719 unsigned FieldAlign;
1720
Devang Patel17800552010-03-09 00:44:50 +00001721 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001722 QualType Type = VD->getType();
1723
1724 FieldOffset = 0;
1725 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001726 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1727 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001728 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001729 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1730 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1731
Devang Patel809b9bb2010-02-10 18:49:08 +00001732 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1733 if (HasCopyAndDispose) {
1734 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001735 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1736 &FieldOffset));
1737 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1738 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001739 }
1740
1741 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1742 if (Align > CharUnits::fromQuantity(
1743 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1744 unsigned AlignedOffsetInBytes
1745 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1746 unsigned NumPaddingBytes
1747 = AlignedOffsetInBytes - FieldOffset/8;
1748
1749 if (NumPaddingBytes > 0) {
1750 llvm::APInt pad(32, NumPaddingBytes);
1751 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1752 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001753 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001754 }
1755 }
1756
1757 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001758 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00001759 FieldSize = CGM.getContext().getTypeSize(FType);
1760 FieldAlign = Align.getQuantity()*8;
1761
1762 *XOffset = FieldOffset;
1763 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001764 VD->getName(), Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001765 0, FieldSize, FieldAlign,
1766 FieldOffset, 0, FieldTy);
1767 EltTys.push_back(FieldTy);
1768 FieldOffset += FieldSize;
1769
1770 llvm::DIArray Elements =
1771 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1772
Devang Patele2472482010-09-29 21:05:52 +00001773 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Devang Patel809b9bb2010-02-10 18:49:08 +00001774
1775 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001776 Unit, "", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001777 0, FieldOffset, 0, 0, Flags,
1778 llvm::DIType(), Elements);
1779
1780}
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001781/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001782void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001783 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001784 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1785
Devang Patel17800552010-03-09 00:44:50 +00001786 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001787 llvm::DIType Ty;
1788 uint64_t XOffset = 0;
1789 if (VD->hasAttr<BlocksAttr>())
1790 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1791 else
1792 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001793
Devang Patelf4e54a22010-05-07 23:05:55 +00001794 // If there is not any debug info for type then do not emit debug info
1795 // for this variable.
1796 if (!Ty)
1797 return;
1798
Chris Lattner9c85ba32008-11-10 06:08:34 +00001799 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001800 unsigned Line = getLineNumber(VD->getLocation());
1801 unsigned Column = getColumnNumber(VD->getLocation());
Devang Patelaca745b2010-09-29 23:09:21 +00001802 unsigned Flags = 0;
1803 if (VD->isImplicit())
1804 Flags |= llvm::DIDescriptor::FlagArtificial;
Chris Lattnerc6034632010-04-01 06:31:43 +00001805 llvm::MDNode *Scope = RegionStack.back();
Devang Patelcebbedd2010-10-12 23:24:54 +00001806
1807 llvm::StringRef Name = VD->getName();
1808 if (!Name.empty()) {
1809 // Create the descriptor for the variable.
1810 llvm::DIVariable D =
1811 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(Scope),
1812 Name, Unit, Line, Ty,
1813 CGM.getLangOptions().Optimize, Flags);
1814
1815 // Insert an llvm.dbg.declare into the current block.
1816 llvm::Instruction *Call =
1817 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
1818
1819 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
1820 }
1821
1822 // If VD is an anonymous union then Storage represents value for
1823 // all union fields.
1824 if (const RecordType *RT = dyn_cast<RecordType>(VD->getType()))
1825 if (const RecordDecl *RD = dyn_cast<RecordDecl>(RT->getDecl()))
1826 if (RD->isUnion()) {
1827 for (RecordDecl::field_iterator I = RD->field_begin(),
1828 E = RD->field_end();
1829 I != E; ++I) {
1830 FieldDecl *Field = *I;
1831 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1832 llvm::StringRef FieldName = Field->getName();
1833
1834 // Ignore unnamed fields. Do not ignore unnamed records.
1835 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
1836 continue;
1837
1838 // Use VarDecl's Tag, Scope and Line number.
1839 llvm::DIVariable D =
1840 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(Scope),
1841 FieldName, Unit, Line, FieldTy,
1842 CGM.getLangOptions().Optimize, Flags);
1843
1844 // Insert an llvm.dbg.declare into the current block.
1845 llvm::Instruction *Call =
1846 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
1847
1848 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
1849 }
1850 }
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001851}
1852
Mike Stumpb1a6e682009-09-30 02:43:10 +00001853/// EmitDeclare - Emit local variable declaration debug info.
1854void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1855 llvm::Value *Storage, CGBuilderTy &Builder,
1856 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001857 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001858 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1859
Devang Patel2b594b92010-04-26 23:28:46 +00001860 if (Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001861 return;
1862
1863 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001864 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001865 llvm::DIType Ty;
1866 if (VD->hasAttr<BlocksAttr>())
1867 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1868 else
1869 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001870
1871 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001872 unsigned Line = getLineNumber(VD->getLocation());
1873 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001874
Devang Pateld6c5a262010-02-01 21:52:22 +00001875 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001876 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001877 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1878 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1879 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1880 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001881 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001882 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1883 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001884 // offset of __forwarding field
1885 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001886 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1887 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1888 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001889 // offset of x field
1890 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001891 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001892 }
1893
1894 // Create the descriptor for the variable.
1895 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001896 DebugFactory.CreateComplexVariable(Tag,
1897 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001898 VD->getName(), Unit, Line, Ty,
Benjamin Kramer3475cfe2010-09-21 15:59:59 +00001899 addr.data(), addr.size());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001900 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001901 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001902 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattnerd5b89022009-12-28 21:44:41 +00001903
Chris Lattnerc6034632010-04-01 06:31:43 +00001904 llvm::MDNode *Scope = RegionStack.back();
Devang Patelf8e10a52010-05-10 23:48:38 +00001905 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001906}
1907
Devang Pateld6c5a262010-02-01 21:52:22 +00001908void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001909 llvm::Value *Storage,
1910 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001911 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001912}
1913
Mike Stumpb1a6e682009-09-30 02:43:10 +00001914void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1915 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1916 CodeGenFunction *CGF) {
1917 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1918}
1919
Chris Lattner9c85ba32008-11-10 06:08:34 +00001920/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1921/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001922void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001923 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001924 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001925}
1926
1927
1928
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001929/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001930void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001931 const VarDecl *D) {
1932
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001933 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001934 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001935 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +00001936
Devang Pateleb6d79b2010-02-01 21:34:11 +00001937 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001938 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001939
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001940 // CodeGen turns int[] into int[1] so we'll do the same here.
1941 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001942
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001943 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001944 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001945
Anders Carlsson20f12a22009-12-06 18:00:51 +00001946 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001947 ArrayType::Normal, 0);
1948 }
Devang Patel5d822f02010-04-29 17:48:37 +00001949 llvm::StringRef DeclName = D->getName();
Devang Patel8b90a782010-05-13 23:52:37 +00001950 llvm::StringRef LinkageName;
Devang Patel0fd3d1f2010-05-14 16:55:25 +00001951 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()))
Devang Patel8b90a782010-05-13 23:52:37 +00001952 LinkageName = Var->getName();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001953 llvm::DIDescriptor DContext =
1954 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
Devang Patel8b90a782010-05-13 23:52:37 +00001955 DebugFactory.CreateGlobalVariable(DContext, DeclName, DeclName, LinkageName,
1956 Unit, LineNo, getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001957 Var->hasInternalLinkage(),
1958 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001959}
1960
Devang Patel9ca36b62009-02-26 21:10:26 +00001961/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001962void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001963 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001964 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001965 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001966 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patel9ca36b62009-02-26 21:10:26 +00001967
Devang Pateld6c5a262010-02-01 21:52:22 +00001968 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001969
Devang Pateld6c5a262010-02-01 21:52:22 +00001970 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001971 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001972
Devang Patel9ca36b62009-02-26 21:10:26 +00001973 // CodeGen turns int[] into int[1] so we'll do the same here.
1974 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001975
Devang Patel9ca36b62009-02-26 21:10:26 +00001976 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001977 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001978
Anders Carlsson20f12a22009-12-06 18:00:51 +00001979 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001980 ArrayType::Normal, 0);
1981 }
1982
Devang Patelf6a39b72009-10-20 18:26:30 +00001983 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001984 getOrCreateType(T, Unit),
1985 Var->hasInternalLinkage(),
1986 true/*definition*/, Var);
1987}
Devang Patelabb485f2010-02-01 19:16:32 +00001988
Devang Patel25c2c8f2010-08-10 17:53:33 +00001989/// EmitGlobalVariable - Emit global variable's debug info.
1990void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
John McCall189d6ef2010-10-09 01:34:31 +00001991 llvm::Constant *Init) {
Devang Patel8d308382010-08-10 07:24:25 +00001992 // Create the descriptor for the variable.
1993 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
1994 llvm::StringRef Name = VD->getName();
Devang Patel0317ab02010-08-10 18:27:15 +00001995 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
Devang Patel6237cea2010-08-23 22:07:25 +00001996 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
1997 if (const EnumDecl *ED = dyn_cast<EnumDecl>(ECD->getDeclContext()))
1998 Ty = CreateEnumType(ED, Unit);
1999 }
Devang Patel0317ab02010-08-10 18:27:15 +00002000 // Do not use DIGlobalVariable for enums.
2001 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
2002 return;
Devang Patel8d308382010-08-10 07:24:25 +00002003 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit,
2004 getLineNumber(VD->getLocation()),
Devang Patel4c4acc02010-08-10 20:16:57 +00002005 Ty, true, true, Init);
Devang Patel8d308382010-08-10 07:24:25 +00002006}
2007
Devang Patelabb485f2010-02-01 19:16:32 +00002008/// getOrCreateNamesSpace - Return namespace descriptor for the given
2009/// namespace decl.
2010llvm::DINameSpace
2011CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
2012 llvm::DIDescriptor Unit) {
2013 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
2014 NameSpaceCache.find(NSDecl);
2015 if (I != NameSpaceCache.end())
2016 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
2017
Devang Patel8ab870d2010-05-12 23:46:38 +00002018 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patelabb485f2010-02-01 19:16:32 +00002019
2020 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00002021 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00002022 llvm::DINameSpace NS =
2023 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
Devang Patel2ed8f002010-08-27 17:47:47 +00002024 llvm::DIFile(Unit), LineNo);
Devang Patelab699792010-05-07 18:12:35 +00002025 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00002026 return NS;
2027}