blob: 7458fa3e6f2ac78e71ebd681ea8b75cd6e94f62b [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 Patel9ca36b62009-02-26 21:10:26 +000018#include "clang/AST/DeclObjC.h"
Devang Patel700a1cb2010-07-20 20:24:18 +000019#include "clang/AST/DeclTemplate.h"
Chris Lattner3cc5c402008-11-11 07:01:36 +000020#include "clang/AST/Expr.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000022#include "clang/Basic/SourceManager.h"
23#include "clang/Basic/FileManager.h"
Mike Stump5a862172009-09-15 21:48:34 +000024#include "clang/Basic/Version.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000025#include "clang/Frontend/CodeGenOptions.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000026#include "llvm/Constants.h"
27#include "llvm/DerivedTypes.h"
28#include "llvm/Instructions.h"
29#include "llvm/Intrinsics.h"
30#include "llvm/Module.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000031#include "llvm/ADT/StringExtras.h"
32#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000033#include "llvm/Support/Dwarf.h"
Devang Patel446c6192009-04-17 21:06:59 +000034#include "llvm/System/Path.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000035#include "llvm/Target/TargetMachine.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000036using namespace clang;
37using namespace clang::CodeGen;
38
Anders Carlsson20f12a22009-12-06 18:00:51 +000039CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Devang Patel17800552010-03-09 00:44:50 +000040 : CGM(CGM), DebugFactory(CGM.getModule()),
Devang Patel7573f8b2010-03-09 21:32:27 +000041 FwdDeclCount(0), BlockLiteralGenericSet(false) {
Devang Patel17800552010-03-09 00:44:50 +000042 CreateCompileUnit();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000043}
44
Chris Lattner9c85ba32008-11-10 06:08:34 +000045CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar66031a52008-10-17 16:15:48 +000046 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000047}
48
Chris Lattner9c85ba32008-11-10 06:08:34 +000049void CGDebugInfo::setLocation(SourceLocation Loc) {
50 if (Loc.isValid())
Anders Carlsson20f12a22009-12-06 18:00:51 +000051 CurLoc = CGM.getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000052}
53
Devang Patel33583052010-01-28 23:15:27 +000054/// getContextDescriptor - Get context info for the decl.
Devang Pateleb6d79b2010-02-01 21:34:11 +000055llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context,
Devang Patel33583052010-01-28 23:15:27 +000056 llvm::DIDescriptor &CompileUnit) {
Devang Pateleb6d79b2010-02-01 21:34:11 +000057 if (!Context)
58 return CompileUnit;
59
60 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
61 I = RegionMap.find(Context);
62 if (I != RegionMap.end())
63 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(I->second));
Devang Patel411894b2010-02-01 22:40:08 +000064
Devang Pateleb6d79b2010-02-01 21:34:11 +000065 // Check namespace.
66 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
67 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl, CompileUnit));
Devang Patel8b90a782010-05-13 23:52:37 +000068
69 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context)) {
70 if (!RDecl->isDependentType()) {
71 llvm::DIType Ty = getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
72 llvm::DIFile(CompileUnit));
73 return llvm::DIDescriptor(Ty);
74 }
75 }
Devang Patel979ec2e2009-10-06 00:35:31 +000076 return CompileUnit;
77}
78
Devang Patel9c6c3a02010-01-14 00:36:21 +000079/// getFunctionName - Get function name for the given FunctionDecl. If the
80/// name is constructred on demand (e.g. C++ destructor) then the name
81/// is stored on the side.
82llvm::StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
83 assert (FD && "Invalid FunctionDecl!");
84 IdentifierInfo *FII = FD->getIdentifier();
85 if (FII)
86 return FII->getName();
87
88 // Otherwise construct human readable name for debug info.
89 std::string NS = FD->getNameAsString();
90
91 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +000092 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer1b627dc2010-01-23 18:16:07 +000093 memcpy(StrPtr, NS.data(), NS.length());
94 return llvm::StringRef(StrPtr, NS.length());
Devang Patel9c6c3a02010-01-14 00:36:21 +000095}
96
Devang Patel700a1cb2010-07-20 20:24:18 +000097/// getClassName - Get class name including template argument list.
98llvm::StringRef
99CGDebugInfo::getClassName(RecordDecl *RD) {
100 ClassTemplateSpecializationDecl *Spec
101 = dyn_cast<ClassTemplateSpecializationDecl>(RD);
102 if (!Spec)
103 return RD->getName();
104
105 const TemplateArgument *Args;
106 unsigned NumArgs;
107 std::string Buffer;
108 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
109 const TemplateSpecializationType *TST =
110 cast<TemplateSpecializationType>(TAW->getType());
111 Args = TST->getArgs();
112 NumArgs = TST->getNumArgs();
113 } else {
114 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
115 Args = TemplateArgs.getFlatArgumentList();
116 NumArgs = TemplateArgs.flat_size();
117 }
118 Buffer = RD->getIdentifier()->getNameStart();
119 PrintingPolicy Policy(CGM.getLangOptions());
120 Buffer += TemplateSpecializationType::PrintTemplateArgumentList(Args,
121 NumArgs,
122 Policy);
123
124 // Copy this name on the side and use its reference.
125 char *StrPtr = DebugInfoNames.Allocate<char>(Buffer.length());
126 memcpy(StrPtr, Buffer.data(), Buffer.length());
127 return llvm::StringRef(StrPtr, Buffer.length());
128
129}
130
Devang Patel17800552010-03-09 00:44:50 +0000131/// getOrCreateFile - Get the file debug info descriptor for the input location.
132llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Ted Kremenek9c250392010-03-30 00:27:51 +0000133 if (!Loc.isValid())
Devang Patel17800552010-03-09 00:44:50 +0000134 // If Location is not valid then use main input file.
135 return DebugFactory.CreateFile(TheCU.getFilename(), TheCU.getDirectory(),
136 TheCU);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000137 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel17800552010-03-09 00:44:50 +0000138 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Ted Kremenek9c250392010-03-30 00:27:51 +0000139
140 // Cache the results.
141 const char *fname = PLoc.getFilename();
142 llvm::DenseMap<const char *, llvm::WeakVH>::iterator it =
143 DIFileCache.find(fname);
144
145 if (it != DIFileCache.end()) {
146 // Verify that the information still exists.
147 if (&*it->second)
148 return llvm::DIFile(cast<llvm::MDNode>(it->second));
149 }
150
151 // FIXME: We shouldn't even need to call 'makeAbsolute()' in the cases
152 // where we can consult the FileEntry.
Devang Patel17800552010-03-09 00:44:50 +0000153 llvm::sys::Path AbsFileName(PLoc.getFilename());
Benjamin Kramer47daf682009-12-08 11:02:29 +0000154 AbsFileName.makeAbsolute();
Devang Patel446c6192009-04-17 21:06:59 +0000155
Ted Kremenek9c250392010-03-30 00:27:51 +0000156 llvm::DIFile F = DebugFactory.CreateFile(AbsFileName.getLast(),
157 AbsFileName.getDirname(), TheCU);
158
Devang Patelab699792010-05-07 18:12:35 +0000159 DIFileCache[fname] = F;
Ted Kremenek9c250392010-03-30 00:27:51 +0000160 return F;
161
Devang Patel17800552010-03-09 00:44:50 +0000162}
Devang Patel8ab870d2010-05-12 23:46:38 +0000163
164/// getLineNumber - Get line number for the location. If location is invalid
165/// then use current location.
166unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
167 assert (CurLoc.isValid() && "Invalid current location!");
168 SourceManager &SM = CGM.getContext().getSourceManager();
169 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
170 return PLoc.getLine();
171}
172
173/// getColumnNumber - Get column number for the location. If location is
174/// invalid then use current location.
175unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) {
176 assert (CurLoc.isValid() && "Invalid current location!");
177 SourceManager &SM = CGM.getContext().getSourceManager();
178 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
179 return PLoc.getColumn();
180}
181
Devang Patel17800552010-03-09 00:44:50 +0000182/// CreateCompileUnit - Create new compile unit.
183void CGDebugInfo::CreateCompileUnit() {
184
185 // Get absolute path name.
Douglas Gregorac91b4c2010-03-18 23:46:43 +0000186 SourceManager &SM = CGM.getContext().getSourceManager();
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000187 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
188 if (MainFileName.empty())
Devang Patel22fe5852010-03-12 21:04:27 +0000189 MainFileName = "<unknown>";
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000190
Douglas Gregorf6728fc2010-03-22 21:28:29 +0000191 // The main file name provided via the "-main-file-name" option contains just
192 // the file name itself with no path information. This file name may have had
193 // a relative path, so we look into the actual file entry for the main
194 // file to determine the real absolute path for the file.
Devang Patelab2e0202010-07-23 20:38:37 +0000195 char *FileNamePtr = NULL;
196 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
197 std::string MainFileDir;
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000198 MainFileDir = MainFile->getDir()->getName();
Devang Patelab2e0202010-07-23 20:38:37 +0000199 llvm::sys::Path AbsFileDirName(MainFileDir);
200 AbsFileDirName.makeAbsolute();
201 AbsFileDirName.appendComponent(MainFileName);
202 FileNamePtr = DebugInfoNames.Allocate<char>(AbsFileDirName.size());
203 memcpy(FileNamePtr, AbsFileDirName.c_str(), AbsFileDirName.size());
204 } else {
205 llvm::sys::Path AbsFileDirName(MainFileName);
206 AbsFileDirName.makeAbsolute();
207 FileNamePtr = DebugInfoNames.Allocate<char>(AbsFileDirName.size());
208 memcpy(FileNamePtr, AbsFileDirName.c_str(), AbsFileDirName.size());
209 }
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000210
Chris Lattner515455a2009-03-25 03:28:08 +0000211 unsigned LangTag;
Devang Patel17800552010-03-09 00:44:50 +0000212 const LangOptions &LO = CGM.getLangOptions();
Chris Lattner515455a2009-03-25 03:28:08 +0000213 if (LO.CPlusPlus) {
214 if (LO.ObjC1)
215 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
216 else
217 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
218 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000219 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000220 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000221 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000222 } else {
223 LangTag = llvm::dwarf::DW_LANG_C89;
224 }
Devang Patel446c6192009-04-17 21:06:59 +0000225
Benjamin Kramer47daf682009-12-08 11:02:29 +0000226 const char *Producer =
Mike Stumpd8945d62009-10-09 18:38:12 +0000227#ifdef CLANG_VENDOR
228 CLANG_VENDOR
229#endif
230 "clang " CLANG_VERSION_STRING;
Chris Lattner4c2577a2009-05-02 01:00:04 +0000231
232 // Figure out which version of the ObjC runtime we have.
233 unsigned RuntimeVers = 0;
234 if (LO.ObjC1)
235 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000237 // Create new compile unit.
Devang Patel17800552010-03-09 00:44:50 +0000238 TheCU = DebugFactory.CreateCompileUnit(
Devang Patelab2e0202010-07-23 20:38:37 +0000239 LangTag, FileNamePtr, llvm::StringRef(), Producer, true,
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000240 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000241}
242
Devang Patel65e99f22009-02-25 01:36:11 +0000243/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000244/// one if necessary.
245llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel17800552010-03-09 00:44:50 +0000246 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000247 unsigned Encoding = 0;
248 switch (BT->getKind()) {
249 default:
250 case BuiltinType::Void:
251 return llvm::DIType();
Devang Pateleaf5ee92010-07-21 22:41:25 +0000252 case BuiltinType::ObjCId:
253 // id is struct objc_object *.
254 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
255 Unit, "objc_object", Unit, 0, 0, 0, 0,
256 llvm::DIType::FlagFwdDecl,
257 llvm::DIType(), llvm::DIArray());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000258 case BuiltinType::UChar:
259 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
260 case BuiltinType::Char_S:
261 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
262 case BuiltinType::UShort:
263 case BuiltinType::UInt:
264 case BuiltinType::ULong:
265 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
266 case BuiltinType::Short:
267 case BuiltinType::Int:
268 case BuiltinType::Long:
269 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
270 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
271 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000272 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000273 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000274 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000275 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000276 uint64_t Size = CGM.getContext().getTypeSize(BT);
277 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000278 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Devang Patelca80a5f2009-10-20 19:55:01 +0000280 llvm::DIType DbgTy =
281 DebugFactory.CreateBasicType(Unit,
Anders Carlsson20f12a22009-12-06 18:00:51 +0000282 BT->getName(CGM.getContext().getLangOptions()),
Devang Patelca80a5f2009-10-20 19:55:01 +0000283 Unit, 0, Size, Align,
284 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000285 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000286}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000287
Chris Lattnerb7003772009-04-23 06:13:01 +0000288llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000289 llvm::DIFile Unit) {
Chris Lattnerb7003772009-04-23 06:13:01 +0000290 // Bit size, align and offset of the type.
291 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
292 if (Ty->isComplexIntegerType())
293 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Anders Carlsson20f12a22009-12-06 18:00:51 +0000295 uint64_t Size = CGM.getContext().getTypeSize(Ty);
296 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattnerb7003772009-04-23 06:13:01 +0000297 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Devang Patelca80a5f2009-10-20 19:55:01 +0000299 llvm::DIType DbgTy =
300 DebugFactory.CreateBasicType(Unit, "complex",
301 Unit, 0, Size, Align,
302 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000303 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000304}
305
John McCalla1805292009-09-25 01:40:47 +0000306/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000307/// a new one if necessary.
Devang Patel17800552010-03-09 00:44:50 +0000308llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +0000309 QualifierCollector Qc;
310 const Type *T = Qc.strip(Ty);
311
312 // Ignore these qualifiers for now.
313 Qc.removeObjCGCAttr();
314 Qc.removeAddressSpace();
315
Chris Lattner9c85ba32008-11-10 06:08:34 +0000316 // We will create one Derived type for one qualifier and recurse to handle any
317 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000318 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000319 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000320 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000321 Qc.removeConst();
322 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000323 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000324 Qc.removeVolatile();
325 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000326 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000327 Qc.removeRestrict();
328 } else {
329 assert(Qc.empty() && "Unknown type qualifier for debug info");
330 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000331 }
Mike Stump1eb44332009-09-09 15:08:12 +0000332
John McCalla1805292009-09-25 01:40:47 +0000333 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
334
Daniel Dunbar3845f862008-10-31 03:54:29 +0000335 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
336 // CVR derived types.
Devang Patelca80a5f2009-10-20 19:55:01 +0000337 llvm::DIType DbgTy =
Devang Pateld58562e2010-03-09 22:49:11 +0000338 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000339 0, 0, 0, 0, 0, FromTy);
Devang Patelca80a5f2009-10-20 19:55:01 +0000340 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000341}
342
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000343llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000344 llvm::DIFile Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000345 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000346 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
347 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000348 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000349}
350
Chris Lattner9c85ba32008-11-10 06:08:34 +0000351llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000352 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000353 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
354 Ty->getPointeeType(), Unit);
355}
356
357llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
358 const Type *Ty,
359 QualType PointeeTy,
Devang Patel17800552010-03-09 00:44:50 +0000360 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000361 llvm::DIType EltTy = getOrCreateType(PointeeTy, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000363 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000364
365 // Size is always the size of a pointer. We can't use getTypeSize here
366 // because that does not return the correct value for references.
367 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000368 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
369 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Devang Patelca80a5f2009-10-20 19:55:01 +0000371 return
Devang Pateld58562e2010-03-09 22:49:11 +0000372 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000373 0, Size, Align, 0, 0, EltTy);
Anders Carlssona031b352009-11-06 19:19:55 +0000374
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000375}
376
Mike Stump9bc093c2009-05-14 02:03:51 +0000377llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000378 llvm::DIFile Unit) {
Mike Stump9bc093c2009-05-14 02:03:51 +0000379 if (BlockLiteralGenericSet)
380 return BlockLiteralGeneric;
381
Mike Stump9bc093c2009-05-14 02:03:51 +0000382 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
383
384 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
385
386 llvm::DIType FieldTy;
387
388 QualType FType;
389 uint64_t FieldSize, FieldOffset;
390 unsigned FieldAlign;
391
392 llvm::DIArray Elements;
393 llvm::DIType EltTy, DescTy;
394
395 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000396 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000397 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
398 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000399
Daniel Dunbarca308df2009-05-26 19:40:20 +0000400 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000401 EltTys.clear();
402
Mike Stump3d363c52009-10-02 02:30:50 +0000403 unsigned Flags = llvm::DIType::FlagAppleBlock;
Devang Patel8ab870d2010-05-12 23:46:38 +0000404 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump3d363c52009-10-02 02:30:50 +0000405
Mike Stump9bc093c2009-05-14 02:03:51 +0000406 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Devang Patel8ab870d2010-05-12 23:46:38 +0000407 Unit, LineNo, FieldOffset, 0, 0,
408 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Mike Stump9bc093c2009-05-14 02:03:51 +0000410 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000411 uint64_t Size = CGM.getContext().getTypeSize(Ty);
412 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Mike Stump9bc093c2009-05-14 02:03:51 +0000414 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000415 Unit, "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000416 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000417
418 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000419 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000420 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson20f12a22009-12-06 18:00:51 +0000421 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000422 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
423 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramerd3651cc2010-04-24 20:26:20 +0000424 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000425 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000426
Anders Carlsson20f12a22009-12-06 18:00:51 +0000427 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000428 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000429 FieldSize = CGM.getContext().getTypeSize(Ty);
430 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000431 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000432 "__descriptor", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000433 LineNo, FieldSize, FieldAlign,
Mike Stump9bc093c2009-05-14 02:03:51 +0000434 FieldOffset, 0, FieldTy);
435 EltTys.push_back(FieldTy);
436
437 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000438 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000439
440 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Devang Patel8ab870d2010-05-12 23:46:38 +0000441 Unit, LineNo, FieldOffset, 0, 0,
442 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000443
Mike Stump9bc093c2009-05-14 02:03:51 +0000444 BlockLiteralGenericSet = true;
445 BlockLiteralGeneric
446 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000447 "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000448 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000449 return BlockLiteralGeneric;
450}
451
Chris Lattner9c85ba32008-11-10 06:08:34 +0000452llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000453 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000454 // Typedefs are derived from some other type. If we have a typedef of a
455 // typedef, make sure to emit the whole chain.
456 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Chris Lattner9c85ba32008-11-10 06:08:34 +0000458 // We don't set size information, but do specify where the typedef was
459 // declared.
Devang Patel8ab870d2010-05-12 23:46:38 +0000460 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000461
Devang Pateleb6d79b2010-02-01 21:34:11 +0000462 llvm::DIDescriptor TyContext
463 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
464 Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000465 llvm::DIType DbgTy =
Devang Pateld5289052010-01-29 22:29:31 +0000466 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Pateleb6d79b2010-02-01 21:34:11 +0000467 TyContext,
Devang Pateld5289052010-01-29 22:29:31 +0000468 Ty->getDecl()->getName(), Unit,
469 Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000470 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000471}
472
Chris Lattner9c85ba32008-11-10 06:08:34 +0000473llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000474 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000475 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000476
Chris Lattner9c85ba32008-11-10 06:08:34 +0000477 // Add the result type at least.
478 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Chris Lattner9c85ba32008-11-10 06:08:34 +0000480 // Set up remainder of arguments if there is a prototype.
481 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000482 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000483 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
484 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
485 } else {
486 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000487 }
488
Chris Lattner9c85ba32008-11-10 06:08:34 +0000489 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000490 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Devang Patelca80a5f2009-10-20 19:55:01 +0000492 llvm::DIType DbgTy =
493 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000494 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000495 0, 0, 0, 0, 0,
496 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000497 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000498}
499
Devang Patel428deb52010-01-19 00:00:59 +0000500/// CollectRecordFields - A helper function to collect debug info for
501/// record fields. This is used while creating debug info entry for a Record.
502void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000503CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit,
Devang Patel428deb52010-01-19 00:00:59 +0000504 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
505 unsigned FieldNo = 0;
Devang Patel239cec62010-02-01 21:39:52 +0000506 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
507 for (RecordDecl::field_iterator I = RD->field_begin(),
508 E = RD->field_end();
Devang Patel428deb52010-01-19 00:00:59 +0000509 I != E; ++I, ++FieldNo) {
510 FieldDecl *Field = *I;
511 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
512
513 llvm::StringRef FieldName = Field->getName();
514
Devang Patel4835fdd2010-02-12 01:31:06 +0000515 // Ignore unnamed fields. Do not ignore unnamed records.
516 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel428deb52010-01-19 00:00:59 +0000517 continue;
518
519 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +0000520 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
521 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel428deb52010-01-19 00:00:59 +0000522 QualType FType = Field->getType();
523 uint64_t FieldSize = 0;
524 unsigned FieldAlign = 0;
525 if (!FType->isIncompleteArrayType()) {
526
527 // Bit size, align and offset of the type.
528 FieldSize = CGM.getContext().getTypeSize(FType);
529 Expr *BitWidth = Field->getBitWidth();
530 if (BitWidth)
531 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
532
533 FieldAlign = CGM.getContext().getTypeAlign(FType);
534 }
535
536 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
537
Devang Patel71f4ff62010-04-21 23:12:37 +0000538 unsigned Flags = 0;
539 AccessSpecifier Access = I->getAccess();
540 if (Access == clang::AS_private)
541 Flags |= llvm::DIType::FlagPrivate;
542 else if (Access == clang::AS_protected)
543 Flags |= llvm::DIType::FlagProtected;
544
Devang Patel428deb52010-01-19 00:00:59 +0000545 // Create a DW_TAG_member node to remember the offset of this field in the
546 // struct. FIXME: This is an absolutely insane way to capture this
547 // information. When we gut debug info, this should be fixed.
548 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
549 FieldName, FieldDefUnit,
550 FieldLine, FieldSize, FieldAlign,
Devang Patel71f4ff62010-04-21 23:12:37 +0000551 FieldOffset, Flags, FieldTy);
Devang Patel428deb52010-01-19 00:00:59 +0000552 EltTys.push_back(FieldTy);
553 }
554}
555
Devang Patela6da1922010-01-28 00:28:01 +0000556/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
557/// function type is not updated to include implicit "this" pointer. Use this
558/// routine to get a method type which includes "this" pointer.
559llvm::DIType
560CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000561 llvm::DIFile Unit) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +0000562 llvm::DIType FnTy
563 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
564 0),
565 Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000566
567 // Static methods do not need "this" pointer argument.
568 if (Method->isStatic())
569 return FnTy;
570
Devang Patela6da1922010-01-28 00:28:01 +0000571 // Add "this" pointer.
572
Devang Patelab699792010-05-07 18:12:35 +0000573 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patela6da1922010-01-28 00:28:01 +0000574 assert (Args.getNumElements() && "Invalid number of arguments!");
575
576 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
577
578 // First element is always return type. For 'void' functions it is NULL.
579 Elts.push_back(Args.getElement(0));
580
581 // "this" pointer is always first argument.
582 ASTContext &Context = CGM.getContext();
583 QualType ThisPtr =
584 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
Devang Patel337472d2010-02-09 17:57:50 +0000585 llvm::DIType ThisPtrType =
586 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
Devang Patel769640e2010-07-13 00:24:30 +0000587
Devang Patelab699792010-05-07 18:12:35 +0000588 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
Devang Patel337472d2010-02-09 17:57:50 +0000589 Elts.push_back(ThisPtrType);
Devang Patela6da1922010-01-28 00:28:01 +0000590
591 // Copy rest of the arguments.
592 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
593 Elts.push_back(Args.getElement(i));
594
595 llvm::DIArray EltTypeArray =
596 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
597
598 return
599 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000600 Unit, "", Unit,
Devang Patela6da1922010-01-28 00:28:01 +0000601 0, 0, 0, 0, 0,
602 llvm::DIType(), EltTypeArray);
603}
604
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000605/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
606/// a single member function GlobalDecl.
607llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000608CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000609 llvm::DIFile Unit,
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000610 llvm::DICompositeType &RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000611 bool IsCtorOrDtor =
612 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
613
614 llvm::StringRef MethodName = getFunctionName(Method);
Devang Patela6da1922010-01-28 00:28:01 +0000615 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000616
617 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
618 // make sense to give a single ctor/dtor a linkage name.
Anders Carlsson9a20d552010-06-22 16:16:50 +0000619 llvm::StringRef MethodLinkageName;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000620 if (!IsCtorOrDtor)
Anders Carlsson9a20d552010-06-22 16:16:50 +0000621 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000622
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000623 // Get the location for the method.
Devang Patel8ab870d2010-05-12 23:46:38 +0000624 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
625 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000626
627 // Collect virtual method info.
628 llvm::DIType ContainingType;
629 unsigned Virtuality = 0;
630 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000631
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000632 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000633 if (Method->isPure())
634 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
635 else
636 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
637
638 // It doesn't make sense to give a virtual destructor a vtable index,
639 // since a single destructor has two entries in the vtable.
640 if (!isa<CXXDestructorDecl>(Method))
Anders Carlsson046c2942010-04-17 20:15:18 +0000641 VIndex = CGM.getVTables().getMethodVTableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000642 ContainingType = RecordTy;
643 }
644
645 llvm::DISubprogram SP =
646 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
647 MethodLinkageName,
648 MethodDefUnit, MethodLine,
649 MethodTy, /*isLocalToUnit=*/false,
Devang Patela5c5bab2010-07-12 22:54:41 +0000650 /* isDefintion=*/ false,
Devang Patel40894912010-07-15 22:57:00 +0000651 Virtuality, VIndex, ContainingType,
Devang Patel15a3d7d2010-07-15 23:09:46 +0000652 Method->isImplicit(),
653 CGM.getLangOptions().Optimize);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000654
655 // Don't cache ctors or dtors since we have to emit multiple functions for
656 // a single ctor or dtor.
657 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
Devang Patelab699792010-05-07 18:12:35 +0000658 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000659
660 return SP;
661}
662
Devang Patel4125fd22010-01-19 01:54:44 +0000663/// CollectCXXMemberFunctions - A helper function to collect debug info for
664/// C++ member functions.This is used while creating debug info entry for
665/// a Record.
666void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000667CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000668 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
669 llvm::DICompositeType &RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000670 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
671 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000672 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000673
Devang Pateld5322da2010-02-09 19:09:28 +0000674 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000675 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000676
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000677 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000678 }
679}
680
Devang Patela245c5b2010-01-25 23:32:18 +0000681/// CollectCXXBases - A helper function to collect debug info for
682/// C++ base classes. This is used while creating debug info entry for
683/// a Record.
684void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000685CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000686 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
687 llvm::DICompositeType &RecordTy) {
688
Devang Patel239cec62010-02-01 21:39:52 +0000689 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
690 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
691 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000692 unsigned BFlags = 0;
693 uint64_t BaseOffset;
694
695 const CXXRecordDecl *Base =
696 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
697
698 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000699 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000700 // expression where it expects +ve number.
Anders Carlssonaf440352010-03-23 04:11:45 +0000701 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base);
Devang Patelca7daed2010-01-28 21:54:15 +0000702 BFlags = llvm::DIType::FlagVirtual;
703 } else
704 BaseOffset = RL.getBaseClassOffset(Base);
705
706 AccessSpecifier Access = BI->getAccessSpecifier();
707 if (Access == clang::AS_private)
708 BFlags |= llvm::DIType::FlagPrivate;
709 else if (Access == clang::AS_protected)
710 BFlags |= llvm::DIType::FlagProtected;
711
712 llvm::DIType DTy =
713 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
714 RecordTy, llvm::StringRef(),
Devang Pateld58562e2010-03-09 22:49:11 +0000715 Unit, 0, 0, 0,
Devang Patelca7daed2010-01-28 21:54:15 +0000716 BaseOffset, BFlags,
717 getOrCreateType(BI->getType(),
718 Unit));
719 EltTys.push_back(DTy);
720 }
Devang Patela245c5b2010-01-25 23:32:18 +0000721}
722
Devang Patel4ce3f202010-01-28 18:11:52 +0000723/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000724llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000725 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000726 return VTablePtrType;
727
728 ASTContext &Context = CGM.getContext();
729
730 /* Function type */
Benjamin Kramerad468862010-03-30 11:36:44 +0000731 llvm::DIDescriptor STy = getOrCreateType(Context.IntTy, Unit);
732 llvm::DIArray SElements = DebugFactory.GetOrCreateArray(&STy, 1);
Devang Patel4ce3f202010-01-28 18:11:52 +0000733 llvm::DIType SubTy =
734 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000735 Unit, "", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000736 0, 0, 0, 0, 0, llvm::DIType(), SElements);
737
738 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
739 llvm::DIType vtbl_ptr_type
740 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000741 Unit, "__vtbl_ptr_type", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000742 0, Size, 0, 0, 0, SubTy);
743
Devang Pateld58562e2010-03-09 22:49:11 +0000744 VTablePtrType =
745 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
746 Unit, "", Unit,
747 0, Size, 0, 0, 0, vtbl_ptr_type);
Devang Patel4ce3f202010-01-28 18:11:52 +0000748 return VTablePtrType;
749}
750
Anders Carlsson046c2942010-04-17 20:15:18 +0000751/// getVTableName - Get vtable name for the given Class.
752llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000753 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000754 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000755
756 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000757 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000758 memcpy(StrPtr, Name.data(), Name.length());
759 return llvm::StringRef(StrPtr, Name.length());
760}
761
762
Anders Carlsson046c2942010-04-17 20:15:18 +0000763/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel4ce3f202010-01-28 18:11:52 +0000764/// debug info entry in EltTys vector.
765void CGDebugInfo::
Anders Carlsson046c2942010-04-17 20:15:18 +0000766CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000767 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000768 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000769
770 // If there is a primary base then it will hold vtable info.
771 if (RL.getPrimaryBase())
772 return;
773
774 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000775 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000776 return;
777
778 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
779 llvm::DIType VPTR
780 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Anders Carlsson046c2942010-04-17 20:15:18 +0000781 getVTableName(RD), Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000782 0, Size, 0, 0, 0,
783 getOrCreateVTablePtrType(Unit));
784 EltTys.push_back(VPTR);
785}
786
Devang Patel65e99f22009-02-25 01:36:11 +0000787/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000788llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000789 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000790 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Chris Lattner9c85ba32008-11-10 06:08:34 +0000792 unsigned Tag;
Devang Pateld6c5a262010-02-01 21:52:22 +0000793 if (RD->isStruct())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000794 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Pateld6c5a262010-02-01 21:52:22 +0000795 else if (RD->isUnion())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000796 Tag = llvm::dwarf::DW_TAG_union_type;
797 else {
Devang Pateld6c5a262010-02-01 21:52:22 +0000798 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000799 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000800 }
801
Chris Lattner9c85ba32008-11-10 06:08:34 +0000802 // Get overall information about the record type for the debug info.
Devang Patel8ab870d2010-05-12 23:46:38 +0000803 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
804 unsigned Line = getLineNumber(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Chris Lattner9c85ba32008-11-10 06:08:34 +0000806 // Records and classes and unions can all be recursive. To handle them, we
807 // first generate a debug descriptor for the struct as a forward declaration.
808 // Then (if it is a definition) we go through and get debug info for all of
809 // its members. Finally, we create a descriptor for the complete type (which
810 // may refer to the forward decl if the struct is recursive) and replace all
811 // uses of the forward declaration with the final definition.
Devang Patel0b897992010-07-08 19:56:29 +0000812 llvm::DIDescriptor FDContext =
813 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
814
815 // If this is just a forward declaration, construct an appropriately
816 // marked node and just return it.
817 if (!RD->getDefinition()) {
818 llvm::DICompositeType FwdDecl =
819 DebugFactory.CreateCompositeType(Tag, FDContext, RD->getName(),
820 DefUnit, Line, 0, 0, 0,
821 llvm::DIType::FlagFwdDecl,
822 llvm::DIType(), llvm::DIArray());
823
824 return FwdDecl;
825 }
Devang Pateld0f251b2010-01-20 23:56:40 +0000826
Devang Pateld6c5a262010-02-01 21:52:22 +0000827 // A RD->getName() is not unique. However, the debug info descriptors
Devang Patelce78c972010-02-01 22:51:29 +0000828 // are uniqued so use type name to ensure uniquness.
Benjamin Kramerfea3d4d2010-03-13 12:06:51 +0000829 llvm::SmallString<128> FwdDeclName;
830 llvm::raw_svector_ostream(FwdDeclName) << "fwd.type." << FwdDeclCount++;
Devang Patel0ce73f62009-07-22 18:57:00 +0000831 llvm::DICompositeType FwdDecl =
Devang Patel7573f8b2010-03-09 21:32:27 +0000832 DebugFactory.CreateCompositeType(Tag, FDContext, FwdDeclName,
Devang Patelab71ff52009-11-12 00:51:46 +0000833 DefUnit, Line, 0, 0, 0, 0,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000834 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000835
Devang Patelab699792010-05-07 18:12:35 +0000836 llvm::MDNode *MN = FwdDecl;
837 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000838 // Otherwise, insert it into the TypeCache so that recursive uses will find
839 // it.
Devang Patelab699792010-05-07 18:12:35 +0000840 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000841 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000842 RegionStack.push_back(FwdDeclNode);
843 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000844
845 // Convert all the elements.
846 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
847
Devang Pateld6c5a262010-02-01 21:52:22 +0000848 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000849 if (CXXDecl) {
850 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson046c2942010-04-17 20:15:18 +0000851 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000852 }
Devang Pateld6c5a262010-02-01 21:52:22 +0000853 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +0000854 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +0000855 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +0000856 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +0000857
858 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +0000859 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel0ac8f312010-01-28 00:54:21 +0000860 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
861 ContainingType =
Devang Patelab699792010-05-07 18:12:35 +0000862 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patel0ac8f312010-01-28 00:54:21 +0000863 else if (CXXDecl->isDynamicClass())
Devang Patelab699792010-05-07 18:12:35 +0000864 ContainingType = FwdDecl;
Devang Patela245c5b2010-01-25 23:32:18 +0000865 }
Mike Stump1eb44332009-09-09 15:08:12 +0000866
Chris Lattner9c85ba32008-11-10 06:08:34 +0000867 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000868 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000869
870 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000871 uint64_t Size = CGM.getContext().getTypeSize(Ty);
872 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000873
Devang Patele4c1ea02010-03-11 20:01:48 +0000874 RegionStack.pop_back();
875 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
876 RegionMap.find(Ty->getDecl());
877 if (RI != RegionMap.end())
878 RegionMap.erase(RI);
879
Devang Patel411894b2010-02-01 22:40:08 +0000880 llvm::DIDescriptor RDContext =
881 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel700a1cb2010-07-20 20:24:18 +0000882
883 llvm::StringRef RDName = RD->getName();
884 // If this is a class, include the template arguments also.
885 if (Tag == llvm::dwarf::DW_TAG_class_type)
886 RDName = getClassName(RD);
887
Devang Patel0ce73f62009-07-22 18:57:00 +0000888 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +0000889 DebugFactory.CreateCompositeType(Tag, RDContext,
Devang Patel700a1cb2010-07-20 20:24:18 +0000890 RDName,
Devang Patelab71ff52009-11-12 00:51:46 +0000891 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +0000892 llvm::DIType(), Elements,
893 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000894
895 // Now that we have a real decl for the struct, replace anything using the
896 // old decl with the new one. This will recursively update the debug info.
Eli Friedman14d63652009-11-16 21:04:30 +0000897 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +0000898 RegionMap[RD] = llvm::WeakVH(RealDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000899 return RealDecl;
900}
901
John McCallc12c5bb2010-05-15 11:32:37 +0000902/// CreateType - get objective-c object type.
903llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
904 llvm::DIFile Unit) {
905 // Ignore protocols.
906 return getOrCreateType(Ty->getBaseType(), Unit);
907}
908
Devang Patel9ca36b62009-02-26 21:10:26 +0000909/// CreateType - get objective-c interface type.
910llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000911 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000912 ObjCInterfaceDecl *ID = Ty->getDecl();
Devang Patel9ca36b62009-02-26 21:10:26 +0000913 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Patel9ca36b62009-02-26 21:10:26 +0000914
915 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +0000916 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +0000917 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel17800552010-03-09 00:44:50 +0000918 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000919
Devang Patel9ca36b62009-02-26 21:10:26 +0000920 // To handle recursive interface, we
921 // first generate a debug descriptor for the struct as a forward declaration.
922 // Then (if it is a definition) we go through and get debug info for all of
923 // its members. Finally, we create a descriptor for the complete type (which
924 // may refer to the forward decl if the struct is recursive) and replace all
925 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000926 llvm::DICompositeType FwdDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +0000927 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000928 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000929 llvm::DIType(), llvm::DIArray(),
930 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Devang Patel9ca36b62009-02-26 21:10:26 +0000932 // If this is just a forward declaration, return it.
Devang Pateld6c5a262010-02-01 21:52:22 +0000933 if (ID->isForwardDecl())
Devang Patel9ca36b62009-02-26 21:10:26 +0000934 return FwdDecl;
935
Devang Patelab699792010-05-07 18:12:35 +0000936 llvm::MDNode *MN = FwdDecl;
937 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patel9ca36b62009-02-26 21:10:26 +0000938 // Otherwise, insert it into the TypeCache so that recursive uses will find
939 // it.
Devang Patelab699792010-05-07 18:12:35 +0000940 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000941 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000942 RegionStack.push_back(FwdDeclNode);
943 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000944
945 // Convert all the elements.
946 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
947
Devang Pateld6c5a262010-02-01 21:52:22 +0000948 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +0000949 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000950 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000951 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000952 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000953 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Pateld58562e2010-03-09 22:49:11 +0000954 Unit, "", Unit, 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000955 0 /* offset */, 0, SClassTy);
956 EltTys.push_back(InhTag);
957 }
958
Devang Pateld6c5a262010-02-01 21:52:22 +0000959 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +0000960
961 unsigned FieldNo = 0;
Devang Pateld6c5a262010-02-01 21:52:22 +0000962 for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(),
963 E = ID->ivar_end(); I != E; ++I, ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +0000964 ObjCIvarDecl *Field = *I;
965 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
966
Devang Patel73621622009-11-25 17:37:31 +0000967 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +0000968
Devang Patelde135022009-04-27 22:40:36 +0000969 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +0000970 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +0000971 continue;
972
Devang Patel9ca36b62009-02-26 21:10:26 +0000973 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +0000974 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
975 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel99c20eb2009-03-20 18:24:39 +0000976 QualType FType = Field->getType();
977 uint64_t FieldSize = 0;
978 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000979
Devang Patel99c20eb2009-03-20 18:24:39 +0000980 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000981
Devang Patel99c20eb2009-03-20 18:24:39 +0000982 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000983 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000984 Expr *BitWidth = Field->getBitWidth();
985 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +0000986 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000987
Anders Carlsson20f12a22009-12-06 18:00:51 +0000988 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000989 }
990
Mike Stump1eb44332009-09-09 15:08:12 +0000991 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
992
Devang Patelc20482b2009-03-19 00:23:53 +0000993 unsigned Flags = 0;
994 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
995 Flags = llvm::DIType::FlagProtected;
996 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
997 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +0000998
Devang Patel9ca36b62009-02-26 21:10:26 +0000999 // Create a DW_TAG_member node to remember the offset of this field in the
1000 // struct. FIXME: This is an absolutely insane way to capture this
1001 // information. When we gut debug info, this should be fixed.
1002 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1003 FieldName, FieldDefUnit,
1004 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +00001005 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +00001006 EltTys.push_back(FieldTy);
1007 }
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Devang Patel9ca36b62009-02-26 21:10:26 +00001009 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +00001010 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +00001011
Devang Patele4c1ea02010-03-11 20:01:48 +00001012 RegionStack.pop_back();
1013 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1014 RegionMap.find(Ty->getDecl());
1015 if (RI != RegionMap.end())
1016 RegionMap.erase(RI);
1017
Devang Patel9ca36b62009-02-26 21:10:26 +00001018 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001019 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1020 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001021
Devang Patel6c1fddf2009-07-27 18:42:03 +00001022 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +00001023 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +00001024 Line, Size, Align, 0, 0, llvm::DIType(),
1025 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +00001026
1027 // Now that we have a real decl for the struct, replace anything using the
1028 // old decl with the new one. This will recursively update the debug info.
Devang Patelffffb032009-11-16 20:09:38 +00001029 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001030 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +00001031
Devang Patel9ca36b62009-02-26 21:10:26 +00001032 return RealDecl;
1033}
1034
Chris Lattner9c85ba32008-11-10 06:08:34 +00001035llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001036 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001037 EnumDecl *ED = Ty->getDecl();
Chris Lattner9c85ba32008-11-10 06:08:34 +00001038
1039 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
1040
1041 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +00001042 for (EnumDecl::enumerator_iterator
Devang Pateld6c5a262010-02-01 21:52:22 +00001043 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +00001044 Enum != EnumEnd; ++Enum) {
Devang Patel73621622009-11-25 17:37:31 +00001045 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001046 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +00001047 }
Mike Stump1eb44332009-09-09 15:08:12 +00001048
Chris Lattner9c85ba32008-11-10 06:08:34 +00001049 // Return a CompositeType for the enum itself.
1050 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +00001051 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001052
Devang Patel8ab870d2010-05-12 23:46:38 +00001053 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1054 unsigned Line = getLineNumber(ED->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001055
Chris Lattner9c85ba32008-11-10 06:08:34 +00001056 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +00001057 uint64_t Size = 0;
1058 unsigned Align = 0;
1059 if (!Ty->isIncompleteType()) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001060 Size = CGM.getContext().getTypeSize(Ty);
1061 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman3189e4b2009-05-04 04:39:55 +00001062 }
Mike Stump1eb44332009-09-09 15:08:12 +00001063
Devang Patelca80a5f2009-10-20 19:55:01 +00001064 llvm::DIType DbgTy =
1065 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Pateld6c5a262010-02-01 21:52:22 +00001066 Unit, ED->getName(), DefUnit, Line,
Devang Patelca80a5f2009-10-20 19:55:01 +00001067 Size, Align, 0, 0,
1068 llvm::DIType(), EltArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001069 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001070}
1071
1072llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001073 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001074 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1075 return CreateType(RT, Unit);
1076 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1077 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001078
Chris Lattner9c85ba32008-11-10 06:08:34 +00001079 return llvm::DIType();
1080}
1081
Devang Patel70c23cd2010-02-23 22:59:39 +00001082llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001083 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001084 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1085 uint64_t NumElems = Ty->getNumElements();
1086 if (NumElems > 0)
1087 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001088
Benjamin Kramerad468862010-03-30 11:36:44 +00001089 llvm::DIDescriptor Subscript = DebugFactory.GetOrCreateSubrange(0, NumElems);
1090 llvm::DIArray SubscriptArray = DebugFactory.GetOrCreateArray(&Subscript, 1);
Devang Patel70c23cd2010-02-23 22:59:39 +00001091
1092 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1093 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1094
1095 return
1096 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001097 Unit, "", Unit,
Devang Patel70c23cd2010-02-23 22:59:39 +00001098 0, Size, Align, 0, 0,
1099 ElementTy, SubscriptArray);
1100}
1101
Chris Lattner9c85ba32008-11-10 06:08:34 +00001102llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001103 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001104 uint64_t Size;
1105 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001106
1107
Nuno Lopes010d5142009-01-28 00:35:17 +00001108 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001109 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001110 Size = 0;
1111 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001112 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001113 } else if (Ty->isIncompleteArrayType()) {
1114 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001115 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001116 } else {
1117 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001118 Size = CGM.getContext().getTypeSize(Ty);
1119 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001120 }
Mike Stump1eb44332009-09-09 15:08:12 +00001121
Chris Lattner9c85ba32008-11-10 06:08:34 +00001122 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1123 // interior arrays, do we care? Why aren't nested arrays represented the
1124 // obvious/recursive way?
1125 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1126 QualType EltTy(Ty, 0);
1127 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +00001128 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001129 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +00001130 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +00001131 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001132 // FIXME: Verify this is right for VLAs.
1133 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1134 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +00001135 }
Mike Stump1eb44332009-09-09 15:08:12 +00001136
Chris Lattner9c85ba32008-11-10 06:08:34 +00001137 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001138 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001139
Devang Patelca80a5f2009-10-20 19:55:01 +00001140 llvm::DIType DbgTy =
1141 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001142 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +00001143 0, Size, Align, 0, 0,
1144 getOrCreateType(EltTy, Unit),
1145 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001146 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001147}
1148
Anders Carlssona031b352009-11-06 19:19:55 +00001149llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001150 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001151 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1152 Ty, Ty->getPointeeType(), Unit);
1153}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001154
Anders Carlsson20f12a22009-12-06 18:00:51 +00001155llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001156 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001157 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1158 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1159
1160 if (!Ty->getPointeeType()->isFunctionType()) {
1161 // We have a data member pointer type.
1162 return PointerDiffDITy;
1163 }
1164
1165 // We have a member function pointer type. Treat it as a struct with two
1166 // ptrdiff_t members.
1167 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1168
1169 uint64_t FieldOffset = 0;
1170 llvm::DIDescriptor ElementTypes[2];
1171
1172 // FIXME: This should probably be a function type instead.
1173 ElementTypes[0] =
1174 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001175 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001176 Info.first, Info.second, FieldOffset, 0,
1177 PointerDiffDITy);
1178 FieldOffset += Info.first;
1179
1180 ElementTypes[1] =
1181 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001182 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001183 Info.first, Info.second, FieldOffset, 0,
1184 PointerDiffDITy);
1185
1186 llvm::DIArray Elements =
1187 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1188 llvm::array_lengthof(ElementTypes));
1189
1190 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1191 U, llvm::StringRef("test"),
Devang Pateld58562e2010-03-09 22:49:11 +00001192 U, 0, FieldOffset,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001193 0, 0, 0, llvm::DIType(), Elements);
1194}
1195
Douglas Gregor840943d2009-12-21 20:18:30 +00001196static QualType UnwrapTypeForDebugInfo(QualType T) {
1197 do {
1198 QualType LastT = T;
1199 switch (T->getTypeClass()) {
1200 default:
1201 return T;
1202 case Type::TemplateSpecialization:
1203 T = cast<TemplateSpecializationType>(T)->desugar();
1204 break;
1205 case Type::TypeOfExpr: {
1206 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1207 T = Ty->getUnderlyingExpr()->getType();
1208 break;
1209 }
1210 case Type::TypeOf:
1211 T = cast<TypeOfType>(T)->getUnderlyingType();
1212 break;
1213 case Type::Decltype:
1214 T = cast<DecltypeType>(T)->getUnderlyingType();
1215 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001216 case Type::Elaborated:
1217 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001218 break;
1219 case Type::SubstTemplateTypeParm:
1220 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1221 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001222 }
1223
1224 assert(T != LastT && "Type unwrapping failed to unwrap!");
1225 if (T == LastT)
1226 return T;
1227 } while (true);
1228
1229 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001230}
1231
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001232/// getOrCreateType - Get the type from the cache or create a new
1233/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001234llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001235 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001236 if (Ty.isNull())
1237 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001238
Douglas Gregor840943d2009-12-21 20:18:30 +00001239 // Unwrap the type as needed for debug information.
1240 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001241
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001242 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001243 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001244 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001245 if (it != TypeCache.end()) {
1246 // Verify that the debug info still exists.
1247 if (&*it->second)
1248 return llvm::DIType(cast<llvm::MDNode>(it->second));
1249 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001250
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001251 // Otherwise create the type.
1252 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001253
1254 // And update the type cache.
Devang Patelab699792010-05-07 18:12:35 +00001255 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001256 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001257}
1258
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001259/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001260llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001261 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001262 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001263 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001264 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001265
Douglas Gregor2101a822009-12-21 19:57:21 +00001266 const char *Diag = 0;
1267
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001268 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001269 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001270#define TYPE(Class, Base)
1271#define ABSTRACT_TYPE(Class, Base)
1272#define NON_CANONICAL_TYPE(Class, Base)
1273#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1274#include "clang/AST/TypeNodes.def"
1275 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001276
Anders Carlssonbfe69952009-11-06 18:24:04 +00001277 // FIXME: Handle these.
1278 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001279 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001280
1281 case Type::Vector:
1282 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001283 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001284 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCallc12c5bb2010-05-15 11:32:37 +00001285 case Type::ObjCObject:
1286 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001287 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001288 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1289 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1290 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1291 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001292 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001293 return CreateType(cast<BlockPointerType>(Ty), Unit);
1294 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001295 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001296 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001297 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001298 case Type::FunctionProto:
1299 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001300 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001301 case Type::ConstantArray:
1302 case Type::VariableArray:
1303 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001304 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001305
1306 case Type::LValueReference:
1307 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1308
Anders Carlsson20f12a22009-12-06 18:00:51 +00001309 case Type::MemberPointer:
1310 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001311
1312 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001313 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001314 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001315 case Type::TypeOfExpr:
1316 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001317 case Type::Decltype:
1318 llvm_unreachable("type should have been unwrapped!");
1319 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001320
1321 case Type::RValueReference:
1322 // FIXME: Implement!
1323 Diag = "rvalue references";
1324 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001325 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001326
1327 assert(Diag && "Fall through without a diagnostic?");
1328 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1329 "debug information for %0 is not yet supported");
1330 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1331 << Diag;
1332 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001333}
1334
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001335/// CreateMemberType - Create new member and increase Offset by FType's size.
1336llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
1337 llvm::StringRef Name,
1338 uint64_t *Offset) {
1339 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1340 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1341 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
1342 llvm::DIType Ty = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1343 Unit, Name, Unit, 0,
1344 FieldSize, FieldAlign,
1345 *Offset, 0, FieldTy);
1346 *Offset += FieldSize;
1347 return Ty;
1348}
1349
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001350/// EmitFunctionStart - Constructs the debug code for entering a function -
1351/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001352void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001353 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001354 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001355
Devang Patel9c6c3a02010-01-14 00:36:21 +00001356 llvm::StringRef Name;
Anders Carlsson9a20d552010-06-22 16:16:50 +00001357 llvm::StringRef LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001358
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001359 FnBeginRegionCount.push_back(RegionStack.size());
1360
Devang Patel9c6c3a02010-01-14 00:36:21 +00001361 const Decl *D = GD.getDecl();
1362 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001363 // If there is a DISubprogram for this function available then use it.
1364 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1365 FI = SPCache.find(FD);
1366 if (FI != SPCache.end()) {
Devang Patel0804e6e2010-03-08 20:53:17 +00001367 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
Devang Patelab699792010-05-07 18:12:35 +00001368 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1369 llvm::MDNode *SPN = SP;
1370 RegionStack.push_back(SPN);
1371 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00001372 return;
1373 }
1374 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001375 Name = getFunctionName(FD);
1376 // Use mangled name as linkage name for c/c++ functions.
Anders Carlsson9a20d552010-06-22 16:16:50 +00001377 LinkageName = CGM.getMangledName(GD);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001378 } else {
1379 // Use llvm function name as linkage name.
1380 Name = Fn->getName();
Anders Carlsson9a20d552010-06-22 16:16:50 +00001381 LinkageName = Name;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001382 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001383 if (!Name.empty() && Name[0] == '\01')
1384 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001385
Devang Patel970c6182010-04-24 00:49:16 +00001386 // It is expected that CurLoc is set before using EmitFunctionStart.
1387 // Usually, CurLoc points to the left bracket location of compound
1388 // statement representing function body.
1389 llvm::DIFile Unit = getOrCreateFile(CurLoc);
Devang Patel8ab870d2010-05-12 23:46:38 +00001390 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001391
Chris Lattner9c85ba32008-11-10 06:08:34 +00001392 llvm::DISubprogram SP =
Devang Patel970c6182010-04-24 00:49:16 +00001393 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001394 getOrCreateType(FnType, Unit),
Devang Patel15a3d7d2010-07-15 23:09:46 +00001395 Fn->hasInternalLinkage(), true/*definition*/,
1396 0, 0, llvm::DIType(),
1397 D->isImplicit(),
1398 CGM.getLangOptions().Optimize, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +00001399
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001400 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001401 llvm::MDNode *SPN = SP;
1402 RegionStack.push_back(SPN);
1403 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001404
1405 // Clear stack used to keep track of #line directives.
1406 LineDirectiveFiles.clear();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001407}
1408
1409
Devang Patel4d939e62010-07-20 22:20:10 +00001410void CGDebugInfo::EmitStopPoint(CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001411 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001412
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001413 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001414 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001415 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001416 || (SM.getInstantiationLineNumber(CurLoc) ==
1417 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001418 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patel4800ea62010-04-05 21:09:15 +00001419 // New Builder may not be in sync with CGDebugInfo.
1420 if (!Builder.getCurrentDebugLocation().isUnknown())
1421 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001422
1423 // Update last state.
1424 PrevLoc = CurLoc;
1425
Chris Lattnerc6034632010-04-01 06:31:43 +00001426 llvm::MDNode *Scope = RegionStack.back();
Devang Patel8ab870d2010-05-12 23:46:38 +00001427 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1428 getColumnNumber(CurLoc),
Chris Lattnere541d012010-04-02 20:21:43 +00001429 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001430}
1431
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001432/// UpdateLineDirectiveRegion - Update region stack only if #line directive
1433/// has introduced scope change.
1434void CGDebugInfo::UpdateLineDirectiveRegion(CGBuilderTy &Builder) {
1435 if (CurLoc.isInvalid() || CurLoc.isMacroID() ||
1436 PrevLoc.isInvalid() || PrevLoc.isMacroID())
1437 return;
1438 SourceManager &SM = CGM.getContext().getSourceManager();
1439 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
1440 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc);
1441
1442 if (!strcmp(PPLoc.getFilename(), PCLoc.getFilename()))
1443 return;
1444
1445 // If #line directive stack is empty then we are entering a new scope.
1446 if (LineDirectiveFiles.empty()) {
1447 EmitRegionStart(Builder);
1448 LineDirectiveFiles.push_back(PCLoc.getFilename());
1449 return;
1450 }
1451
1452 assert (RegionStack.size() >= LineDirectiveFiles.size()
1453 && "error handling #line regions!");
1454
1455 bool SeenThisFile = false;
1456 for(std::vector<const char *>::iterator I = LineDirectiveFiles.begin(),
1457 E = LineDirectiveFiles.end(); I != E; ++I)
1458 if (!strcmp(PPLoc.getFilename(), *I)) {
1459 SeenThisFile = true;
1460 break;
1461 }
1462
1463 // If #line for this file is seen earlier then pop out #line regions.
1464 if (SeenThisFile) {
1465 while (!LineDirectiveFiles.empty()) {
1466 const char *LastFile = LineDirectiveFiles.back();
1467 RegionStack.pop_back();
1468 LineDirectiveFiles.pop_back();
1469 if (!strcmp(PPLoc.getFilename(), LastFile))
1470 break;
1471 }
1472 return;
1473 }
1474
1475 // .. otherwise insert new #line region.
1476 EmitRegionStart(Builder);
1477 LineDirectiveFiles.push_back(PCLoc.getFilename());
1478
1479 return;
1480}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001481/// EmitRegionStart- Constructs the debug code for entering a declarative
1482/// region - "llvm.dbg.region.start.".
Devang Patel4d939e62010-07-20 22:20:10 +00001483void CGDebugInfo::EmitRegionStart(CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001484 llvm::DIDescriptor D =
1485 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1486 llvm::DIDescriptor() :
Devang Pateld19429f2010-02-16 21:41:20 +00001487 llvm::DIDescriptor(RegionStack.back()),
Stuart Hastings257d1d32010-07-19 23:56:31 +00001488 getOrCreateFile(CurLoc),
Devang Patel8ab870d2010-05-12 23:46:38 +00001489 getLineNumber(CurLoc),
1490 getColumnNumber(CurLoc));
Devang Patelab699792010-05-07 18:12:35 +00001491 llvm::MDNode *DN = D;
1492 RegionStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001493}
1494
1495/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1496/// region - "llvm.dbg.region.end."
Devang Patel4d939e62010-07-20 22:20:10 +00001497void CGDebugInfo::EmitRegionEnd(CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001498 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1499
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001500 // Provide an region stop point.
Devang Patel4d939e62010-07-20 22:20:10 +00001501 EmitStopPoint(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001502
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001503 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001504}
1505
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001506/// EmitFunctionEnd - Constructs the debug code for exiting a function.
1507void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
1508 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1509 unsigned RCount = FnBeginRegionCount.back();
1510 assert(RCount <= RegionStack.size() && "Region stack mismatch");
1511
1512 // Pop all regions for this function.
1513 while (RegionStack.size() != RCount)
1514 EmitRegionEnd(Builder);
1515 FnBeginRegionCount.pop_back();
1516}
1517
Devang Patel809b9bb2010-02-10 18:49:08 +00001518// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1519// See BuildByRefType.
1520llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1521 uint64_t *XOffset) {
1522
1523 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1524
1525 QualType FType;
1526 uint64_t FieldSize, FieldOffset;
1527 unsigned FieldAlign;
1528
Devang Patel17800552010-03-09 00:44:50 +00001529 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001530 QualType Type = VD->getType();
1531
1532 FieldOffset = 0;
1533 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001534 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1535 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001536 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001537 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1538 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1539
Devang Patel809b9bb2010-02-10 18:49:08 +00001540 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1541 if (HasCopyAndDispose) {
1542 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001543 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1544 &FieldOffset));
1545 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1546 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001547 }
1548
1549 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1550 if (Align > CharUnits::fromQuantity(
1551 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1552 unsigned AlignedOffsetInBytes
1553 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1554 unsigned NumPaddingBytes
1555 = AlignedOffsetInBytes - FieldOffset/8;
1556
1557 if (NumPaddingBytes > 0) {
1558 llvm::APInt pad(32, NumPaddingBytes);
1559 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1560 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001561 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001562 }
1563 }
1564
1565 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001566 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00001567 FieldSize = CGM.getContext().getTypeSize(FType);
1568 FieldAlign = Align.getQuantity()*8;
1569
1570 *XOffset = FieldOffset;
1571 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001572 VD->getName(), Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001573 0, FieldSize, FieldAlign,
1574 FieldOffset, 0, FieldTy);
1575 EltTys.push_back(FieldTy);
1576 FieldOffset += FieldSize;
1577
1578 llvm::DIArray Elements =
1579 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1580
1581 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1582
1583 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001584 Unit, "", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001585 0, FieldOffset, 0, 0, Flags,
1586 llvm::DIType(), Elements);
1587
1588}
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001589/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001590void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001591 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001592 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1593
Devang Patel17800552010-03-09 00:44:50 +00001594 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001595 llvm::DIType Ty;
1596 uint64_t XOffset = 0;
1597 if (VD->hasAttr<BlocksAttr>())
1598 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1599 else
1600 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001601
Devang Patelf4e54a22010-05-07 23:05:55 +00001602 // If there is not any debug info for type then do not emit debug info
1603 // for this variable.
1604 if (!Ty)
1605 return;
1606
Chris Lattner9c85ba32008-11-10 06:08:34 +00001607 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001608 unsigned Line = getLineNumber(VD->getLocation());
1609 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001610
Chris Lattner9c85ba32008-11-10 06:08:34 +00001611 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001612 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001613 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel239cec62010-02-01 21:39:52 +00001614 VD->getName(),
Devang Patel44eeeba2010-06-05 01:14:40 +00001615 Unit, Line, Ty, CGM.getLangOptions().Optimize);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001616 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001617 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001618 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001619
Chris Lattnerc6034632010-04-01 06:31:43 +00001620 llvm::MDNode *Scope = RegionStack.back();
Chris Lattnere541d012010-04-02 20:21:43 +00001621 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001622}
1623
Mike Stumpb1a6e682009-09-30 02:43:10 +00001624/// EmitDeclare - Emit local variable declaration debug info.
1625void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1626 llvm::Value *Storage, CGBuilderTy &Builder,
1627 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001628 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001629 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1630
Devang Patel2b594b92010-04-26 23:28:46 +00001631 if (Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001632 return;
1633
1634 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001635 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001636 llvm::DIType Ty;
1637 if (VD->hasAttr<BlocksAttr>())
1638 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1639 else
1640 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001641
1642 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001643 unsigned Line = getLineNumber(VD->getLocation());
1644 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001645
Devang Pateld6c5a262010-02-01 21:52:22 +00001646 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001647 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001648 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1649 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1650 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1651 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001652 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001653 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1654 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001655 // offset of __forwarding field
1656 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001657 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1658 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1659 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001660 // offset of x field
1661 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001662 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001663 }
1664
1665 // Create the descriptor for the variable.
1666 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001667 DebugFactory.CreateComplexVariable(Tag,
1668 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001669 VD->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001670 addr);
1671 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001672 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001673 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattnerd5b89022009-12-28 21:44:41 +00001674
Chris Lattnerc6034632010-04-01 06:31:43 +00001675 llvm::MDNode *Scope = RegionStack.back();
Devang Patelf8e10a52010-05-10 23:48:38 +00001676 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001677}
1678
Devang Pateld6c5a262010-02-01 21:52:22 +00001679void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001680 llvm::Value *Storage,
1681 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001682 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001683}
1684
Mike Stumpb1a6e682009-09-30 02:43:10 +00001685void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1686 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1687 CodeGenFunction *CGF) {
1688 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1689}
1690
Chris Lattner9c85ba32008-11-10 06:08:34 +00001691/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1692/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001693void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001694 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001695 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001696}
1697
1698
1699
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001700/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001701void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001702 const VarDecl *D) {
1703
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001704 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001705 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001706 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +00001707
Devang Pateleb6d79b2010-02-01 21:34:11 +00001708 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001709 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001710
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001711 // CodeGen turns int[] into int[1] so we'll do the same here.
1712 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001713
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001714 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001715 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001716
Anders Carlsson20f12a22009-12-06 18:00:51 +00001717 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001718 ArrayType::Normal, 0);
1719 }
Devang Patel5d822f02010-04-29 17:48:37 +00001720 llvm::StringRef DeclName = D->getName();
Devang Patel8b90a782010-05-13 23:52:37 +00001721 llvm::StringRef LinkageName;
Devang Patel0fd3d1f2010-05-14 16:55:25 +00001722 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()))
Devang Patel8b90a782010-05-13 23:52:37 +00001723 LinkageName = Var->getName();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001724 llvm::DIDescriptor DContext =
1725 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
Devang Patel8b90a782010-05-13 23:52:37 +00001726 DebugFactory.CreateGlobalVariable(DContext, DeclName, DeclName, LinkageName,
1727 Unit, LineNo, getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001728 Var->hasInternalLinkage(),
1729 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001730}
1731
Devang Patel9ca36b62009-02-26 21:10:26 +00001732/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001733void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001734 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001735 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001736 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001737 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patel9ca36b62009-02-26 21:10:26 +00001738
Devang Pateld6c5a262010-02-01 21:52:22 +00001739 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001740
Devang Pateld6c5a262010-02-01 21:52:22 +00001741 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001742 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001743
Devang Patel9ca36b62009-02-26 21:10:26 +00001744 // CodeGen turns int[] into int[1] so we'll do the same here.
1745 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001746
Devang Patel9ca36b62009-02-26 21:10:26 +00001747 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001748 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001749
Anders Carlsson20f12a22009-12-06 18:00:51 +00001750 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001751 ArrayType::Normal, 0);
1752 }
1753
Devang Patelf6a39b72009-10-20 18:26:30 +00001754 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001755 getOrCreateType(T, Unit),
1756 Var->hasInternalLinkage(),
1757 true/*definition*/, Var);
1758}
Devang Patelabb485f2010-02-01 19:16:32 +00001759
1760/// getOrCreateNamesSpace - Return namespace descriptor for the given
1761/// namespace decl.
1762llvm::DINameSpace
1763CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1764 llvm::DIDescriptor Unit) {
1765 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1766 NameSpaceCache.find(NSDecl);
1767 if (I != NameSpaceCache.end())
1768 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1769
Devang Patel8ab870d2010-05-12 23:46:38 +00001770 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patelabb485f2010-02-01 19:16:32 +00001771
1772 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00001773 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00001774 llvm::DINameSpace NS =
1775 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
Devang Patelab699792010-05-07 18:12:35 +00001776 llvm::DIFile(Unit), LineNo);
1777 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00001778 return NS;
1779}