blob: 2c72996392fe4ba9badd01dfaec59b220f0258a5 [file] [log] [blame]
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the debug information generation while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGDebugInfo.h"
Mike Stumpb1a6e682009-09-30 02:43:10 +000015#include "CodeGenFunction.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000016#include "CodeGenModule.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000017#include "clang/AST/ASTContext.h"
Devang Patel2ed8f002010-08-27 17:47:47 +000018#include "clang/AST/DeclFriend.h"
Devang Patel9ca36b62009-02-26 21:10:26 +000019#include "clang/AST/DeclObjC.h"
Devang Patel700a1cb2010-07-20 20:24:18 +000020#include "clang/AST/DeclTemplate.h"
Chris Lattner3cc5c402008-11-11 07:01:36 +000021#include "clang/AST/Expr.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000022#include "clang/AST/RecordLayout.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000023#include "clang/Basic/SourceManager.h"
24#include "clang/Basic/FileManager.h"
Mike Stump5a862172009-09-15 21:48:34 +000025#include "clang/Basic/Version.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000026#include "clang/Frontend/CodeGenOptions.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000027#include "llvm/Constants.h"
28#include "llvm/DerivedTypes.h"
29#include "llvm/Instructions.h"
30#include "llvm/Intrinsics.h"
31#include "llvm/Module.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000032#include "llvm/ADT/StringExtras.h"
33#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000034#include "llvm/Support/Dwarf.h"
Devang Patel446c6192009-04-17 21:06:59 +000035#include "llvm/System/Path.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000036#include "llvm/Target/TargetMachine.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000037using namespace clang;
38using namespace clang::CodeGen;
39
Anders Carlsson20f12a22009-12-06 18:00:51 +000040CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Devang Patel17800552010-03-09 00:44:50 +000041 : CGM(CGM), DebugFactory(CGM.getModule()),
Dan Gohman4cac5b42010-08-20 22:02:57 +000042 BlockLiteralGenericSet(false) {
Devang Patel17800552010-03-09 00:44:50 +000043 CreateCompileUnit();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000044}
45
Chris Lattner9c85ba32008-11-10 06:08:34 +000046CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar66031a52008-10-17 16:15:48 +000047 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000048}
49
Chris Lattner9c85ba32008-11-10 06:08:34 +000050void CGDebugInfo::setLocation(SourceLocation Loc) {
51 if (Loc.isValid())
Anders Carlsson20f12a22009-12-06 18:00:51 +000052 CurLoc = CGM.getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000053}
54
Devang Patel33583052010-01-28 23:15:27 +000055/// getContextDescriptor - Get context info for the decl.
Devang Pateleb6d79b2010-02-01 21:34:11 +000056llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context,
Devang Patel33583052010-01-28 23:15:27 +000057 llvm::DIDescriptor &CompileUnit) {
Devang Pateleb6d79b2010-02-01 21:34:11 +000058 if (!Context)
59 return CompileUnit;
60
61 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
62 I = RegionMap.find(Context);
63 if (I != RegionMap.end())
Gabor Greif38c9b172010-09-18 13:00:17 +000064 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(&*I->second));
Devang Patel411894b2010-02-01 22:40:08 +000065
Devang Pateleb6d79b2010-02-01 21:34:11 +000066 // Check namespace.
67 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
68 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl, CompileUnit));
Devang Patel8b90a782010-05-13 23:52:37 +000069
70 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context)) {
71 if (!RDecl->isDependentType()) {
72 llvm::DIType Ty = getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
73 llvm::DIFile(CompileUnit));
74 return llvm::DIDescriptor(Ty);
75 }
76 }
Devang Patel979ec2e2009-10-06 00:35:31 +000077 return CompileUnit;
78}
79
Devang Patel9c6c3a02010-01-14 00:36:21 +000080/// getFunctionName - Get function name for the given FunctionDecl. If the
81/// name is constructred on demand (e.g. C++ destructor) then the name
82/// is stored on the side.
83llvm::StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
84 assert (FD && "Invalid FunctionDecl!");
85 IdentifierInfo *FII = FD->getIdentifier();
86 if (FII)
87 return FII->getName();
88
89 // Otherwise construct human readable name for debug info.
90 std::string NS = FD->getNameAsString();
91
92 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +000093 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer1b627dc2010-01-23 18:16:07 +000094 memcpy(StrPtr, NS.data(), NS.length());
95 return llvm::StringRef(StrPtr, NS.length());
Devang Patel9c6c3a02010-01-14 00:36:21 +000096}
97
David Chisnall52044a22010-09-02 18:01:51 +000098llvm::StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
99 llvm::SmallString<256> MethodName;
100 llvm::raw_svector_ostream OS(MethodName);
101 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
102 const DeclContext *DC = OMD->getDeclContext();
103 if (const ObjCImplementationDecl *OID = dyn_cast<const ObjCImplementationDecl>(DC)) {
104 OS << OID->getName();
105 } else if (const ObjCCategoryImplDecl *OCD = dyn_cast<const ObjCCategoryImplDecl>(DC)){
106 OS << ((NamedDecl *)OCD)->getIdentifier()->getNameStart() << '(' <<
107 OCD->getIdentifier()->getNameStart() << ')';
108 }
109 OS << ' ' << OMD->getSelector().getAsString() << ']';
110
111 char *StrPtr = DebugInfoNames.Allocate<char>(OS.tell());
112 memcpy(StrPtr, MethodName.begin(), OS.tell());
113 return llvm::StringRef(StrPtr, OS.tell());
114}
115
Devang Patel700a1cb2010-07-20 20:24:18 +0000116/// getClassName - Get class name including template argument list.
117llvm::StringRef
118CGDebugInfo::getClassName(RecordDecl *RD) {
119 ClassTemplateSpecializationDecl *Spec
120 = dyn_cast<ClassTemplateSpecializationDecl>(RD);
121 if (!Spec)
122 return RD->getName();
123
124 const TemplateArgument *Args;
125 unsigned NumArgs;
126 std::string Buffer;
127 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
128 const TemplateSpecializationType *TST =
129 cast<TemplateSpecializationType>(TAW->getType());
130 Args = TST->getArgs();
131 NumArgs = TST->getNumArgs();
132 } else {
133 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
134 Args = TemplateArgs.getFlatArgumentList();
135 NumArgs = TemplateArgs.flat_size();
136 }
137 Buffer = RD->getIdentifier()->getNameStart();
138 PrintingPolicy Policy(CGM.getLangOptions());
139 Buffer += TemplateSpecializationType::PrintTemplateArgumentList(Args,
140 NumArgs,
141 Policy);
142
143 // Copy this name on the side and use its reference.
144 char *StrPtr = DebugInfoNames.Allocate<char>(Buffer.length());
145 memcpy(StrPtr, Buffer.data(), Buffer.length());
146 return llvm::StringRef(StrPtr, Buffer.length());
147
148}
149
Devang Patel17800552010-03-09 00:44:50 +0000150/// getOrCreateFile - Get the file debug info descriptor for the input location.
151llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Ted Kremenek9c250392010-03-30 00:27:51 +0000152 if (!Loc.isValid())
Devang Patel17800552010-03-09 00:44:50 +0000153 // If Location is not valid then use main input file.
154 return DebugFactory.CreateFile(TheCU.getFilename(), TheCU.getDirectory(),
155 TheCU);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000156 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel17800552010-03-09 00:44:50 +0000157 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Ted Kremenek9c250392010-03-30 00:27:51 +0000158
159 // Cache the results.
160 const char *fname = PLoc.getFilename();
161 llvm::DenseMap<const char *, llvm::WeakVH>::iterator it =
162 DIFileCache.find(fname);
163
164 if (it != DIFileCache.end()) {
165 // Verify that the information still exists.
166 if (&*it->second)
167 return llvm::DIFile(cast<llvm::MDNode>(it->second));
168 }
169
Devang Patelac4d13c2010-07-27 15:17:16 +0000170 llvm::DIFile F = DebugFactory.CreateFile(PLoc.getFilename(),
171 getCurrentDirname(), TheCU);
Ted Kremenek9c250392010-03-30 00:27:51 +0000172
Devang Patelab699792010-05-07 18:12:35 +0000173 DIFileCache[fname] = F;
Ted Kremenek9c250392010-03-30 00:27:51 +0000174 return F;
175
Devang Patel17800552010-03-09 00:44:50 +0000176}
Devang Patel8ab870d2010-05-12 23:46:38 +0000177
178/// getLineNumber - Get line number for the location. If location is invalid
179/// then use current location.
180unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
181 assert (CurLoc.isValid() && "Invalid current location!");
182 SourceManager &SM = CGM.getContext().getSourceManager();
183 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
184 return PLoc.getLine();
185}
186
187/// getColumnNumber - Get column number for the location. If location is
188/// invalid then use current location.
189unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) {
190 assert (CurLoc.isValid() && "Invalid current location!");
191 SourceManager &SM = CGM.getContext().getSourceManager();
192 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
193 return PLoc.getColumn();
194}
195
Devang Patelac4d13c2010-07-27 15:17:16 +0000196llvm::StringRef CGDebugInfo::getCurrentDirname() {
197 if (!CWDName.empty())
198 return CWDName;
199 char *CompDirnamePtr = NULL;
200 llvm::sys::Path CWD = llvm::sys::Path::GetCurrentDirectory();
201 CompDirnamePtr = DebugInfoNames.Allocate<char>(CWD.size());
202 memcpy(CompDirnamePtr, CWD.c_str(), CWD.size());
203 return CWDName = llvm::StringRef(CompDirnamePtr, CWD.size());
204}
205
Devang Patel17800552010-03-09 00:44:50 +0000206/// CreateCompileUnit - Create new compile unit.
207void CGDebugInfo::CreateCompileUnit() {
208
209 // Get absolute path name.
Douglas Gregorac91b4c2010-03-18 23:46:43 +0000210 SourceManager &SM = CGM.getContext().getSourceManager();
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000211 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
212 if (MainFileName.empty())
Devang Patel22fe5852010-03-12 21:04:27 +0000213 MainFileName = "<unknown>";
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000214
Douglas Gregorf6728fc2010-03-22 21:28:29 +0000215 // The main file name provided via the "-main-file-name" option contains just
216 // the file name itself with no path information. This file name may have had
217 // a relative path, so we look into the actual file entry for the main
218 // file to determine the real absolute path for the file.
Devang Patel6e6bc392010-07-23 23:04:28 +0000219 std::string MainFileDir;
Devang Patelac4d13c2010-07-27 15:17:16 +0000220 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000221 MainFileDir = MainFile->getDir()->getName();
Devang Patelac4d13c2010-07-27 15:17:16 +0000222 if (MainFileDir != ".")
223 MainFileName = MainFileDir + "/" + MainFileName;
224 }
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000225
Devang Patelac4d13c2010-07-27 15:17:16 +0000226 // Save filename string.
227 char *FilenamePtr = DebugInfoNames.Allocate<char>(MainFileName.length());
228 memcpy(FilenamePtr, MainFileName.c_str(), MainFileName.length());
229 llvm::StringRef Filename(FilenamePtr, MainFileName.length());
230
Chris Lattner515455a2009-03-25 03:28:08 +0000231 unsigned LangTag;
Devang Patel17800552010-03-09 00:44:50 +0000232 const LangOptions &LO = CGM.getLangOptions();
Chris Lattner515455a2009-03-25 03:28:08 +0000233 if (LO.CPlusPlus) {
234 if (LO.ObjC1)
235 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
236 else
237 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
238 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000239 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000240 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000241 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000242 } else {
243 LangTag = llvm::dwarf::DW_LANG_C89;
244 }
Devang Patel446c6192009-04-17 21:06:59 +0000245
Daniel Dunbar19f19832010-08-24 17:41:09 +0000246 std::string Producer = getClangFullVersion();
Chris Lattner4c2577a2009-05-02 01:00:04 +0000247
248 // Figure out which version of the ObjC runtime we have.
249 unsigned RuntimeVers = 0;
250 if (LO.ObjC1)
251 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000253 // Create new compile unit.
Devang Patel17800552010-03-09 00:44:50 +0000254 TheCU = DebugFactory.CreateCompileUnit(
Devang Patel58115002010-07-27 20:49:59 +0000255 LangTag, Filename, getCurrentDirname(),
Devang Patelac4d13c2010-07-27 15:17:16 +0000256 Producer, true,
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000257 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000258}
259
Devang Patel65e99f22009-02-25 01:36:11 +0000260/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000261/// one if necessary.
262llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel17800552010-03-09 00:44:50 +0000263 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000264 unsigned Encoding = 0;
Devang Patel05127ca2010-07-28 23:23:29 +0000265 const char *BTName = NULL;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000266 switch (BT->getKind()) {
267 default:
268 case BuiltinType::Void:
269 return llvm::DIType();
Devang Patelc8972c62010-07-28 01:33:15 +0000270 case BuiltinType::ObjCClass:
Devang Pateleaf5ee92010-07-21 22:41:25 +0000271 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Patelc8972c62010-07-28 01:33:15 +0000272 Unit, "objc_class", Unit, 0, 0, 0, 0,
Devang Patele2472482010-09-29 21:05:52 +0000273 llvm::DIDescriptor::FlagFwdDecl,
Devang Pateleaf5ee92010-07-21 22:41:25 +0000274 llvm::DIType(), llvm::DIArray());
Devang Patelc8972c62010-07-28 01:33:15 +0000275 case BuiltinType::ObjCId: {
276 // typedef struct objc_class *Class;
277 // typedef struct objc_object {
278 // Class isa;
279 // } *id;
280
281 llvm::DIType OCTy =
282 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
283 Unit, "objc_class", Unit, 0, 0, 0, 0,
Devang Patele2472482010-09-29 21:05:52 +0000284 llvm::DIDescriptor::FlagFwdDecl,
Devang Patelc8972c62010-07-28 01:33:15 +0000285 llvm::DIType(), llvm::DIArray());
286 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
287
288 llvm::DIType ISATy =
289 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
290 Unit, "", Unit,
291 0, Size, 0, 0, 0, OCTy);
292
293 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
294
295 llvm::DIType FieldTy =
296 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
297 "isa", Unit,
298 0,Size, 0, 0, 0, ISATy);
299 EltTys.push_back(FieldTy);
300 llvm::DIArray Elements =
301 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
302
303 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
304 Unit, "objc_object", Unit, 0, 0, 0, 0,
305 0,
306 llvm::DIType(), Elements);
307 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000308 case BuiltinType::UChar:
309 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
310 case BuiltinType::Char_S:
311 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
312 case BuiltinType::UShort:
313 case BuiltinType::UInt:
314 case BuiltinType::ULong:
315 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
316 case BuiltinType::Short:
317 case BuiltinType::Int:
318 case BuiltinType::Long:
319 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
320 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
321 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000322 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000323 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000324 }
Devang Patel05127ca2010-07-28 23:23:29 +0000325
326 switch (BT->getKind()) {
327 case BuiltinType::Long: BTName = "long int"; break;
328 case BuiltinType::LongLong: BTName = "long long int"; break;
329 case BuiltinType::ULong: BTName = "long unsigned int"; break;
330 case BuiltinType::ULongLong: BTName = "long long unsigned int"; break;
331 default:
332 BTName = BT->getName(CGM.getContext().getLangOptions());
333 break;
334 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000335 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000336 uint64_t Size = CGM.getContext().getTypeSize(BT);
337 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000338 uint64_t Offset = 0;
Devang Patel05127ca2010-07-28 23:23:29 +0000339
Devang Patelca80a5f2009-10-20 19:55:01 +0000340 llvm::DIType DbgTy =
Devang Patel05127ca2010-07-28 23:23:29 +0000341 DebugFactory.CreateBasicType(Unit, BTName,
Devang Patelca80a5f2009-10-20 19:55:01 +0000342 Unit, 0, Size, Align,
343 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000344 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000345}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000346
Chris Lattnerb7003772009-04-23 06:13:01 +0000347llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000348 llvm::DIFile Unit) {
Chris Lattnerb7003772009-04-23 06:13:01 +0000349 // Bit size, align and offset of the type.
350 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
351 if (Ty->isComplexIntegerType())
352 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Anders Carlsson20f12a22009-12-06 18:00:51 +0000354 uint64_t Size = CGM.getContext().getTypeSize(Ty);
355 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattnerb7003772009-04-23 06:13:01 +0000356 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Devang Patelca80a5f2009-10-20 19:55:01 +0000358 llvm::DIType DbgTy =
359 DebugFactory.CreateBasicType(Unit, "complex",
360 Unit, 0, Size, Align,
361 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000362 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000363}
364
John McCalla1805292009-09-25 01:40:47 +0000365/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000366/// a new one if necessary.
Devang Patel17800552010-03-09 00:44:50 +0000367llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +0000368 QualifierCollector Qc;
369 const Type *T = Qc.strip(Ty);
370
371 // Ignore these qualifiers for now.
372 Qc.removeObjCGCAttr();
373 Qc.removeAddressSpace();
374
Chris Lattner9c85ba32008-11-10 06:08:34 +0000375 // We will create one Derived type for one qualifier and recurse to handle any
376 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000377 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000378 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000379 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000380 Qc.removeConst();
381 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000382 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000383 Qc.removeVolatile();
384 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000385 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000386 Qc.removeRestrict();
387 } else {
388 assert(Qc.empty() && "Unknown type qualifier for debug info");
389 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000390 }
Mike Stump1eb44332009-09-09 15:08:12 +0000391
John McCalla1805292009-09-25 01:40:47 +0000392 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
393
Daniel Dunbar3845f862008-10-31 03:54:29 +0000394 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
395 // CVR derived types.
Devang Patelca80a5f2009-10-20 19:55:01 +0000396 llvm::DIType DbgTy =
Devang Pateld58562e2010-03-09 22:49:11 +0000397 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000398 0, 0, 0, 0, 0, FromTy);
Devang Patelca80a5f2009-10-20 19:55:01 +0000399 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000400}
401
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000402llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000403 llvm::DIFile Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000404 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000405 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
406 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000407 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000408}
409
Chris Lattner9c85ba32008-11-10 06:08:34 +0000410llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000411 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000412 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
413 Ty->getPointeeType(), Unit);
414}
415
Devang Patelc69e1cf2010-09-30 19:05:55 +0000416/// CreatePointeeType - Create PointTee type. If Pointee is a record
417/// then emit record's fwd if debug info size reduction is enabled.
418llvm::DIType CGDebugInfo::CreatePointeeType(QualType PointeeTy,
419 llvm::DIFile Unit) {
420 if (!CGM.getCodeGenOpts().LimitDebugInfo)
421 return getOrCreateType(PointeeTy, Unit);
422
423 if (const RecordType *RTy = dyn_cast<RecordType>(PointeeTy)) {
424 RecordDecl *RD = RTy->getDecl();
425 unsigned RTag;
426 if (RD->isStruct())
427 RTag = llvm::dwarf::DW_TAG_structure_type;
428 else if (RD->isUnion())
429 RTag = llvm::dwarf::DW_TAG_union_type;
430 else {
431 assert(RD->isClass() && "Unknown RecordType!");
432 RTag = llvm::dwarf::DW_TAG_class_type;
433 }
434
435 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
436 unsigned Line = getLineNumber(RD->getLocation());
437 llvm::DIDescriptor FDContext =
438 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
439
440 return
441 DebugFactory.CreateCompositeType(RTag, FDContext, RD->getName(),
442 DefUnit, Line, 0, 0, 0,
443 llvm::DIType::FlagFwdDecl,
444 llvm::DIType(), llvm::DIArray());
445 }
446 return getOrCreateType(PointeeTy, Unit);
447
448}
449
Anders Carlssona031b352009-11-06 19:19:55 +0000450llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
451 const Type *Ty,
452 QualType PointeeTy,
Devang Patel17800552010-03-09 00:44:50 +0000453 llvm::DIFile Unit) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000454 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000455
456 // Size is always the size of a pointer. We can't use getTypeSize here
457 // because that does not return the correct value for references.
458 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000459 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
460 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Devang Patelc69e1cf2010-09-30 19:05:55 +0000462 return DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
463 0, Size, Align, 0, 0,
464 CreatePointeeType(PointeeTy, Unit));
465
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000466}
467
Mike Stump9bc093c2009-05-14 02:03:51 +0000468llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000469 llvm::DIFile Unit) {
Mike Stump9bc093c2009-05-14 02:03:51 +0000470 if (BlockLiteralGenericSet)
471 return BlockLiteralGeneric;
472
Mike Stump9bc093c2009-05-14 02:03:51 +0000473 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
474
475 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
476
477 llvm::DIType FieldTy;
478
479 QualType FType;
480 uint64_t FieldSize, FieldOffset;
481 unsigned FieldAlign;
482
483 llvm::DIArray Elements;
484 llvm::DIType EltTy, DescTy;
485
486 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000487 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000488 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
489 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000490
Daniel Dunbarca308df2009-05-26 19:40:20 +0000491 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000492 EltTys.clear();
493
Devang Patele2472482010-09-29 21:05:52 +0000494 unsigned Flags = llvm::DIDescriptor::FlagAppleBlock;
Devang Patel8ab870d2010-05-12 23:46:38 +0000495 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump3d363c52009-10-02 02:30:50 +0000496
Mike Stump9bc093c2009-05-14 02:03:51 +0000497 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Devang Patel8ab870d2010-05-12 23:46:38 +0000498 Unit, LineNo, FieldOffset, 0, 0,
499 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Mike Stump9bc093c2009-05-14 02:03:51 +0000501 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000502 uint64_t Size = CGM.getContext().getTypeSize(Ty);
503 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Mike Stump9bc093c2009-05-14 02:03:51 +0000505 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000506 Unit, "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000507 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000508
509 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000510 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000511 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson20f12a22009-12-06 18:00:51 +0000512 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000513 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
514 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramerd3651cc2010-04-24 20:26:20 +0000515 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000516 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000517
Anders Carlsson20f12a22009-12-06 18:00:51 +0000518 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000519 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000520 FieldSize = CGM.getContext().getTypeSize(Ty);
521 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000522 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000523 "__descriptor", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000524 LineNo, FieldSize, FieldAlign,
Mike Stump9bc093c2009-05-14 02:03:51 +0000525 FieldOffset, 0, FieldTy);
526 EltTys.push_back(FieldTy);
527
528 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000529 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000530
531 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Devang Patel8ab870d2010-05-12 23:46:38 +0000532 Unit, LineNo, FieldOffset, 0, 0,
533 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Mike Stump9bc093c2009-05-14 02:03:51 +0000535 BlockLiteralGenericSet = true;
536 BlockLiteralGeneric
537 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000538 "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000539 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000540 return BlockLiteralGeneric;
541}
542
Chris Lattner9c85ba32008-11-10 06:08:34 +0000543llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000544 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000545 // Typedefs are derived from some other type. If we have a typedef of a
546 // typedef, make sure to emit the whole chain.
547 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Chris Lattner9c85ba32008-11-10 06:08:34 +0000549 // We don't set size information, but do specify where the typedef was
550 // declared.
Devang Patel8ab870d2010-05-12 23:46:38 +0000551 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000552
Devang Pateleb6d79b2010-02-01 21:34:11 +0000553 llvm::DIDescriptor TyContext
554 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
555 Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000556 llvm::DIType DbgTy =
Devang Pateld5289052010-01-29 22:29:31 +0000557 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Pateleb6d79b2010-02-01 21:34:11 +0000558 TyContext,
Devang Pateld5289052010-01-29 22:29:31 +0000559 Ty->getDecl()->getName(), Unit,
560 Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000561 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000562}
563
Chris Lattner9c85ba32008-11-10 06:08:34 +0000564llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000565 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000566 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000567
Chris Lattner9c85ba32008-11-10 06:08:34 +0000568 // Add the result type at least.
569 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Chris Lattner9c85ba32008-11-10 06:08:34 +0000571 // Set up remainder of arguments if there is a prototype.
572 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000573 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000574 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
575 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
576 } else {
577 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000578 }
579
Chris Lattner9c85ba32008-11-10 06:08:34 +0000580 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000581 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000582
Devang Patelca80a5f2009-10-20 19:55:01 +0000583 llvm::DIType DbgTy =
584 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000585 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000586 0, 0, 0, 0, 0,
587 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000588 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000589}
590
Devang Patel428deb52010-01-19 00:00:59 +0000591/// CollectRecordFields - A helper function to collect debug info for
592/// record fields. This is used while creating debug info entry for a Record.
593void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000594CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit,
Devang Patel428deb52010-01-19 00:00:59 +0000595 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
596 unsigned FieldNo = 0;
Devang Patel239cec62010-02-01 21:39:52 +0000597 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
598 for (RecordDecl::field_iterator I = RD->field_begin(),
599 E = RD->field_end();
Devang Patel428deb52010-01-19 00:00:59 +0000600 I != E; ++I, ++FieldNo) {
601 FieldDecl *Field = *I;
602 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Devang Patel428deb52010-01-19 00:00:59 +0000603 llvm::StringRef FieldName = Field->getName();
604
Devang Patel4835fdd2010-02-12 01:31:06 +0000605 // Ignore unnamed fields. Do not ignore unnamed records.
606 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel428deb52010-01-19 00:00:59 +0000607 continue;
608
609 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +0000610 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
611 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel428deb52010-01-19 00:00:59 +0000612 QualType FType = Field->getType();
613 uint64_t FieldSize = 0;
614 unsigned FieldAlign = 0;
615 if (!FType->isIncompleteArrayType()) {
616
617 // Bit size, align and offset of the type.
618 FieldSize = CGM.getContext().getTypeSize(FType);
619 Expr *BitWidth = Field->getBitWidth();
620 if (BitWidth)
621 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Devang Patel428deb52010-01-19 00:00:59 +0000622 FieldAlign = CGM.getContext().getTypeAlign(FType);
623 }
624
625 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
626
Devang Patel71f4ff62010-04-21 23:12:37 +0000627 unsigned Flags = 0;
628 AccessSpecifier Access = I->getAccess();
629 if (Access == clang::AS_private)
Devang Patele2472482010-09-29 21:05:52 +0000630 Flags |= llvm::DIDescriptor::FlagPrivate;
Devang Patel71f4ff62010-04-21 23:12:37 +0000631 else if (Access == clang::AS_protected)
Devang Patele2472482010-09-29 21:05:52 +0000632 Flags |= llvm::DIDescriptor::FlagProtected;
Devang Patel71f4ff62010-04-21 23:12:37 +0000633
Devang Patel428deb52010-01-19 00:00:59 +0000634 // Create a DW_TAG_member node to remember the offset of this field in the
635 // struct. FIXME: This is an absolutely insane way to capture this
636 // information. When we gut debug info, this should be fixed.
637 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
638 FieldName, FieldDefUnit,
639 FieldLine, FieldSize, FieldAlign,
Devang Patel71f4ff62010-04-21 23:12:37 +0000640 FieldOffset, Flags, FieldTy);
Devang Patel428deb52010-01-19 00:00:59 +0000641 EltTys.push_back(FieldTy);
642 }
643}
644
Devang Patela6da1922010-01-28 00:28:01 +0000645/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
646/// function type is not updated to include implicit "this" pointer. Use this
647/// routine to get a method type which includes "this" pointer.
648llvm::DIType
649CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000650 llvm::DIFile Unit) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +0000651 llvm::DIType FnTy
652 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
653 0),
654 Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000655
Devang Patela6da1922010-01-28 00:28:01 +0000656 // Add "this" pointer.
657
Devang Patelab699792010-05-07 18:12:35 +0000658 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patela6da1922010-01-28 00:28:01 +0000659 assert (Args.getNumElements() && "Invalid number of arguments!");
660
661 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
662
663 // First element is always return type. For 'void' functions it is NULL.
664 Elts.push_back(Args.getElement(0));
665
Devang Patel2ed8f002010-08-27 17:47:47 +0000666 if (!Method->isStatic())
667 {
668 // "this" pointer is always first argument.
669 ASTContext &Context = CGM.getContext();
670 QualType ThisPtr =
671 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
672 llvm::DIType ThisPtrType =
673 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
Devang Patel769640e2010-07-13 00:24:30 +0000674
Devang Patel2ed8f002010-08-27 17:47:47 +0000675 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
676 Elts.push_back(ThisPtrType);
677 }
Devang Patela6da1922010-01-28 00:28:01 +0000678
679 // Copy rest of the arguments.
680 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
681 Elts.push_back(Args.getElement(i));
682
683 llvm::DIArray EltTypeArray =
684 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
685
686 return
687 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000688 Unit, "", Unit,
Devang Patela6da1922010-01-28 00:28:01 +0000689 0, 0, 0, 0, 0,
690 llvm::DIType(), EltTypeArray);
691}
692
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000693/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
694/// a single member function GlobalDecl.
695llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000696CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000697 llvm::DIFile Unit,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000698 llvm::DIType RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000699 bool IsCtorOrDtor =
700 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
701
702 llvm::StringRef MethodName = getFunctionName(Method);
Devang Patela6da1922010-01-28 00:28:01 +0000703 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000704
705 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
706 // make sense to give a single ctor/dtor a linkage name.
Anders Carlsson9a20d552010-06-22 16:16:50 +0000707 llvm::StringRef MethodLinkageName;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000708 if (!IsCtorOrDtor)
Anders Carlsson9a20d552010-06-22 16:16:50 +0000709 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000710
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000711 // Get the location for the method.
Devang Patel8ab870d2010-05-12 23:46:38 +0000712 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
713 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000714
715 // Collect virtual method info.
716 llvm::DIType ContainingType;
717 unsigned Virtuality = 0;
718 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000719
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000720 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000721 if (Method->isPure())
722 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
723 else
724 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
725
726 // It doesn't make sense to give a virtual destructor a vtable index,
727 // since a single destructor has two entries in the vtable.
728 if (!isa<CXXDestructorDecl>(Method))
Anders Carlsson046c2942010-04-17 20:15:18 +0000729 VIndex = CGM.getVTables().getMethodVTableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000730 ContainingType = RecordTy;
731 }
732
Devang Patele2472482010-09-29 21:05:52 +0000733 unsigned Flags = 0;
734 if (Method->isImplicit())
735 Flags |= llvm::DIDescriptor::FlagArtificial;
Devang Patel10a7a6a2010-09-29 21:46:16 +0000736 AccessSpecifier Access = Method->getAccess();
737 if (Access == clang::AS_private)
738 Flags |= llvm::DIDescriptor::FlagPrivate;
739 else if (Access == clang::AS_protected)
740 Flags |= llvm::DIDescriptor::FlagProtected;
Devang Pateld78a0192010-10-01 23:32:17 +0000741 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
742 if (CXXC->isExplicit())
743 Flags |= llvm::DIDescriptor::FlagExplicit;
744 } else if (const CXXConversionDecl *CXXC =
745 dyn_cast<CXXConversionDecl>(Method)) {
746 if (CXXC->isExplicit())
747 Flags |= llvm::DIDescriptor::FlagExplicit;
748 }
749
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000750 llvm::DISubprogram SP =
751 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
752 MethodLinkageName,
753 MethodDefUnit, MethodLine,
754 MethodTy, /*isLocalToUnit=*/false,
Devang Patela5c5bab2010-07-12 22:54:41 +0000755 /* isDefintion=*/ false,
Devang Patel40894912010-07-15 22:57:00 +0000756 Virtuality, VIndex, ContainingType,
Devang Patele2472482010-09-29 21:05:52 +0000757 Flags,
Devang Patel15a3d7d2010-07-15 23:09:46 +0000758 CGM.getLangOptions().Optimize);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000759
760 // Don't cache ctors or dtors since we have to emit multiple functions for
761 // a single ctor or dtor.
762 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
Devang Patelab699792010-05-07 18:12:35 +0000763 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000764
765 return SP;
766}
767
Devang Patel4125fd22010-01-19 01:54:44 +0000768/// CollectCXXMemberFunctions - A helper function to collect debug info for
769/// C++ member functions.This is used while creating debug info entry for
770/// a Record.
771void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000772CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000773 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000774 llvm::DIType RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000775 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
776 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000777 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000778
Devang Pateld5322da2010-02-09 19:09:28 +0000779 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000780 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000781
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000782 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000783 }
784}
785
Devang Patel2ed8f002010-08-27 17:47:47 +0000786/// CollectCXXFriends - A helper function to collect debug info for
787/// C++ base classes. This is used while creating debug info entry for
788/// a Record.
789void CGDebugInfo::
790CollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit,
791 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
792 llvm::DIType RecordTy) {
793
794 for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(),
795 BE = RD->friend_end(); BI != BE; ++BI) {
796
797 TypeSourceInfo *TInfo = (*BI)->getFriendType();
798 if(TInfo)
799 {
800 llvm::DIType Ty = getOrCreateType(TInfo->getType(), Unit);
801
802 llvm::DIType DTy =
803 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_friend,
804 RecordTy, llvm::StringRef(),
805 Unit, 0, 0, 0,
806 0, 0, Ty);
807
808 EltTys.push_back(DTy);
809 }
810
811 }
812}
813
Devang Patela245c5b2010-01-25 23:32:18 +0000814/// CollectCXXBases - A helper function to collect debug info for
815/// C++ base classes. This is used while creating debug info entry for
816/// a Record.
817void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000818CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000819 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000820 llvm::DIType RecordTy) {
Devang Patela245c5b2010-01-25 23:32:18 +0000821
Devang Patel239cec62010-02-01 21:39:52 +0000822 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
823 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
824 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000825 unsigned BFlags = 0;
826 uint64_t BaseOffset;
827
828 const CXXRecordDecl *Base =
829 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
830
831 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000832 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000833 // expression where it expects +ve number.
Anders Carlssonaf440352010-03-23 04:11:45 +0000834 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base);
Devang Patele2472482010-09-29 21:05:52 +0000835 BFlags = llvm::DIDescriptor::FlagVirtual;
Devang Patelca7daed2010-01-28 21:54:15 +0000836 } else
837 BaseOffset = RL.getBaseClassOffset(Base);
838
839 AccessSpecifier Access = BI->getAccessSpecifier();
840 if (Access == clang::AS_private)
Devang Patele2472482010-09-29 21:05:52 +0000841 BFlags |= llvm::DIDescriptor::FlagPrivate;
Devang Patelca7daed2010-01-28 21:54:15 +0000842 else if (Access == clang::AS_protected)
Devang Patele2472482010-09-29 21:05:52 +0000843 BFlags |= llvm::DIDescriptor::FlagProtected;
Devang Patelca7daed2010-01-28 21:54:15 +0000844
845 llvm::DIType DTy =
846 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
847 RecordTy, llvm::StringRef(),
Devang Pateld58562e2010-03-09 22:49:11 +0000848 Unit, 0, 0, 0,
Devang Patelca7daed2010-01-28 21:54:15 +0000849 BaseOffset, BFlags,
850 getOrCreateType(BI->getType(),
851 Unit));
852 EltTys.push_back(DTy);
853 }
Devang Patela245c5b2010-01-25 23:32:18 +0000854}
855
Devang Patel4ce3f202010-01-28 18:11:52 +0000856/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000857llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000858 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000859 return VTablePtrType;
860
861 ASTContext &Context = CGM.getContext();
862
863 /* Function type */
Benjamin Kramerad468862010-03-30 11:36:44 +0000864 llvm::DIDescriptor STy = getOrCreateType(Context.IntTy, Unit);
865 llvm::DIArray SElements = DebugFactory.GetOrCreateArray(&STy, 1);
Devang Patel4ce3f202010-01-28 18:11:52 +0000866 llvm::DIType SubTy =
867 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000868 Unit, "", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000869 0, 0, 0, 0, 0, llvm::DIType(), SElements);
870
871 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
872 llvm::DIType vtbl_ptr_type
873 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000874 Unit, "__vtbl_ptr_type", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000875 0, Size, 0, 0, 0, SubTy);
876
Devang Pateld58562e2010-03-09 22:49:11 +0000877 VTablePtrType =
878 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
879 Unit, "", Unit,
880 0, Size, 0, 0, 0, vtbl_ptr_type);
Devang Patel4ce3f202010-01-28 18:11:52 +0000881 return VTablePtrType;
882}
883
Anders Carlsson046c2942010-04-17 20:15:18 +0000884/// getVTableName - Get vtable name for the given Class.
885llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000886 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000887 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000888
889 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000890 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000891 memcpy(StrPtr, Name.data(), Name.length());
892 return llvm::StringRef(StrPtr, Name.length());
893}
894
895
Anders Carlsson046c2942010-04-17 20:15:18 +0000896/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel4ce3f202010-01-28 18:11:52 +0000897/// debug info entry in EltTys vector.
898void CGDebugInfo::
Anders Carlsson046c2942010-04-17 20:15:18 +0000899CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000900 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000901 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000902
903 // If there is a primary base then it will hold vtable info.
904 if (RL.getPrimaryBase())
905 return;
906
907 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000908 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000909 return;
910
911 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
912 llvm::DIType VPTR
913 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Anders Carlsson046c2942010-04-17 20:15:18 +0000914 getVTableName(RD), Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000915 0, Size, 0, 0, 0,
916 getOrCreateVTablePtrType(Unit));
917 EltTys.push_back(VPTR);
918}
919
Devang Patelc69e1cf2010-09-30 19:05:55 +0000920/// getOrCreateRecordType - Emit record type's standalone debug info.
921llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
922 SourceLocation Loc) {
923 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
924 DebugFactory.RecordType(T);
925 return T;
926}
927
Devang Patel65e99f22009-02-25 01:36:11 +0000928/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000929llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000930 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000931 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000932
Chris Lattner9c85ba32008-11-10 06:08:34 +0000933 unsigned Tag;
Devang Pateld6c5a262010-02-01 21:52:22 +0000934 if (RD->isStruct())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000935 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Pateld6c5a262010-02-01 21:52:22 +0000936 else if (RD->isUnion())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000937 Tag = llvm::dwarf::DW_TAG_union_type;
938 else {
Devang Pateld6c5a262010-02-01 21:52:22 +0000939 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000940 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000941 }
942
Chris Lattner9c85ba32008-11-10 06:08:34 +0000943 // Get overall information about the record type for the debug info.
Devang Patel8ab870d2010-05-12 23:46:38 +0000944 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
945 unsigned Line = getLineNumber(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000946
Chris Lattner9c85ba32008-11-10 06:08:34 +0000947 // Records and classes and unions can all be recursive. To handle them, we
948 // first generate a debug descriptor for the struct as a forward declaration.
949 // Then (if it is a definition) we go through and get debug info for all of
950 // its members. Finally, we create a descriptor for the complete type (which
951 // may refer to the forward decl if the struct is recursive) and replace all
952 // uses of the forward declaration with the final definition.
Devang Patel0b897992010-07-08 19:56:29 +0000953 llvm::DIDescriptor FDContext =
954 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
955
956 // If this is just a forward declaration, construct an appropriately
957 // marked node and just return it.
958 if (!RD->getDefinition()) {
959 llvm::DICompositeType FwdDecl =
960 DebugFactory.CreateCompositeType(Tag, FDContext, RD->getName(),
961 DefUnit, Line, 0, 0, 0,
Devang Patele2472482010-09-29 21:05:52 +0000962 llvm::DIDescriptor::FlagFwdDecl,
Devang Patel0b897992010-07-08 19:56:29 +0000963 llvm::DIType(), llvm::DIArray());
964
965 return FwdDecl;
966 }
Devang Pateld0f251b2010-01-20 23:56:40 +0000967
Dan Gohman86968372010-08-20 22:39:57 +0000968 llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType();
Mike Stump1eb44332009-09-09 15:08:12 +0000969
Devang Patelab699792010-05-07 18:12:35 +0000970 llvm::MDNode *MN = FwdDecl;
971 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000972 // Otherwise, insert it into the TypeCache so that recursive uses will find
973 // it.
Devang Patelab699792010-05-07 18:12:35 +0000974 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000975 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000976 RegionStack.push_back(FwdDeclNode);
977 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000978
979 // Convert all the elements.
980 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
981
Devang Pateld6c5a262010-02-01 21:52:22 +0000982 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000983 if (CXXDecl) {
984 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson046c2942010-04-17 20:15:18 +0000985 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000986 }
Devang Pateldabc3e92010-08-12 00:02:44 +0000987
988 // Collect static variables with initializers.
989 for (RecordDecl::decl_iterator I = RD->decls_begin(), E = RD->decls_end();
990 I != E; ++I)
991 if (const VarDecl *V = dyn_cast<VarDecl>(*I)) {
992 if (const Expr *Init = V->getInit()) {
993 Expr::EvalResult Result;
994 if (Init->Evaluate(Result, CGM.getContext()) && Result.Val.isInt()) {
995 llvm::ConstantInt *CI
996 = llvm::ConstantInt::get(CGM.getLLVMContext(), Result.Val.getInt());
997
998 // Create the descriptor for static variable.
999 llvm::DIFile VUnit = getOrCreateFile(V->getLocation());
1000 llvm::StringRef VName = V->getName();
1001 llvm::DIType VTy = getOrCreateType(V->getType(), VUnit);
1002 // Do not use DIGlobalVariable for enums.
1003 if (VTy.getTag() != llvm::dwarf::DW_TAG_enumeration_type) {
1004 DebugFactory.CreateGlobalVariable(FwdDecl, VName, VName, VName, VUnit,
1005 getLineNumber(V->getLocation()),
1006 VTy, true, true, CI);
1007 }
1008 }
1009 }
1010 }
1011
Devang Pateld6c5a262010-02-01 21:52:22 +00001012 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +00001013 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +00001014 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +00001015 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel2ed8f002010-08-27 17:47:47 +00001016 CollectCXXFriends(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +00001017
1018 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +00001019 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel0ac8f312010-01-28 00:54:21 +00001020 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
1021 ContainingType =
Devang Patelab699792010-05-07 18:12:35 +00001022 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patel0ac8f312010-01-28 00:54:21 +00001023 else if (CXXDecl->isDynamicClass())
Devang Patelab699792010-05-07 18:12:35 +00001024 ContainingType = FwdDecl;
Devang Patela245c5b2010-01-25 23:32:18 +00001025 }
Mike Stump1eb44332009-09-09 15:08:12 +00001026
Chris Lattner9c85ba32008-11-10 06:08:34 +00001027 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001028 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001029
1030 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001031 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1032 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001033
Devang Patele4c1ea02010-03-11 20:01:48 +00001034 RegionStack.pop_back();
1035 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1036 RegionMap.find(Ty->getDecl());
1037 if (RI != RegionMap.end())
1038 RegionMap.erase(RI);
1039
Devang Patel411894b2010-02-01 22:40:08 +00001040 llvm::DIDescriptor RDContext =
1041 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel700a1cb2010-07-20 20:24:18 +00001042
1043 llvm::StringRef RDName = RD->getName();
1044 // If this is a class, include the template arguments also.
1045 if (Tag == llvm::dwarf::DW_TAG_class_type)
1046 RDName = getClassName(RD);
1047
Devang Patel0ce73f62009-07-22 18:57:00 +00001048 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +00001049 DebugFactory.CreateCompositeType(Tag, RDContext,
Devang Patel700a1cb2010-07-20 20:24:18 +00001050 RDName,
Devang Patelab71ff52009-11-12 00:51:46 +00001051 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +00001052 llvm::DIType(), Elements,
1053 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001054
1055 // Now that we have a real decl for the struct, replace anything using the
1056 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001057 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001058 RegionMap[RD] = llvm::WeakVH(RealDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001059 return RealDecl;
1060}
1061
John McCallc12c5bb2010-05-15 11:32:37 +00001062/// CreateType - get objective-c object type.
1063llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1064 llvm::DIFile Unit) {
1065 // Ignore protocols.
1066 return getOrCreateType(Ty->getBaseType(), Unit);
1067}
1068
Devang Patel9ca36b62009-02-26 21:10:26 +00001069/// CreateType - get objective-c interface type.
1070llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001071 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001072 ObjCInterfaceDecl *ID = Ty->getDecl();
Devang Patel9ca36b62009-02-26 21:10:26 +00001073 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Patel9ca36b62009-02-26 21:10:26 +00001074
1075 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +00001076 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001077 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel17800552010-03-09 00:44:50 +00001078 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +00001079
Dan Gohman45f7c782010-08-23 21:15:56 +00001080 // If this is just a forward declaration, return a special forward-declaration
1081 // debug type.
1082 if (ID->isForwardDecl()) {
1083 llvm::DICompositeType FwdDecl =
1084 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
1085 DefUnit, Line, 0, 0, 0, 0,
1086 llvm::DIType(), llvm::DIArray(),
1087 RuntimeLang);
1088 return FwdDecl;
1089 }
1090
Devang Patel9ca36b62009-02-26 21:10:26 +00001091 // To handle recursive interface, we
1092 // first generate a debug descriptor for the struct as a forward declaration.
1093 // Then (if it is a definition) we go through and get debug info for all of
1094 // its members. Finally, we create a descriptor for the complete type (which
1095 // may refer to the forward decl if the struct is recursive) and replace all
1096 // uses of the forward declaration with the final definition.
Dan Gohman86968372010-08-20 22:39:57 +00001097 llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType();
Mike Stump1eb44332009-09-09 15:08:12 +00001098
Devang Patelab699792010-05-07 18:12:35 +00001099 llvm::MDNode *MN = FwdDecl;
1100 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patel9ca36b62009-02-26 21:10:26 +00001101 // Otherwise, insert it into the TypeCache so that recursive uses will find
1102 // it.
Devang Patelab699792010-05-07 18:12:35 +00001103 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +00001104 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001105 RegionStack.push_back(FwdDeclNode);
1106 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001107
1108 // Convert all the elements.
1109 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
1110
Devang Pateld6c5a262010-02-01 21:52:22 +00001111 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +00001112 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +00001113 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001114 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001115 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +00001116 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Pateld58562e2010-03-09 22:49:11 +00001117 Unit, "", Unit, 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +00001118 0 /* offset */, 0, SClassTy);
1119 EltTys.push_back(InhTag);
1120 }
1121
Devang Pateld6c5a262010-02-01 21:52:22 +00001122 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001123
1124 unsigned FieldNo = 0;
Fariborz Jahanian97477392010-10-01 00:01:53 +00001125 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1126 Field = Field->getNextIvar()) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001127 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1128
Devang Patel73621622009-11-25 17:37:31 +00001129 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001130
Devang Patelde135022009-04-27 22:40:36 +00001131 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +00001132 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +00001133 continue;
1134
Devang Patel9ca36b62009-02-26 21:10:26 +00001135 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +00001136 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1137 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel99c20eb2009-03-20 18:24:39 +00001138 QualType FType = Field->getType();
1139 uint64_t FieldSize = 0;
1140 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +00001141
Devang Patel99c20eb2009-03-20 18:24:39 +00001142 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001143
Devang Patel99c20eb2009-03-20 18:24:39 +00001144 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001145 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001146 Expr *BitWidth = Field->getBitWidth();
1147 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +00001148 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +00001149
Anders Carlsson20f12a22009-12-06 18:00:51 +00001150 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001151 }
1152
Mike Stump1eb44332009-09-09 15:08:12 +00001153 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
1154
Devang Patelc20482b2009-03-19 00:23:53 +00001155 unsigned Flags = 0;
1156 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Devang Patele2472482010-09-29 21:05:52 +00001157 Flags = llvm::DIDescriptor::FlagProtected;
Devang Patelc20482b2009-03-19 00:23:53 +00001158 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Devang Patele2472482010-09-29 21:05:52 +00001159 Flags = llvm::DIDescriptor::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +00001160
Devang Patel9ca36b62009-02-26 21:10:26 +00001161 // Create a DW_TAG_member node to remember the offset of this field in the
1162 // struct. FIXME: This is an absolutely insane way to capture this
1163 // information. When we gut debug info, this should be fixed.
1164 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1165 FieldName, FieldDefUnit,
1166 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +00001167 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +00001168 EltTys.push_back(FieldTy);
1169 }
Mike Stump1eb44332009-09-09 15:08:12 +00001170
Devang Patel9ca36b62009-02-26 21:10:26 +00001171 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +00001172 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +00001173
Devang Patele4c1ea02010-03-11 20:01:48 +00001174 RegionStack.pop_back();
1175 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1176 RegionMap.find(Ty->getDecl());
1177 if (RI != RegionMap.end())
1178 RegionMap.erase(RI);
1179
Devang Patel9ca36b62009-02-26 21:10:26 +00001180 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001181 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1182 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Devang Patel6c1fddf2009-07-27 18:42:03 +00001184 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +00001185 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +00001186 Line, Size, Align, 0, 0, llvm::DIType(),
1187 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +00001188
1189 // Now that we have a real decl for the struct, replace anything using the
1190 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001191 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001192 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +00001193
Devang Patel9ca36b62009-02-26 21:10:26 +00001194 return RealDecl;
1195}
1196
Chris Lattner9c85ba32008-11-10 06:08:34 +00001197llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001198 llvm::DIFile Unit) {
Devang Patel6237cea2010-08-23 22:07:25 +00001199 return CreateEnumType(Ty->getDecl(), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001200
Chris Lattner9c85ba32008-11-10 06:08:34 +00001201}
1202
1203llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001204 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001205 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1206 return CreateType(RT, Unit);
1207 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1208 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001209
Chris Lattner9c85ba32008-11-10 06:08:34 +00001210 return llvm::DIType();
1211}
1212
Devang Patel70c23cd2010-02-23 22:59:39 +00001213llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Eli Friedmana7e68452010-08-22 01:00:03 +00001214 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001215 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1216 uint64_t NumElems = Ty->getNumElements();
1217 if (NumElems > 0)
1218 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001219
Benjamin Kramerad468862010-03-30 11:36:44 +00001220 llvm::DIDescriptor Subscript = DebugFactory.GetOrCreateSubrange(0, NumElems);
1221 llvm::DIArray SubscriptArray = DebugFactory.GetOrCreateArray(&Subscript, 1);
Devang Patel70c23cd2010-02-23 22:59:39 +00001222
1223 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1224 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1225
1226 return
1227 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001228 Unit, "", Unit,
Devang Patel70c23cd2010-02-23 22:59:39 +00001229 0, Size, Align, 0, 0,
Eli Friedmana7e68452010-08-22 01:00:03 +00001230 ElementTy, SubscriptArray);
Devang Patel70c23cd2010-02-23 22:59:39 +00001231}
1232
Chris Lattner9c85ba32008-11-10 06:08:34 +00001233llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001234 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001235 uint64_t Size;
1236 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001237
1238
Nuno Lopes010d5142009-01-28 00:35:17 +00001239 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001240 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001241 Size = 0;
1242 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001243 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001244 } else if (Ty->isIncompleteArrayType()) {
1245 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001246 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001247 } else {
1248 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001249 Size = CGM.getContext().getTypeSize(Ty);
1250 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001251 }
Mike Stump1eb44332009-09-09 15:08:12 +00001252
Chris Lattner9c85ba32008-11-10 06:08:34 +00001253 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1254 // interior arrays, do we care? Why aren't nested arrays represented the
1255 // obvious/recursive way?
1256 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1257 QualType EltTy(Ty, 0);
Devang Patelcdf523c2010-10-06 18:30:00 +00001258 if (Ty->isIncompleteArrayType())
Chris Lattner9c85ba32008-11-10 06:08:34 +00001259 EltTy = Ty->getElementType();
Devang Patelcdf523c2010-10-06 18:30:00 +00001260 else {
1261 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1262 uint64_t Upper = 0;
1263 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1264 if (CAT->getSize().getZExtValue())
1265 Upper = CAT->getSize().getZExtValue() - 1;
1266 // FIXME: Verify this is right for VLAs.
1267 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1268 EltTy = Ty->getElementType();
1269 }
Sanjiv Gupta507de852008-06-09 10:47:41 +00001270 }
Mike Stump1eb44332009-09-09 15:08:12 +00001271
Chris Lattner9c85ba32008-11-10 06:08:34 +00001272 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001273 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001274
Devang Patelca80a5f2009-10-20 19:55:01 +00001275 llvm::DIType DbgTy =
1276 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001277 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +00001278 0, Size, Align, 0, 0,
1279 getOrCreateType(EltTy, Unit),
1280 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001281 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001282}
1283
Anders Carlssona031b352009-11-06 19:19:55 +00001284llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001285 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001286 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1287 Ty, Ty->getPointeeType(), Unit);
1288}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001289
Anders Carlsson20f12a22009-12-06 18:00:51 +00001290llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001291 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001292 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1293 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1294
1295 if (!Ty->getPointeeType()->isFunctionType()) {
1296 // We have a data member pointer type.
1297 return PointerDiffDITy;
1298 }
1299
1300 // We have a member function pointer type. Treat it as a struct with two
1301 // ptrdiff_t members.
1302 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1303
1304 uint64_t FieldOffset = 0;
1305 llvm::DIDescriptor ElementTypes[2];
1306
1307 // FIXME: This should probably be a function type instead.
1308 ElementTypes[0] =
1309 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001310 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001311 Info.first, Info.second, FieldOffset, 0,
1312 PointerDiffDITy);
1313 FieldOffset += Info.first;
1314
1315 ElementTypes[1] =
1316 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001317 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001318 Info.first, Info.second, FieldOffset, 0,
1319 PointerDiffDITy);
1320
1321 llvm::DIArray Elements =
1322 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1323 llvm::array_lengthof(ElementTypes));
1324
1325 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1326 U, llvm::StringRef("test"),
Devang Pateld58562e2010-03-09 22:49:11 +00001327 U, 0, FieldOffset,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001328 0, 0, 0, llvm::DIType(), Elements);
1329}
1330
Devang Patel6237cea2010-08-23 22:07:25 +00001331/// CreateEnumType - get enumeration type.
1332llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED, llvm::DIFile Unit){
1333 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
1334
1335 // Create DIEnumerator elements for each enumerator.
1336 for (EnumDecl::enumerator_iterator
1337 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
1338 Enum != EnumEnd; ++Enum) {
1339 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
1340 Enum->getInitVal().getZExtValue()));
1341 }
1342
1343 // Return a CompositeType for the enum itself.
1344 llvm::DIArray EltArray =
1345 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
1346
1347 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1348 unsigned Line = getLineNumber(ED->getLocation());
1349 uint64_t Size = 0;
Devang Patelffc52e72010-08-24 18:14:06 +00001350 uint64_t Align = 0;
1351 if (!ED->getTypeForDecl()->isIncompleteType()) {
1352 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1353 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1354 }
Devang Patel6237cea2010-08-23 22:07:25 +00001355 llvm::DIType DbgTy =
1356 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
1357 Unit, ED->getName(), DefUnit, Line,
Devang Patelffc52e72010-08-24 18:14:06 +00001358 Size, Align, 0, 0,
Devang Patel6237cea2010-08-23 22:07:25 +00001359 llvm::DIType(), EltArray);
1360 return DbgTy;
1361}
1362
Douglas Gregor840943d2009-12-21 20:18:30 +00001363static QualType UnwrapTypeForDebugInfo(QualType T) {
1364 do {
1365 QualType LastT = T;
1366 switch (T->getTypeClass()) {
1367 default:
1368 return T;
1369 case Type::TemplateSpecialization:
1370 T = cast<TemplateSpecializationType>(T)->desugar();
1371 break;
1372 case Type::TypeOfExpr: {
1373 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1374 T = Ty->getUnderlyingExpr()->getType();
1375 break;
1376 }
1377 case Type::TypeOf:
1378 T = cast<TypeOfType>(T)->getUnderlyingType();
1379 break;
1380 case Type::Decltype:
1381 T = cast<DecltypeType>(T)->getUnderlyingType();
1382 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001383 case Type::Elaborated:
1384 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001385 break;
1386 case Type::SubstTemplateTypeParm:
1387 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1388 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001389 }
1390
1391 assert(T != LastT && "Type unwrapping failed to unwrap!");
1392 if (T == LastT)
1393 return T;
1394 } while (true);
1395
1396 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001397}
1398
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001399/// getOrCreateType - Get the type from the cache or create a new
1400/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001401llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001402 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001403 if (Ty.isNull())
1404 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001405
Douglas Gregor840943d2009-12-21 20:18:30 +00001406 // Unwrap the type as needed for debug information.
1407 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001408
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001409 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001410 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001411 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001412 if (it != TypeCache.end()) {
1413 // Verify that the debug info still exists.
1414 if (&*it->second)
1415 return llvm::DIType(cast<llvm::MDNode>(it->second));
1416 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001417
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001418 // Otherwise create the type.
1419 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001420
1421 // And update the type cache.
Devang Patelab699792010-05-07 18:12:35 +00001422 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001423 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001424}
1425
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001426/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001427llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001428 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001429 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001430 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001431 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001432
Douglas Gregor2101a822009-12-21 19:57:21 +00001433 const char *Diag = 0;
1434
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001435 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001436 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001437#define TYPE(Class, Base)
1438#define ABSTRACT_TYPE(Class, Base)
1439#define NON_CANONICAL_TYPE(Class, Base)
1440#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1441#include "clang/AST/TypeNodes.def"
1442 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001443
Anders Carlssonbfe69952009-11-06 18:24:04 +00001444 // FIXME: Handle these.
1445 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001446 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001447
1448 case Type::Vector:
1449 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001450 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001451 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCallc12c5bb2010-05-15 11:32:37 +00001452 case Type::ObjCObject:
1453 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001454 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001455 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1456 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1457 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1458 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001459 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001460 return CreateType(cast<BlockPointerType>(Ty), Unit);
1461 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001462 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001463 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001464 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001465 case Type::FunctionProto:
1466 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001467 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001468 case Type::ConstantArray:
1469 case Type::VariableArray:
1470 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001471 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001472
1473 case Type::LValueReference:
1474 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1475
Anders Carlsson20f12a22009-12-06 18:00:51 +00001476 case Type::MemberPointer:
1477 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001478
1479 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001480 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001481 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001482 case Type::TypeOfExpr:
1483 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001484 case Type::Decltype:
1485 llvm_unreachable("type should have been unwrapped!");
1486 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001487
1488 case Type::RValueReference:
1489 // FIXME: Implement!
1490 Diag = "rvalue references";
1491 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001492 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001493
1494 assert(Diag && "Fall through without a diagnostic?");
1495 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1496 "debug information for %0 is not yet supported");
1497 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1498 << Diag;
1499 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001500}
1501
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001502/// CreateMemberType - Create new member and increase Offset by FType's size.
1503llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
1504 llvm::StringRef Name,
1505 uint64_t *Offset) {
1506 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1507 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1508 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
1509 llvm::DIType Ty = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1510 Unit, Name, Unit, 0,
1511 FieldSize, FieldAlign,
1512 *Offset, 0, FieldTy);
1513 *Offset += FieldSize;
1514 return Ty;
1515}
1516
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001517/// EmitFunctionStart - Constructs the debug code for entering a function -
1518/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001519void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001520 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001521 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001522
Devang Patel9c6c3a02010-01-14 00:36:21 +00001523 llvm::StringRef Name;
Anders Carlsson9a20d552010-06-22 16:16:50 +00001524 llvm::StringRef LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001525
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001526 FnBeginRegionCount.push_back(RegionStack.size());
1527
Devang Patel9c6c3a02010-01-14 00:36:21 +00001528 const Decl *D = GD.getDecl();
1529 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001530 // If there is a DISubprogram for this function available then use it.
1531 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1532 FI = SPCache.find(FD);
1533 if (FI != SPCache.end()) {
Gabor Greif38c9b172010-09-18 13:00:17 +00001534 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(&*FI->second));
Devang Patelab699792010-05-07 18:12:35 +00001535 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1536 llvm::MDNode *SPN = SP;
1537 RegionStack.push_back(SPN);
1538 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00001539 return;
1540 }
1541 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001542 Name = getFunctionName(FD);
1543 // Use mangled name as linkage name for c/c++ functions.
Anders Carlsson9a20d552010-06-22 16:16:50 +00001544 LinkageName = CGM.getMangledName(GD);
David Chisnall70b9b442010-09-02 17:16:32 +00001545 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
David Chisnall52044a22010-09-02 18:01:51 +00001546 Name = getObjCMethodName(OMD);
David Chisnall70b9b442010-09-02 17:16:32 +00001547 LinkageName = Name;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001548 } else {
1549 // Use llvm function name as linkage name.
1550 Name = Fn->getName();
Anders Carlsson9a20d552010-06-22 16:16:50 +00001551 LinkageName = Name;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001552 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001553 if (!Name.empty() && Name[0] == '\01')
1554 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001555
Devang Patel970c6182010-04-24 00:49:16 +00001556 // It is expected that CurLoc is set before using EmitFunctionStart.
1557 // Usually, CurLoc points to the left bracket location of compound
1558 // statement representing function body.
1559 llvm::DIFile Unit = getOrCreateFile(CurLoc);
Devang Patel8ab870d2010-05-12 23:46:38 +00001560 unsigned LineNo = getLineNumber(CurLoc);
Devang Patele2472482010-09-29 21:05:52 +00001561 unsigned Flags = 0;
1562 if (D->isImplicit())
1563 Flags |= llvm::DIDescriptor::FlagArtificial;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001564 llvm::DISubprogram SP =
Devang Patel970c6182010-04-24 00:49:16 +00001565 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001566 getOrCreateType(FnType, Unit),
Devang Patel15a3d7d2010-07-15 23:09:46 +00001567 Fn->hasInternalLinkage(), true/*definition*/,
1568 0, 0, llvm::DIType(),
Devang Patele2472482010-09-29 21:05:52 +00001569 Flags,
Devang Patel15a3d7d2010-07-15 23:09:46 +00001570 CGM.getLangOptions().Optimize, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +00001571
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001572 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001573 llvm::MDNode *SPN = SP;
1574 RegionStack.push_back(SPN);
1575 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001576
1577 // Clear stack used to keep track of #line directives.
1578 LineDirectiveFiles.clear();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001579}
1580
1581
Devang Patel4d939e62010-07-20 22:20:10 +00001582void CGDebugInfo::EmitStopPoint(CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001583 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001584
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001585 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001586 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001587 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001588 || (SM.getInstantiationLineNumber(CurLoc) ==
1589 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001590 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patel4800ea62010-04-05 21:09:15 +00001591 // New Builder may not be in sync with CGDebugInfo.
1592 if (!Builder.getCurrentDebugLocation().isUnknown())
1593 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001594
1595 // Update last state.
1596 PrevLoc = CurLoc;
1597
Chris Lattnerc6034632010-04-01 06:31:43 +00001598 llvm::MDNode *Scope = RegionStack.back();
Devang Patel8ab870d2010-05-12 23:46:38 +00001599 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1600 getColumnNumber(CurLoc),
Chris Lattnere541d012010-04-02 20:21:43 +00001601 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001602}
1603
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001604/// UpdateLineDirectiveRegion - Update region stack only if #line directive
1605/// has introduced scope change.
1606void CGDebugInfo::UpdateLineDirectiveRegion(CGBuilderTy &Builder) {
1607 if (CurLoc.isInvalid() || CurLoc.isMacroID() ||
1608 PrevLoc.isInvalid() || PrevLoc.isMacroID())
1609 return;
1610 SourceManager &SM = CGM.getContext().getSourceManager();
1611 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
1612 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc);
1613
1614 if (!strcmp(PPLoc.getFilename(), PCLoc.getFilename()))
1615 return;
1616
1617 // If #line directive stack is empty then we are entering a new scope.
1618 if (LineDirectiveFiles.empty()) {
1619 EmitRegionStart(Builder);
1620 LineDirectiveFiles.push_back(PCLoc.getFilename());
1621 return;
1622 }
1623
1624 assert (RegionStack.size() >= LineDirectiveFiles.size()
1625 && "error handling #line regions!");
1626
1627 bool SeenThisFile = false;
Devang Patel424a5c62010-09-15 20:50:40 +00001628 // Chek if current file is already seen earlier.
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001629 for(std::vector<const char *>::iterator I = LineDirectiveFiles.begin(),
1630 E = LineDirectiveFiles.end(); I != E; ++I)
Devang Patel424a5c62010-09-15 20:50:40 +00001631 if (!strcmp(PCLoc.getFilename(), *I)) {
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001632 SeenThisFile = true;
1633 break;
1634 }
1635
1636 // If #line for this file is seen earlier then pop out #line regions.
1637 if (SeenThisFile) {
1638 while (!LineDirectiveFiles.empty()) {
1639 const char *LastFile = LineDirectiveFiles.back();
1640 RegionStack.pop_back();
1641 LineDirectiveFiles.pop_back();
1642 if (!strcmp(PPLoc.getFilename(), LastFile))
1643 break;
1644 }
1645 return;
1646 }
1647
1648 // .. otherwise insert new #line region.
1649 EmitRegionStart(Builder);
1650 LineDirectiveFiles.push_back(PCLoc.getFilename());
1651
1652 return;
1653}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001654/// EmitRegionStart- Constructs the debug code for entering a declarative
1655/// region - "llvm.dbg.region.start.".
Devang Patel4d939e62010-07-20 22:20:10 +00001656void CGDebugInfo::EmitRegionStart(CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001657 llvm::DIDescriptor D =
1658 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1659 llvm::DIDescriptor() :
Devang Pateld19429f2010-02-16 21:41:20 +00001660 llvm::DIDescriptor(RegionStack.back()),
Stuart Hastings257d1d32010-07-19 23:56:31 +00001661 getOrCreateFile(CurLoc),
Devang Patel8ab870d2010-05-12 23:46:38 +00001662 getLineNumber(CurLoc),
1663 getColumnNumber(CurLoc));
Devang Patelab699792010-05-07 18:12:35 +00001664 llvm::MDNode *DN = D;
1665 RegionStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001666}
1667
1668/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1669/// region - "llvm.dbg.region.end."
Devang Patel4d939e62010-07-20 22:20:10 +00001670void CGDebugInfo::EmitRegionEnd(CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001671 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1672
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001673 // Provide an region stop point.
Devang Patel4d939e62010-07-20 22:20:10 +00001674 EmitStopPoint(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001675
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001676 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001677}
1678
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001679/// EmitFunctionEnd - Constructs the debug code for exiting a function.
1680void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
1681 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1682 unsigned RCount = FnBeginRegionCount.back();
1683 assert(RCount <= RegionStack.size() && "Region stack mismatch");
1684
1685 // Pop all regions for this function.
1686 while (RegionStack.size() != RCount)
1687 EmitRegionEnd(Builder);
1688 FnBeginRegionCount.pop_back();
1689}
1690
Devang Patel809b9bb2010-02-10 18:49:08 +00001691// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1692// See BuildByRefType.
1693llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1694 uint64_t *XOffset) {
1695
1696 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1697
1698 QualType FType;
1699 uint64_t FieldSize, FieldOffset;
1700 unsigned FieldAlign;
1701
Devang Patel17800552010-03-09 00:44:50 +00001702 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001703 QualType Type = VD->getType();
1704
1705 FieldOffset = 0;
1706 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001707 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1708 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001709 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001710 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1711 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1712
Devang Patel809b9bb2010-02-10 18:49:08 +00001713 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1714 if (HasCopyAndDispose) {
1715 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001716 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1717 &FieldOffset));
1718 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1719 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001720 }
1721
1722 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1723 if (Align > CharUnits::fromQuantity(
1724 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1725 unsigned AlignedOffsetInBytes
1726 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1727 unsigned NumPaddingBytes
1728 = AlignedOffsetInBytes - FieldOffset/8;
1729
1730 if (NumPaddingBytes > 0) {
1731 llvm::APInt pad(32, NumPaddingBytes);
1732 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1733 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001734 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001735 }
1736 }
1737
1738 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001739 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00001740 FieldSize = CGM.getContext().getTypeSize(FType);
1741 FieldAlign = Align.getQuantity()*8;
1742
1743 *XOffset = FieldOffset;
1744 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001745 VD->getName(), Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001746 0, FieldSize, FieldAlign,
1747 FieldOffset, 0, FieldTy);
1748 EltTys.push_back(FieldTy);
1749 FieldOffset += FieldSize;
1750
1751 llvm::DIArray Elements =
1752 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1753
Devang Patele2472482010-09-29 21:05:52 +00001754 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Devang Patel809b9bb2010-02-10 18:49:08 +00001755
1756 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001757 Unit, "", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001758 0, FieldOffset, 0, 0, Flags,
1759 llvm::DIType(), Elements);
1760
1761}
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001762/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001763void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001764 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001765 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1766
Devang Patel17800552010-03-09 00:44:50 +00001767 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001768 llvm::DIType Ty;
1769 uint64_t XOffset = 0;
1770 if (VD->hasAttr<BlocksAttr>())
1771 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1772 else
1773 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001774
Devang Patelf4e54a22010-05-07 23:05:55 +00001775 // If there is not any debug info for type then do not emit debug info
1776 // for this variable.
1777 if (!Ty)
1778 return;
1779
Chris Lattner9c85ba32008-11-10 06:08:34 +00001780 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001781 unsigned Line = getLineNumber(VD->getLocation());
1782 unsigned Column = getColumnNumber(VD->getLocation());
Devang Patelaca745b2010-09-29 23:09:21 +00001783 unsigned Flags = 0;
1784 if (VD->isImplicit())
1785 Flags |= llvm::DIDescriptor::FlagArtificial;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001786 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001787 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001788 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patelaca745b2010-09-29 23:09:21 +00001789 VD->getName(), Unit, Line, Ty,
1790 CGM.getLangOptions().Optimize, Flags);
1791
Chris Lattner9c85ba32008-11-10 06:08:34 +00001792 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001793 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001794 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001795
Chris Lattnerc6034632010-04-01 06:31:43 +00001796 llvm::MDNode *Scope = RegionStack.back();
Chris Lattnere541d012010-04-02 20:21:43 +00001797 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001798}
1799
Mike Stumpb1a6e682009-09-30 02:43:10 +00001800/// EmitDeclare - Emit local variable declaration debug info.
1801void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1802 llvm::Value *Storage, CGBuilderTy &Builder,
1803 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001804 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001805 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1806
Devang Patel2b594b92010-04-26 23:28:46 +00001807 if (Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001808 return;
1809
1810 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001811 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001812 llvm::DIType Ty;
1813 if (VD->hasAttr<BlocksAttr>())
1814 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1815 else
1816 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001817
1818 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001819 unsigned Line = getLineNumber(VD->getLocation());
1820 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001821
Devang Pateld6c5a262010-02-01 21:52:22 +00001822 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001823 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001824 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1825 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1826 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1827 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001828 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001829 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1830 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001831 // offset of __forwarding field
1832 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001833 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1834 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1835 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001836 // offset of x field
1837 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001838 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001839 }
1840
1841 // Create the descriptor for the variable.
1842 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001843 DebugFactory.CreateComplexVariable(Tag,
1844 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001845 VD->getName(), Unit, Line, Ty,
Benjamin Kramer3475cfe2010-09-21 15:59:59 +00001846 addr.data(), addr.size());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001847 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001848 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001849 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattnerd5b89022009-12-28 21:44:41 +00001850
Chris Lattnerc6034632010-04-01 06:31:43 +00001851 llvm::MDNode *Scope = RegionStack.back();
Devang Patelf8e10a52010-05-10 23:48:38 +00001852 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001853}
1854
Devang Pateld6c5a262010-02-01 21:52:22 +00001855void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001856 llvm::Value *Storage,
1857 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001858 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001859}
1860
Mike Stumpb1a6e682009-09-30 02:43:10 +00001861void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1862 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1863 CodeGenFunction *CGF) {
1864 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1865}
1866
Chris Lattner9c85ba32008-11-10 06:08:34 +00001867/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1868/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001869void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001870 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001871 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001872}
1873
1874
1875
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001876/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001877void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001878 const VarDecl *D) {
1879
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001880 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001881 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001882 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +00001883
Devang Pateleb6d79b2010-02-01 21:34:11 +00001884 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001885 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001886
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001887 // CodeGen turns int[] into int[1] so we'll do the same here.
1888 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001889
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001890 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001891 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001892
Anders Carlsson20f12a22009-12-06 18:00:51 +00001893 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001894 ArrayType::Normal, 0);
1895 }
Devang Patel5d822f02010-04-29 17:48:37 +00001896 llvm::StringRef DeclName = D->getName();
Devang Patel8b90a782010-05-13 23:52:37 +00001897 llvm::StringRef LinkageName;
Devang Patel0fd3d1f2010-05-14 16:55:25 +00001898 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()))
Devang Patel8b90a782010-05-13 23:52:37 +00001899 LinkageName = Var->getName();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001900 llvm::DIDescriptor DContext =
1901 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
Devang Patel8b90a782010-05-13 23:52:37 +00001902 DebugFactory.CreateGlobalVariable(DContext, DeclName, DeclName, LinkageName,
1903 Unit, LineNo, getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001904 Var->hasInternalLinkage(),
1905 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001906}
1907
Devang Patel9ca36b62009-02-26 21:10:26 +00001908/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001909void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001910 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001911 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001912 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001913 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patel9ca36b62009-02-26 21:10:26 +00001914
Devang Pateld6c5a262010-02-01 21:52:22 +00001915 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001916
Devang Pateld6c5a262010-02-01 21:52:22 +00001917 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001918 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001919
Devang Patel9ca36b62009-02-26 21:10:26 +00001920 // CodeGen turns int[] into int[1] so we'll do the same here.
1921 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001922
Devang Patel9ca36b62009-02-26 21:10:26 +00001923 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001924 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001925
Anders Carlsson20f12a22009-12-06 18:00:51 +00001926 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001927 ArrayType::Normal, 0);
1928 }
1929
Devang Patelf6a39b72009-10-20 18:26:30 +00001930 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001931 getOrCreateType(T, Unit),
1932 Var->hasInternalLinkage(),
1933 true/*definition*/, Var);
1934}
Devang Patelabb485f2010-02-01 19:16:32 +00001935
Devang Patel25c2c8f2010-08-10 17:53:33 +00001936/// EmitGlobalVariable - Emit global variable's debug info.
1937void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
Devang Pateld2829b72010-10-06 15:58:57 +00001938 llvm::ConstantInt *Init) {
Devang Patel8d308382010-08-10 07:24:25 +00001939 // Create the descriptor for the variable.
1940 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
1941 llvm::StringRef Name = VD->getName();
Devang Patel0317ab02010-08-10 18:27:15 +00001942 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
Devang Patel6237cea2010-08-23 22:07:25 +00001943 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
1944 if (const EnumDecl *ED = dyn_cast<EnumDecl>(ECD->getDeclContext()))
1945 Ty = CreateEnumType(ED, Unit);
1946 }
Devang Patel0317ab02010-08-10 18:27:15 +00001947 // Do not use DIGlobalVariable for enums.
1948 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
1949 return;
Devang Patel8d308382010-08-10 07:24:25 +00001950 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit,
1951 getLineNumber(VD->getLocation()),
Devang Patel4c4acc02010-08-10 20:16:57 +00001952 Ty, true, true, Init);
Devang Patel8d308382010-08-10 07:24:25 +00001953}
1954
Devang Patelabb485f2010-02-01 19:16:32 +00001955/// getOrCreateNamesSpace - Return namespace descriptor for the given
1956/// namespace decl.
1957llvm::DINameSpace
1958CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1959 llvm::DIDescriptor Unit) {
1960 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1961 NameSpaceCache.find(NSDecl);
1962 if (I != NameSpaceCache.end())
1963 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1964
Devang Patel8ab870d2010-05-12 23:46:38 +00001965 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patelabb485f2010-02-01 19:16:32 +00001966
1967 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00001968 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00001969 llvm::DINameSpace NS =
1970 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
Devang Patel2ed8f002010-08-27 17:47:47 +00001971 llvm::DIFile(Unit), LineNo);
Devang Patelab699792010-05-07 18:12:35 +00001972 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00001973 return NS;
1974}