blob: e8c1ae754d23a3bc885f0140aa2eda7c5a831f02 [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();
Fariborz Jahanian1a4c9372010-10-18 17:51:06 +0000105 } else if (const ObjCInterfaceDecl *OID = dyn_cast<const ObjCInterfaceDecl>(DC)) {
106 OS << OID->getName();
David Chisnall52044a22010-09-02 18:01:51 +0000107 } else if (const ObjCCategoryImplDecl *OCD = dyn_cast<const ObjCCategoryImplDecl>(DC)){
108 OS << ((NamedDecl *)OCD)->getIdentifier()->getNameStart() << '(' <<
109 OCD->getIdentifier()->getNameStart() << ')';
110 }
111 OS << ' ' << OMD->getSelector().getAsString() << ']';
112
113 char *StrPtr = DebugInfoNames.Allocate<char>(OS.tell());
114 memcpy(StrPtr, MethodName.begin(), OS.tell());
115 return llvm::StringRef(StrPtr, OS.tell());
116}
117
Devang Patel700a1cb2010-07-20 20:24:18 +0000118/// getClassName - Get class name including template argument list.
119llvm::StringRef
120CGDebugInfo::getClassName(RecordDecl *RD) {
121 ClassTemplateSpecializationDecl *Spec
122 = dyn_cast<ClassTemplateSpecializationDecl>(RD);
123 if (!Spec)
124 return RD->getName();
125
126 const TemplateArgument *Args;
127 unsigned NumArgs;
128 std::string Buffer;
129 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
130 const TemplateSpecializationType *TST =
131 cast<TemplateSpecializationType>(TAW->getType());
132 Args = TST->getArgs();
133 NumArgs = TST->getNumArgs();
134 } else {
135 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
136 Args = TemplateArgs.getFlatArgumentList();
137 NumArgs = TemplateArgs.flat_size();
138 }
139 Buffer = RD->getIdentifier()->getNameStart();
140 PrintingPolicy Policy(CGM.getLangOptions());
141 Buffer += TemplateSpecializationType::PrintTemplateArgumentList(Args,
142 NumArgs,
143 Policy);
144
145 // Copy this name on the side and use its reference.
146 char *StrPtr = DebugInfoNames.Allocate<char>(Buffer.length());
147 memcpy(StrPtr, Buffer.data(), Buffer.length());
148 return llvm::StringRef(StrPtr, Buffer.length());
Devang Patel700a1cb2010-07-20 20:24:18 +0000149}
150
Devang Patel17800552010-03-09 00:44:50 +0000151/// getOrCreateFile - Get the file debug info descriptor for the input location.
152llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Ted Kremenek9c250392010-03-30 00:27:51 +0000153 if (!Loc.isValid())
Devang Patel17800552010-03-09 00:44:50 +0000154 // If Location is not valid then use main input file.
155 return DebugFactory.CreateFile(TheCU.getFilename(), TheCU.getDirectory(),
156 TheCU);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000157 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel17800552010-03-09 00:44:50 +0000158 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Ted Kremenek9c250392010-03-30 00:27:51 +0000159
160 // Cache the results.
161 const char *fname = PLoc.getFilename();
162 llvm::DenseMap<const char *, llvm::WeakVH>::iterator it =
163 DIFileCache.find(fname);
164
165 if (it != DIFileCache.end()) {
166 // Verify that the information still exists.
167 if (&*it->second)
168 return llvm::DIFile(cast<llvm::MDNode>(it->second));
169 }
170
Devang Patelac4d13c2010-07-27 15:17:16 +0000171 llvm::DIFile F = DebugFactory.CreateFile(PLoc.getFilename(),
172 getCurrentDirname(), TheCU);
Ted Kremenek9c250392010-03-30 00:27:51 +0000173
Devang Patelab699792010-05-07 18:12:35 +0000174 DIFileCache[fname] = F;
Ted Kremenek9c250392010-03-30 00:27:51 +0000175 return F;
176
Devang Patel17800552010-03-09 00:44:50 +0000177}
Devang Patel8ab870d2010-05-12 23:46:38 +0000178
179/// getLineNumber - Get line number for the location. If location is invalid
180/// then use current location.
181unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
182 assert (CurLoc.isValid() && "Invalid current location!");
183 SourceManager &SM = CGM.getContext().getSourceManager();
184 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
185 return PLoc.getLine();
186}
187
188/// getColumnNumber - Get column number for the location. If location is
189/// invalid then use current location.
190unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) {
191 assert (CurLoc.isValid() && "Invalid current location!");
192 SourceManager &SM = CGM.getContext().getSourceManager();
193 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
194 return PLoc.getColumn();
195}
196
Devang Patelac4d13c2010-07-27 15:17:16 +0000197llvm::StringRef CGDebugInfo::getCurrentDirname() {
198 if (!CWDName.empty())
199 return CWDName;
200 char *CompDirnamePtr = NULL;
201 llvm::sys::Path CWD = llvm::sys::Path::GetCurrentDirectory();
202 CompDirnamePtr = DebugInfoNames.Allocate<char>(CWD.size());
203 memcpy(CompDirnamePtr, CWD.c_str(), CWD.size());
204 return CWDName = llvm::StringRef(CompDirnamePtr, CWD.size());
205}
206
Devang Patel17800552010-03-09 00:44:50 +0000207/// CreateCompileUnit - Create new compile unit.
208void CGDebugInfo::CreateCompileUnit() {
209
210 // Get absolute path name.
Douglas Gregorac91b4c2010-03-18 23:46:43 +0000211 SourceManager &SM = CGM.getContext().getSourceManager();
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000212 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
213 if (MainFileName.empty())
Devang Patel22fe5852010-03-12 21:04:27 +0000214 MainFileName = "<unknown>";
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000215
Douglas Gregorf6728fc2010-03-22 21:28:29 +0000216 // The main file name provided via the "-main-file-name" option contains just
217 // the file name itself with no path information. This file name may have had
218 // a relative path, so we look into the actual file entry for the main
219 // file to determine the real absolute path for the file.
Devang Patel6e6bc392010-07-23 23:04:28 +0000220 std::string MainFileDir;
Devang Patelac4d13c2010-07-27 15:17:16 +0000221 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000222 MainFileDir = MainFile->getDir()->getName();
Devang Patelac4d13c2010-07-27 15:17:16 +0000223 if (MainFileDir != ".")
224 MainFileName = MainFileDir + "/" + MainFileName;
225 }
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000226
Devang Patelac4d13c2010-07-27 15:17:16 +0000227 // Save filename string.
228 char *FilenamePtr = DebugInfoNames.Allocate<char>(MainFileName.length());
229 memcpy(FilenamePtr, MainFileName.c_str(), MainFileName.length());
230 llvm::StringRef Filename(FilenamePtr, MainFileName.length());
231
Chris Lattner515455a2009-03-25 03:28:08 +0000232 unsigned LangTag;
Devang Patel17800552010-03-09 00:44:50 +0000233 const LangOptions &LO = CGM.getLangOptions();
Chris Lattner515455a2009-03-25 03:28:08 +0000234 if (LO.CPlusPlus) {
235 if (LO.ObjC1)
236 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
237 else
238 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
239 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000240 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000241 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000242 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000243 } else {
244 LangTag = llvm::dwarf::DW_LANG_C89;
245 }
Devang Patel446c6192009-04-17 21:06:59 +0000246
Daniel Dunbar19f19832010-08-24 17:41:09 +0000247 std::string Producer = getClangFullVersion();
Chris Lattner4c2577a2009-05-02 01:00:04 +0000248
249 // Figure out which version of the ObjC runtime we have.
250 unsigned RuntimeVers = 0;
251 if (LO.ObjC1)
252 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000253
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000254 // Create new compile unit.
Devang Patel17800552010-03-09 00:44:50 +0000255 TheCU = DebugFactory.CreateCompileUnit(
Devang Patel58115002010-07-27 20:49:59 +0000256 LangTag, Filename, getCurrentDirname(),
Devang Patelac4d13c2010-07-27 15:17:16 +0000257 Producer, true,
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000258 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000259}
260
Devang Patel65e99f22009-02-25 01:36:11 +0000261/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000262/// one if necessary.
263llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel17800552010-03-09 00:44:50 +0000264 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000265 unsigned Encoding = 0;
Devang Patel05127ca2010-07-28 23:23:29 +0000266 const char *BTName = NULL;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000267 switch (BT->getKind()) {
268 default:
269 case BuiltinType::Void:
270 return llvm::DIType();
Devang Patelc8972c62010-07-28 01:33:15 +0000271 case BuiltinType::ObjCClass:
Devang Pateleaf5ee92010-07-21 22:41:25 +0000272 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Patelc8972c62010-07-28 01:33:15 +0000273 Unit, "objc_class", Unit, 0, 0, 0, 0,
Devang Patele2472482010-09-29 21:05:52 +0000274 llvm::DIDescriptor::FlagFwdDecl,
Devang Pateleaf5ee92010-07-21 22:41:25 +0000275 llvm::DIType(), llvm::DIArray());
Devang Patelc8972c62010-07-28 01:33:15 +0000276 case BuiltinType::ObjCId: {
277 // typedef struct objc_class *Class;
278 // typedef struct objc_object {
279 // Class isa;
280 // } *id;
281
282 llvm::DIType OCTy =
283 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
284 Unit, "objc_class", Unit, 0, 0, 0, 0,
Devang Patele2472482010-09-29 21:05:52 +0000285 llvm::DIDescriptor::FlagFwdDecl,
Devang Patelc8972c62010-07-28 01:33:15 +0000286 llvm::DIType(), llvm::DIArray());
287 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
288
289 llvm::DIType ISATy =
290 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
291 Unit, "", Unit,
292 0, Size, 0, 0, 0, OCTy);
293
294 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
295
296 llvm::DIType FieldTy =
297 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
298 "isa", Unit,
299 0,Size, 0, 0, 0, ISATy);
300 EltTys.push_back(FieldTy);
301 llvm::DIArray Elements =
302 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
303
304 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
305 Unit, "objc_object", Unit, 0, 0, 0, 0,
306 0,
307 llvm::DIType(), Elements);
308 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000309 case BuiltinType::UChar:
310 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
311 case BuiltinType::Char_S:
312 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
313 case BuiltinType::UShort:
314 case BuiltinType::UInt:
315 case BuiltinType::ULong:
316 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
317 case BuiltinType::Short:
318 case BuiltinType::Int:
319 case BuiltinType::Long:
320 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
321 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
322 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000323 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000324 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000325 }
Devang Patel05127ca2010-07-28 23:23:29 +0000326
327 switch (BT->getKind()) {
328 case BuiltinType::Long: BTName = "long int"; break;
329 case BuiltinType::LongLong: BTName = "long long int"; break;
330 case BuiltinType::ULong: BTName = "long unsigned int"; break;
331 case BuiltinType::ULongLong: BTName = "long long unsigned int"; break;
332 default:
333 BTName = BT->getName(CGM.getContext().getLangOptions());
334 break;
335 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000336 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000337 uint64_t Size = CGM.getContext().getTypeSize(BT);
338 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000339 uint64_t Offset = 0;
Devang Patel05127ca2010-07-28 23:23:29 +0000340
Devang Patelca80a5f2009-10-20 19:55:01 +0000341 llvm::DIType DbgTy =
Devang Patel05127ca2010-07-28 23:23:29 +0000342 DebugFactory.CreateBasicType(Unit, BTName,
Devang Patelca80a5f2009-10-20 19:55:01 +0000343 Unit, 0, Size, Align,
344 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000345 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000346}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000347
Chris Lattnerb7003772009-04-23 06:13:01 +0000348llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000349 llvm::DIFile Unit) {
Chris Lattnerb7003772009-04-23 06:13:01 +0000350 // Bit size, align and offset of the type.
351 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
352 if (Ty->isComplexIntegerType())
353 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Anders Carlsson20f12a22009-12-06 18:00:51 +0000355 uint64_t Size = CGM.getContext().getTypeSize(Ty);
356 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattnerb7003772009-04-23 06:13:01 +0000357 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Devang Patelca80a5f2009-10-20 19:55:01 +0000359 llvm::DIType DbgTy =
360 DebugFactory.CreateBasicType(Unit, "complex",
361 Unit, 0, Size, Align,
362 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000363 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000364}
365
John McCalla1805292009-09-25 01:40:47 +0000366/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000367/// a new one if necessary.
Devang Patel17800552010-03-09 00:44:50 +0000368llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +0000369 QualifierCollector Qc;
370 const Type *T = Qc.strip(Ty);
371
372 // Ignore these qualifiers for now.
373 Qc.removeObjCGCAttr();
374 Qc.removeAddressSpace();
375
Chris Lattner9c85ba32008-11-10 06:08:34 +0000376 // We will create one Derived type for one qualifier and recurse to handle any
377 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000378 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000379 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000380 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000381 Qc.removeConst();
382 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000383 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000384 Qc.removeVolatile();
385 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000386 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000387 Qc.removeRestrict();
388 } else {
389 assert(Qc.empty() && "Unknown type qualifier for debug info");
390 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000391 }
Mike Stump1eb44332009-09-09 15:08:12 +0000392
John McCalla1805292009-09-25 01:40:47 +0000393 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
394
Daniel Dunbar3845f862008-10-31 03:54:29 +0000395 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
396 // CVR derived types.
Devang Patelca80a5f2009-10-20 19:55:01 +0000397 llvm::DIType DbgTy =
Devang Pateld58562e2010-03-09 22:49:11 +0000398 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000399 0, 0, 0, 0, 0, FromTy);
Devang Patelca80a5f2009-10-20 19:55:01 +0000400 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000401}
402
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000403llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000404 llvm::DIFile Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000405 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000406 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
407 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000408 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000409}
410
Chris Lattner9c85ba32008-11-10 06:08:34 +0000411llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000412 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000413 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
414 Ty->getPointeeType(), Unit);
415}
416
Devang Patelc69e1cf2010-09-30 19:05:55 +0000417/// CreatePointeeType - Create PointTee type. If Pointee is a record
418/// then emit record's fwd if debug info size reduction is enabled.
419llvm::DIType CGDebugInfo::CreatePointeeType(QualType PointeeTy,
420 llvm::DIFile Unit) {
421 if (!CGM.getCodeGenOpts().LimitDebugInfo)
422 return getOrCreateType(PointeeTy, Unit);
423
424 if (const RecordType *RTy = dyn_cast<RecordType>(PointeeTy)) {
425 RecordDecl *RD = RTy->getDecl();
426 unsigned RTag;
427 if (RD->isStruct())
428 RTag = llvm::dwarf::DW_TAG_structure_type;
429 else if (RD->isUnion())
430 RTag = llvm::dwarf::DW_TAG_union_type;
431 else {
432 assert(RD->isClass() && "Unknown RecordType!");
433 RTag = llvm::dwarf::DW_TAG_class_type;
434 }
435
436 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
437 unsigned Line = getLineNumber(RD->getLocation());
438 llvm::DIDescriptor FDContext =
439 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
440
441 return
442 DebugFactory.CreateCompositeType(RTag, FDContext, RD->getName(),
443 DefUnit, Line, 0, 0, 0,
444 llvm::DIType::FlagFwdDecl,
445 llvm::DIType(), llvm::DIArray());
446 }
447 return getOrCreateType(PointeeTy, Unit);
448
449}
450
Anders Carlssona031b352009-11-06 19:19:55 +0000451llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
452 const Type *Ty,
453 QualType PointeeTy,
Devang Patel17800552010-03-09 00:44:50 +0000454 llvm::DIFile Unit) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000455 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000456
457 // Size is always the size of a pointer. We can't use getTypeSize here
458 // because that does not return the correct value for references.
459 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000460 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
461 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Devang Patelc69e1cf2010-09-30 19:05:55 +0000463 return DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
464 0, Size, Align, 0, 0,
465 CreatePointeeType(PointeeTy, Unit));
466
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000467}
468
Mike Stump9bc093c2009-05-14 02:03:51 +0000469llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000470 llvm::DIFile Unit) {
Mike Stump9bc093c2009-05-14 02:03:51 +0000471 if (BlockLiteralGenericSet)
472 return BlockLiteralGeneric;
473
Mike Stump9bc093c2009-05-14 02:03:51 +0000474 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
475
476 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
477
478 llvm::DIType FieldTy;
479
480 QualType FType;
481 uint64_t FieldSize, FieldOffset;
482 unsigned FieldAlign;
483
484 llvm::DIArray Elements;
485 llvm::DIType EltTy, DescTy;
486
487 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000488 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000489 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
490 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000491
Daniel Dunbarca308df2009-05-26 19:40:20 +0000492 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000493 EltTys.clear();
494
Devang Patele2472482010-09-29 21:05:52 +0000495 unsigned Flags = llvm::DIDescriptor::FlagAppleBlock;
Devang Patel8ab870d2010-05-12 23:46:38 +0000496 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump3d363c52009-10-02 02:30:50 +0000497
Mike Stump9bc093c2009-05-14 02:03:51 +0000498 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Devang Patel8ab870d2010-05-12 23:46:38 +0000499 Unit, LineNo, FieldOffset, 0, 0,
500 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Mike Stump9bc093c2009-05-14 02:03:51 +0000502 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000503 uint64_t Size = CGM.getContext().getTypeSize(Ty);
504 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Mike Stump9bc093c2009-05-14 02:03:51 +0000506 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000507 Unit, "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000508 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000509
510 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000511 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000512 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson20f12a22009-12-06 18:00:51 +0000513 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000514 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
515 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramerd3651cc2010-04-24 20:26:20 +0000516 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000517 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000518
Anders Carlsson20f12a22009-12-06 18:00:51 +0000519 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000520 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000521 FieldSize = CGM.getContext().getTypeSize(Ty);
522 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000523 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000524 "__descriptor", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000525 LineNo, FieldSize, FieldAlign,
Mike Stump9bc093c2009-05-14 02:03:51 +0000526 FieldOffset, 0, FieldTy);
527 EltTys.push_back(FieldTy);
528
529 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000530 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000531
532 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Devang Patel8ab870d2010-05-12 23:46:38 +0000533 Unit, LineNo, FieldOffset, 0, 0,
534 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Mike Stump9bc093c2009-05-14 02:03:51 +0000536 BlockLiteralGenericSet = true;
537 BlockLiteralGeneric
538 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000539 "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000540 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000541 return BlockLiteralGeneric;
542}
543
Chris Lattner9c85ba32008-11-10 06:08:34 +0000544llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000545 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000546 // Typedefs are derived from some other type. If we have a typedef of a
547 // typedef, make sure to emit the whole chain.
548 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Chris Lattner9c85ba32008-11-10 06:08:34 +0000550 // We don't set size information, but do specify where the typedef was
551 // declared.
Devang Patel8ab870d2010-05-12 23:46:38 +0000552 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000553
Devang Pateleb6d79b2010-02-01 21:34:11 +0000554 llvm::DIDescriptor TyContext
555 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
556 Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000557 llvm::DIType DbgTy =
Devang Pateld5289052010-01-29 22:29:31 +0000558 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Pateleb6d79b2010-02-01 21:34:11 +0000559 TyContext,
Devang Pateld5289052010-01-29 22:29:31 +0000560 Ty->getDecl()->getName(), Unit,
561 Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000562 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000563}
564
Chris Lattner9c85ba32008-11-10 06:08:34 +0000565llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000566 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000567 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000568
Chris Lattner9c85ba32008-11-10 06:08:34 +0000569 // Add the result type at least.
570 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000571
Chris Lattner9c85ba32008-11-10 06:08:34 +0000572 // Set up remainder of arguments if there is a prototype.
573 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Devang Patelaf164bb2010-10-06 20:51:45 +0000574 if (isa<FunctionNoProtoType>(Ty))
575 EltTys.push_back(DebugFactory.CreateUnspecifiedParameter());
576 else if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000577 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
578 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000579 }
580
Chris Lattner9c85ba32008-11-10 06:08:34 +0000581 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000582 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000583
Devang Patelca80a5f2009-10-20 19:55:01 +0000584 llvm::DIType DbgTy =
585 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000586 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000587 0, 0, 0, 0, 0,
588 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000589 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000590}
591
Devang Patel428deb52010-01-19 00:00:59 +0000592/// CollectRecordFields - A helper function to collect debug info for
593/// record fields. This is used while creating debug info entry for a Record.
594void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000595CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit,
Devang Patel428deb52010-01-19 00:00:59 +0000596 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
597 unsigned FieldNo = 0;
Devang Patel239cec62010-02-01 21:39:52 +0000598 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
599 for (RecordDecl::field_iterator I = RD->field_begin(),
600 E = RD->field_end();
Devang Patel428deb52010-01-19 00:00:59 +0000601 I != E; ++I, ++FieldNo) {
602 FieldDecl *Field = *I;
603 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Devang Patel428deb52010-01-19 00:00:59 +0000604 llvm::StringRef FieldName = Field->getName();
605
Devang Patel4835fdd2010-02-12 01:31:06 +0000606 // Ignore unnamed fields. Do not ignore unnamed records.
607 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel428deb52010-01-19 00:00:59 +0000608 continue;
609
610 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +0000611 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
612 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel428deb52010-01-19 00:00:59 +0000613 QualType FType = Field->getType();
614 uint64_t FieldSize = 0;
615 unsigned FieldAlign = 0;
616 if (!FType->isIncompleteArrayType()) {
617
618 // Bit size, align and offset of the type.
619 FieldSize = CGM.getContext().getTypeSize(FType);
620 Expr *BitWidth = Field->getBitWidth();
621 if (BitWidth)
622 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Devang Patel428deb52010-01-19 00:00:59 +0000623 FieldAlign = CGM.getContext().getTypeAlign(FType);
624 }
625
626 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
627
Devang Patel71f4ff62010-04-21 23:12:37 +0000628 unsigned Flags = 0;
629 AccessSpecifier Access = I->getAccess();
630 if (Access == clang::AS_private)
Devang Patele2472482010-09-29 21:05:52 +0000631 Flags |= llvm::DIDescriptor::FlagPrivate;
Devang Patel71f4ff62010-04-21 23:12:37 +0000632 else if (Access == clang::AS_protected)
Devang Patele2472482010-09-29 21:05:52 +0000633 Flags |= llvm::DIDescriptor::FlagProtected;
Devang Patel71f4ff62010-04-21 23:12:37 +0000634
Devang Patel428deb52010-01-19 00:00:59 +0000635 // Create a DW_TAG_member node to remember the offset of this field in the
636 // struct. FIXME: This is an absolutely insane way to capture this
637 // information. When we gut debug info, this should be fixed.
638 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
639 FieldName, FieldDefUnit,
640 FieldLine, FieldSize, FieldAlign,
Devang Patel71f4ff62010-04-21 23:12:37 +0000641 FieldOffset, Flags, FieldTy);
Devang Patel428deb52010-01-19 00:00:59 +0000642 EltTys.push_back(FieldTy);
643 }
644}
645
Devang Patela6da1922010-01-28 00:28:01 +0000646/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
647/// function type is not updated to include implicit "this" pointer. Use this
648/// routine to get a method type which includes "this" pointer.
649llvm::DIType
650CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000651 llvm::DIFile Unit) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +0000652 llvm::DIType FnTy
653 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
654 0),
655 Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000656
Devang Patela6da1922010-01-28 00:28:01 +0000657 // Add "this" pointer.
658
Devang Patelab699792010-05-07 18:12:35 +0000659 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patela6da1922010-01-28 00:28:01 +0000660 assert (Args.getNumElements() && "Invalid number of arguments!");
661
662 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
663
664 // First element is always return type. For 'void' functions it is NULL.
665 Elts.push_back(Args.getElement(0));
666
Devang Patel2ed8f002010-08-27 17:47:47 +0000667 if (!Method->isStatic())
668 {
669 // "this" pointer is always first argument.
670 ASTContext &Context = CGM.getContext();
671 QualType ThisPtr =
672 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
673 llvm::DIType ThisPtrType =
674 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
Devang Patel769640e2010-07-13 00:24:30 +0000675
Devang Patel2ed8f002010-08-27 17:47:47 +0000676 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
677 Elts.push_back(ThisPtrType);
678 }
Devang Patela6da1922010-01-28 00:28:01 +0000679
680 // Copy rest of the arguments.
681 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
682 Elts.push_back(Args.getElement(i));
683
684 llvm::DIArray EltTypeArray =
685 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
686
687 return
688 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000689 Unit, "", Unit,
Devang Patela6da1922010-01-28 00:28:01 +0000690 0, 0, 0, 0, 0,
691 llvm::DIType(), EltTypeArray);
692}
693
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000694/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
695/// a single member function GlobalDecl.
696llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000697CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000698 llvm::DIFile Unit,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000699 llvm::DIType RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000700 bool IsCtorOrDtor =
701 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
702
703 llvm::StringRef MethodName = getFunctionName(Method);
Devang Patela6da1922010-01-28 00:28:01 +0000704 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000705
706 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
707 // make sense to give a single ctor/dtor a linkage name.
Anders Carlsson9a20d552010-06-22 16:16:50 +0000708 llvm::StringRef MethodLinkageName;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000709 if (!IsCtorOrDtor)
Anders Carlsson9a20d552010-06-22 16:16:50 +0000710 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000711
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000712 // Get the location for the method.
Devang Patel8ab870d2010-05-12 23:46:38 +0000713 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
714 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000715
716 // Collect virtual method info.
717 llvm::DIType ContainingType;
718 unsigned Virtuality = 0;
719 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000720
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000721 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000722 if (Method->isPure())
723 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
724 else
725 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
726
727 // It doesn't make sense to give a virtual destructor a vtable index,
728 // since a single destructor has two entries in the vtable.
729 if (!isa<CXXDestructorDecl>(Method))
Anders Carlsson046c2942010-04-17 20:15:18 +0000730 VIndex = CGM.getVTables().getMethodVTableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000731 ContainingType = RecordTy;
732 }
733
Devang Patele2472482010-09-29 21:05:52 +0000734 unsigned Flags = 0;
735 if (Method->isImplicit())
736 Flags |= llvm::DIDescriptor::FlagArtificial;
Devang Patel10a7a6a2010-09-29 21:46:16 +0000737 AccessSpecifier Access = Method->getAccess();
738 if (Access == clang::AS_private)
739 Flags |= llvm::DIDescriptor::FlagPrivate;
740 else if (Access == clang::AS_protected)
741 Flags |= llvm::DIDescriptor::FlagProtected;
Devang Pateld78a0192010-10-01 23:32:17 +0000742 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
743 if (CXXC->isExplicit())
744 Flags |= llvm::DIDescriptor::FlagExplicit;
745 } else if (const CXXConversionDecl *CXXC =
746 dyn_cast<CXXConversionDecl>(Method)) {
747 if (CXXC->isExplicit())
748 Flags |= llvm::DIDescriptor::FlagExplicit;
749 }
Devang Patel3951e712010-10-07 22:03:49 +0000750 if (Method->hasPrototype())
751 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Pateld78a0192010-10-01 23:32:17 +0000752
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000753 llvm::DISubprogram SP =
754 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
755 MethodLinkageName,
756 MethodDefUnit, MethodLine,
757 MethodTy, /*isLocalToUnit=*/false,
Devang Patela5c5bab2010-07-12 22:54:41 +0000758 /* isDefintion=*/ false,
Devang Patel40894912010-07-15 22:57:00 +0000759 Virtuality, VIndex, ContainingType,
Devang Patele2472482010-09-29 21:05:52 +0000760 Flags,
Devang Patel15a3d7d2010-07-15 23:09:46 +0000761 CGM.getLangOptions().Optimize);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000762
763 // Don't cache ctors or dtors since we have to emit multiple functions for
764 // a single ctor or dtor.
765 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
Devang Patelab699792010-05-07 18:12:35 +0000766 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000767
768 return SP;
769}
770
Devang Patel4125fd22010-01-19 01:54:44 +0000771/// CollectCXXMemberFunctions - A helper function to collect debug info for
772/// C++ member functions.This is used while creating debug info entry for
773/// a Record.
774void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000775CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000776 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000777 llvm::DIType RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000778 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
779 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000780 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000781
Devang Pateld5322da2010-02-09 19:09:28 +0000782 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000783 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000784
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000785 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000786 }
787}
788
Devang Patel2ed8f002010-08-27 17:47:47 +0000789/// CollectCXXFriends - A helper function to collect debug info for
790/// C++ base classes. This is used while creating debug info entry for
791/// a Record.
792void CGDebugInfo::
793CollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit,
794 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
795 llvm::DIType RecordTy) {
796
797 for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(),
798 BE = RD->friend_end(); BI != BE; ++BI) {
799
800 TypeSourceInfo *TInfo = (*BI)->getFriendType();
801 if(TInfo)
802 {
803 llvm::DIType Ty = getOrCreateType(TInfo->getType(), Unit);
804
805 llvm::DIType DTy =
806 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_friend,
807 RecordTy, llvm::StringRef(),
808 Unit, 0, 0, 0,
809 0, 0, Ty);
810
811 EltTys.push_back(DTy);
812 }
813
814 }
815}
816
Devang Patela245c5b2010-01-25 23:32:18 +0000817/// CollectCXXBases - A helper function to collect debug info for
818/// C++ base classes. This is used while creating debug info entry for
819/// a Record.
820void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000821CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000822 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000823 llvm::DIType RecordTy) {
Devang Patela245c5b2010-01-25 23:32:18 +0000824
Devang Patel239cec62010-02-01 21:39:52 +0000825 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
826 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
827 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000828 unsigned BFlags = 0;
829 uint64_t BaseOffset;
830
831 const CXXRecordDecl *Base =
832 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
833
834 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000835 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000836 // expression where it expects +ve number.
Anders Carlssonaf440352010-03-23 04:11:45 +0000837 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base);
Devang Patele2472482010-09-29 21:05:52 +0000838 BFlags = llvm::DIDescriptor::FlagVirtual;
Devang Patelca7daed2010-01-28 21:54:15 +0000839 } else
840 BaseOffset = RL.getBaseClassOffset(Base);
841
842 AccessSpecifier Access = BI->getAccessSpecifier();
843 if (Access == clang::AS_private)
Devang Patele2472482010-09-29 21:05:52 +0000844 BFlags |= llvm::DIDescriptor::FlagPrivate;
Devang Patelca7daed2010-01-28 21:54:15 +0000845 else if (Access == clang::AS_protected)
Devang Patele2472482010-09-29 21:05:52 +0000846 BFlags |= llvm::DIDescriptor::FlagProtected;
Devang Patelca7daed2010-01-28 21:54:15 +0000847
848 llvm::DIType DTy =
849 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
850 RecordTy, llvm::StringRef(),
Devang Pateld58562e2010-03-09 22:49:11 +0000851 Unit, 0, 0, 0,
Devang Patelca7daed2010-01-28 21:54:15 +0000852 BaseOffset, BFlags,
853 getOrCreateType(BI->getType(),
854 Unit));
855 EltTys.push_back(DTy);
856 }
Devang Patela245c5b2010-01-25 23:32:18 +0000857}
858
Devang Patel4ce3f202010-01-28 18:11:52 +0000859/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000860llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000861 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000862 return VTablePtrType;
863
864 ASTContext &Context = CGM.getContext();
865
866 /* Function type */
Benjamin Kramerad468862010-03-30 11:36:44 +0000867 llvm::DIDescriptor STy = getOrCreateType(Context.IntTy, Unit);
868 llvm::DIArray SElements = DebugFactory.GetOrCreateArray(&STy, 1);
Devang Patel4ce3f202010-01-28 18:11:52 +0000869 llvm::DIType SubTy =
870 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000871 Unit, "", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000872 0, 0, 0, 0, 0, llvm::DIType(), SElements);
873
874 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
875 llvm::DIType vtbl_ptr_type
876 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000877 Unit, "__vtbl_ptr_type", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000878 0, Size, 0, 0, 0, SubTy);
879
Devang Pateld58562e2010-03-09 22:49:11 +0000880 VTablePtrType =
881 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
882 Unit, "", Unit,
883 0, Size, 0, 0, 0, vtbl_ptr_type);
Devang Patel4ce3f202010-01-28 18:11:52 +0000884 return VTablePtrType;
885}
886
Anders Carlsson046c2942010-04-17 20:15:18 +0000887/// getVTableName - Get vtable name for the given Class.
888llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000889 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000890 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000891
892 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000893 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000894 memcpy(StrPtr, Name.data(), Name.length());
895 return llvm::StringRef(StrPtr, Name.length());
896}
897
898
Anders Carlsson046c2942010-04-17 20:15:18 +0000899/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel4ce3f202010-01-28 18:11:52 +0000900/// debug info entry in EltTys vector.
901void CGDebugInfo::
Anders Carlsson046c2942010-04-17 20:15:18 +0000902CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000903 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000904 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000905
906 // If there is a primary base then it will hold vtable info.
907 if (RL.getPrimaryBase())
908 return;
909
910 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000911 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000912 return;
913
914 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
915 llvm::DIType VPTR
916 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Anders Carlsson046c2942010-04-17 20:15:18 +0000917 getVTableName(RD), Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000918 0, Size, 0, 0, 0,
919 getOrCreateVTablePtrType(Unit));
920 EltTys.push_back(VPTR);
921}
922
Devang Patelc69e1cf2010-09-30 19:05:55 +0000923/// getOrCreateRecordType - Emit record type's standalone debug info.
924llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
925 SourceLocation Loc) {
926 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
927 DebugFactory.RecordType(T);
928 return T;
929}
930
Devang Patel65e99f22009-02-25 01:36:11 +0000931/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000932llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000933 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000934 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000935
Chris Lattner9c85ba32008-11-10 06:08:34 +0000936 unsigned Tag;
Devang Pateld6c5a262010-02-01 21:52:22 +0000937 if (RD->isStruct())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000938 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Pateld6c5a262010-02-01 21:52:22 +0000939 else if (RD->isUnion())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000940 Tag = llvm::dwarf::DW_TAG_union_type;
941 else {
Devang Pateld6c5a262010-02-01 21:52:22 +0000942 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000943 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000944 }
945
Chris Lattner9c85ba32008-11-10 06:08:34 +0000946 // Get overall information about the record type for the debug info.
Devang Patel8ab870d2010-05-12 23:46:38 +0000947 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
948 unsigned Line = getLineNumber(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000949
Chris Lattner9c85ba32008-11-10 06:08:34 +0000950 // Records and classes and unions can all be recursive. To handle them, we
951 // first generate a debug descriptor for the struct as a forward declaration.
952 // Then (if it is a definition) we go through and get debug info for all of
953 // its members. Finally, we create a descriptor for the complete type (which
954 // may refer to the forward decl if the struct is recursive) and replace all
955 // uses of the forward declaration with the final definition.
Devang Patel0b897992010-07-08 19:56:29 +0000956 llvm::DIDescriptor FDContext =
957 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
958
959 // If this is just a forward declaration, construct an appropriately
960 // marked node and just return it.
961 if (!RD->getDefinition()) {
962 llvm::DICompositeType FwdDecl =
963 DebugFactory.CreateCompositeType(Tag, FDContext, RD->getName(),
964 DefUnit, Line, 0, 0, 0,
Devang Patele2472482010-09-29 21:05:52 +0000965 llvm::DIDescriptor::FlagFwdDecl,
Devang Patel0b897992010-07-08 19:56:29 +0000966 llvm::DIType(), llvm::DIArray());
967
968 return FwdDecl;
969 }
Devang Pateld0f251b2010-01-20 23:56:40 +0000970
Dan Gohman86968372010-08-20 22:39:57 +0000971 llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType();
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Devang Patelab699792010-05-07 18:12:35 +0000973 llvm::MDNode *MN = FwdDecl;
974 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000975 // Otherwise, insert it into the TypeCache so that recursive uses will find
976 // it.
Devang Patelab699792010-05-07 18:12:35 +0000977 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000978 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000979 RegionStack.push_back(FwdDeclNode);
980 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000981
982 // Convert all the elements.
983 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
984
Devang Pateld6c5a262010-02-01 21:52:22 +0000985 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000986 if (CXXDecl) {
987 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson046c2942010-04-17 20:15:18 +0000988 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000989 }
Devang Pateldabc3e92010-08-12 00:02:44 +0000990
991 // Collect static variables with initializers.
992 for (RecordDecl::decl_iterator I = RD->decls_begin(), E = RD->decls_end();
993 I != E; ++I)
994 if (const VarDecl *V = dyn_cast<VarDecl>(*I)) {
995 if (const Expr *Init = V->getInit()) {
996 Expr::EvalResult Result;
997 if (Init->Evaluate(Result, CGM.getContext()) && Result.Val.isInt()) {
998 llvm::ConstantInt *CI
999 = llvm::ConstantInt::get(CGM.getLLVMContext(), Result.Val.getInt());
1000
1001 // Create the descriptor for static variable.
1002 llvm::DIFile VUnit = getOrCreateFile(V->getLocation());
1003 llvm::StringRef VName = V->getName();
1004 llvm::DIType VTy = getOrCreateType(V->getType(), VUnit);
1005 // Do not use DIGlobalVariable for enums.
1006 if (VTy.getTag() != llvm::dwarf::DW_TAG_enumeration_type) {
1007 DebugFactory.CreateGlobalVariable(FwdDecl, VName, VName, VName, VUnit,
1008 getLineNumber(V->getLocation()),
1009 VTy, true, true, CI);
1010 }
1011 }
1012 }
1013 }
1014
Devang Pateld6c5a262010-02-01 21:52:22 +00001015 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +00001016 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +00001017 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +00001018 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel2ed8f002010-08-27 17:47:47 +00001019 CollectCXXFriends(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +00001020
1021 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +00001022 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel5bc794f2010-10-14 22:59:23 +00001023 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
1024 // Seek non virtual primary base root.
1025 while (1) {
1026 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
1027 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
1028 if (PBT && !BRL.getPrimaryBaseWasVirtual())
1029 PBase = PBT;
1030 else
1031 break;
1032 }
Devang Patel0ac8f312010-01-28 00:54:21 +00001033 ContainingType =
Devang Patelab699792010-05-07 18:12:35 +00001034 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patel5bc794f2010-10-14 22:59:23 +00001035 }
Devang Patel0ac8f312010-01-28 00:54:21 +00001036 else if (CXXDecl->isDynamicClass())
Devang Patelab699792010-05-07 18:12:35 +00001037 ContainingType = FwdDecl;
Devang Patela245c5b2010-01-25 23:32:18 +00001038 }
Mike Stump1eb44332009-09-09 15:08:12 +00001039
Chris Lattner9c85ba32008-11-10 06:08:34 +00001040 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001041 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001042
1043 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001044 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1045 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001046
Devang Patele4c1ea02010-03-11 20:01:48 +00001047 RegionStack.pop_back();
1048 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1049 RegionMap.find(Ty->getDecl());
1050 if (RI != RegionMap.end())
1051 RegionMap.erase(RI);
1052
Devang Patel411894b2010-02-01 22:40:08 +00001053 llvm::DIDescriptor RDContext =
1054 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel700a1cb2010-07-20 20:24:18 +00001055
1056 llvm::StringRef RDName = RD->getName();
1057 // If this is a class, include the template arguments also.
1058 if (Tag == llvm::dwarf::DW_TAG_class_type)
1059 RDName = getClassName(RD);
1060
Devang Patel0ce73f62009-07-22 18:57:00 +00001061 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +00001062 DebugFactory.CreateCompositeType(Tag, RDContext,
Devang Patel700a1cb2010-07-20 20:24:18 +00001063 RDName,
Devang Patelab71ff52009-11-12 00:51:46 +00001064 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +00001065 llvm::DIType(), Elements,
1066 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001067
1068 // Now that we have a real decl for the struct, replace anything using the
1069 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001070 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001071 RegionMap[RD] = llvm::WeakVH(RealDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001072 return RealDecl;
1073}
1074
John McCallc12c5bb2010-05-15 11:32:37 +00001075/// CreateType - get objective-c object type.
1076llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1077 llvm::DIFile Unit) {
1078 // Ignore protocols.
1079 return getOrCreateType(Ty->getBaseType(), Unit);
1080}
1081
Devang Patel9ca36b62009-02-26 21:10:26 +00001082/// CreateType - get objective-c interface type.
1083llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001084 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001085 ObjCInterfaceDecl *ID = Ty->getDecl();
Devang Patel9ca36b62009-02-26 21:10:26 +00001086 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Patel9ca36b62009-02-26 21:10:26 +00001087
1088 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +00001089 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001090 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel17800552010-03-09 00:44:50 +00001091 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +00001092
Dan Gohman45f7c782010-08-23 21:15:56 +00001093 // If this is just a forward declaration, return a special forward-declaration
1094 // debug type.
1095 if (ID->isForwardDecl()) {
1096 llvm::DICompositeType FwdDecl =
1097 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
1098 DefUnit, Line, 0, 0, 0, 0,
1099 llvm::DIType(), llvm::DIArray(),
1100 RuntimeLang);
1101 return FwdDecl;
1102 }
1103
Devang Patel9ca36b62009-02-26 21:10:26 +00001104 // To handle recursive interface, we
1105 // first generate a debug descriptor for the struct as a forward declaration.
1106 // Then (if it is a definition) we go through and get debug info for all of
1107 // its members. Finally, we create a descriptor for the complete type (which
1108 // may refer to the forward decl if the struct is recursive) and replace all
1109 // uses of the forward declaration with the final definition.
Dan Gohman86968372010-08-20 22:39:57 +00001110 llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType();
Mike Stump1eb44332009-09-09 15:08:12 +00001111
Devang Patelab699792010-05-07 18:12:35 +00001112 llvm::MDNode *MN = FwdDecl;
1113 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patel9ca36b62009-02-26 21:10:26 +00001114 // Otherwise, insert it into the TypeCache so that recursive uses will find
1115 // it.
Devang Patelab699792010-05-07 18:12:35 +00001116 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +00001117 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001118 RegionStack.push_back(FwdDeclNode);
1119 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001120
1121 // Convert all the elements.
1122 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
1123
Devang Pateld6c5a262010-02-01 21:52:22 +00001124 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +00001125 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +00001126 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001127 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001128 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +00001129 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Pateld58562e2010-03-09 22:49:11 +00001130 Unit, "", Unit, 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +00001131 0 /* offset */, 0, SClassTy);
1132 EltTys.push_back(InhTag);
1133 }
1134
Devang Pateld6c5a262010-02-01 21:52:22 +00001135 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001136
1137 unsigned FieldNo = 0;
Fariborz Jahanian97477392010-10-01 00:01:53 +00001138 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
Fariborz Jahanianfe8fdba2010-10-11 23:55:47 +00001139 Field = Field->getNextIvar(), ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001140 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1141
Devang Patel73621622009-11-25 17:37:31 +00001142 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001143
Devang Patelde135022009-04-27 22:40:36 +00001144 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +00001145 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +00001146 continue;
1147
Devang Patel9ca36b62009-02-26 21:10:26 +00001148 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +00001149 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1150 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel99c20eb2009-03-20 18:24:39 +00001151 QualType FType = Field->getType();
1152 uint64_t FieldSize = 0;
1153 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +00001154
Devang Patel99c20eb2009-03-20 18:24:39 +00001155 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001156
Devang Patel99c20eb2009-03-20 18:24:39 +00001157 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001158 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001159 Expr *BitWidth = Field->getBitWidth();
1160 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +00001161 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +00001162
Anders Carlsson20f12a22009-12-06 18:00:51 +00001163 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001164 }
1165
Mike Stump1eb44332009-09-09 15:08:12 +00001166 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
1167
Devang Patelc20482b2009-03-19 00:23:53 +00001168 unsigned Flags = 0;
1169 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Devang Patele2472482010-09-29 21:05:52 +00001170 Flags = llvm::DIDescriptor::FlagProtected;
Devang Patelc20482b2009-03-19 00:23:53 +00001171 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Devang Patele2472482010-09-29 21:05:52 +00001172 Flags = llvm::DIDescriptor::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +00001173
Devang Patel9ca36b62009-02-26 21:10:26 +00001174 // Create a DW_TAG_member node to remember the offset of this field in the
1175 // struct. FIXME: This is an absolutely insane way to capture this
1176 // information. When we gut debug info, this should be fixed.
1177 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1178 FieldName, FieldDefUnit,
1179 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +00001180 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +00001181 EltTys.push_back(FieldTy);
1182 }
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Devang Patel9ca36b62009-02-26 21:10:26 +00001184 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +00001185 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +00001186
Devang Patele4c1ea02010-03-11 20:01:48 +00001187 RegionStack.pop_back();
1188 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1189 RegionMap.find(Ty->getDecl());
1190 if (RI != RegionMap.end())
1191 RegionMap.erase(RI);
1192
Devang Patel9ca36b62009-02-26 21:10:26 +00001193 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001194 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1195 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001196
Devang Patel6c1fddf2009-07-27 18:42:03 +00001197 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +00001198 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +00001199 Line, Size, Align, 0, 0, llvm::DIType(),
1200 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +00001201
1202 // Now that we have a real decl for the struct, replace anything using the
1203 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001204 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001205 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +00001206
Devang Patel9ca36b62009-02-26 21:10:26 +00001207 return RealDecl;
1208}
1209
Chris Lattner9c85ba32008-11-10 06:08:34 +00001210llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001211 llvm::DIFile Unit) {
Devang Patel6237cea2010-08-23 22:07:25 +00001212 return CreateEnumType(Ty->getDecl(), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001213
Chris Lattner9c85ba32008-11-10 06:08:34 +00001214}
1215
1216llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001217 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001218 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1219 return CreateType(RT, Unit);
1220 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1221 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001222
Chris Lattner9c85ba32008-11-10 06:08:34 +00001223 return llvm::DIType();
1224}
1225
Devang Patel70c23cd2010-02-23 22:59:39 +00001226llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Eli Friedmana7e68452010-08-22 01:00:03 +00001227 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001228 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1229 uint64_t NumElems = Ty->getNumElements();
1230 if (NumElems > 0)
1231 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001232
Benjamin Kramerad468862010-03-30 11:36:44 +00001233 llvm::DIDescriptor Subscript = DebugFactory.GetOrCreateSubrange(0, NumElems);
1234 llvm::DIArray SubscriptArray = DebugFactory.GetOrCreateArray(&Subscript, 1);
Devang Patel70c23cd2010-02-23 22:59:39 +00001235
1236 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1237 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1238
1239 return
1240 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001241 Unit, "", Unit,
Devang Patel70c23cd2010-02-23 22:59:39 +00001242 0, Size, Align, 0, 0,
Eli Friedmana7e68452010-08-22 01:00:03 +00001243 ElementTy, SubscriptArray);
Devang Patel70c23cd2010-02-23 22:59:39 +00001244}
1245
Chris Lattner9c85ba32008-11-10 06:08:34 +00001246llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001247 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001248 uint64_t Size;
1249 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001250
1251
Nuno Lopes010d5142009-01-28 00:35:17 +00001252 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001253 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001254 Size = 0;
1255 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001256 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001257 } else if (Ty->isIncompleteArrayType()) {
1258 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001259 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001260 } else {
1261 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001262 Size = CGM.getContext().getTypeSize(Ty);
1263 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001264 }
Mike Stump1eb44332009-09-09 15:08:12 +00001265
Chris Lattner9c85ba32008-11-10 06:08:34 +00001266 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1267 // interior arrays, do we care? Why aren't nested arrays represented the
1268 // obvious/recursive way?
1269 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1270 QualType EltTy(Ty, 0);
Devang Patelcdf523c2010-10-06 18:30:00 +00001271 if (Ty->isIncompleteArrayType())
Chris Lattner9c85ba32008-11-10 06:08:34 +00001272 EltTy = Ty->getElementType();
Devang Patelcdf523c2010-10-06 18:30:00 +00001273 else {
1274 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1275 uint64_t Upper = 0;
1276 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1277 if (CAT->getSize().getZExtValue())
1278 Upper = CAT->getSize().getZExtValue() - 1;
1279 // FIXME: Verify this is right for VLAs.
1280 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1281 EltTy = Ty->getElementType();
1282 }
Sanjiv Gupta507de852008-06-09 10:47:41 +00001283 }
Mike Stump1eb44332009-09-09 15:08:12 +00001284
Chris Lattner9c85ba32008-11-10 06:08:34 +00001285 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001286 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001287
Devang Patelca80a5f2009-10-20 19:55:01 +00001288 llvm::DIType DbgTy =
1289 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001290 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +00001291 0, Size, Align, 0, 0,
1292 getOrCreateType(EltTy, Unit),
1293 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001294 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001295}
1296
Anders Carlssona031b352009-11-06 19:19:55 +00001297llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001298 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001299 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1300 Ty, Ty->getPointeeType(), Unit);
1301}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001302
Anders Carlsson20f12a22009-12-06 18:00:51 +00001303llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001304 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001305 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1306 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1307
1308 if (!Ty->getPointeeType()->isFunctionType()) {
1309 // We have a data member pointer type.
1310 return PointerDiffDITy;
1311 }
1312
1313 // We have a member function pointer type. Treat it as a struct with two
1314 // ptrdiff_t members.
1315 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1316
1317 uint64_t FieldOffset = 0;
1318 llvm::DIDescriptor ElementTypes[2];
1319
1320 // FIXME: This should probably be a function type instead.
1321 ElementTypes[0] =
1322 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001323 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001324 Info.first, Info.second, FieldOffset, 0,
1325 PointerDiffDITy);
1326 FieldOffset += Info.first;
1327
1328 ElementTypes[1] =
1329 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001330 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001331 Info.first, Info.second, FieldOffset, 0,
1332 PointerDiffDITy);
1333
1334 llvm::DIArray Elements =
1335 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1336 llvm::array_lengthof(ElementTypes));
1337
1338 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1339 U, llvm::StringRef("test"),
Devang Pateld58562e2010-03-09 22:49:11 +00001340 U, 0, FieldOffset,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001341 0, 0, 0, llvm::DIType(), Elements);
1342}
1343
Devang Patel6237cea2010-08-23 22:07:25 +00001344/// CreateEnumType - get enumeration type.
1345llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED, llvm::DIFile Unit){
1346 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
1347
1348 // Create DIEnumerator elements for each enumerator.
1349 for (EnumDecl::enumerator_iterator
1350 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
1351 Enum != EnumEnd; ++Enum) {
1352 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
1353 Enum->getInitVal().getZExtValue()));
1354 }
1355
1356 // Return a CompositeType for the enum itself.
1357 llvm::DIArray EltArray =
1358 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
1359
1360 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1361 unsigned Line = getLineNumber(ED->getLocation());
1362 uint64_t Size = 0;
Devang Patelffc52e72010-08-24 18:14:06 +00001363 uint64_t Align = 0;
1364 if (!ED->getTypeForDecl()->isIncompleteType()) {
1365 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1366 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1367 }
Devang Patel6237cea2010-08-23 22:07:25 +00001368 llvm::DIType DbgTy =
1369 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
1370 Unit, ED->getName(), DefUnit, Line,
Devang Patelffc52e72010-08-24 18:14:06 +00001371 Size, Align, 0, 0,
Devang Patel6237cea2010-08-23 22:07:25 +00001372 llvm::DIType(), EltArray);
1373 return DbgTy;
1374}
1375
Douglas Gregor840943d2009-12-21 20:18:30 +00001376static QualType UnwrapTypeForDebugInfo(QualType T) {
1377 do {
1378 QualType LastT = T;
1379 switch (T->getTypeClass()) {
1380 default:
1381 return T;
1382 case Type::TemplateSpecialization:
1383 T = cast<TemplateSpecializationType>(T)->desugar();
1384 break;
1385 case Type::TypeOfExpr: {
1386 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1387 T = Ty->getUnderlyingExpr()->getType();
1388 break;
1389 }
1390 case Type::TypeOf:
1391 T = cast<TypeOfType>(T)->getUnderlyingType();
1392 break;
1393 case Type::Decltype:
1394 T = cast<DecltypeType>(T)->getUnderlyingType();
1395 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001396 case Type::Elaborated:
1397 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001398 break;
1399 case Type::SubstTemplateTypeParm:
1400 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1401 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001402 }
1403
1404 assert(T != LastT && "Type unwrapping failed to unwrap!");
1405 if (T == LastT)
1406 return T;
1407 } while (true);
1408
1409 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001410}
1411
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001412/// getOrCreateType - Get the type from the cache or create a new
1413/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001414llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001415 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001416 if (Ty.isNull())
1417 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001418
Douglas Gregor840943d2009-12-21 20:18:30 +00001419 // Unwrap the type as needed for debug information.
1420 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001421
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001422 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001423 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001424 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001425 if (it != TypeCache.end()) {
1426 // Verify that the debug info still exists.
1427 if (&*it->second)
1428 return llvm::DIType(cast<llvm::MDNode>(it->second));
1429 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001430
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001431 // Otherwise create the type.
1432 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001433
1434 // And update the type cache.
Devang Patelab699792010-05-07 18:12:35 +00001435 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001436 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001437}
1438
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001439/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001440llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001441 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001442 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001443 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001444 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001445
Douglas Gregor2101a822009-12-21 19:57:21 +00001446 const char *Diag = 0;
1447
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001448 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001449 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001450#define TYPE(Class, Base)
1451#define ABSTRACT_TYPE(Class, Base)
1452#define NON_CANONICAL_TYPE(Class, Base)
1453#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1454#include "clang/AST/TypeNodes.def"
1455 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001456
Anders Carlssonbfe69952009-11-06 18:24:04 +00001457 // FIXME: Handle these.
1458 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001459 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001460
1461 case Type::Vector:
1462 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001463 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001464 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCallc12c5bb2010-05-15 11:32:37 +00001465 case Type::ObjCObject:
1466 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001467 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001468 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1469 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1470 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1471 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001472 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001473 return CreateType(cast<BlockPointerType>(Ty), Unit);
1474 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001475 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001476 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001477 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001478 case Type::FunctionProto:
1479 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001480 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001481 case Type::ConstantArray:
1482 case Type::VariableArray:
1483 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001484 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001485
1486 case Type::LValueReference:
1487 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1488
Anders Carlsson20f12a22009-12-06 18:00:51 +00001489 case Type::MemberPointer:
1490 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001491
1492 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001493 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001494 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001495 case Type::TypeOfExpr:
1496 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001497 case Type::Decltype:
1498 llvm_unreachable("type should have been unwrapped!");
1499 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001500
1501 case Type::RValueReference:
1502 // FIXME: Implement!
1503 Diag = "rvalue references";
1504 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001505 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001506
1507 assert(Diag && "Fall through without a diagnostic?");
1508 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1509 "debug information for %0 is not yet supported");
1510 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1511 << Diag;
1512 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001513}
1514
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001515/// CreateMemberType - Create new member and increase Offset by FType's size.
1516llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
1517 llvm::StringRef Name,
1518 uint64_t *Offset) {
1519 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1520 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1521 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
1522 llvm::DIType Ty = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1523 Unit, Name, Unit, 0,
1524 FieldSize, FieldAlign,
1525 *Offset, 0, FieldTy);
1526 *Offset += FieldSize;
1527 return Ty;
1528}
1529
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001530/// EmitFunctionStart - Constructs the debug code for entering a function -
1531/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001532void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001533 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001534 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001535
Devang Patel9c6c3a02010-01-14 00:36:21 +00001536 llvm::StringRef Name;
Anders Carlsson9a20d552010-06-22 16:16:50 +00001537 llvm::StringRef LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001538
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001539 FnBeginRegionCount.push_back(RegionStack.size());
1540
Devang Patel9c6c3a02010-01-14 00:36:21 +00001541 const Decl *D = GD.getDecl();
Devang Patel3951e712010-10-07 22:03:49 +00001542 unsigned Flags = 0;
Devang Patel0692f832010-10-11 21:58:41 +00001543 llvm::DIFile Unit = getOrCreateFile(CurLoc);
1544 llvm::DIDescriptor FDContext(Unit);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001545 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001546 // If there is a DISubprogram for this function available then use it.
1547 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1548 FI = SPCache.find(FD);
1549 if (FI != SPCache.end()) {
Gabor Greif38c9b172010-09-18 13:00:17 +00001550 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(&*FI->second));
Devang Patelab699792010-05-07 18:12:35 +00001551 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1552 llvm::MDNode *SPN = SP;
1553 RegionStack.push_back(SPN);
1554 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00001555 return;
1556 }
1557 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001558 Name = getFunctionName(FD);
1559 // Use mangled name as linkage name for c/c++ functions.
Anders Carlsson9a20d552010-06-22 16:16:50 +00001560 LinkageName = CGM.getMangledName(GD);
Devang Patel3951e712010-10-07 22:03:49 +00001561 if (FD->hasPrototype())
1562 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel0692f832010-10-11 21:58:41 +00001563 if (const NamespaceDecl *NSDecl =
1564 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
1565 FDContext = getOrCreateNameSpace(NSDecl, Unit);
David Chisnall70b9b442010-09-02 17:16:32 +00001566 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
David Chisnall52044a22010-09-02 18:01:51 +00001567 Name = getObjCMethodName(OMD);
David Chisnall70b9b442010-09-02 17:16:32 +00001568 LinkageName = Name;
Devang Patel3951e712010-10-07 22:03:49 +00001569 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001570 } else {
1571 // Use llvm function name as linkage name.
1572 Name = Fn->getName();
Anders Carlsson9a20d552010-06-22 16:16:50 +00001573 LinkageName = Name;
Devang Patel3951e712010-10-07 22:03:49 +00001574 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001575 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001576 if (!Name.empty() && Name[0] == '\01')
1577 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001578
Devang Patel970c6182010-04-24 00:49:16 +00001579 // It is expected that CurLoc is set before using EmitFunctionStart.
1580 // Usually, CurLoc points to the left bracket location of compound
1581 // statement representing function body.
Devang Patel8ab870d2010-05-12 23:46:38 +00001582 unsigned LineNo = getLineNumber(CurLoc);
Devang Patele2472482010-09-29 21:05:52 +00001583 if (D->isImplicit())
1584 Flags |= llvm::DIDescriptor::FlagArtificial;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001585 llvm::DISubprogram SP =
Devang Patel0692f832010-10-11 21:58:41 +00001586 DebugFactory.CreateSubprogram(FDContext, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001587 getOrCreateType(FnType, Unit),
Devang Patel15a3d7d2010-07-15 23:09:46 +00001588 Fn->hasInternalLinkage(), true/*definition*/,
1589 0, 0, llvm::DIType(),
Devang Patele2472482010-09-29 21:05:52 +00001590 Flags,
Devang Patel15a3d7d2010-07-15 23:09:46 +00001591 CGM.getLangOptions().Optimize, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +00001592
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001593 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001594 llvm::MDNode *SPN = SP;
1595 RegionStack.push_back(SPN);
1596 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001597
1598 // Clear stack used to keep track of #line directives.
1599 LineDirectiveFiles.clear();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001600}
1601
1602
Devang Patel4d939e62010-07-20 22:20:10 +00001603void CGDebugInfo::EmitStopPoint(CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001604 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001605
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001606 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001607 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001608 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001609 || (SM.getInstantiationLineNumber(CurLoc) ==
1610 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001611 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patel4800ea62010-04-05 21:09:15 +00001612 // New Builder may not be in sync with CGDebugInfo.
1613 if (!Builder.getCurrentDebugLocation().isUnknown())
1614 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001615
1616 // Update last state.
1617 PrevLoc = CurLoc;
1618
Chris Lattnerc6034632010-04-01 06:31:43 +00001619 llvm::MDNode *Scope = RegionStack.back();
Devang Patel8ab870d2010-05-12 23:46:38 +00001620 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1621 getColumnNumber(CurLoc),
Chris Lattnere541d012010-04-02 20:21:43 +00001622 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001623}
1624
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001625/// UpdateLineDirectiveRegion - Update region stack only if #line directive
1626/// has introduced scope change.
1627void CGDebugInfo::UpdateLineDirectiveRegion(CGBuilderTy &Builder) {
1628 if (CurLoc.isInvalid() || CurLoc.isMacroID() ||
1629 PrevLoc.isInvalid() || PrevLoc.isMacroID())
1630 return;
1631 SourceManager &SM = CGM.getContext().getSourceManager();
1632 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
1633 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc);
1634
1635 if (!strcmp(PPLoc.getFilename(), PCLoc.getFilename()))
1636 return;
1637
1638 // If #line directive stack is empty then we are entering a new scope.
1639 if (LineDirectiveFiles.empty()) {
1640 EmitRegionStart(Builder);
1641 LineDirectiveFiles.push_back(PCLoc.getFilename());
1642 return;
1643 }
1644
1645 assert (RegionStack.size() >= LineDirectiveFiles.size()
1646 && "error handling #line regions!");
1647
1648 bool SeenThisFile = false;
Devang Patel424a5c62010-09-15 20:50:40 +00001649 // Chek if current file is already seen earlier.
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001650 for(std::vector<const char *>::iterator I = LineDirectiveFiles.begin(),
1651 E = LineDirectiveFiles.end(); I != E; ++I)
Devang Patel424a5c62010-09-15 20:50:40 +00001652 if (!strcmp(PCLoc.getFilename(), *I)) {
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001653 SeenThisFile = true;
1654 break;
1655 }
1656
1657 // If #line for this file is seen earlier then pop out #line regions.
1658 if (SeenThisFile) {
1659 while (!LineDirectiveFiles.empty()) {
1660 const char *LastFile = LineDirectiveFiles.back();
1661 RegionStack.pop_back();
1662 LineDirectiveFiles.pop_back();
1663 if (!strcmp(PPLoc.getFilename(), LastFile))
1664 break;
1665 }
1666 return;
1667 }
1668
1669 // .. otherwise insert new #line region.
1670 EmitRegionStart(Builder);
1671 LineDirectiveFiles.push_back(PCLoc.getFilename());
1672
1673 return;
1674}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001675/// EmitRegionStart- Constructs the debug code for entering a declarative
1676/// region - "llvm.dbg.region.start.".
Devang Patel4d939e62010-07-20 22:20:10 +00001677void CGDebugInfo::EmitRegionStart(CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001678 llvm::DIDescriptor D =
1679 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1680 llvm::DIDescriptor() :
Devang Pateld19429f2010-02-16 21:41:20 +00001681 llvm::DIDescriptor(RegionStack.back()),
Stuart Hastings257d1d32010-07-19 23:56:31 +00001682 getOrCreateFile(CurLoc),
Devang Patel8ab870d2010-05-12 23:46:38 +00001683 getLineNumber(CurLoc),
1684 getColumnNumber(CurLoc));
Devang Patelab699792010-05-07 18:12:35 +00001685 llvm::MDNode *DN = D;
1686 RegionStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001687}
1688
1689/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1690/// region - "llvm.dbg.region.end."
Devang Patel4d939e62010-07-20 22:20:10 +00001691void CGDebugInfo::EmitRegionEnd(CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001692 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1693
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001694 // Provide an region stop point.
Devang Patel4d939e62010-07-20 22:20:10 +00001695 EmitStopPoint(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001696
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001697 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001698}
1699
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001700/// EmitFunctionEnd - Constructs the debug code for exiting a function.
1701void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
1702 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1703 unsigned RCount = FnBeginRegionCount.back();
1704 assert(RCount <= RegionStack.size() && "Region stack mismatch");
1705
1706 // Pop all regions for this function.
1707 while (RegionStack.size() != RCount)
1708 EmitRegionEnd(Builder);
1709 FnBeginRegionCount.pop_back();
1710}
1711
Devang Patel809b9bb2010-02-10 18:49:08 +00001712// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1713// See BuildByRefType.
1714llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1715 uint64_t *XOffset) {
1716
1717 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1718
1719 QualType FType;
1720 uint64_t FieldSize, FieldOffset;
1721 unsigned FieldAlign;
1722
Devang Patel17800552010-03-09 00:44:50 +00001723 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001724 QualType Type = VD->getType();
1725
1726 FieldOffset = 0;
1727 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001728 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1729 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001730 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001731 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1732 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1733
Devang Patel809b9bb2010-02-10 18:49:08 +00001734 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1735 if (HasCopyAndDispose) {
1736 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001737 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1738 &FieldOffset));
1739 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1740 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001741 }
1742
1743 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1744 if (Align > CharUnits::fromQuantity(
1745 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1746 unsigned AlignedOffsetInBytes
1747 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1748 unsigned NumPaddingBytes
1749 = AlignedOffsetInBytes - FieldOffset/8;
1750
1751 if (NumPaddingBytes > 0) {
1752 llvm::APInt pad(32, NumPaddingBytes);
1753 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1754 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001755 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001756 }
1757 }
1758
1759 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001760 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00001761 FieldSize = CGM.getContext().getTypeSize(FType);
1762 FieldAlign = Align.getQuantity()*8;
1763
1764 *XOffset = FieldOffset;
1765 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001766 VD->getName(), Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001767 0, FieldSize, FieldAlign,
1768 FieldOffset, 0, FieldTy);
1769 EltTys.push_back(FieldTy);
1770 FieldOffset += FieldSize;
1771
1772 llvm::DIArray Elements =
1773 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1774
Devang Patele2472482010-09-29 21:05:52 +00001775 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Devang Patel809b9bb2010-02-10 18:49:08 +00001776
1777 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001778 Unit, "", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001779 0, FieldOffset, 0, 0, Flags,
1780 llvm::DIType(), Elements);
1781
1782}
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001783/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001784void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001785 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001786 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1787
Devang Patel17800552010-03-09 00:44:50 +00001788 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001789 llvm::DIType Ty;
1790 uint64_t XOffset = 0;
1791 if (VD->hasAttr<BlocksAttr>())
1792 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1793 else
1794 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001795
Devang Patelf4e54a22010-05-07 23:05:55 +00001796 // If there is not any debug info for type then do not emit debug info
1797 // for this variable.
1798 if (!Ty)
1799 return;
1800
Chris Lattner9c85ba32008-11-10 06:08:34 +00001801 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001802 unsigned Line = getLineNumber(VD->getLocation());
1803 unsigned Column = getColumnNumber(VD->getLocation());
Devang Patelaca745b2010-09-29 23:09:21 +00001804 unsigned Flags = 0;
1805 if (VD->isImplicit())
1806 Flags |= llvm::DIDescriptor::FlagArtificial;
Chris Lattnerc6034632010-04-01 06:31:43 +00001807 llvm::MDNode *Scope = RegionStack.back();
Devang Patelcebbedd2010-10-12 23:24:54 +00001808
1809 llvm::StringRef Name = VD->getName();
1810 if (!Name.empty()) {
1811 // Create the descriptor for the variable.
1812 llvm::DIVariable D =
1813 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(Scope),
1814 Name, Unit, Line, Ty,
1815 CGM.getLangOptions().Optimize, Flags);
1816
1817 // Insert an llvm.dbg.declare into the current block.
1818 llvm::Instruction *Call =
1819 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
1820
1821 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
1822 }
1823
1824 // If VD is an anonymous union then Storage represents value for
1825 // all union fields.
1826 if (const RecordType *RT = dyn_cast<RecordType>(VD->getType()))
1827 if (const RecordDecl *RD = dyn_cast<RecordDecl>(RT->getDecl()))
1828 if (RD->isUnion()) {
1829 for (RecordDecl::field_iterator I = RD->field_begin(),
1830 E = RD->field_end();
1831 I != E; ++I) {
1832 FieldDecl *Field = *I;
1833 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1834 llvm::StringRef FieldName = Field->getName();
1835
1836 // Ignore unnamed fields. Do not ignore unnamed records.
1837 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
1838 continue;
1839
1840 // Use VarDecl's Tag, Scope and Line number.
1841 llvm::DIVariable D =
1842 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(Scope),
1843 FieldName, Unit, Line, FieldTy,
1844 CGM.getLangOptions().Optimize, Flags);
1845
1846 // Insert an llvm.dbg.declare into the current block.
1847 llvm::Instruction *Call =
1848 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
1849
1850 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
1851 }
1852 }
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001853}
1854
Mike Stumpb1a6e682009-09-30 02:43:10 +00001855/// EmitDeclare - Emit local variable declaration debug info.
1856void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1857 llvm::Value *Storage, CGBuilderTy &Builder,
1858 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001859 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001860 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1861
Devang Patel2b594b92010-04-26 23:28:46 +00001862 if (Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001863 return;
1864
1865 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001866 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001867 llvm::DIType Ty;
1868 if (VD->hasAttr<BlocksAttr>())
1869 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1870 else
1871 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001872
1873 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001874 unsigned Line = getLineNumber(VD->getLocation());
1875 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001876
Devang Pateld6c5a262010-02-01 21:52:22 +00001877 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001878 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001879 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1880 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1881 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1882 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001883 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001884 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1885 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001886 // offset of __forwarding field
1887 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001888 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1889 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1890 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001891 // offset of x field
1892 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001893 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001894 }
1895
1896 // Create the descriptor for the variable.
1897 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001898 DebugFactory.CreateComplexVariable(Tag,
1899 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001900 VD->getName(), Unit, Line, Ty,
Benjamin Kramer3475cfe2010-09-21 15:59:59 +00001901 addr.data(), addr.size());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001902 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001903 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001904 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattnerd5b89022009-12-28 21:44:41 +00001905
Chris Lattnerc6034632010-04-01 06:31:43 +00001906 llvm::MDNode *Scope = RegionStack.back();
Devang Patelf8e10a52010-05-10 23:48:38 +00001907 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001908}
1909
Devang Pateld6c5a262010-02-01 21:52:22 +00001910void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001911 llvm::Value *Storage,
1912 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001913 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001914}
1915
Mike Stumpb1a6e682009-09-30 02:43:10 +00001916void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1917 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1918 CodeGenFunction *CGF) {
1919 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1920}
1921
Chris Lattner9c85ba32008-11-10 06:08:34 +00001922/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1923/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001924void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001925 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001926 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001927}
1928
1929
1930
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001931/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001932void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001933 const VarDecl *D) {
1934
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001935 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001936 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001937 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +00001938
Devang Pateleb6d79b2010-02-01 21:34:11 +00001939 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001940 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001941
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001942 // CodeGen turns int[] into int[1] so we'll do the same here.
1943 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001944
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001945 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001946 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001947
Anders Carlsson20f12a22009-12-06 18:00:51 +00001948 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001949 ArrayType::Normal, 0);
1950 }
Devang Patel5d822f02010-04-29 17:48:37 +00001951 llvm::StringRef DeclName = D->getName();
Devang Patel8b90a782010-05-13 23:52:37 +00001952 llvm::StringRef LinkageName;
Devang Patel0fd3d1f2010-05-14 16:55:25 +00001953 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()))
Devang Patel8b90a782010-05-13 23:52:37 +00001954 LinkageName = Var->getName();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001955 llvm::DIDescriptor DContext =
1956 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
Devang Patel8b90a782010-05-13 23:52:37 +00001957 DebugFactory.CreateGlobalVariable(DContext, DeclName, DeclName, LinkageName,
1958 Unit, LineNo, getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001959 Var->hasInternalLinkage(),
1960 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001961}
1962
Devang Patel9ca36b62009-02-26 21:10:26 +00001963/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001964void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001965 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001966 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001967 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001968 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patel9ca36b62009-02-26 21:10:26 +00001969
Devang Pateld6c5a262010-02-01 21:52:22 +00001970 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001971
Devang Pateld6c5a262010-02-01 21:52:22 +00001972 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001973 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001974
Devang Patel9ca36b62009-02-26 21:10:26 +00001975 // CodeGen turns int[] into int[1] so we'll do the same here.
1976 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001977
Devang Patel9ca36b62009-02-26 21:10:26 +00001978 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001979 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001980
Anders Carlsson20f12a22009-12-06 18:00:51 +00001981 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001982 ArrayType::Normal, 0);
1983 }
1984
Devang Patelf6a39b72009-10-20 18:26:30 +00001985 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001986 getOrCreateType(T, Unit),
1987 Var->hasInternalLinkage(),
1988 true/*definition*/, Var);
1989}
Devang Patelabb485f2010-02-01 19:16:32 +00001990
Devang Patel25c2c8f2010-08-10 17:53:33 +00001991/// EmitGlobalVariable - Emit global variable's debug info.
1992void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
John McCall189d6ef2010-10-09 01:34:31 +00001993 llvm::Constant *Init) {
Devang Patel8d308382010-08-10 07:24:25 +00001994 // Create the descriptor for the variable.
1995 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
1996 llvm::StringRef Name = VD->getName();
Devang Patel0317ab02010-08-10 18:27:15 +00001997 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
Devang Patel6237cea2010-08-23 22:07:25 +00001998 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
1999 if (const EnumDecl *ED = dyn_cast<EnumDecl>(ECD->getDeclContext()))
2000 Ty = CreateEnumType(ED, Unit);
2001 }
Devang Patel0317ab02010-08-10 18:27:15 +00002002 // Do not use DIGlobalVariable for enums.
2003 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
2004 return;
Devang Patel8d308382010-08-10 07:24:25 +00002005 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit,
2006 getLineNumber(VD->getLocation()),
Devang Patel4c4acc02010-08-10 20:16:57 +00002007 Ty, true, true, Init);
Devang Patel8d308382010-08-10 07:24:25 +00002008}
2009
Devang Patelabb485f2010-02-01 19:16:32 +00002010/// getOrCreateNamesSpace - Return namespace descriptor for the given
2011/// namespace decl.
2012llvm::DINameSpace
2013CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
2014 llvm::DIDescriptor Unit) {
2015 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
2016 NameSpaceCache.find(NSDecl);
2017 if (I != NameSpaceCache.end())
2018 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
2019
Devang Patel8ab870d2010-05-12 23:46:38 +00002020 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patelabb485f2010-02-01 19:16:32 +00002021
2022 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00002023 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00002024 llvm::DINameSpace NS =
2025 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
Devang Patel2ed8f002010-08-27 17:47:47 +00002026 llvm::DIFile(Unit), LineNo);
Devang Patelab699792010-05-07 18:12:35 +00002027 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00002028 return NS;
2029}