blob: 378be1ef4931317386710308984013e42e6d6f18 [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 '...'!
Devang Patelaf164bb2010-10-06 20:51:45 +0000573 if (isa<FunctionNoProtoType>(Ty))
574 EltTys.push_back(DebugFactory.CreateUnspecifiedParameter());
575 else if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000576 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
577 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
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 }
Devang Patel3951e712010-10-07 22:03:49 +0000749 if (Method->hasPrototype())
750 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Pateld78a0192010-10-01 23:32:17 +0000751
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000752 llvm::DISubprogram SP =
753 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
754 MethodLinkageName,
755 MethodDefUnit, MethodLine,
756 MethodTy, /*isLocalToUnit=*/false,
Devang Patela5c5bab2010-07-12 22:54:41 +0000757 /* isDefintion=*/ false,
Devang Patel40894912010-07-15 22:57:00 +0000758 Virtuality, VIndex, ContainingType,
Devang Patele2472482010-09-29 21:05:52 +0000759 Flags,
Devang Patel15a3d7d2010-07-15 23:09:46 +0000760 CGM.getLangOptions().Optimize);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000761
762 // Don't cache ctors or dtors since we have to emit multiple functions for
763 // a single ctor or dtor.
764 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
Devang Patelab699792010-05-07 18:12:35 +0000765 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000766
767 return SP;
768}
769
Devang Patel4125fd22010-01-19 01:54:44 +0000770/// CollectCXXMemberFunctions - A helper function to collect debug info for
771/// C++ member functions.This is used while creating debug info entry for
772/// a Record.
773void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000774CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000775 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000776 llvm::DIType RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000777 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
778 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000779 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000780
Devang Pateld5322da2010-02-09 19:09:28 +0000781 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000782 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000783
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000784 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000785 }
786}
787
Devang Patel2ed8f002010-08-27 17:47:47 +0000788/// CollectCXXFriends - A helper function to collect debug info for
789/// C++ base classes. This is used while creating debug info entry for
790/// a Record.
791void CGDebugInfo::
792CollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit,
793 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
794 llvm::DIType RecordTy) {
795
796 for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(),
797 BE = RD->friend_end(); BI != BE; ++BI) {
798
799 TypeSourceInfo *TInfo = (*BI)->getFriendType();
800 if(TInfo)
801 {
802 llvm::DIType Ty = getOrCreateType(TInfo->getType(), Unit);
803
804 llvm::DIType DTy =
805 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_friend,
806 RecordTy, llvm::StringRef(),
807 Unit, 0, 0, 0,
808 0, 0, Ty);
809
810 EltTys.push_back(DTy);
811 }
812
813 }
814}
815
Devang Patela245c5b2010-01-25 23:32:18 +0000816/// CollectCXXBases - A helper function to collect debug info for
817/// C++ base classes. This is used while creating debug info entry for
818/// a Record.
819void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000820CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000821 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000822 llvm::DIType RecordTy) {
Devang Patela245c5b2010-01-25 23:32:18 +0000823
Devang Patel239cec62010-02-01 21:39:52 +0000824 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
825 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
826 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000827 unsigned BFlags = 0;
828 uint64_t BaseOffset;
829
830 const CXXRecordDecl *Base =
831 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
832
833 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000834 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000835 // expression where it expects +ve number.
Anders Carlssonaf440352010-03-23 04:11:45 +0000836 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base);
Devang Patele2472482010-09-29 21:05:52 +0000837 BFlags = llvm::DIDescriptor::FlagVirtual;
Devang Patelca7daed2010-01-28 21:54:15 +0000838 } else
839 BaseOffset = RL.getBaseClassOffset(Base);
840
841 AccessSpecifier Access = BI->getAccessSpecifier();
842 if (Access == clang::AS_private)
Devang Patele2472482010-09-29 21:05:52 +0000843 BFlags |= llvm::DIDescriptor::FlagPrivate;
Devang Patelca7daed2010-01-28 21:54:15 +0000844 else if (Access == clang::AS_protected)
Devang Patele2472482010-09-29 21:05:52 +0000845 BFlags |= llvm::DIDescriptor::FlagProtected;
Devang Patelca7daed2010-01-28 21:54:15 +0000846
847 llvm::DIType DTy =
848 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
849 RecordTy, llvm::StringRef(),
Devang Pateld58562e2010-03-09 22:49:11 +0000850 Unit, 0, 0, 0,
Devang Patelca7daed2010-01-28 21:54:15 +0000851 BaseOffset, BFlags,
852 getOrCreateType(BI->getType(),
853 Unit));
854 EltTys.push_back(DTy);
855 }
Devang Patela245c5b2010-01-25 23:32:18 +0000856}
857
Devang Patel4ce3f202010-01-28 18:11:52 +0000858/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000859llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000860 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000861 return VTablePtrType;
862
863 ASTContext &Context = CGM.getContext();
864
865 /* Function type */
Benjamin Kramerad468862010-03-30 11:36:44 +0000866 llvm::DIDescriptor STy = getOrCreateType(Context.IntTy, Unit);
867 llvm::DIArray SElements = DebugFactory.GetOrCreateArray(&STy, 1);
Devang Patel4ce3f202010-01-28 18:11:52 +0000868 llvm::DIType SubTy =
869 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000870 Unit, "", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000871 0, 0, 0, 0, 0, llvm::DIType(), SElements);
872
873 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
874 llvm::DIType vtbl_ptr_type
875 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000876 Unit, "__vtbl_ptr_type", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000877 0, Size, 0, 0, 0, SubTy);
878
Devang Pateld58562e2010-03-09 22:49:11 +0000879 VTablePtrType =
880 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
881 Unit, "", Unit,
882 0, Size, 0, 0, 0, vtbl_ptr_type);
Devang Patel4ce3f202010-01-28 18:11:52 +0000883 return VTablePtrType;
884}
885
Anders Carlsson046c2942010-04-17 20:15:18 +0000886/// getVTableName - Get vtable name for the given Class.
887llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000888 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000889 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000890
891 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000892 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000893 memcpy(StrPtr, Name.data(), Name.length());
894 return llvm::StringRef(StrPtr, Name.length());
895}
896
897
Anders Carlsson046c2942010-04-17 20:15:18 +0000898/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel4ce3f202010-01-28 18:11:52 +0000899/// debug info entry in EltTys vector.
900void CGDebugInfo::
Anders Carlsson046c2942010-04-17 20:15:18 +0000901CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000902 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000903 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000904
905 // If there is a primary base then it will hold vtable info.
906 if (RL.getPrimaryBase())
907 return;
908
909 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000910 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000911 return;
912
913 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
914 llvm::DIType VPTR
915 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Anders Carlsson046c2942010-04-17 20:15:18 +0000916 getVTableName(RD), Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000917 0, Size, 0, 0, 0,
918 getOrCreateVTablePtrType(Unit));
919 EltTys.push_back(VPTR);
920}
921
Devang Patelc69e1cf2010-09-30 19:05:55 +0000922/// getOrCreateRecordType - Emit record type's standalone debug info.
923llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
924 SourceLocation Loc) {
925 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
926 DebugFactory.RecordType(T);
927 return T;
928}
929
Devang Patel65e99f22009-02-25 01:36:11 +0000930/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000931llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000932 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000933 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000934
Chris Lattner9c85ba32008-11-10 06:08:34 +0000935 unsigned Tag;
Devang Pateld6c5a262010-02-01 21:52:22 +0000936 if (RD->isStruct())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000937 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Pateld6c5a262010-02-01 21:52:22 +0000938 else if (RD->isUnion())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000939 Tag = llvm::dwarf::DW_TAG_union_type;
940 else {
Devang Pateld6c5a262010-02-01 21:52:22 +0000941 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000942 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000943 }
944
Chris Lattner9c85ba32008-11-10 06:08:34 +0000945 // Get overall information about the record type for the debug info.
Devang Patel8ab870d2010-05-12 23:46:38 +0000946 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
947 unsigned Line = getLineNumber(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000948
Chris Lattner9c85ba32008-11-10 06:08:34 +0000949 // Records and classes and unions can all be recursive. To handle them, we
950 // first generate a debug descriptor for the struct as a forward declaration.
951 // Then (if it is a definition) we go through and get debug info for all of
952 // its members. Finally, we create a descriptor for the complete type (which
953 // may refer to the forward decl if the struct is recursive) and replace all
954 // uses of the forward declaration with the final definition.
Devang Patel0b897992010-07-08 19:56:29 +0000955 llvm::DIDescriptor FDContext =
956 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
957
958 // If this is just a forward declaration, construct an appropriately
959 // marked node and just return it.
960 if (!RD->getDefinition()) {
961 llvm::DICompositeType FwdDecl =
962 DebugFactory.CreateCompositeType(Tag, FDContext, RD->getName(),
963 DefUnit, Line, 0, 0, 0,
Devang Patele2472482010-09-29 21:05:52 +0000964 llvm::DIDescriptor::FlagFwdDecl,
Devang Patel0b897992010-07-08 19:56:29 +0000965 llvm::DIType(), llvm::DIArray());
966
967 return FwdDecl;
968 }
Devang Pateld0f251b2010-01-20 23:56:40 +0000969
Dan Gohman86968372010-08-20 22:39:57 +0000970 llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType();
Mike Stump1eb44332009-09-09 15:08:12 +0000971
Devang Patelab699792010-05-07 18:12:35 +0000972 llvm::MDNode *MN = FwdDecl;
973 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000974 // Otherwise, insert it into the TypeCache so that recursive uses will find
975 // it.
Devang Patelab699792010-05-07 18:12:35 +0000976 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000977 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000978 RegionStack.push_back(FwdDeclNode);
979 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000980
981 // Convert all the elements.
982 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
983
Devang Pateld6c5a262010-02-01 21:52:22 +0000984 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000985 if (CXXDecl) {
986 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson046c2942010-04-17 20:15:18 +0000987 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000988 }
Devang Pateldabc3e92010-08-12 00:02:44 +0000989
990 // Collect static variables with initializers.
991 for (RecordDecl::decl_iterator I = RD->decls_begin(), E = RD->decls_end();
992 I != E; ++I)
993 if (const VarDecl *V = dyn_cast<VarDecl>(*I)) {
994 if (const Expr *Init = V->getInit()) {
995 Expr::EvalResult Result;
996 if (Init->Evaluate(Result, CGM.getContext()) && Result.Val.isInt()) {
997 llvm::ConstantInt *CI
998 = llvm::ConstantInt::get(CGM.getLLVMContext(), Result.Val.getInt());
999
1000 // Create the descriptor for static variable.
1001 llvm::DIFile VUnit = getOrCreateFile(V->getLocation());
1002 llvm::StringRef VName = V->getName();
1003 llvm::DIType VTy = getOrCreateType(V->getType(), VUnit);
1004 // Do not use DIGlobalVariable for enums.
1005 if (VTy.getTag() != llvm::dwarf::DW_TAG_enumeration_type) {
1006 DebugFactory.CreateGlobalVariable(FwdDecl, VName, VName, VName, VUnit,
1007 getLineNumber(V->getLocation()),
1008 VTy, true, true, CI);
1009 }
1010 }
1011 }
1012 }
1013
Devang Pateld6c5a262010-02-01 21:52:22 +00001014 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +00001015 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +00001016 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +00001017 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel2ed8f002010-08-27 17:47:47 +00001018 CollectCXXFriends(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +00001019
1020 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +00001021 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel0ac8f312010-01-28 00:54:21 +00001022 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
1023 ContainingType =
Devang Patelab699792010-05-07 18:12:35 +00001024 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patel0ac8f312010-01-28 00:54:21 +00001025 else if (CXXDecl->isDynamicClass())
Devang Patelab699792010-05-07 18:12:35 +00001026 ContainingType = FwdDecl;
Devang Patela245c5b2010-01-25 23:32:18 +00001027 }
Mike Stump1eb44332009-09-09 15:08:12 +00001028
Chris Lattner9c85ba32008-11-10 06:08:34 +00001029 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001030 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001031
1032 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001033 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1034 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001035
Devang Patele4c1ea02010-03-11 20:01:48 +00001036 RegionStack.pop_back();
1037 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1038 RegionMap.find(Ty->getDecl());
1039 if (RI != RegionMap.end())
1040 RegionMap.erase(RI);
1041
Devang Patel411894b2010-02-01 22:40:08 +00001042 llvm::DIDescriptor RDContext =
1043 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel700a1cb2010-07-20 20:24:18 +00001044
1045 llvm::StringRef RDName = RD->getName();
1046 // If this is a class, include the template arguments also.
1047 if (Tag == llvm::dwarf::DW_TAG_class_type)
1048 RDName = getClassName(RD);
1049
Devang Patel0ce73f62009-07-22 18:57:00 +00001050 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +00001051 DebugFactory.CreateCompositeType(Tag, RDContext,
Devang Patel700a1cb2010-07-20 20:24:18 +00001052 RDName,
Devang Patelab71ff52009-11-12 00:51:46 +00001053 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +00001054 llvm::DIType(), Elements,
1055 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001056
1057 // Now that we have a real decl for the struct, replace anything using the
1058 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001059 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001060 RegionMap[RD] = llvm::WeakVH(RealDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001061 return RealDecl;
1062}
1063
John McCallc12c5bb2010-05-15 11:32:37 +00001064/// CreateType - get objective-c object type.
1065llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1066 llvm::DIFile Unit) {
1067 // Ignore protocols.
1068 return getOrCreateType(Ty->getBaseType(), Unit);
1069}
1070
Devang Patel9ca36b62009-02-26 21:10:26 +00001071/// CreateType - get objective-c interface type.
1072llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001073 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001074 ObjCInterfaceDecl *ID = Ty->getDecl();
Devang Patel9ca36b62009-02-26 21:10:26 +00001075 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Patel9ca36b62009-02-26 21:10:26 +00001076
1077 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +00001078 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001079 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel17800552010-03-09 00:44:50 +00001080 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +00001081
Dan Gohman45f7c782010-08-23 21:15:56 +00001082 // If this is just a forward declaration, return a special forward-declaration
1083 // debug type.
1084 if (ID->isForwardDecl()) {
1085 llvm::DICompositeType FwdDecl =
1086 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
1087 DefUnit, Line, 0, 0, 0, 0,
1088 llvm::DIType(), llvm::DIArray(),
1089 RuntimeLang);
1090 return FwdDecl;
1091 }
1092
Devang Patel9ca36b62009-02-26 21:10:26 +00001093 // To handle recursive interface, we
1094 // first generate a debug descriptor for the struct as a forward declaration.
1095 // Then (if it is a definition) we go through and get debug info for all of
1096 // its members. Finally, we create a descriptor for the complete type (which
1097 // may refer to the forward decl if the struct is recursive) and replace all
1098 // uses of the forward declaration with the final definition.
Dan Gohman86968372010-08-20 22:39:57 +00001099 llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType();
Mike Stump1eb44332009-09-09 15:08:12 +00001100
Devang Patelab699792010-05-07 18:12:35 +00001101 llvm::MDNode *MN = FwdDecl;
1102 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patel9ca36b62009-02-26 21:10:26 +00001103 // Otherwise, insert it into the TypeCache so that recursive uses will find
1104 // it.
Devang Patelab699792010-05-07 18:12:35 +00001105 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +00001106 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001107 RegionStack.push_back(FwdDeclNode);
1108 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001109
1110 // Convert all the elements.
1111 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
1112
Devang Pateld6c5a262010-02-01 21:52:22 +00001113 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +00001114 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +00001115 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001116 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001117 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +00001118 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Pateld58562e2010-03-09 22:49:11 +00001119 Unit, "", Unit, 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +00001120 0 /* offset */, 0, SClassTy);
1121 EltTys.push_back(InhTag);
1122 }
1123
Devang Pateld6c5a262010-02-01 21:52:22 +00001124 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001125
1126 unsigned FieldNo = 0;
Fariborz Jahanian97477392010-10-01 00:01:53 +00001127 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1128 Field = Field->getNextIvar()) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001129 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1130
Devang Patel73621622009-11-25 17:37:31 +00001131 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001132
Devang Patelde135022009-04-27 22:40:36 +00001133 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +00001134 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +00001135 continue;
1136
Devang Patel9ca36b62009-02-26 21:10:26 +00001137 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +00001138 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1139 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel99c20eb2009-03-20 18:24:39 +00001140 QualType FType = Field->getType();
1141 uint64_t FieldSize = 0;
1142 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +00001143
Devang Patel99c20eb2009-03-20 18:24:39 +00001144 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001145
Devang Patel99c20eb2009-03-20 18:24:39 +00001146 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001147 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001148 Expr *BitWidth = Field->getBitWidth();
1149 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +00001150 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +00001151
Anders Carlsson20f12a22009-12-06 18:00:51 +00001152 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001153 }
1154
Mike Stump1eb44332009-09-09 15:08:12 +00001155 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
1156
Devang Patelc20482b2009-03-19 00:23:53 +00001157 unsigned Flags = 0;
1158 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Devang Patele2472482010-09-29 21:05:52 +00001159 Flags = llvm::DIDescriptor::FlagProtected;
Devang Patelc20482b2009-03-19 00:23:53 +00001160 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Devang Patele2472482010-09-29 21:05:52 +00001161 Flags = llvm::DIDescriptor::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +00001162
Devang Patel9ca36b62009-02-26 21:10:26 +00001163 // Create a DW_TAG_member node to remember the offset of this field in the
1164 // struct. FIXME: This is an absolutely insane way to capture this
1165 // information. When we gut debug info, this should be fixed.
1166 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1167 FieldName, FieldDefUnit,
1168 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +00001169 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +00001170 EltTys.push_back(FieldTy);
1171 }
Mike Stump1eb44332009-09-09 15:08:12 +00001172
Devang Patel9ca36b62009-02-26 21:10:26 +00001173 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +00001174 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +00001175
Devang Patele4c1ea02010-03-11 20:01:48 +00001176 RegionStack.pop_back();
1177 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1178 RegionMap.find(Ty->getDecl());
1179 if (RI != RegionMap.end())
1180 RegionMap.erase(RI);
1181
Devang Patel9ca36b62009-02-26 21:10:26 +00001182 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001183 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1184 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Devang Patel6c1fddf2009-07-27 18:42:03 +00001186 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +00001187 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +00001188 Line, Size, Align, 0, 0, llvm::DIType(),
1189 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +00001190
1191 // Now that we have a real decl for the struct, replace anything using the
1192 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001193 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001194 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +00001195
Devang Patel9ca36b62009-02-26 21:10:26 +00001196 return RealDecl;
1197}
1198
Chris Lattner9c85ba32008-11-10 06:08:34 +00001199llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001200 llvm::DIFile Unit) {
Devang Patel6237cea2010-08-23 22:07:25 +00001201 return CreateEnumType(Ty->getDecl(), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001202
Chris Lattner9c85ba32008-11-10 06:08:34 +00001203}
1204
1205llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001206 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001207 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1208 return CreateType(RT, Unit);
1209 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1210 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001211
Chris Lattner9c85ba32008-11-10 06:08:34 +00001212 return llvm::DIType();
1213}
1214
Devang Patel70c23cd2010-02-23 22:59:39 +00001215llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Eli Friedmana7e68452010-08-22 01:00:03 +00001216 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001217 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1218 uint64_t NumElems = Ty->getNumElements();
1219 if (NumElems > 0)
1220 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001221
Benjamin Kramerad468862010-03-30 11:36:44 +00001222 llvm::DIDescriptor Subscript = DebugFactory.GetOrCreateSubrange(0, NumElems);
1223 llvm::DIArray SubscriptArray = DebugFactory.GetOrCreateArray(&Subscript, 1);
Devang Patel70c23cd2010-02-23 22:59:39 +00001224
1225 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1226 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1227
1228 return
1229 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001230 Unit, "", Unit,
Devang Patel70c23cd2010-02-23 22:59:39 +00001231 0, Size, Align, 0, 0,
Eli Friedmana7e68452010-08-22 01:00:03 +00001232 ElementTy, SubscriptArray);
Devang Patel70c23cd2010-02-23 22:59:39 +00001233}
1234
Chris Lattner9c85ba32008-11-10 06:08:34 +00001235llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001236 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001237 uint64_t Size;
1238 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001239
1240
Nuno Lopes010d5142009-01-28 00:35:17 +00001241 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001242 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001243 Size = 0;
1244 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001245 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001246 } else if (Ty->isIncompleteArrayType()) {
1247 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001248 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001249 } else {
1250 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001251 Size = CGM.getContext().getTypeSize(Ty);
1252 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001253 }
Mike Stump1eb44332009-09-09 15:08:12 +00001254
Chris Lattner9c85ba32008-11-10 06:08:34 +00001255 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1256 // interior arrays, do we care? Why aren't nested arrays represented the
1257 // obvious/recursive way?
1258 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1259 QualType EltTy(Ty, 0);
Devang Patelcdf523c2010-10-06 18:30:00 +00001260 if (Ty->isIncompleteArrayType())
Chris Lattner9c85ba32008-11-10 06:08:34 +00001261 EltTy = Ty->getElementType();
Devang Patelcdf523c2010-10-06 18:30:00 +00001262 else {
1263 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1264 uint64_t Upper = 0;
1265 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1266 if (CAT->getSize().getZExtValue())
1267 Upper = CAT->getSize().getZExtValue() - 1;
1268 // FIXME: Verify this is right for VLAs.
1269 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1270 EltTy = Ty->getElementType();
1271 }
Sanjiv Gupta507de852008-06-09 10:47:41 +00001272 }
Mike Stump1eb44332009-09-09 15:08:12 +00001273
Chris Lattner9c85ba32008-11-10 06:08:34 +00001274 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001275 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001276
Devang Patelca80a5f2009-10-20 19:55:01 +00001277 llvm::DIType DbgTy =
1278 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001279 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +00001280 0, Size, Align, 0, 0,
1281 getOrCreateType(EltTy, Unit),
1282 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001283 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001284}
1285
Anders Carlssona031b352009-11-06 19:19:55 +00001286llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001287 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001288 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1289 Ty, Ty->getPointeeType(), Unit);
1290}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001291
Anders Carlsson20f12a22009-12-06 18:00:51 +00001292llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001293 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001294 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1295 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1296
1297 if (!Ty->getPointeeType()->isFunctionType()) {
1298 // We have a data member pointer type.
1299 return PointerDiffDITy;
1300 }
1301
1302 // We have a member function pointer type. Treat it as a struct with two
1303 // ptrdiff_t members.
1304 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1305
1306 uint64_t FieldOffset = 0;
1307 llvm::DIDescriptor ElementTypes[2];
1308
1309 // FIXME: This should probably be a function type instead.
1310 ElementTypes[0] =
1311 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001312 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001313 Info.first, Info.second, FieldOffset, 0,
1314 PointerDiffDITy);
1315 FieldOffset += Info.first;
1316
1317 ElementTypes[1] =
1318 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001319 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001320 Info.first, Info.second, FieldOffset, 0,
1321 PointerDiffDITy);
1322
1323 llvm::DIArray Elements =
1324 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1325 llvm::array_lengthof(ElementTypes));
1326
1327 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1328 U, llvm::StringRef("test"),
Devang Pateld58562e2010-03-09 22:49:11 +00001329 U, 0, FieldOffset,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001330 0, 0, 0, llvm::DIType(), Elements);
1331}
1332
Devang Patel6237cea2010-08-23 22:07:25 +00001333/// CreateEnumType - get enumeration type.
1334llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED, llvm::DIFile Unit){
1335 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
1336
1337 // Create DIEnumerator elements for each enumerator.
1338 for (EnumDecl::enumerator_iterator
1339 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
1340 Enum != EnumEnd; ++Enum) {
1341 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
1342 Enum->getInitVal().getZExtValue()));
1343 }
1344
1345 // Return a CompositeType for the enum itself.
1346 llvm::DIArray EltArray =
1347 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
1348
1349 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1350 unsigned Line = getLineNumber(ED->getLocation());
1351 uint64_t Size = 0;
Devang Patelffc52e72010-08-24 18:14:06 +00001352 uint64_t Align = 0;
1353 if (!ED->getTypeForDecl()->isIncompleteType()) {
1354 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1355 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1356 }
Devang Patel6237cea2010-08-23 22:07:25 +00001357 llvm::DIType DbgTy =
1358 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
1359 Unit, ED->getName(), DefUnit, Line,
Devang Patelffc52e72010-08-24 18:14:06 +00001360 Size, Align, 0, 0,
Devang Patel6237cea2010-08-23 22:07:25 +00001361 llvm::DIType(), EltArray);
1362 return DbgTy;
1363}
1364
Douglas Gregor840943d2009-12-21 20:18:30 +00001365static QualType UnwrapTypeForDebugInfo(QualType T) {
1366 do {
1367 QualType LastT = T;
1368 switch (T->getTypeClass()) {
1369 default:
1370 return T;
1371 case Type::TemplateSpecialization:
1372 T = cast<TemplateSpecializationType>(T)->desugar();
1373 break;
1374 case Type::TypeOfExpr: {
1375 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1376 T = Ty->getUnderlyingExpr()->getType();
1377 break;
1378 }
1379 case Type::TypeOf:
1380 T = cast<TypeOfType>(T)->getUnderlyingType();
1381 break;
1382 case Type::Decltype:
1383 T = cast<DecltypeType>(T)->getUnderlyingType();
1384 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001385 case Type::Elaborated:
1386 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001387 break;
1388 case Type::SubstTemplateTypeParm:
1389 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1390 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001391 }
1392
1393 assert(T != LastT && "Type unwrapping failed to unwrap!");
1394 if (T == LastT)
1395 return T;
1396 } while (true);
1397
1398 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001399}
1400
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001401/// getOrCreateType - Get the type from the cache or create a new
1402/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001403llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001404 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001405 if (Ty.isNull())
1406 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001407
Douglas Gregor840943d2009-12-21 20:18:30 +00001408 // Unwrap the type as needed for debug information.
1409 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001410
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001411 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001412 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001413 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001414 if (it != TypeCache.end()) {
1415 // Verify that the debug info still exists.
1416 if (&*it->second)
1417 return llvm::DIType(cast<llvm::MDNode>(it->second));
1418 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001419
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001420 // Otherwise create the type.
1421 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001422
1423 // And update the type cache.
Devang Patelab699792010-05-07 18:12:35 +00001424 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001425 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001426}
1427
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001428/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001429llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001430 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001431 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001432 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001433 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001434
Douglas Gregor2101a822009-12-21 19:57:21 +00001435 const char *Diag = 0;
1436
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001437 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001438 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001439#define TYPE(Class, Base)
1440#define ABSTRACT_TYPE(Class, Base)
1441#define NON_CANONICAL_TYPE(Class, Base)
1442#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1443#include "clang/AST/TypeNodes.def"
1444 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001445
Anders Carlssonbfe69952009-11-06 18:24:04 +00001446 // FIXME: Handle these.
1447 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001448 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001449
1450 case Type::Vector:
1451 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001452 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001453 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCallc12c5bb2010-05-15 11:32:37 +00001454 case Type::ObjCObject:
1455 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001456 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001457 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1458 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1459 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1460 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001461 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001462 return CreateType(cast<BlockPointerType>(Ty), Unit);
1463 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001464 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001465 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001466 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001467 case Type::FunctionProto:
1468 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001469 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001470 case Type::ConstantArray:
1471 case Type::VariableArray:
1472 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001473 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001474
1475 case Type::LValueReference:
1476 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1477
Anders Carlsson20f12a22009-12-06 18:00:51 +00001478 case Type::MemberPointer:
1479 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001480
1481 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001482 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001483 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001484 case Type::TypeOfExpr:
1485 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001486 case Type::Decltype:
1487 llvm_unreachable("type should have been unwrapped!");
1488 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001489
1490 case Type::RValueReference:
1491 // FIXME: Implement!
1492 Diag = "rvalue references";
1493 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001494 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001495
1496 assert(Diag && "Fall through without a diagnostic?");
1497 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1498 "debug information for %0 is not yet supported");
1499 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1500 << Diag;
1501 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001502}
1503
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001504/// CreateMemberType - Create new member and increase Offset by FType's size.
1505llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
1506 llvm::StringRef Name,
1507 uint64_t *Offset) {
1508 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1509 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1510 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
1511 llvm::DIType Ty = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1512 Unit, Name, Unit, 0,
1513 FieldSize, FieldAlign,
1514 *Offset, 0, FieldTy);
1515 *Offset += FieldSize;
1516 return Ty;
1517}
1518
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001519/// EmitFunctionStart - Constructs the debug code for entering a function -
1520/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001521void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001522 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001523 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001524
Devang Patel9c6c3a02010-01-14 00:36:21 +00001525 llvm::StringRef Name;
Anders Carlsson9a20d552010-06-22 16:16:50 +00001526 llvm::StringRef LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001527
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001528 FnBeginRegionCount.push_back(RegionStack.size());
1529
Devang Patel9c6c3a02010-01-14 00:36:21 +00001530 const Decl *D = GD.getDecl();
Devang Patel3951e712010-10-07 22:03:49 +00001531 unsigned Flags = 0;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001532 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001533 // If there is a DISubprogram for this function available then use it.
1534 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1535 FI = SPCache.find(FD);
1536 if (FI != SPCache.end()) {
Gabor Greif38c9b172010-09-18 13:00:17 +00001537 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(&*FI->second));
Devang Patelab699792010-05-07 18:12:35 +00001538 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1539 llvm::MDNode *SPN = SP;
1540 RegionStack.push_back(SPN);
1541 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00001542 return;
1543 }
1544 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001545 Name = getFunctionName(FD);
1546 // Use mangled name as linkage name for c/c++ functions.
Anders Carlsson9a20d552010-06-22 16:16:50 +00001547 LinkageName = CGM.getMangledName(GD);
Devang Patel3951e712010-10-07 22:03:49 +00001548 if (FD->hasPrototype())
1549 Flags |= llvm::DIDescriptor::FlagPrototyped;
David Chisnall70b9b442010-09-02 17:16:32 +00001550 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
David Chisnall52044a22010-09-02 18:01:51 +00001551 Name = getObjCMethodName(OMD);
David Chisnall70b9b442010-09-02 17:16:32 +00001552 LinkageName = Name;
Devang Patel3951e712010-10-07 22:03:49 +00001553 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001554 } else {
1555 // Use llvm function name as linkage name.
1556 Name = Fn->getName();
Anders Carlsson9a20d552010-06-22 16:16:50 +00001557 LinkageName = Name;
Devang Patel3951e712010-10-07 22:03:49 +00001558 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001559 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001560 if (!Name.empty() && Name[0] == '\01')
1561 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001562
Devang Patel970c6182010-04-24 00:49:16 +00001563 // It is expected that CurLoc is set before using EmitFunctionStart.
1564 // Usually, CurLoc points to the left bracket location of compound
1565 // statement representing function body.
1566 llvm::DIFile Unit = getOrCreateFile(CurLoc);
Devang Patel8ab870d2010-05-12 23:46:38 +00001567 unsigned LineNo = getLineNumber(CurLoc);
Devang Patele2472482010-09-29 21:05:52 +00001568 if (D->isImplicit())
1569 Flags |= llvm::DIDescriptor::FlagArtificial;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001570 llvm::DISubprogram SP =
Devang Patel970c6182010-04-24 00:49:16 +00001571 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001572 getOrCreateType(FnType, Unit),
Devang Patel15a3d7d2010-07-15 23:09:46 +00001573 Fn->hasInternalLinkage(), true/*definition*/,
1574 0, 0, llvm::DIType(),
Devang Patele2472482010-09-29 21:05:52 +00001575 Flags,
Devang Patel15a3d7d2010-07-15 23:09:46 +00001576 CGM.getLangOptions().Optimize, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +00001577
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001578 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001579 llvm::MDNode *SPN = SP;
1580 RegionStack.push_back(SPN);
1581 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001582
1583 // Clear stack used to keep track of #line directives.
1584 LineDirectiveFiles.clear();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001585}
1586
1587
Devang Patel4d939e62010-07-20 22:20:10 +00001588void CGDebugInfo::EmitStopPoint(CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001589 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001590
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001591 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001592 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001593 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001594 || (SM.getInstantiationLineNumber(CurLoc) ==
1595 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001596 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patel4800ea62010-04-05 21:09:15 +00001597 // New Builder may not be in sync with CGDebugInfo.
1598 if (!Builder.getCurrentDebugLocation().isUnknown())
1599 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001600
1601 // Update last state.
1602 PrevLoc = CurLoc;
1603
Chris Lattnerc6034632010-04-01 06:31:43 +00001604 llvm::MDNode *Scope = RegionStack.back();
Devang Patel8ab870d2010-05-12 23:46:38 +00001605 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1606 getColumnNumber(CurLoc),
Chris Lattnere541d012010-04-02 20:21:43 +00001607 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001608}
1609
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001610/// UpdateLineDirectiveRegion - Update region stack only if #line directive
1611/// has introduced scope change.
1612void CGDebugInfo::UpdateLineDirectiveRegion(CGBuilderTy &Builder) {
1613 if (CurLoc.isInvalid() || CurLoc.isMacroID() ||
1614 PrevLoc.isInvalid() || PrevLoc.isMacroID())
1615 return;
1616 SourceManager &SM = CGM.getContext().getSourceManager();
1617 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
1618 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc);
1619
1620 if (!strcmp(PPLoc.getFilename(), PCLoc.getFilename()))
1621 return;
1622
1623 // If #line directive stack is empty then we are entering a new scope.
1624 if (LineDirectiveFiles.empty()) {
1625 EmitRegionStart(Builder);
1626 LineDirectiveFiles.push_back(PCLoc.getFilename());
1627 return;
1628 }
1629
1630 assert (RegionStack.size() >= LineDirectiveFiles.size()
1631 && "error handling #line regions!");
1632
1633 bool SeenThisFile = false;
Devang Patel424a5c62010-09-15 20:50:40 +00001634 // Chek if current file is already seen earlier.
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001635 for(std::vector<const char *>::iterator I = LineDirectiveFiles.begin(),
1636 E = LineDirectiveFiles.end(); I != E; ++I)
Devang Patel424a5c62010-09-15 20:50:40 +00001637 if (!strcmp(PCLoc.getFilename(), *I)) {
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001638 SeenThisFile = true;
1639 break;
1640 }
1641
1642 // If #line for this file is seen earlier then pop out #line regions.
1643 if (SeenThisFile) {
1644 while (!LineDirectiveFiles.empty()) {
1645 const char *LastFile = LineDirectiveFiles.back();
1646 RegionStack.pop_back();
1647 LineDirectiveFiles.pop_back();
1648 if (!strcmp(PPLoc.getFilename(), LastFile))
1649 break;
1650 }
1651 return;
1652 }
1653
1654 // .. otherwise insert new #line region.
1655 EmitRegionStart(Builder);
1656 LineDirectiveFiles.push_back(PCLoc.getFilename());
1657
1658 return;
1659}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001660/// EmitRegionStart- Constructs the debug code for entering a declarative
1661/// region - "llvm.dbg.region.start.".
Devang Patel4d939e62010-07-20 22:20:10 +00001662void CGDebugInfo::EmitRegionStart(CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001663 llvm::DIDescriptor D =
1664 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1665 llvm::DIDescriptor() :
Devang Pateld19429f2010-02-16 21:41:20 +00001666 llvm::DIDescriptor(RegionStack.back()),
Stuart Hastings257d1d32010-07-19 23:56:31 +00001667 getOrCreateFile(CurLoc),
Devang Patel8ab870d2010-05-12 23:46:38 +00001668 getLineNumber(CurLoc),
1669 getColumnNumber(CurLoc));
Devang Patelab699792010-05-07 18:12:35 +00001670 llvm::MDNode *DN = D;
1671 RegionStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001672}
1673
1674/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1675/// region - "llvm.dbg.region.end."
Devang Patel4d939e62010-07-20 22:20:10 +00001676void CGDebugInfo::EmitRegionEnd(CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001677 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1678
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001679 // Provide an region stop point.
Devang Patel4d939e62010-07-20 22:20:10 +00001680 EmitStopPoint(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001681
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001682 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001683}
1684
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001685/// EmitFunctionEnd - Constructs the debug code for exiting a function.
1686void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
1687 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1688 unsigned RCount = FnBeginRegionCount.back();
1689 assert(RCount <= RegionStack.size() && "Region stack mismatch");
1690
1691 // Pop all regions for this function.
1692 while (RegionStack.size() != RCount)
1693 EmitRegionEnd(Builder);
1694 FnBeginRegionCount.pop_back();
1695}
1696
Devang Patel809b9bb2010-02-10 18:49:08 +00001697// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1698// See BuildByRefType.
1699llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1700 uint64_t *XOffset) {
1701
1702 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1703
1704 QualType FType;
1705 uint64_t FieldSize, FieldOffset;
1706 unsigned FieldAlign;
1707
Devang Patel17800552010-03-09 00:44:50 +00001708 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001709 QualType Type = VD->getType();
1710
1711 FieldOffset = 0;
1712 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001713 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1714 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001715 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001716 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1717 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1718
Devang Patel809b9bb2010-02-10 18:49:08 +00001719 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1720 if (HasCopyAndDispose) {
1721 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001722 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1723 &FieldOffset));
1724 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1725 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001726 }
1727
1728 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1729 if (Align > CharUnits::fromQuantity(
1730 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1731 unsigned AlignedOffsetInBytes
1732 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1733 unsigned NumPaddingBytes
1734 = AlignedOffsetInBytes - FieldOffset/8;
1735
1736 if (NumPaddingBytes > 0) {
1737 llvm::APInt pad(32, NumPaddingBytes);
1738 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1739 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001740 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001741 }
1742 }
1743
1744 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001745 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00001746 FieldSize = CGM.getContext().getTypeSize(FType);
1747 FieldAlign = Align.getQuantity()*8;
1748
1749 *XOffset = FieldOffset;
1750 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001751 VD->getName(), Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001752 0, FieldSize, FieldAlign,
1753 FieldOffset, 0, FieldTy);
1754 EltTys.push_back(FieldTy);
1755 FieldOffset += FieldSize;
1756
1757 llvm::DIArray Elements =
1758 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1759
Devang Patele2472482010-09-29 21:05:52 +00001760 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Devang Patel809b9bb2010-02-10 18:49:08 +00001761
1762 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001763 Unit, "", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001764 0, FieldOffset, 0, 0, Flags,
1765 llvm::DIType(), Elements);
1766
1767}
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001768/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001769void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001770 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001771 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1772
Devang Patel17800552010-03-09 00:44:50 +00001773 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001774 llvm::DIType Ty;
1775 uint64_t XOffset = 0;
1776 if (VD->hasAttr<BlocksAttr>())
1777 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1778 else
1779 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001780
Devang Patelf4e54a22010-05-07 23:05:55 +00001781 // If there is not any debug info for type then do not emit debug info
1782 // for this variable.
1783 if (!Ty)
1784 return;
1785
Chris Lattner9c85ba32008-11-10 06:08:34 +00001786 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001787 unsigned Line = getLineNumber(VD->getLocation());
1788 unsigned Column = getColumnNumber(VD->getLocation());
Devang Patelaca745b2010-09-29 23:09:21 +00001789 unsigned Flags = 0;
1790 if (VD->isImplicit())
1791 Flags |= llvm::DIDescriptor::FlagArtificial;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001792 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001793 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001794 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patelaca745b2010-09-29 23:09:21 +00001795 VD->getName(), Unit, Line, Ty,
1796 CGM.getLangOptions().Optimize, Flags);
1797
Chris Lattner9c85ba32008-11-10 06:08:34 +00001798 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001799 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001800 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001801
Chris Lattnerc6034632010-04-01 06:31:43 +00001802 llvm::MDNode *Scope = RegionStack.back();
Chris Lattnere541d012010-04-02 20:21:43 +00001803 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001804}
1805
Mike Stumpb1a6e682009-09-30 02:43:10 +00001806/// EmitDeclare - Emit local variable declaration debug info.
1807void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1808 llvm::Value *Storage, CGBuilderTy &Builder,
1809 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001810 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001811 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1812
Devang Patel2b594b92010-04-26 23:28:46 +00001813 if (Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001814 return;
1815
1816 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001817 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001818 llvm::DIType Ty;
1819 if (VD->hasAttr<BlocksAttr>())
1820 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1821 else
1822 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001823
1824 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001825 unsigned Line = getLineNumber(VD->getLocation());
1826 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001827
Devang Pateld6c5a262010-02-01 21:52:22 +00001828 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001829 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001830 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1831 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1832 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1833 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001834 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001835 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1836 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001837 // offset of __forwarding field
1838 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001839 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1840 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1841 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001842 // offset of x field
1843 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001844 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001845 }
1846
1847 // Create the descriptor for the variable.
1848 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001849 DebugFactory.CreateComplexVariable(Tag,
1850 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001851 VD->getName(), Unit, Line, Ty,
Benjamin Kramer3475cfe2010-09-21 15:59:59 +00001852 addr.data(), addr.size());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001853 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001854 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001855 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattnerd5b89022009-12-28 21:44:41 +00001856
Chris Lattnerc6034632010-04-01 06:31:43 +00001857 llvm::MDNode *Scope = RegionStack.back();
Devang Patelf8e10a52010-05-10 23:48:38 +00001858 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001859}
1860
Devang Pateld6c5a262010-02-01 21:52:22 +00001861void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001862 llvm::Value *Storage,
1863 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001864 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001865}
1866
Mike Stumpb1a6e682009-09-30 02:43:10 +00001867void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1868 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1869 CodeGenFunction *CGF) {
1870 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1871}
1872
Chris Lattner9c85ba32008-11-10 06:08:34 +00001873/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1874/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001875void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001876 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001877 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001878}
1879
1880
1881
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001882/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001883void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001884 const VarDecl *D) {
1885
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001886 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001887 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001888 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +00001889
Devang Pateleb6d79b2010-02-01 21:34:11 +00001890 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001891 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001892
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001893 // CodeGen turns int[] into int[1] so we'll do the same here.
1894 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001895
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001896 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001897 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001898
Anders Carlsson20f12a22009-12-06 18:00:51 +00001899 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001900 ArrayType::Normal, 0);
1901 }
Devang Patel5d822f02010-04-29 17:48:37 +00001902 llvm::StringRef DeclName = D->getName();
Devang Patel8b90a782010-05-13 23:52:37 +00001903 llvm::StringRef LinkageName;
Devang Patel0fd3d1f2010-05-14 16:55:25 +00001904 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()))
Devang Patel8b90a782010-05-13 23:52:37 +00001905 LinkageName = Var->getName();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001906 llvm::DIDescriptor DContext =
1907 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
Devang Patel8b90a782010-05-13 23:52:37 +00001908 DebugFactory.CreateGlobalVariable(DContext, DeclName, DeclName, LinkageName,
1909 Unit, LineNo, getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001910 Var->hasInternalLinkage(),
1911 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001912}
1913
Devang Patel9ca36b62009-02-26 21:10:26 +00001914/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001915void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001916 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001917 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001918 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001919 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patel9ca36b62009-02-26 21:10:26 +00001920
Devang Pateld6c5a262010-02-01 21:52:22 +00001921 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001922
Devang Pateld6c5a262010-02-01 21:52:22 +00001923 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001924 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001925
Devang Patel9ca36b62009-02-26 21:10:26 +00001926 // CodeGen turns int[] into int[1] so we'll do the same here.
1927 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001928
Devang Patel9ca36b62009-02-26 21:10:26 +00001929 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001930 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001931
Anders Carlsson20f12a22009-12-06 18:00:51 +00001932 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001933 ArrayType::Normal, 0);
1934 }
1935
Devang Patelf6a39b72009-10-20 18:26:30 +00001936 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001937 getOrCreateType(T, Unit),
1938 Var->hasInternalLinkage(),
1939 true/*definition*/, Var);
1940}
Devang Patelabb485f2010-02-01 19:16:32 +00001941
Devang Patel25c2c8f2010-08-10 17:53:33 +00001942/// EmitGlobalVariable - Emit global variable's debug info.
1943void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
Devang Pateld2829b72010-10-06 15:58:57 +00001944 llvm::ConstantInt *Init) {
Devang Patel8d308382010-08-10 07:24:25 +00001945 // Create the descriptor for the variable.
1946 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
1947 llvm::StringRef Name = VD->getName();
Devang Patel0317ab02010-08-10 18:27:15 +00001948 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
Devang Patel6237cea2010-08-23 22:07:25 +00001949 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
1950 if (const EnumDecl *ED = dyn_cast<EnumDecl>(ECD->getDeclContext()))
1951 Ty = CreateEnumType(ED, Unit);
1952 }
Devang Patel0317ab02010-08-10 18:27:15 +00001953 // Do not use DIGlobalVariable for enums.
1954 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
1955 return;
Devang Patel8d308382010-08-10 07:24:25 +00001956 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit,
1957 getLineNumber(VD->getLocation()),
Devang Patel4c4acc02010-08-10 20:16:57 +00001958 Ty, true, true, Init);
Devang Patel8d308382010-08-10 07:24:25 +00001959}
1960
Devang Patelabb485f2010-02-01 19:16:32 +00001961/// getOrCreateNamesSpace - Return namespace descriptor for the given
1962/// namespace decl.
1963llvm::DINameSpace
1964CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1965 llvm::DIDescriptor Unit) {
1966 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1967 NameSpaceCache.find(NSDecl);
1968 if (I != NameSpaceCache.end())
1969 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1970
Devang Patel8ab870d2010-05-12 23:46:38 +00001971 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patelabb485f2010-02-01 19:16:32 +00001972
1973 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00001974 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00001975 llvm::DINameSpace NS =
1976 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
Devang Patel2ed8f002010-08-27 17:47:47 +00001977 llvm::DIFile(Unit), LineNo);
Devang Patelab699792010-05-07 18:12:35 +00001978 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00001979 return NS;
1980}